home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l200 / 6.ddi / TRAN / ATVAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-21  |  13.2 KB  |  472 lines

  1. /* atvar.c -- Administrator Translator Variables
  2.         
  3.     Copyright (c) 1983 by James F. Gimpel
  4.     Copyright (c) 1983, 1984, 1985 by JMI Software Consultants, Inc.
  5.  
  6.         This module allows tailoring of the Basic to C
  7.         translator to allow for version peculiarities.
  8.         Modification of variables can take place prior to
  9.         compilation or at run-time as the result of user 
  10.         options (see u_opt this module).
  11.  */
  12. #include "com.h"
  13.  
  14. /* Global variables
  15.  */
  16.  
  17. GLOBAL FLAG     bcomm = 0             ; /*
  18.                     -----
  19.         * a flag to indicate whether to incorporate the
  20.         * original Basic statements as comments in the
  21.         * C program.
  22.         */
  23.  
  24. GLOBAL FLAG     bv_blt = 1         ; /*
  25.                     ------
  26.         * indicates that a blank will terminate basic 
  27.         * variables. Normally blanks are ignored.
  28.         */
  29.  
  30. GLOBAL FLAG     bv_kwpr =  1         ; /*
  31.                     -------
  32.         * KeyWord PRefix -- if on (1) this allows identifiers
  33.         * to have a keyword (reserved word) as a prefix such as
  34.         * ENDOFFILE or TOWN. If on, keywords may not be followed
  35.         * by alphanumerics without intervening blanks.
  36.         */
  37.  
  38. GLOBAL INT         bv_mxa = 0         ; /*
  39.                     ------
  40.         * the maximum number of leading alphabetic characters
  41.         * in an identifier. For minimal Basic, this should be
  42.         * 1. If 0, there is no limit but then, to avoid keywords
  43.         * from being sucked up, turn bv_blt on.
  44.         */
  45.  
  46. GLOBAL INT         bv_mxc = 0         ; /*
  47.                     ------
  48.         * the maximum number of characters in a Basic variable
  49.         * exclusive of trailing $ or other type-specifying
  50.         * characters. If = 0, there is no limit. If nonzero,
  51.         * it must be no less than bv_mxa.
  52.         */
  53.  
  54. GLOBAL FLAG     bv_rkch = 0         ; /*
  55.                     -------
  56.         * Repeated Key-word CHeck -- causes a check for a keyword
  57.         * (reserved word) at each point within a user identifier
  58.         * and if one is found the identifier is assumed terminated.
  59.         * This is usually necessary if blanks do not terminate
  60.         * identifiers (see bv_blt).
  61.         */
  62.  
  63. GLOBAL INT         chs_len = 3000        ;   /*
  64.                     -------
  65.         * length in chars. of short-term character storage
  66.         * (everything used just for a single statement)
  67.         */
  68.  
  69. GLOBAL TEXT     cmp_dir = '%'         ; /*
  70.                     -------
  71.         * Compiler-Directive Character:
  72.         * Appearance of this character at the beginning a line 
  73.         * indicates the beginning of a compiler directive.
  74.         */
  75.  
  76. GLOBAL TEXT     cmtch = '\''     ; /*
  77.                     -----
  78.         * Comment character -- introduces a comment in mid-line.
  79.         * Has no effect in a REMark or in a quoted string.
  80.         * This has precedence over the character-type declaration
  81.         * in atbls.c (array char_type).
  82.         * A zero value can be used if no comment character is wanted.
  83.         */
  84.  
  85. GLOBAL TEXT     con_ch = '\\'     ; /*
  86.                     ------
  87.         * Continuation Character: Appearance of this char.
  88.         * as the last character of a line causes the next line
  89.         * to be annexed as part of the current line with the
  90.         * con_ch replaced by blank. Appearances of con_ch other
  91.         * than as the last char. of a line are not affected by this
  92.         * declaration.
  93.         */
  94.  
  95. GLOBAL FLAG         crflag = 1         ; /*
  96.                     ------
  97.         * A flag to indicate that CR/LF pairs occur at the
  98.         * end of each input line, and should be output at
  99.         * the end of each output line. If zero, input and
  100.         * output lines end with a LF.
  101.         */
  102.  
  103. GLOBAL FLAG     def_fn  = 0         ; /*
  104.                     ------
  105.         * A flag to indicate whether "FN" is a required
  106.         * prefix for DEFined functions
  107.         */
  108.  
  109. GLOBAL FLAG     def_oa  = 1         ; /*
  110.                     ------
  111.         * A flag to indicate whether arguments are optional
  112.         * on the DEF statement
  113.         */
  114.  
  115.  
  116. GLOBAL    TEXT        ebuf[MAXEBUF] = {0}        ;    /*
  117.  
  118.         * A buffer that is used to store the current line of BASIC
  119.         * code that is being translated
  120.         */
  121.  
  122. GLOBAL    INT            endebuf = MAXEBUF        ;    /*
  123.  
  124.         * Maximum size of storage used for BASIC statement
  125.         */
  126.  
  127. GLOBAL FLAG     eflag =   0           ;   /*
  128.                     -------
  129.         * quickerer everything flag -- all BASIC variables that are
  130.         * not decalred as integer (end in %) yet never use any
  131.         * floating point operations (including explicitly set
  132.         * variables and input variables) are declared as integers
  133.         * when this flag is set.
  134.         */
  135.  
  136. GLOBAL FLAG     end_end  = 0         ; /*
  137.                     -------
  138.         * A flag to indicate whether an END statement will end the
  139.         * program text or merely cause a termination when executed.
  140.         * An END as a part of a then clause never is taken as an
  141.         * end to program text.
  142.         */
  143.  
  144. GLOBAL STRING     erprfx = "E_"     ; /*
  145.                     ------
  146.         * Error Label Prefix --
  147.         * Error labels are generated when ON ERROR GOTOs
  148.         * occur in the program. One label is generated
  149.         * for each BASIC statement. The prefix is followed
  150.         * by a numeric string.
  151.         */
  152.  
  153.  
  154. GLOBAL STRING     fbprfx = "FB"     ; /*
  155.                     ------
  156.         * Begin FOR Loop Label Prefix --
  157.         * FOR loop labels are generated when FOR-NEXT statements
  158.         * can not be translated into a for(;;) statement. This
  159.         * occurs when the NEXT occurs at a different level of
  160.         * nesting within IF-THEN-ELSE statements than the FOR
  161.         * statement it corresponds to.
  162.         */
  163.  
  164. GLOBAL STRING     feprfx = "FE"     ; /*
  165.                     ------
  166.         * End FOR Loop Label Prefix --
  167.         * FOR loop labels are generated when FOR-NEXT statements
  168.         * can not be translated into a for(;;) statement. This
  169.         * occurs when the NEXT occurs at a different level of
  170.         * nesting within IF-THEN-ELSE statements than the FOR
  171.         * statement it corresponds to.
  172.         */
  173.  
  174.  
  175. GLOBAL INT         idlimit = 7         ; /*
  176.                     -------
  177.         * Maximum discrimination on identifiers for C.
  178.         * It is presumed that the target language can support
  179.         * identifiers to an unlimited length and that it 
  180.         * imploys the first 'idlimit' of these for comparisons.
  181.         */
  182.  
  183. GLOBAL STRING     lbprfx = "l_"         ; /*
  184.                     ------
  185.         * Label Prefix -- The string prepended to Basic 
  186.         * labels to form C labels
  187.         */
  188.  
  189. GLOBAL FLAG     main_prog = 1         ; /*
  190.                     ---------
  191.         * If this flag is on (the default case) the Basic program
  192.         * is presumed to be a main program. Otherwise it is assumed
  193.         * to be a sub-program with the first label indicating the
  194.         * entry point. If proc is on other entry points may also 
  195.         * exist.
  196.         */
  197.  
  198. GLOBAL STRING     mlprfx = "ml_"     ; /*
  199.                     ------
  200.         * Multipliers for dynamic arrays are constructed by
  201.         * prepending this prefix to a generated integer.
  202.         */
  203.  
  204. GLOBAL INT         mstmlen = 2000         ; /*
  205.                     -------
  206.         * Maximum Statement Length 
  207.         */
  208.  
  209. GLOBAL TEXT     mxposint[] = "32767" ; /*
  210.                 --------
  211.         * Maximum Positive Integer (as a text string)
  212.         * Numbers in the input BASIC program that
  213.         * are larger than this, are represented as
  214.         * floating point numbers
  215.         */
  216.  
  217. GLOBAL FLAG     neediv = 0         ; 
  218.         /*         ------
  219.         * Need Initial Value for defined variables
  220.         * With some compilers, notably Whitesmiths', defined
  221.         * variables need initializers.
  222.         */
  223.  
  224. GLOBAL FLAG     optlet =  1         ; /*
  225.                     ------
  226.         *  a flag to indicate whether LET is optional 
  227.         *  in Basic assignment statements.
  228.         */
  229.  
  230. GLOBAL FLAG     proc = 1         ; /*
  231.                     ----
  232.         * If on (=1), the translator will attempt to create
  233.         * procedures from GOSUB segments. Without this
  234.         * flag, the translator produces one long program.
  235.         * If on, inter-GOSUB GOTO's are prohibited.
  236.         */
  237.  
  238. GLOBAL STRING     prprfx = "pr_"         ; /*
  239.                     ------
  240.         * procedure names are constructed by appending
  241.         * a label to the prefix (the procedure prefix)
  242.         */
  243.  
  244.  
  245. GLOBAL FLAG     qflag =   0           ;   /*
  246.                     -------
  247.         * quickerer flag -- all BASIC variables that are
  248.         * not decalred as integer (end in %) yet never use any
  249.         * floating point operations (excluding explicitly set
  250.         * variables and input variables) are declared as integers
  251.         * when this flag is set.
  252.         */
  253.  
  254. GLOBAL FLAG     sflag =   0           ;   /*
  255.         * speed flag -- invoke code optimizations (eg. ++x, etc.)
  256.         */
  257.  
  258.  
  259. GLOBAL FLAG     st_flag = 1         ; /*
  260.                     -------
  261.         * static flag -- all BASIC variables are defined as static
  262.         * (and hence can not be referenced from other source files).
  263.         * Otherwise, all variables are globally defined (and may
  264.         * conflict library function names)
  265.         */
  266.  
  267. GLOBAL TEXT     stmsep = ':'         ; /*
  268.                     ------
  269.         * Statement Separator; This character allows
  270.         * multiple Basic statements to be
  271.         * placed on the same line (with a single label). 
  272.         */
  273.  
  274. GLOBAL STRING     stndtp = "float"     ; /*
  275.                     ------
  276.         * Standard Type for arithmetic variables;
  277.         * may be "float" or "double".
  278.         */
  279.  
  280. GLOBAL FLAG     strace = 0         ; /*
  281.                     ------
  282.         *  Statement Trace -- a flag that controls the insertion
  283.         *  into the C code of a message-producing call on each
  284.         *  statement label.
  285.         */
  286.  
  287.  
  288. GLOBAL INT         strlpfx = 1;             /*
  289.                     -------
  290.         * String Length Prefix
  291.         * Determines the numbe of bytes used to represent the
  292.         * length of a BASIC string variable. A value of zero
  293.         * means strings can have unlimited length, but can not
  294.         * have embedded null characters (binary zero bytes).
  295.         * A value of one allows strings of up to 255 charaters.
  296.         * A value of two allows strings of up to 65535 characters.
  297.         */
  298.  
  299. GLOBAL COUNT     systabs = 8;             /*
  300.                     -------
  301.         * System Tab Stops
  302.         * Indicates the number of spaces that the host operating
  303.         * system translates tab characters into. This is used to
  304.         * produce uniformly indented C code on all systems. See
  305.         * the description of the tabstops variable (below) for
  306.         * more information.
  307.         */
  308.  
  309. GLOBAL COUNT     tabstops = 4;             /*
  310.                     --------
  311.         * Tab Stops
  312.         * This value determines the amount of leading whitespace
  313.         * for each indent level in the output C code. It is used
  314.         * in conjunction with the systabs value to output a
  315.         * combination of tabs and spaces at the beginning of each
  316.         * output line.
  317.         */
  318.  
  319. GLOBAL STRING     tlprfx = "g_"         ; /*
  320.                     ------
  321.         * Temporary Label Prefix --
  322.         * A so-called temporary label, better named generated
  323.         * label, is one generated by the translator, usually
  324.         * for the purpose of receiving control back from a call
  325.         */
  326.  
  327. GLOBAL STRING     tiprfx = "it_"     ; /*
  328.                     ------
  329.         * Temporary Integer Prefix --
  330.         * Integer Temporaries are denoted by identifiers
  331.         * consisting of this prefix followed by some numeral
  332.         */
  333.  
  334. GLOBAL STRING     tfprfx = "ft_"     ; /*
  335.                     ------
  336.         * Temporary Float Prefix --
  337.         * Same as tiprfx except for Float
  338.         */
  339.  
  340. GLOBAL STRING     tdprfx = "dt_"     ; /*
  341.                     ------
  342.         * Temporary Double Prefix --
  343.         * Same as tiprfx except for Double
  344.         */
  345.  
  346. GLOBAL STRING     tsprfx = "st_"     ; /*
  347.                     ------
  348.         * Temporary String Prefix --
  349.         * String Temporaries are denoted by identifiers
  350.         * consisting of this prefix followed by some numeral
  351.         */
  352.  
  353. GLOBAL FLAG     vflag =   0           ;   /*
  354.                     -------
  355.         * quickerer verify flag -- all BASIC variables that are
  356.         * not decalred as integer (end in %) yet never use any
  357.         * floating point operations (including explicitly set
  358.         * variables and input variables) are declared as integers,
  359.         * if the user specifies them to be integer when the
  360.         * program interactively queries the user,
  361.         * when this flag is set.
  362.         */
  363.  
  364. /* u_opt() allows the administrator to set his options
  365.     based on flags provided by the end user.
  366.     Arguments are exactly the same as provided to the
  367.     main program. This routine is called after bfinit() but 
  368.     in time to allow parameters such as chs_len to
  369.     be reset.
  370.  */
  371.  
  372. GLOBAL STRING infile = NULL; /* name of input file */
  373. GLOBAL STRING outfile = NULL; /* name of output file */
  374.  
  375. VOID u_opt(argc, argv)
  376.     COUNT argc;
  377.     STRING *argv;
  378.     {
  379. #ifdef TRACE
  380.     IMPORT TRBITS trace;
  381. #endif
  382.     IMPORT INT strlpfx;
  383.     IMPORT FLAG bcomm, bv_blt, bv_kwpr, bv_rkch,
  384.         main_prog, proc, st_flag, qflag, vflag, eflag, sflag;
  385.     EXFUNC LONG cvt_sh();
  386.     EXFUNC VOID err_cat(), err_gen();
  387.     EXFUNC XCHAR to_low();
  388.     IMPORT TEXT *infile, *outfile;
  389.     TEXT *s;
  390.     BOOL oflag;
  391.  
  392.     oflag = NO;
  393.     while (--argc > 0)
  394.         {
  395.         if (*(s = *(++argv)) == '-')
  396.             while (*++s)
  397.                 switch (*s)
  398.                     {
  399.                 case 'b':     /* blank-oriented scanning */
  400.                 case 'B':
  401.                     bv_blt = 1 - bv_blt;
  402.                     bv_kwpr = 1 - bv_kwpr;
  403.                     bv_rkch = 1 - bv_rkch;
  404.                     break;
  405.                 case 'c':     /* insert BASIC statements as comments */
  406.                 case 'C':
  407.                     bcomm = 1;
  408.                     break;
  409.                 case 'e':     /* optimize all variables whenever possible*/
  410.                 case 'E':
  411.                     eflag = 1;
  412.                     break;
  413.                 case 'f':     /* translate a function */
  414.                 case 'F':
  415.                     main_prog = 0;
  416.                     break;
  417.                 case 'g':     /* make all variables global  */
  418.                 case 'G':
  419.                     st_flag = 0;
  420.                     break;
  421.                 case 'm':     /* make one monolithic function */
  422.                 case 'M':
  423.                     proc = 0;
  424.                     break;
  425.                 case 'n':     /* make null terminated strings */
  426.                 case 'N':
  427.                     strlpfx = 0;
  428.                     break;
  429.                 case 'o':     /* next argument is output file name */
  430.                 case 'O':
  431.                     oflag = YES;
  432.                     break;
  433.                 case 'q':     /* optimize variables */
  434.                 case 'Q':
  435.                     qflag = 1;
  436.                     break;
  437.                 case 's':     /* invoke code optimizations */
  438.                 case 'S':
  439.                     sflag = YES;
  440.                     break;
  441.                 case 't':     /* enable statement execution tracing */
  442.                 case 'T':
  443.                     strace = YES;
  444.                     break;
  445.                 case 'v':     /* optimize to INTS but verify */
  446.                 case 'V':
  447.                     vflag = 1;
  448.                     break;
  449. #ifdef TRACE
  450.                 case 'x':   /* refers to tracing the internals of BASTOC */
  451.                 case 'X':
  452.                     if (s[1])
  453.                         trace = cvt_sh(s + 1);
  454.                     else
  455.                         trace = ~0L; /* turn everything on */
  456.                     break;
  457. #endif
  458.                 default: 
  459.                     err_gen("flag ignored $s\n", s);
  460.                     break;
  461.                     }
  462.         else if (oflag && outfile == NULL)
  463.             outfile = s;
  464.         else if (infile == NULL)
  465.             infile = s;
  466.         else if (outfile == NULL)
  467.             outfile = s;
  468.         else
  469.             err_cat("redundant argument: $s\n", s);
  470.         }
  471.     }
  472.