Related articles |
---|
[2 earlier articles] |
Re: Translating OO program to procedural program oliverhunt@gmail.com (oliverhunt@gmail.com) (2006-10-11) |
Re: Translating OO program to procedural program napi@axiomsol.com (napi) (2006-10-11) |
Re: Translating OO program to procedural program mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2006-10-11) |
Re: Translating OO program to procedural program torbenm@app-6.diku.dk (2006-10-11) |
Re: Translating OO program to procedural program englere_geo@yahoo.com (Eric) (2006-10-11) |
Re: Translating OO program to procedural program DrDiettrich1@aol.com (Hans-Peter Diettrich) (2006-10-11) |
Re: Translating OO program to procedural program int2k@gmx.net (Wolfram Fenske) (2006-10-11) |
Re: Translating OO program to procedural program tommy.thorn@gmail.com (Tommy Thorn) (2006-10-11) |
Re: Translating OO program to procedural program JoachimPimiskern@web.de (Joachim Pimiskern) (2006-10-12) |
Re: Translating OO program to procedural program gnorik@gmail.com (2006-10-24) |
From: | "Wolfram Fenske" <int2k@gmx.net> |
Newsgroups: | comp.compilers |
Date: | 11 Oct 2006 23:22:35 -0400 |
Organization: | Compilers Central |
References: | 06-10-039 |
Keywords: | OOP |
Posted-Date: | 11 Oct 2006 23:22:35 EDT |
moop wrote:
> Hi,
>
> I am working on a project translates OO programs to procedural
> programs, such as translating C++ to C and the like. I hope this
> effort can be spreaded out to other langs, so I am working on to
> abstract the common issues of doing so. I know there is a pinoneer
> attempt is C Front which produce C++ programs via a C compiler, I want
> to have a look on that, but still cannot find it now, anyone can
> suggest this to me?
>
> My approach is just to rename the methods with the instance name so
> that they can be placed in a single source file and then be compiled
> later by the procedural lang compiler. For instance,
[...]
> But I feel it is really not an adaptable way when dealing with
> declaration inside iterations whose number of iteration is not sure,
> means that we cannot change the names of the reference inside the
> iteration.
> for(...){
> A a = new A();
> }
> So far I am a little bit frustrated on this approach, anyone has
> better idea? Pls share your thoughts, thank you!
AFAIK, the right thing to do (TM) would be to transform the classes
into structs that contain instance variables and to transform the
methods to take as their first parameter an implicit this-pointer,
which points to the instance. I. e. you need to transform this:
--8<---------------cut here---------------start------------->8---
#include <stdio.h>
class A
{
int x;
public:
A() { x = 0; }
void set_x(int new_x) { x = new_x; }
int get_x() { return x; }
};
int main(int argc, char *argv[])
{
A a;
a.set_x(argc);
printf("%d\n", a.get_x());
return 0;
}
--8<---------------cut here---------------end--------------->8---
into this
--8<---------------cut here---------------start------------->8---
#include <stdio.h>
typedef struct A
{
int x;
} A;
void A__init(A *this)
{
this->x = 0;
}
void A__set_x(A *this, int new_x) { this->x = new_x; }
int A__get_x(A *this) { return this->x; }
int main(int argc, char *argv[])
{
A a; A__init(&a);
A__set_x(&a, argc);
printf("%d\n", A__get_x(&a));
/* A__finalize(&a); */
return 0;
}
--8<---------------cut here---------------end--------------->8---
Some thoughts:
* Function names like A__set_x() etc. are just a lame attempt at
name-mangeling. In practice you would have to generate unique
names.
* Because the this pointer in instance methods is implicit in C++,
you have to do some additional work in resolving variable names in
a method definition.
* The comment /* A__finalize(&a); */ is there to indicate that you
have to call the destructor on every lexical variable before
exiting a function.
* There is no inheritance in my example. I have some ideas, though,
on how it could be done in a relatively straight forward fashion.
* There's a lot of other stuff you'll have to figure out,
e. g. templates, exception handling, virtual functions.
IMO, the only way to accomplish all this is to implement a full C++
compiler, which is going to be a lot of work. But you probably knew
that. ;-) Maybe you can leverage an existing C++ compiler.
Wolfram
Return to the
comp.compilers page.
Search the
comp.compilers archives again.