home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD2.mdf
/
c
/
crosscom
/
tpc
/
tpc.doc
< prev
next >
Wrap
Text File
|
1987-05-26
|
11KB
|
353 lines
TPC14 - Translate Pascal to C
Version 1.4, 26-May-87
(C) Copyright 1986, 1987 by Samuel H. Smith
All rights reserved.
Please refer all inquiries to:
Samuel H. Smith The Tool Shop BBS
5119 N 11 Ave 332 (602) 279-2673
Phoenix, AZ 85013 2400/1200/300
You may copy and distribute this program freely, provided that:
1) No fee is charged for such copying and distribution, and
2) It is distributed ONLY in its original, unmodified state.
If you like this program, and find it of use, then your contribution will
be appreciated. If you are using this product in a commercial
environment, then the contribution is not voluntary.
This program will read a turbo pascal source file and convert it into the
corresponding C source code. It does about 95% of the work required to
do the translation.
Usage: TPC d:path\filename[.ext] outfile
d: drive and path are optional
ext defaults to .PAS
Example:
TPC program.pas program.c
The following language constructs are translated:
------------------------------------------------
Comments are translated from either {...} or (*...*) into /*...*/.
Begin and End are translated into { and }.
Const declarations are translated from
ID = VALUE
into
static ID = VALUE.
Simple Var declarations are translated from
ID TYPE
into
TYPE ID.
The following simple types are translated
from BOOLEAN to char,
INTEGER to int,
REAL to double.
Integer subrange types are translated into integers.
Record types are translated from
ID = record MEMBER-LIST end
into
typedef struct { MEMBER-LIST } ID.
Enumeration types are translated from
ID = (...)
into
typedef enum {...} ID.
Array types are translated from
ID = array [RANGE] of TYPE
into
typedef TYPE ID[RANGE].
Pointer types are translated from
ID = ^DEFINED-TYPE
into
DEFINED-TYPE *ID.
String types are translated from
ID = string[N]
into
typedef char ID[N].
File types are translated from
ID = text[N]
ID = text
into
FILE *ID
int ID.
For statements are translated from
for VAR := FIRST to LAST do STATEMENT
for VAR := FIRST downto LAST do statement
into
for (VAR = FIRST; VAR <= LAST; VAR++) STATEMENT
for (VAR = FIRST; VAR >= LAST; VAR--) STATEMENT
While statements are translated from
while COND do STATEMENT
into
while (COND) statement.
Repeat statements are translated from
repeat STATEMENTS until COND
into
do { STATEMENTS } while(!COND).
If statements are translated from
if COND then STATEMENT else STATEMENT
into
if (COND) STATEMENT; else STATEMENT.
Case statements are translated from
case VALUE of
V: STATEMENT;
V,U: STATEMENT;
else STATEMENT
end
into
switch (VALUE) {
case V: STATEMENT; break;
case V:
case U: STATEMENT; break;
default: STATEMENT;
}.
The IN operator is translated from
VAL in [A,B,C]
into
inset(VAL, setof(A,B,C,-1)).
The ParamCount and ParamStr functions are translated from
paramcount
paramstr(n)
into
argc
argv[n].
Dummy parameter lists are added to function and procedure calls,
where they are required in C but not in Pascal.
The following expression operators are translated
from DIV to / , MOD to % ,
AND to &&, OR to ||,
XOR to ~ , <> to !=,
NOT to ! , SHR to >>,
SHL to <<, = to ==,
:= to = .
The '^' symbol is translated
from VAR^ to *VAR,
VAR^.MEMBER to VAR->MEMBER.
Exit statements are translated
from exit to return.
The New operator is translated from
new(VAR)
into
VAR = malloc(sizeof(*VAR)).
Procedure/function formal parameter lists are translated into a
separate procedure declaration and parameter variable
declarations, as required by C, e.g.
from
function NAME(V1: TYPE1; V2: TYPE2): TYPE3
into
TYPE3 NAME(V1,V2)
TYPE1 V1;
TYPE2 V2;
Procedures are translated into functions with 'void' return types.
The special character literal syntax, ^C or #nn, is translated into
'\ooo', where ooo is the octal notation for the ascii code.
Hex constants $hhhh are translated into 0xhhhh.
Write and WriteLn are translated from:
write(VAR,VAR:n,VAR:n:m)
writeln(FILE,VAR,VAR,VAR)
into
printf("%d%nd%n.md",VAR,VAR,VAR)
fprintf(FILE,"%d%d%d\n",VAR,VAR,VAR).
Read and ReadLn are translated from:
read(VAR,VAR,VAR)
readln(FILE,VAR,VAR,VAR)
into
scanf("%d%nd%d",&VAR,&VAR,&VAR)
fscanf(FILE,"%d%d%d\n",&VAR,&VAR,&VAR).
String assignments are translated from:
VAR := "string"
VAR := VAR1 + "string"
into
strcpy(VAR, "string")
strcpy(VAR, concat(VAR1, "string")).
String comparisons are translated from:
VAR == "string"
VAR < "string"
"string" >= VAR
into
(strcmp(VAR,"string") == 0)
(strcmp(VAR,"string") < 0)
(strcmp("string",VAR) >= 0).
Function value assignments are translated from:
FUN_NAME := expr
into
return expr.
Compiler directives are translated
from {$B+/-} to /* #pragma */ standard_io(ON/OFF),
{$C+/-} /* #pragma */ control_c_check(ON/OFF),
{$D+/-} /* #pragma */ device_check(ON/OFF),
{$Fn} /* #pragma */ max_files(n),
{$Gn} /* #pragma */ input_file_buffer(n),
{$I+/-} /* #pragma */ io_error_check(ON/OFF),
{$K+/-} /* #pragma */ stack_check(ON/OFF),
{$Pn} /* #pragma */ output_file_buffer(n),
{$R+/-} /* #pragma */ range_check(ON/OFF),
{$U+/-} /* #pragma */ user_interrupt(ON/OFF),
{$V+/-} /* #pragma */ param_type_check(ON/OFF)
{$I name} #include "name"
Numeric statement labels are translated to label_nn.
Label identifiers are not changed.
Local GOTO statements are handled properly.
Nested procedures are "flattened" out, but local variable sharing and
local scoping are not translated.
The following translations were added to support Pascal/MT+:
(version TPC 1.4)
Var declarations are translated from
ID external TYPE
into
extern TYPE ID.
The following expression operators are translated
from ! to | , | to |,
& to & , ~ to !,
? to ! , \ to !.
External function declarations are translated
from
external function NAME(V1: TYPE1; V2: TYPE2): TYPE3
external [n] function NAME(V1: TYPE1; V2: TYPE2): TYPE3
into
extern TYPE3 NAME()
External procedure declarations are translated
from
external procedure NAME(V1: TYPE1; V2: TYPE2)
external [n] procedure NAME(V1: TYPE1; V2: TYPE2)
into
extern void NAME()
Write and WriteLn are translated from:
write([ADDR(FUN)],VAR:n,VAR:n:m)
write([],VAR:n,VAR:n:m)
into
iprintf(FUN,"%nd%n.md",VAR,VAR)
printf("%nd%n.md",VAR,VAR)
Read and ReadLn are translated from:
read([ADDR(FUN)],VAR,VAR)
read([],VAR,VAR)
into
iscanf(FUN,"%d%nd%d",&VAR,&VAR,&VAR)
scanf("%d%nd%d",&VAR,&VAR,&VAR)
Long integer constants #nnn are translated into nnnL.
Some language features that are not translated:
-----------------------------------------------
Local variable sharing among nested procedures is not translated.
VAR parameters must be manually recoded.
File access procedures are only partially supported (assign, close,
etc.).
Ranges in the form VAL..VAL are not translated in case statements.
Forward pointer type declarations are translated, but will not compile
in C. They must be manually recoded.
Variant record types should be translated into unions, but aren't.
Bitwise AND and OR operators are always translated into the logical
operators && and ||.
C operator precedence differs from that of Pascal, and the differences
are not translated.
The WITH statement is not translated.
The MEM[] and PORT[] arrays are not translated. These should be
turned int function calls.
Absolute variables are not (and probably cannot be) translated.
Revision history
----------------
09/09/85 v0.0
Initial coding by Samuel H. Smith. Never released.
12/19/86 v1.0
First distributed as TPC10 under shareware concept.
04/15/87 v1.1
Corrected handling of unary minus. Improved error messages; added
error messages to object file. Added handler for integer subrange
types. Added handling for goto statement and numeric labels. The
macro header, tpcmac.h, now contains more declarations. Distributed
as TPC11.
04/22/87 v1.2
Corrected an error that led to a crash on lines with more than 40
leading spaces. Distributed as TPC12.
05/20/87 v1.3
Added support for pascal/MT+: external procedures and variables,
special write/read indirect syntax, & and ! operators, default
string size for string declarations. Distributed as TPC13.
05/26/87 v1.4
Additional support for pascal/MT+. The translator "shifts" into a
MT+ specific mode when it recognizes the 'MODULE' statement. The
'|' operator is recognized for bitwise OR. The '\', '?' and '~'
operators are all translated into a unary not (is this right,
Noam?). Read(ln) and Write(ln) now support the special case of "[]"
for the I/O routine. Long integer literals are translated from
'#nnn' to 'nnnL'. Distributed as TPC14.