home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPTC16.ZIP / TPTC.DOC < prev    next >
Encoding:
Text File  |  1993-01-04  |  12.8 KB  |  441 lines

  1.  
  2.  
  3.                        TPTC16 - Translate Pascal to C
  4.                            Version 1.6, 13-Feb-88
  5.  
  6.                  (C) Copyright 1986, 1988 by Samuel H. Smith
  7.                             All rights reserved.
  8.  
  9.  
  10. This program will read a turbo pascal source file and convert it into the 
  11. corresponding C source code.   It does about much of the work required in
  12. a full translation.
  13.  
  14. Usage:  TPTC d:path\filename[.ext] outfile
  15.  
  16.   d:   drive and path are optional
  17.   ext  defaults to .PAS
  18.  
  19. Example:
  20.    TPTC program.pas program.c
  21.  
  22.  
  23.  
  24. LICENSE
  25. =======
  26.  
  27.    SourceWare: What is it?
  28.    -----------------------
  29.    SourceWare is my name for a unique concept in user supported software. 
  30.    
  31.    Programs distributed under the SourceWare concept always offer complete 
  32.    source code. 
  33.  
  34.    This package can be freely distributed so long as it is not modified 
  35.    or sold for profit.  If you find that this program is valuable,  you 
  36.    can send me a donation for what you think it is worth.  I suggest 
  37.    about $20. 
  38.  
  39.    Send your contributions to:
  40.       Samuel. H. Smith
  41.       5119 N. 11 ave 332
  42.       Phoenix, Az 85013
  43.  
  44.  
  45.    Why SourceWare?
  46.    ---------------
  47.    Why do I include source code?  Why isn't the donation manditory?  The 
  48.    value of good software should be self-evident.  The source code is the 
  49.    key to complete understanding of a program.  You can read it to find 
  50.    out how things are done.  You can also change it to suit your needs, 
  51.    so long as you do not distribute the modified version without my 
  52.    consent. 
  53.  
  54.  
  55.    Copyright
  56.    ---------
  57.    If you modify this program, I would appreciate a copy of the new 
  58.    source code.   I am holding the copyright on the source code, so 
  59.    please don't delete my name from the program files or from the 
  60.    documentation. 
  61.  
  62.  
  63. SUPPORT
  64. =======
  65.  
  66.    I work very hard to produce a software package of the highest 
  67.    quality and functionality.  I try to look into all reported 
  68.    bugs, and will generally fix reported problems within a few 
  69.    days.  
  70.  
  71.    Since this is user supported software under the SourceWare 
  72.    concept,  I don't expect you to contribute if you don't like it 
  73.    or if it doesn't meet your needs. 
  74.  
  75.    If you have any questions, bugs, or suggestions, please contact 
  76.    me at: 
  77.        The Tool Shop BBS
  78.         (602) 279-2673
  79.  
  80.    The latest version is always available for downloading. 
  81.  
  82.    Enjoy!     Samuel H. Smith
  83.               Author and Sysop of The Tool Shop.
  84.  
  85.  
  86.  
  87.  
  88. The following language constructs are translated:
  89. ------------------------------------------------
  90.  
  91.    Comments are translated from either {...} or (*...*) into /*...*/.
  92.  
  93.    Begin and End are translated into { and }.
  94.  
  95.    Const declarations are translated from
  96.       ID = VALUE
  97.    into
  98.       static ID = VALUE.
  99.  
  100.    Simple Var declarations are translated from
  101.       ID TYPE
  102.    into
  103.       TYPE ID.
  104.  
  105.    The following simple types are translated
  106.       from  BOOLEAN    to   char,
  107.             INTEGER    to   int,
  108.             REAL       to   double.
  109.  
  110.    Integer subrange types are translated into integers.
  111.  
  112.    Record types are translated from
  113.       ID = record MEMBER-LIST end
  114.    into
  115.       typedef struct { MEMBER-LIST } ID.
  116.  
  117.    Enumeration types are translated from
  118.       ID = (...)
  119.    into
  120.       typedef enum {...} ID.
  121.  
  122.    Array types are translated from
  123.       ID = array [RANGE] of TYPE
  124.    into
  125.       typedef TYPE ID[RANGE].
  126.  
  127.    Pointer types are translated from
  128.       ID = ^DEFINED-TYPE
  129.    into
  130.       DEFINED-TYPE *ID.
  131.  
  132.    String types are translated from
  133.       ID = string[N]
  134.    into
  135.       typedef char ID[N].
  136.  
  137.    File types are translated from
  138.       ID = text[N]
  139.       ID = text
  140.    into
  141.       FILE *ID
  142.       int ID.
  143.  
  144.    For statements are translated from
  145.       for VAR := FIRST to LAST do STATEMENT
  146.       for VAR := FIRST downto LAST do statement
  147.    into
  148.       for (VAR = FIRST; VAR <= LAST; VAR++) STATEMENT
  149.       for (VAR = FIRST; VAR >= LAST; VAR--) STATEMENT
  150.  
  151.    While statements are translated from
  152.       while COND do STATEMENT
  153.    into
  154.       while (COND) statement.
  155.  
  156.    Repeat statements are translated from
  157.       repeat STATEMENTS until COND
  158.    into
  159.       do { STATEMENTS } while(!COND).
  160.  
  161.    If statements are translated from
  162.       if COND then STATEMENT else STATEMENT
  163.    into
  164.       if (COND) STATEMENT; else STATEMENT.
  165.  
  166. Case statements are translated from
  167.       case VALUE of
  168.          V:    STATEMENT;
  169.          V,U:  STATEMENT;
  170.          else  STATEMENT
  171.       end
  172.    into
  173.       switch (VALUE) {
  174.          case V:  STATEMENT; break;
  175.          case V:
  176.          case U:  STATEMENT; break;
  177.          default: STATEMENT;
  178.       }.
  179.  
  180.    The IN operator is translated from
  181.       VAL in [A,B,C]
  182.     into
  183.       inset(VAL, setof(A,B,C,-1)).
  184.  
  185.    The ParamCount and ParamStr functions are translated from
  186.       paramcount
  187.       paramstr(n)
  188.    into
  189.       argc
  190.       argv[n].
  191.  
  192.    Dummy parameter lists are added to function and procedure calls,
  193.    where they are required in C but not in Pascal.
  194.  
  195.    The following expression operators are translated
  196.       from  DIV  to  / ,     MOD  to  % ,
  197.             AND  to  &&,     OR   to  ||,
  198.             XOR  to  ~ ,     <>   to  !=,
  199.             NOT  to  ! ,     SHR  to  >>,
  200.             SHL  to  <<,     =    to  ==,
  201.             :=   to  = .
  202.  
  203.    The '^' symbol is translated
  204.       from  VAR^          to  *VAR,
  205.             VAR^.MEMBER   to  VAR->MEMBER.
  206.  
  207.    Exit statements are translated
  208.       from  exit    to  return.
  209.  
  210.    The New operator is translated from
  211.       new(VAR)
  212.    into
  213.       VAR = malloc(sizeof(*VAR)).
  214.  
  215.  
  216.    Procedure/function formal parameter lists are translated into a
  217.       separate procedure declaration and parameter variable
  218.       declarations, as required by C, e.g.
  219.    from
  220.       function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  221.    into
  222.       TYPE3 NAME(V1,V2)
  223.       TYPE1 V1;
  224.       TYPE2 V2;
  225.  
  226.    Procedures are translated into functions with 'void' return types.
  227.  
  228.    The special character literal syntax, ^C or #nn, is translated into
  229.    '\ooo', where ooo is the octal notation for the ascii code.
  230.  
  231.    Hex constants $hhhh are translated into 0xhhhh.
  232.  
  233.    Write and WriteLn are translated from:
  234.       write(VAR,VAR:n,VAR:n:m)
  235.       writeln(FILE,VAR,VAR,VAR)
  236.    into
  237.       printf("%d%nd%n.md",VAR,VAR,VAR)
  238.       fprintf(FILE,"%d%d%d\n",VAR,VAR,VAR).
  239.  
  240.    Read and ReadLn are translated from:
  241.       read(VAR,VAR,VAR)
  242.       readln(FILE,VAR,VAR,VAR)
  243.    into
  244.       scanf("%d%nd%d",&VAR,&VAR,&VAR)
  245.       fscanf(FILE,"%d%d%d\n",&VAR,&VAR,&VAR).
  246.  
  247.    String assignments are translated from:
  248.       VAR := "string"
  249.       VAR := VAR1 + "string"
  250.    into
  251.       strcpy(VAR, "string")
  252.       strcpy(VAR, concat(VAR1, "string")).
  253.  
  254.    String comparisons are translated from:
  255.       VAR == "string"
  256.       VAR < "string"
  257.       "string" >= VAR
  258.    into
  259.       (strcmp(VAR,"string") == 0)
  260.       (strcmp(VAR,"string") < 0)
  261.       (strcmp("string",VAR) >= 0).
  262.  
  263.    Function value assignments are translated from:
  264.       FUN_NAME := expr
  265.    into
  266.       return expr.
  267.  
  268.    Numeric statement labels are translated to label_nn.  
  269.    Label identifiers are not changed.  
  270.    Local GOTO statements are handled properly. 
  271.  
  272.    Nested procedures are "flattened" out, but local variable sharing and 
  273.    local scoping are not translated. 
  274.  
  275.  
  276. The following translations were added to support Pascal/MT+:
  277. (version TPTC 1.4)
  278.  
  279.    Var declarations are translated from
  280.       ID external TYPE
  281.    into
  282.       extern TYPE ID.
  283.  
  284.    The following expression operators are translated
  285.       from   !   to  | ,    |    to   |,
  286.              &   to  & ,    ~    to   !,
  287.              ?   to  ! ,    \    to   !.
  288.  
  289.    External function declarations are translated
  290.    from
  291.       external function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  292.       external [n] function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  293.    into
  294.       extern TYPE3 NAME()
  295.  
  296.    External procedure declarations are translated
  297.    from
  298.       external procedure NAME(V1: TYPE1; V2: TYPE2)
  299.       external [n] procedure NAME(V1: TYPE1; V2: TYPE2)
  300.    into
  301.       extern void NAME()
  302.  
  303.    Write and WriteLn are translated from:
  304.       write([ADDR(FUN)],VAR:n,VAR:n:m)
  305.       write([],VAR:n,VAR:n:m)
  306.    into
  307.       iprintf(FUN,"%nd%n.md",VAR,VAR)
  308.       printf("%nd%n.md",VAR,VAR)
  309.  
  310.    Read and ReadLn are translated from:
  311.       read([ADDR(FUN)],VAR,VAR)
  312.       read([],VAR,VAR)
  313.    into
  314.       iscanf(FUN,"%d%nd%d",&VAR,&VAR,&VAR)
  315.       scanf("%d%nd%d",&VAR,&VAR,&VAR)
  316.  
  317.    Long integer constants #nnn are translated into nnnL.
  318.  
  319.    Direct I/O port and memory references are translated:
  320.       portw[expr] := expr + port[n]
  321.       mem[seg:ofs] := memw[seg:ofs] + expr
  322.    into
  323.       outport(expr, expr+inportb(n))
  324.       pokeb(seg,ofs, peek(seg,ofs)+expr)
  325.  
  326.  
  327.  
  328. Some language features that are not translated:
  329. -----------------------------------------------
  330.  
  331.    Local variable sharing among nested procedures is not translated. 
  332.  
  333. ***********
  334.    VAR parameters must be manually recoded.
  335.  
  336.    File access procedures are only partially supported (assign, close, 
  337.    etc.). 
  338.  
  339.    Ranges in the form VAL..VAL are not translated in case statements. 
  340.  
  341.    Forward pointer type declarations are translated, but will not compile 
  342.    in C.  They must be manually recoded. 
  343.  
  344.    Variant record types should be translated into unions, but aren't. 
  345.  
  346.    Bitwise AND and OR operators are always translated into the logical 
  347.    operators && and ||. 
  348.  
  349.    C operator precedence differs from that of Pascal, and the differences 
  350.    are not translated. 
  351.  
  352.    The WITH statement is not translated.
  353.  
  354. ************
  355.    The MEM[] and PORT[] arrays are not translated.  These should be
  356.    turned into function calls.
  357.  
  358.    Absolute variables are not (and probably cannot be) translated.
  359.  
  360.  
  361. Revision history
  362. ----------------
  363.  
  364.   09/09/85  v0.0
  365.      Initial coding by Samuel H. Smith.  Never released.
  366.  
  367.   12/19/86  v1.0
  368.      First distributed as TPC10 under shareware concept.
  369.  
  370.   04/15/87  v1.1
  371.      Corrected handling of unary minus.  Improved error messages; added 
  372.      error messages to object file.  Added handler for integer subrange 
  373.      types.  Added handling for goto statement and numeric labels.  The 
  374.      macro header, tpcmac.h, now contains more declarations.  Distributed 
  375.      as TPC11. 
  376.  
  377.   04/22/87  v1.2
  378.      Corrected an error that led to a crash on lines with more than 40 
  379.      leading spaces.  Distributed as TPC12. 
  380.  
  381.   05/20/87  v1.3
  382.      Added support for pascal/MT+:  external procedures and variables, 
  383.      special write/read indirect syntax, & and ! operators, default 
  384.      string size for string declarations.  Distributed as TPC13. 
  385.  
  386.   05/26/87  v1.4
  387.      Additional support for pascal/MT+.   The translator "shifts" into a 
  388.      MT+ specific mode when it recognizes the 'MODULE' statement. The 
  389.      '|' operator is recognized for bitwise OR.  The '\', '?' and '~' 
  390.      operators are all translated into a unary not (is this right, 
  391.      Noam?).  Read(ln) and Write(ln) now support the special case of "[]" 
  392.      for the I/O routine.  Long integer literals are translated from 
  393.      '#nnn' to 'nnnL'.  Distributed as TPC14. 
  394.  
  395. additions - pretpc15
  396.    translation if mem, memw, port and portw into turbo-c
  397.    fixed bug in 'else' part of tpas case statement
  398.    added some tpcmac.h declarations
  399.  
  400. changes
  401.    -- turbo-c procedure declaration syntax
  402.    -- arrays subscripted by enumeration types
  403.    -- fails to handle null else clause in case statement
  404.    -- include intermediate cases in swith() .. case x..y
  405.    -- pointer/var parameter translation *id.mem should be id->mem
  406.    -- pointer/var parameter translation *id[n] should be id[n]
  407.    -- concat(...)+char and string+char not detected as string/character
  408.         concat operation.
  409.    -- detect concat(concat... and replace with a sprintf variant
  410.  
  411. -- changed sprintf calls to sbld calls to preserve sources during build
  412. -- pos(c,str) and pos(str,str) are now separately translated
  413.  
  414. -- added 'base' to symbol table; use to add base-subscript offset
  415.    in all subscript references.
  416. -- moved typename translations to tpcmac.h header
  417. -- fixed bug in non-translation of tshell directives
  418. -- forward pointer declarations
  419. -- translate inline into asm statements
  420. -- complete forward translation
  421.  
  422. ---------------
  423. 13-oct-87
  424. -- improved string and array parameter translations
  425. -- string returns are now translated into char *
  426.  
  427. 15-oct-87
  428. -- corrected error in typed constant translation where nested
  429.    records are initialized.
  430. -- variant record declarations are translated into unions
  431.    but no variant expression translations are done.
  432. -- changed nested procedure error messages to include procedure name.
  433.  
  434.  
  435.   02/13/88  v1.6
  436.      Converted to TPAS 4.0 format; released under the SourceWare concept
  437.      (see LICENSE.DOC).  Documentation is still not ready...
  438.  
  439.  
  440.  
  441.