PDA

View Full Version : How do you translate from C++ to Pascal ?



Zampoteh
27-01-2007, 02:42 AM
How do you translate from C++ to Pascal ?
Any programs? I have found one : http://sourceforge.net/projects/C2pas
But that seems desolated.
So may be any ideas? ;)

Robert Kosek
27-01-2007, 03:32 AM
Yes. Do not use an automatic translator/interpreter. On rare occasion they work well, but they almost never generate human readable code (Note that I do not consider C/C++ as human readable :P) if they do function.

The best way is to just do all the footwork yourself.

K4Z
27-01-2007, 08:21 AM
I don't really trust automatic translators. You will get better, cleaner, faster code if you manually translate it. Most of the time you don't really have to understand every line and 'word' exactly, if you get the basic idea, just rewrite it in pascal.

However, if your doing something funky like translating a massive 10,000 line library, or an entire project with hundreds of units, then you'd probably have to use an automatic translator, but I think the result won't really be worth it.

JSoftware
27-01-2007, 10:03 AM
I have a pet project which will ultimately(in a few decades...) be able to translate any imperative language to another

Atm it'll translate pascal to c pretty fine

Legolas
27-01-2007, 12:06 PM
I have a pet project which will ultimately(in a few decades...) be able to translate any imperative language to another

Atm it'll translate pascal to c pretty fine

WOW!! :shock:
I can't wait to see it working :D

The best c to pascal translator I have found is c2pas32 (http://www.astonshell.com/freeware/c2pas32/), though it does not make miracles.
The best option is to try to understand the c code meaning and port it to pascal and, if necessary, you can isolate the piece of code you can't understand and try to pass it to translator.

bigsofty
27-01-2007, 06:19 PM
This is a handy doc...

http://www.stonehill.edu/cs1/TE/notespdf.htm

Pascal to C++ (Easier to understand for us Pascalies)

Zampoteh
28-01-2007, 12:56 AM
Yes. Do not use an automatic translator/interpreter. On rare occasion they work well, but they almost never generate human readable code (Note that I do not consider C/C++ as human readable :P) if they do function.

The best way is to just do all the footwork yourself.
But how to do it without mistakes?
For example. I want to translate function
327 int dBoxBox
http://opende.svn.sourceforge.net/viewvc/opende/trunk/ode/src/box.cpp?revision=887&view=markup
It is big and used many macroses.

And sometimes we really doesn't need to know how any function work. We just want to get a fast result. ;)

Zampoteh
28-01-2007, 01:04 AM
I don't really trust automatic translators. You will get better, cleaner, faster code if you manually translate it. Most of the time you don't really have to understand every line and 'word' exactly, if you get the basic idea, just rewrite it in pascal.


I can write that function by myself. But i don't know how ODE generate information about contacts.

Legolas
28-01-2007, 01:35 AM
Yes. Do not use an automatic translator/interpreter. On rare occasion they work well, but they almost never generate human readable code (Note that I do not consider C/C++ as human readable :P) if they do function.

The best way is to just do all the footwork yourself.
But how to do it without mistakes?
For example. I want to translate function
327 int dBoxBox
http://opende.svn.sourceforge.net/viewvc/opende/trunk/ode/src/box.cpp?revision=887&view=markup
It is big and used many macroses.

And sometimes we really doesn't need to know how any function work. We just want to get a fast result. ;)

YAY! That's huge, ugly and scary! First of all: put all these macros out of your function and translate it as pascal procedures. About macro's parameters: I'm not sure, but seems that are all float values... Try to declare it as real. :think:
The remaining part should be clean enough to pass to a translation software or to do it by hand :)

LP
28-01-2007, 05:32 AM
Long time ago we've worked on C-to-Pascal compiler (not "translator" that just replaces C words by Pascal equivalents) while working on AsphyreSound back in 2004.

I've uploaded it for ya with all source code, in case you want to play with it. MPL 1.1 license applies.

http://www.afterwarp.net/downloads/ac2p.7z

alexione
28-01-2007, 08:13 AM
I've never actually tried this myself, but just an idea: wouldn't it be helpful if you, first, let C/C++ preprocessor do the job, and apply C->Pascal translator on the result of it? Of course, you will loose any important #ifdef's (for example those conserning OS or CPU used), but you wouldn't need to worry about macro-hell. Looking at your example, I'm pretty sure it would do the job :)

Zampoteh
29-01-2007, 02:56 AM
Yes. Do not use an automatic translator/interpreter. On rare occasion they work well, but they almost never generate human readable code (Note that I do not consider C/C++ as human readable :P) if they do function.

The best way is to just do all the footwork yourself.
But how to do it without mistakes?
For example. I want to translate function
327 int dBoxBox
http://opende.svn.sourceforge.net/viewvc/opende/trunk/ode/src/box.cpp?revision=887&view=markup
It is big and used many macroses.

And sometimes we really doesn't need to know how any function work. We just want to get a fast result. ;)

YAY! That's huge, ugly and scary! First of all: put all these macros out of your function and translate it as pascal procedures.
I don't know how to do it without mistakes.
How to find one small mistake in that heap of macroses? If i did it?

I tried to translate them with C2PAS32.EXE. But it doesn't know how to translate some of them too. :)

Zampoteh
29-01-2007, 03:05 AM
For example
?ë++

#define dMULTIPLYOP0_331(A,op,B,C) \
do { \
(A)[0] op dDOT((B),(C)); \
(A)[1] op dDOT((B+4),(C)); \
(A)[2] op dDOT((B+8),(C)); \
} while(0)

DECL dMULTIPLY0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,=,B,C); }

DECL dMULTIPLYADD0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,+=,B,C); }


C2PAS32.EXE

function dMULTIPLYOP0_331(A: integer; op: integer; B: integer; C: integer): integer;
begin
result:= do((A)[0]opdDOT((B),(C));(A)[1]opdDOT((B+4),(C));( A)[2]opdDOT((B+8),(C));)while(0)
end;


http://opende.svn.sourceforge.net/viewvc/opende/trunk/include/ode/odemath.h?revision=1041&view=markup

Ñuño Martínez
31-01-2007, 11:40 AM
#define dMULTIPLYOP0_331(A,op,B,C) \
do { \
(A)[0] op dDOT((B),(C)); \
(A)[1] op dDOT((B+4),(C)); \
(A)[2] op dDOT((B+8),(C)); \
} while(0)

DECL dMULTIPLY0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,=,B,C); }

DECL dMULTIPLYADD0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,+=,B,C); }


The dMULTIPLYOP0_331 can't be translated to Pascal because it's an "expression builder". The "op" parameter means an operator and the result is an expression that should be compilled. I mean, when C compiles

DECL dMULTIPLY0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,=,B,C); }

the preprocessor returns:


extern pascal void dMULTIPLY0_331 (int *A, const int *B, const int *C)
{
do {
(A)[0] = dDOT((B),(C));
(A)[1] = dDOT((B+4),(C));
(A)[2] = dDOT((B+8),(C));
} while(0)
}

AFAIK it's not possible to do it in Pascal. You must "unroll" those macros by hand to translate it to Pascal.

savage
31-01-2007, 12:06 PM
AFAIK it's not possible to do it in Pascal. You must "unroll" those macros by hand to translate it to Pascal.

FreePascal supports macros, but I have not used them so can't comment further on them.

bigsofty
07-02-2007, 03:22 AM
http://cc.codegear.com/Item.aspx?id=23991

C to Pascal conversion util.