Dec 132017
 
Turbo source converted to 'c' source.
File TP-TO-C.ZIP from The Programmer’s Corner in
Category C Source Code
Turbo source converted to ‘c’ source.
File Name File Size Zip Size Zip Type
TPC.COM 34426 21701 deflated
TPC.DOC 7309 2472 deflated
TPCMAC.H 1239 605 deflated

Download File TP-TO-C.ZIP Here

Contents of the TPC.DOC file



TPC - Translate Pascal to C.
(C) Copyright 1986 by Samuel H. Smith; All rights reserved.

Please refer all inquiries to:
S.H.Smith
5119 N 11 Ave 332
Phoenix, AZ 85013

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.


Usage: TPC d:path\filename[.ext] [>outfile]

d: drive and path are optional
ext defaults to .PAS

Example:
TPC program.pas >program.c


This program will read a turbo pascal source file and convert it into
the corresponding C source code. It does about 90% of the work
required to do the translation.


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.

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
arcv
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)).

The special value, nil, is translated into NULL.

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 name} #include "name",
{$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).




Some language features that are not translated:
-----------------------------------------------

Nested procedures or function cannot be coded in C.

VAR parameters must be manually recoded.

File access procedures need to be coded (reset, 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 precidence 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.





 December 13, 2017  Add comments

Leave a Reply