home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / pascal / tpc14.arc / TPC.DOC < prev    next >
Text File  |  1987-05-26  |  11KB  |  353 lines

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