home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TPAS_C13.ZIP / TPC.DOC < prev    next >
Encoding:
Text File  |  1987-05-20  |  9.4 KB  |  332 lines

  1.  
  2.                      TPC13 - Translate Pascal to C
  3.                          Version 1.3, 20-May-87
  4.  
  5.               (C) Copyright 1986, 1987 by Samuel H. Smith;
  6.                           All rights reserved.
  7.  
  8.  
  9. Please refer all inquiries to:
  10.                 Samuel H. Smith       The Tool Shop BBS
  11.                 5119 N 11 Ave 332      (602) 279-2673
  12.                 Phoenix, AZ 85013       2400/1200/300
  13.  
  14.  
  15. You may copy and distribute this program freely, provided that:
  16.     1)   No fee is charged for such copying and distribution, and
  17.     2)   It is distributed ONLY in its original, unmodified state.
  18.  
  19. If you like this program, and find it of use, then your contribution
  20. will be appreciated.  If you are using this product in a commercial
  21. environment, then the contribution is not voluntary.
  22.  
  23.  
  24. Usage:  TPC d:path\filename[.ext] outfile
  25.  
  26.   d:   drive and path are optional
  27.   ext  defaults to .PAS
  28.  
  29. Example:
  30.    TPC program.pas program.c
  31.  
  32.  
  33. This program will read a turbo pascal source file and convert it into
  34. the corresponding C source code.   It does about 95% of the work
  35. required to do the translation.
  36.  
  37.  
  38. The following language constructs are translated:
  39. ------------------------------------------------
  40.  
  41.    Comments are translated from either {...} or (*...*) into /*...*/.
  42.  
  43.    Begin and End are translated into { and }.
  44.  
  45.    Const declarations are translated from
  46.       ID = VALUE
  47.    into
  48.       static ID = VALUE.
  49.  
  50.    Simple Var declarations are translated from
  51.       ID TYPE
  52.    into
  53.       TYPE ID.
  54.  
  55.    The following simple types are translated
  56.       from  BOOLEAN    to   char,
  57.             INTEGER    to   int,
  58.             REAL       to   double.
  59. 
  60.    Integer subrange types are translated into integers.
  61.  
  62.    Record types are translated from
  63.       ID = record MEMBER-LIST end
  64.    into
  65.       typedef struct { MEMBER-LIST } ID.
  66.  
  67.    Enumeration types are translated from
  68.       ID = (...)
  69.    into
  70.       typedef enum {...} ID.
  71.  
  72.    Array types are translated from
  73.       ID = array [RANGE] of TYPE
  74.    into
  75.       typedef TYPE ID[RANGE].
  76.  
  77.    Pointer types are translated from
  78.       ID = ^DEFINED-TYPE
  79.    into
  80.       DEFINED-TYPE *ID.
  81.  
  82.    String types are translated from
  83.       ID = string[N]
  84.    into
  85.       typedef char ID[N].
  86.  
  87.    File types are translated from
  88.       ID = text[N]
  89.       ID = text
  90.    into
  91.       FILE *ID
  92.       int ID.
  93.  
  94.    For statements are translated from
  95.       for VAR := FIRST to LAST do STATEMENT
  96.       for VAR := FIRST downto LAST do statement
  97.    into
  98.       for (VAR = FIRST; VAR <= LAST; VAR++) STATEMENT
  99.       for (VAR = FIRST; VAR >= LAST; VAR--) STATEMENT
  100.  
  101.    While statements are translated from
  102.       while COND do STATEMENT
  103.    into
  104.       while (COND) statement.
  105.  
  106.    Repeat statements are translated from
  107.       repeat STATEMENTS until COND
  108.    into
  109.       do { STATEMENTS } while(!COND).
  110.  
  111.    If statements are translated from
  112.       if COND then STATEMENT else STATEMENT
  113.    into
  114.       if (COND) STATEMENT; else STATEMENT.
  115. 
  116.    Case statements are translated from
  117.       case VALUE of
  118.          V:    STATEMENT;
  119.          V,U:  STATEMENT;
  120.          else  STATEMENT
  121.       end
  122.    into
  123.       switch (VALUE) {
  124.          case V:  STATEMENT; break;
  125.          case V:
  126.          case U:  STATEMENT; break;
  127.          default: STATEMENT;
  128.       }.
  129.  
  130.    The IN operator is translated from
  131.       VAL in [A,B,C]
  132.     into
  133.       inset(VAL, setof(A,B,C,-1)).
  134.  
  135.    The ParamCount and ParamStr functions are translated from
  136.       paramcount
  137.       paramstr(n)
  138.    into
  139.       argc
  140.       argv[n].
  141.  
  142.    Dummy parameter lists are added to function and procedure calls,
  143.    where they are required in C but not in Pascal.
  144.  
  145.    The following expression operators are translated
  146.       from  DIV  to  / ,     MOD  to  % ,
  147.             AND  to  &&,     OR   to  ||,
  148.             XOR  to  ~ ,     <>   to  !=,
  149.             NOT  to  ! ,     SHR  to  >>,
  150.             SHL  to  <<,     =    to  ==,
  151.             :=   to  = .
  152.  
  153.    The '^' symbol is translated
  154.       from  VAR^          to  *VAR,
  155.             VAR^.MEMBER   to  VAR->MEMBER.
  156.  
  157.    Exit statements are translated
  158.       from  exit    to  return.
  159.  
  160.    The New operator is translated from
  161.       new(VAR)
  162.    into
  163.       VAR = malloc(sizeof(*VAR)).
  164.  
  165. 
  166.    Procedure/function formal parameter lists are translated into a
  167.       separate procedure declaration and parameter variable
  168.       declarations, as required by C, e.g.
  169.    from
  170.       function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  171.    into
  172.       TYPE3 NAME(V1,V2)
  173.       TYPE1 V1;
  174.       TYPE2 V2;
  175.  
  176.    Procedures are translated into functions with 'void' return types.
  177.  
  178.    The special character literal syntax, ^C or #nn, is translated into
  179.    '\ooo', where ooo is the octal notation for the ascii code.
  180.  
  181.    Hex constants $hhhh are translated into 0xhhhh.
  182.  
  183.    Write and WriteLn are translated from:
  184.       write(VAR,VAR:n,VAR:n:m)
  185.       writeln(FILE,VAR,VAR,VAR)
  186.    into
  187.       printf("%d%nd%n.md",VAR,VAR,VAR)
  188.       fprintf(FILE,"%d%d%d\n",VAR,VAR,VAR).
  189.  
  190.    Read and ReadLn are translated from:
  191.       read(VAR,VAR,VAR)
  192.       readln(FILE,VAR,VAR,VAR)
  193.    into
  194.       scanf("%d%nd%d",&VAR,&VAR,&VAR)
  195.       fscanf(FILE,"%d%d%d\n",&VAR,&VAR,&VAR).
  196.  
  197.    String assignments are translated from:
  198.       VAR := "string"
  199.       VAR := VAR1 + "string"
  200.    into
  201.       strcpy(VAR, "string")
  202.       strcpy(VAR, concat(VAR1, "string")).
  203.  
  204.    String comparisons are translated from:
  205.       VAR == "string"
  206.       VAR < "string"
  207.       "string" >= VAR
  208.    into
  209.       (strcmp(VAR,"string") == 0)
  210.       (strcmp(VAR,"string") < 0)
  211.       (strcmp("string",VAR) >= 0).
  212.  
  213.    Function value assignments are translated from:
  214.       FUN_NAME := expr
  215.    into
  216.       return expr.
  217. 
  218.    Compiler directives are translated
  219.       from  {$B+/-}   to  /* #pragma */  standard_io(ON/OFF),
  220.             {$C+/-}       /* #pragma */  control_c_check(ON/OFF),
  221.             {$D+/-}       /* #pragma */  device_check(ON/OFF),
  222.             {$Fn}         /* #pragma */  max_files(n),
  223.             {$Gn}         /* #pragma */  input_file_buffer(n),
  224.             {$I+/-}       /* #pragma */  io_error_check(ON/OFF),
  225.             {$K+/-}       /* #pragma */  stack_check(ON/OFF),
  226.             {$Pn}         /* #pragma */  output_file_buffer(n),
  227.             {$R+/-}       /* #pragma */  range_check(ON/OFF),
  228.             {$U+/-}       /* #pragma */  user_interrupt(ON/OFF),
  229.             {$V+/-}       /* #pragma */  param_type_check(ON/OFF)
  230.             {$I name}     #include "name"
  231.  
  232.    Numeric statement labels are translated to label_nn.  Label identifiers
  233.    are not changed.  Local GOTO statements are handled properly.
  234.  
  235. 
  236.  
  237. The following translations were added to support Pascal/MT+:
  238. (version TPC 1.3)
  239.  
  240.    Simple Var declarations are translated from
  241.       ID external TYPE
  242.    into
  243.       extern TYPE ID.
  244.  
  245.    The following expression operators are translated
  246.       from   !   to  | ,
  247.              &   to  &.
  248.  
  249.    External function declarations are translated
  250.    from
  251.       external function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  252.       external [n] function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  253.    into
  254.       extern TYPE3 NAME()
  255.  
  256.    External procedure declarations are translated
  257.    from
  258.       external procedure NAME(V1: TYPE1; V2: TYPE2)
  259.       external [n] procedure NAME(V1: TYPE1; V2: TYPE2)
  260.    into
  261.       extern void NAME()
  262.  
  263.    Write and WriteLn are translated from:
  264.       write([ADDR(FUN)],VAR:n,VAR:n:m)
  265.    into
  266.       iprintf(FUN,"%nd%n.md",VAR,VAR)
  267.  
  268.    Read and ReadLn are translated from:
  269.       read([ADDR(FUN)],VAR,VAR)
  270.    into
  271.       iscanf(FUN,"%d%nd%d",&VAR,&VAR,&VAR)
  272.  
  273. 
  274.  
  275. Some language features that are not translated:
  276. -----------------------------------------------
  277.  
  278.    Nested procedures or function cannot be coded in C.
  279.  
  280.    VAR parameters must be manually recoded.
  281.  
  282.    File access procedures are only partially supported (assign, close, etc.).
  283.  
  284.    Ranges in the form VAL..VAL are not translated in case statements.
  285.  
  286.    Forward pointer type declarations are translated, but will not
  287.    compile in C.  They must be manually recoded.
  288.  
  289.    Variant record types should be translated into unions, but aren't.
  290.  
  291.    Bitwise AND and OR operators are always translated into the logical
  292.    operators && and ||.
  293.  
  294.    C operator precedence differs from that of Pascal, and the
  295.    differences are not translated.
  296.  
  297.    The WITH statement is not translated.
  298.  
  299.    The MEM[] and PORT[] arrays are not translated.  These should be
  300.    turned int function calls.
  301.  
  302.    Absolute variables are not (and probably cannot be) translated.
  303.  
  304. 
  305. Revision history
  306. ----------------
  307.  
  308.   09/09/85  v0.0
  309.      Initial coding by Samuel H. Smith.  Never released.
  310.  
  311.   12/19/86  v1.0
  312.      First distributed as TPC10 under shareware concept.
  313.  
  314.   04/15/87  v1.1
  315.      Corrected handling of unary minus.
  316.      Improved error messages; added error messages to object file.
  317.      Added handler for integer subrange types.
  318.      Added handling for goto statement and numeric labels.
  319.      The macro header, tpcmac.h, now contains more declarations.
  320.      Distributed as TPC11.
  321.  
  322.   04/22/87  v1.2
  323.      Corrected an error that led to a crash on lines with more than 40
  324.      leading spaces.  Distributed as TPC12.
  325.  
  326.   05/20/87  v1.3
  327.      Added support for pascal/MT+:  external procedures and variables,
  328.      special write/read indirect syntax, & and ! operators,
  329.      default string size for string declarations.
  330.      Distributed as TPC13.
  331.  
  332.