Greetiings!
Does anyone know of a program to translate C or C++ code to Basic, especially FreeBasic? What I'm referring to is a direct statement-to-statement translation.
I would not think that it would be too difficult, but I'm not that conversant with the C language, so maybe it is impossible.
C to FreeBasic Translator?
Re: C to FreeBasic Translator?
I would love to just see a side by side list------maybe with examples of a few short typical programs. I think it is probably pretty easy once you get started---its getting started that is hard for me. There are some on this forum that appear to be fluent in both C and FB and happily switch back and forth, and can translate headers in their sleep----but for me, a "Dummy's Guide" would be a huge boost.
Re: C to FreeBasic Translator?
There is no program to translate C to FreeBASIC. The problem is that you cannot cut any corners when parsing C. You need full symbol table information to parse C (you need that for parsing FreeBASIC as well). And you need to take care of preprocessor mayhem.NormJ wrote:Greetiings!
Does anyone know of a program to translate C or C++ code to Basic, especially FreeBasic? What I'm referring to is a direct statement-to-statement translation.
I would not think that it would be too difficult, but I'm not that conversant with the C language, so maybe it is impossible.
What you want is a source2source translation without removal of comments/preprocessor statement. The problem is
that C source code becomes C source code according to the C syntax after preprocessing. But you don't want to translate C code that has been preprocessed to FreeBASIC as it will not look pretty (even unreadable). You want to translate non - preprocessed C code to non - preprocessed FreeBASIC code (you want to keep the preprocessor statements, the comments, 'everything' intact).
Translating preprocessed C code to freebasic sounds like a doable project. It's a lot of work (I'd use an existing C parser and start from there) but it can be done.
Translating non - preprocessed C code to non - preprocessed freebasic code (a 'real' source2source translation) is a problem. It's a problem so big that programs that do code instrumentation involving C source code (basically translating C to C and adding/removing statements from the source/to the source) always use preprocessed C code as input.
An example of a project that translates C to C: https://sites.google.com/a/gapp.msrg.ut ... a/aspectc/
(developers involved with aspectc state that input to their tool must be preprocessed C code).
The most promising C parser/C translation project I've found (http://cs.nyu.edu/rgrimm/xtc/) can parse C and (almost) keeps preprocessing statements intact while still being able to do 'something' with the C code. Almost as some parsing results I've seen clearly show the parser operating on preprocessed C code.
The developers involved with xtc (superC is part of that project) have even written a paper on the subject of parsing C while keeping preprocessing symbols intact (very informative):
http://cs.nyu.edu/rgrimm/papers/pldi12.pdf
I thought about things involved when doing c2fb quite a few times and it's the preprocessor that kills the possibility of a decent source2source translation.
The only solution I could come up with is an elaborate scheme where the parser pre-processes the source but also labels tokens that are a result from preprocessing as such. After translation of the expanded piece of source code it would then be possible to replace the translated statements with the name of the macro. The translated source code can then be added to the translated source code as a macro at the position it originated from (file x, line y).
One example to clarify.
Code: Select all
#define DOLLAR_SIGN 0x24
y = X;
Code: Select all
y = 0x24;
Code: Select all
#define DOLLAR_SIGN 0x24
y = DOLLAR_SIGN;
y = DOLLAR_SIGN (label:macro,DOLLAR_SIGN, line 1);
preprocessing result (of course the input gets turned into some sort of concrete syntax
tree but for the sake of discussion using plain text is easier):
y = 0x24 (label:macro, DOLLAR_SIGN, line 1);
Translate 0x24 to &h24 and create a macro called DOLLAR_SIGN at line 1
with the value &h24. Replace 0x24 with DOLLAR_SIGN.
Translation:
Code: Select all
#define DOLLAR_SIGN &h24
y = DOLLAR_SIGN
of the above scheme. The translator would still have to take care of a whole list of differences between
C and freebasic (some differences are tricky to translate like gcc specific extensions (function attributes!),
the use of variables declared using volatile etc....). To fully automate the translation of C to freebasic
might prove difficult but automating a big part of the translation process is feasible.
C++2fb.... is even more interesting than C2fb but it's also a LOT harder. While c2fb might be doable I cannot
see how anything but the most trivial C++ code could be translated to freebasic in an automated manner
(java2fb sounds more feasible).
Re: C to FreeBasic Translator?
The FB header translation sticky in the Libraries forum has the list of currently available C-to-FB translation tools. Of course they're made for header translation, not generic code/programs.
Not every C statement can be translated to FB directly, for example, FB does not allow structure and typedef or structure and variable declarations to be combined into a single statement, for example:
Also, I now started a C/FB comparison wiki page at TblComparisonC, something I wanted to have for a while. More points could be added easily, so feel free to do that. I found the formatting to be hard and time consuming though, if anyone comes up with a better way to represent the comparison on the wiki, feel free to change the whole page.
Not every C statement can be translated to FB directly, for example, FB does not allow structure and typedef or structure and variable declarations to be combined into a single statement, for example:
Code: Select all
struct {
int i;
} my_global_var;
typedef struct {
int i;
} my_typedef;
-
- Posts: 165
- Joined: Oct 16, 2009 14:44
- Location: Jodhpur, Rajputana, Empire of Aryavart
- Contact:
Re: C to FreeBasic Translator?
Hm... I disagree with the title "hello world" it should be changed to "Minimal Hello World Program" because it is indeed representation of minimal hello world program in both languages instead of 100% literal copy of c program which should be:dkl wrote: Also, I now started a C/FB comparison wiki page at TblComparisonC, something I wanted to have for a while. More points could be added easily, so feel free to do that. I found the formatting to be hard and time consuming though, if anyone comes up with a better way to represent the comparison on the wiki, feel free to change the whole page.
Code: Select all
#Include "stdio.bi"
function main() as int
printf("Hello World!")
function = 0
end function
As computers love formal languages ;) it is rule that they don't change ;).
So calling them minimal hello world programs would be better...
Just my $0.02
Re: C to FreeBasic Translator?
AGS: nice writeup.