Re: Converting Pascal to C++, C# or VB

Waldek Hebisch <hebisch@math.uni.wroc.pl>
12 Apr 2006 22:49:35 -0400

          From comp.compilers

Related articles
[10 earlier articles]
Re: Converting Pascal to C++, C# or VB marcov@stack.nl (Marco van de Voort) (2006-04-09)
Re: Converting Pascal to C++, C# or VB marcov@stack.nl (Marco van de Voort) (2006-04-09)
Re: Converting Pascal to C++, C# or VB marcov@stack.nl (Marco van de Voort) (2006-04-09)
Re: Converting Pascal to C++, C# or VB DrDiettrich@compuserve.de (Hans-Peter Diettrich) (2006-04-10)
Re: Converting Pascal to C++, C# or VB gneuner2@comcast.net (George Neuner) (2006-04-10)
Re: Converting Pascal to C++, C# or VB englere.geo@yahoo.com (Eric) (2006-04-12)
Re: Converting Pascal to C++, C# or VB hebisch@math.uni.wroc.pl (Waldek Hebisch) (2006-04-12)
Re: Converting Pascal to C++, C# or VB oliver@first.in-berlin.de (Oliver Bandel) (2006-04-14)
| List of all articles for this month |

From: Waldek Hebisch <hebisch@math.uni.wroc.pl>
Newsgroups: comp.compilers
Date: 12 Apr 2006 22:49:35 -0400
Organization: Politechnika Wroclawska
References: 06-04-017
Keywords: Pascal
Posted-Date: 12 Apr 2006 22:49:35 EDT

Steve <steve@rh12.co.uk> wrote:
> I have some legacy machine control code written in ISO std Pascal that I
> would like to convert to one of the languages supported by our current
> development environment : MS Visual Studio .NET 2003. So that means C++, C
> sharp or VB.
>
> It's a big chunk of code - about 500k lines in 200 files. I currently have a
> translator that converts it to Delphi. This enables us to run it on a PC,
> but it will not fit easily with our newer code which is in C++ and C#. I
> plan to extend this translator to generate the new language too. The
> translator creates an AST for all the source, and keeps track of
> publics/externs. Several passes are then made over the AST to generate the
> Delphi units. It's a complicated process, especially for the structured
> constants.
>
> So the question is which language to use? I know C++ a little, but the other
> two are new to me.
>
> So far the main obstacle I can see with C++ is the nested routines of
> Pascal. I don't know of any way to do this in C++. Initial investigation
> suggests that the struct in C# might be a suitable alternative. It has to
> allow access to constants, types and variables declared in an outer scope.
>
> We also have a few instances of Pascal procedural parameters. Can these be
> emulated?
>
> Fortunately no-one ever used conformant arrays. :)


First, you should decide your objective. If the goal is to link
together code in Pascal and C++ then GNU Pascal may do it: GNU Pascal
support all ISO 7185 constructs and most of ISO 10206. GNU Pascal uses
code genarator from GNU C and offers special types for C
interfacing. ATM GNU Pascal still does not support some esoteric
Extened Pascal (ISO 10206) features but there is good chance that it
can compile your program without changes.


You can also use Delphi to compile both Pascal and C++ (but you wrote
that you need to covert your code to make it acceptable to Delphi).


If you _must_ deliver as .NET then you can still use Delphi (GNU
Pascal generates only native code).


If you want to convert source once and continue development on
converted version then problem is more tricky. First, it is definitely
easier to convert taking C as a target language then to other
languages (but the result is likly to be .NET unfriendly). In general
it is hard to get both correct and readable code after conversion. For
ISO 7185 Pascal old p2c is likely to do good job. Beware that if your
code contains errors or constructs beyond what p2c can handle then p2c
may crash or silently generate wrong output. As long as you give it
good input p2c works quite well.


If p2c can not handle your program you may still get hints from code
p2c generates: it handles nested procedures introducing special
records to gather variables from parent routine and passes its address
as an extra parameter (this technique is slower then other, but quite
general).


If you have nested procedures as procedural parameters then you need
to add extra parameter to all routines that are passed to the same
procedural parameter as the nested procedure (and then repeat adding
extra parameter until you reach a fixpoint). In other words, treat
nested procedures as a new type and promote non-nested procedures to
this new type as long as needed to make program type correct.


Non-nested procedural parameters can be easily translated to function
pointers in C/C++.


--
                                                            Waldek Hebisch
hebisch@math.uni.wroc.pl


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.