home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / turbopas / tutorpas.arc / TUTOR10.DOC < prev    next >
Text File  |  1989-05-21  |  113KB  |  3,986 lines

  1. OPA A
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.                             LET'S BUILD A COMPILER!
  30.  
  31.                                        By
  32.  
  33.                             Jack W. Crenshaw, Ph.D.
  34.  
  35.                                   21 May 1989
  36.  
  37.  
  38.                            Part X: INTRODUCING "TINY"
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. PA A
  68.  
  69.  
  70.  
  71.  
  72.  
  73.        *****************************************************************
  74.        *                                                               *
  75.        *                        COPYRIGHT NOTICE                       *
  76.        *                                                               *
  77.        *   Copyright (C) 1989 Jack W. Crenshaw. All rights reserved.   *
  78.        *                                                               *
  79.        *****************************************************************
  80.  
  81.  
  82.        INTRODUCTION
  83.  
  84.        In the last installment, I showed you the general  idea  for  the
  85.        top-down development of  a  compiler.    I gave you the first few
  86.        steps  of  the process for compilers for  Pascal  and  C,  but  I
  87.        stopped  far  short  of  pushing  it through to completion.   The
  88.        reason was simple: if we're going to produce  a  real, functional
  89.        compiler  for  any  language, I'd rather  do  it  for  KISS,  the
  90.        language that I've been defining in this tutorial series.
  91.  
  92.        In this installment, we're going to do just that, for a subset of
  93.        KISS which I've chosen to call TINY.
  94.  
  95.        The process  will be essentially that outlined in Installment IX,
  96.        except  for  one  notable  difference.   In that  installment,  I
  97.        suggested  that  you  begin  with  a full BNF description of  the
  98.        language.  That's fine for something like Pascal or C,  for which
  99.        the language definition is  firm.   In the case of TINY, however,
  100.        we don't yet have a full  description  ... we seem to be defining
  101.        the language as we go.  That's OK.    In  fact,  it's preferable,
  102.        since we can tailor the language  slightly  as we go, to keep the
  103.        parsing easy.
  104.  
  105.        So in the development  that  follows,  we'll  actually be doing a
  106.        top-down development of BOTH the  language and its compiler.  The
  107.        BNF description will grow along with the compiler.
  108.  
  109.        In this process, there will be a number of decisions to  be made,
  110.        each of which will influence the BNF and therefore the  nature of
  111.        the language.   At  each  decision  point I'll try to remember to
  112.        explain  the  decision  and the rationale behind my choice.  That
  113.        way, if you happen to hold a different opinion and would prefer a
  114.        different option, you can choose it instead.  You  now  have  the
  115.        background  to  do  that.  I guess the important thing to note is
  116.        that  nothing  we  do  here  is  cast  in  concrete.  When YOU'RE
  117.        designing YOUR language, you should feel free to do it YOUR way.
  118.  
  119.        Many of you may be asking at this point: Why bother starting over
  120.        from  scratch?  We had a working subset of KISS as the outcome of
  121.        Installment  VII  (lexical  scanning).  Why not just extend it as
  122.        needed?  The  answer  is  threefold.    First of all, I have been
  123.        making  a  number  of changes to further simplify the program ...
  124.        changes  like  encapsulating  the  code generation procedures, so
  125.        that  we  can  convert to a different target machine more easily.
  126.        Second, I want you to see how the development can indeed  be doneA*A*
  127.                                      - 2 -
  128. PA A
  129.  
  130.  
  131.  
  132.  
  133.  
  134.        from the top down as outlined in the last installment.   Finally,
  135.        we both need the practice.  Each time I go through this exercise,
  136.        I get a little better at it, and you will, also.
  137.  
  138.  
  139.        GETTING STARTED
  140.  
  141.        Many  years  ago  there were languages called  Tiny  BASIC,  Tiny
  142.        Pascal, and Tiny C, each of which was a subset of its parent full
  143.        language.  Tiny BASIC,  for  example,  had  only single-character
  144.        variable names and global variables.   It supported only a single
  145.        data type.  Sound familiar?  At this point we have almost all the
  146.        tools we need to build a compiler like that.
  147.  
  148.        Yet a language called Tiny-anything  still  carries  some baggage
  149.        inherited from its parent language.   I've often wondered if this
  150.        is a  good  idea.    Granted,  a  language based upon some parent
  151.        language will have the  advantage  of  familiarity, but there may
  152.        also  be  some  peculiar syntax carried over from the parent that
  153.        may tend  to add unnecessary complexity to the compiler. (Nowhere
  154.        is this more true than in Small C.)
  155.  
  156.        I've wondered just how small and simple a compiler could  be made
  157.        and  still  be  useful, if it were designed from the outset to be
  158.        both easy to use and to  parse.    Let's find out.  This language
  159.        will just be called "TINY," period.  It's a subset of KISS, which
  160.        I  also  haven't  fully  defined,  so  that  at  least  makes  us
  161.        consistent (!).  I suppose you could call it TINY KISS.  But that
  162.        opens  up a whole can of worms involving  cuter  and  cuter  (and
  163.        perhaps more risque) names, so let's just stick with TINY.
  164.  
  165.        The main limitations  of  TINY  will  be because of the things we
  166.        haven't yet covered, such as data types.  Like its cousins Tiny C
  167.        and Tiny BASIC,  TINY  will  have  only one data type, the 16-bit
  168.        integer.    The  first  version  we  develop  will also  have  no
  169.        procedure  calls  and  will  use single-character variable names,
  170.        although as you will see we can remove these restrictions without
  171.        much effort.
  172.  
  173.        The language I have in mind will share some of the  good features
  174.        of  Pascal,  C,  and Ada.  Taking a lesson from the comparison of
  175.        the Pascal and  C  compilers in the previous installment, though,
  176.        TINY will have a decided Pascal flavor.  Wherever  feasible,    a
  177.        language structure will  be  bracketed by keywords or symbols, so
  178.        that  the parser will know where it's  going  without  having  to
  179.        guess.
  180.  
  181.        One other ground rule:  As we go, I'd like  to  keep the compiler
  182.        producing real, executable code.  Even though it may not  DO much
  183.        at the beginning, it will at least do it correctly.
  184.  
  185.        Finally,  I'll  use  a couple of Pascal  restrictions  that  make
  186.        sense:  All data and procedures must be declared before  they are
  187.        used.  That makes good sense,  even  though for now the only dataA*A*
  188.                                      - 3 -
  189. PA A
  190.  
  191.  
  192.  
  193.  
  194.  
  195.        type we'll use  is a word.  This rule in turn means that the only
  196.        reasonable place to put the  executable code for the main program
  197.        is at the end of the listing.
  198.  
  199.        The top-level definition will be similar to Pascal:
  200.  
  201.  
  202.             <program> ::= PROGRAM <top-level decl> <main> '.'
  203.  
  204.  
  205.        Already, we've reached a decision point.  My first thought was to
  206.        make the main block optional.   It  doesn't seem to make sense to
  207.        write a "program" with no main program, but it does make sense if
  208.        we're  allowing  for  multiple modules, linked together.    As  a
  209.        matter of fact,  I intend to allow for this in KISS.  But then we
  210.        begin  to open up a can of worms that I'd rather leave closed for
  211.        now.  For example, the  term "PROGRAM" really becomes a misnomer.
  212.        The MODULE of Modula-2 or the Unit of Turbo Pascal would  be more
  213.        appropriate.  Second,  what  about  scope  rules?    We'd  need a
  214.        convention for  dealing  with  name  visibility  across  modules.
  215.        Better  for  now  to  just  keep  it  simple  and ignore the idea
  216.        altogether.
  217.  
  218.        There's also a decision in choosing to require  the  main program
  219.        to  be  last.    I  toyed  with  the idea of making its  position
  220.        optional,  as  in  C.  The nature of SK*DOS, the OS I'm compiling
  221.        for, make this very easy to do.   But  this  doesn't  really make
  222.        much sense in view of the Pascal-like requirement  that  all data
  223.        and procedures  be declared before they're referenced.  Since the
  224.        main  program can only call procedures  that  have  already  been
  225.        declared, the only position that makes sense is at the end,  a la
  226.        Pascal.
  227.  
  228.        Given  the  BNF  above, let's write a parser that just recognizes
  229.        the brackets:
  230.  
  231.  
  232.        {--------------------------------------------------------------}
  233.        {  Parse and Translate a Program }
  234.  
  235.        procedure Prog;
  236.        begin
  237.           Match('p');
  238.           Header;
  239.           Prolog;
  240.           Match('.');
  241.           Epilog;
  242.        end;
  243.        {--------------------------------------------------------------}
  244.  
  245.  
  246.        The procedure Header just emits  the startup code required by the
  247.        assembler:A6A6
  248.                                      - 4 -A*A*
  249. PA A
  250.  
  251.  
  252.  
  253.  
  254.  
  255.        {--------------------------------------------------------------}
  256.        { Write Header Info }
  257.  
  258.        procedure Header;
  259.        begin
  260.           WriteLn('WARMST', TAB, 'EQU $A01E');
  261.        end;
  262.        {--------------------------------------------------------------}
  263.  
  264.  
  265.        The procedures Prolog and  Epilog  emit  the code for identifying
  266.        the main program, and for returning to the OS:
  267.  
  268.  
  269.        {--------------------------------------------------------------}
  270.        { Write the Prolog }
  271.  
  272.        procedure Prolog;
  273.        begin
  274.           PostLabel('MAIN');
  275.        end;
  276.  
  277.  
  278.        {--------------------------------------------------------------}
  279.        { Write the Epilog }
  280.  
  281.        procedure Epilog;
  282.        begin
  283.           EmitLn('DC WARMST');
  284.           EmitLn('END MAIN');
  285.        end;
  286.        {--------------------------------------------------------------}
  287.  
  288.  
  289.        The  main program just calls Prog, and then  looks  for  a  clean
  290.        ending:
  291.  
  292.  
  293.        {--------------------------------------------------------------}
  294.        { Main Program }
  295.  
  296.        begin
  297.           Init;
  298.           Prog;
  299.           if Look <> CR then Abort('Unexpected data after ''.''');
  300.        end.
  301.        {--------------------------------------------------------------}
  302.  
  303.  
  304.        At this point, TINY  will  accept  only  one input "program," the
  305.        null program:
  306.  
  307.  
  308.             PROGRAM .   (or 'p.' in our shorthand.)A*A*
  309.                                      - 5 -
  310. PA A
  311.  
  312.  
  313.  
  314.  
  315.  
  316.        Note, though, that the  compiler  DOES  generate correct code for
  317.        this program.  It will run, and do  what  you'd  expect  the null
  318.        program to do, that is, nothing but return gracefully to the OS.
  319.  
  320.        As a matter of interest, one of my  favorite  compiler benchmarks
  321.        is to compile, link,  and  execute  the  null program in whatever
  322.        language   is   involved.     You  can  learn  a  lot  about  the
  323.        implementation by measuring  the  overhead  in  time  required to
  324.        compile what should be a trivial case.  It's also  interesting to
  325.        measure the amount of code produced.  In many compilers, the code
  326.        can be fairly large, because they always include  the  whole run-
  327.        time  library whether they need it or not.    Early  versions  of
  328.        Turbo Pascal produced a 12K object file for  this  case.    VAX C
  329.        generates 50K!
  330.  
  331.        The  smallest  null  programs  I've  seen are those  produced  by
  332.        Modula-2 compilers, and they run about 200-800 bytes.
  333.  
  334.        In the case of TINY, we HAVE no run-time library  as  yet, so the
  335.        object code is indeed tiny:  two  bytes.    That's  got  to  be a
  336.        record, and it's  likely  to  remain  one since it is the minimum
  337.        size required by the OS.
  338.  
  339.        The  next step is to process the code for the main program.  I'll
  340.        use the Pascal BEGIN-block:
  341.  
  342.  
  343.             <main> ::= BEGIN <block> END
  344.  
  345.  
  346.        Here,  again,  we  have made a decision.  We could have chosen to
  347.        require a "PROCEDURE MAIN" sort of declaration, similar to C.   I
  348.        must  admit  that  this  is  not  a bad idea at all ...  I  don't
  349.        particularly  like  the  Pascal  approach  since I tend  to  have
  350.        trouble locating the main  program  in a Pascal listing.  But the
  351.        alternative is a little awkward, too, since you have to deal with
  352.        the  error condition where the user omits  the  main  program  or
  353.        misspells its name.  Here I'm taking the easy way out.
  354.  
  355.        Another solution to the "where is the main program" problem might
  356.        be to require a name for  the  program, and then bracket the main
  357.        by
  358.  
  359.  
  360.             BEGIN <name>
  361.             END <name>
  362.  
  363.  
  364.        similar to the convention of  Modula  2.    This  adds  a  bit of
  365.        "syntactic sugar" to the language.  Things like this are  easy to
  366.        add or change to your liking, if the language is your own design.
  367.  
  368.        To parse this definition of a main block,  change  procedure Prog
  369.        to read:A*A*
  370.                                      - 6 -
  371. PA A
  372.  
  373.  
  374.  
  375.  
  376.  
  377.        {--------------------------------------------------------------}
  378.        {  Parse and Translate a Program }
  379.  
  380.        procedure Prog;
  381.        begin
  382.           Match('p');
  383.           Header;
  384.           Main;
  385.           Match('.');
  386.        end;
  387.        {--------------------------------------------------------------}
  388.  
  389.  
  390.        and add the new procedure:
  391.  
  392.  
  393.        {--------------------------------------------------------------}
  394.        { Parse and Translate a Main Program }
  395.  
  396.        procedure Main;
  397.        begin
  398.           Match('b');
  399.           Prolog;
  400.           Match('e');
  401.           Epilog;
  402.        end;
  403.        {--------------------------------------------------------------}
  404.  
  405.  
  406.        Now, the only legal program is:
  407.  
  408.  
  409.             PROGRAM BEGIN END . (or 'pbe.')
  410.  
  411.  
  412.        Aren't we making progress???  Well, as usual it gets better.  You
  413.        might try some deliberate errors here, like omitting  the  'b' or
  414.        the 'e', and see what happens.  As always,  the  compiler  should
  415.        flag all illegal inputs.
  416.  
  417.  
  418.        DECLARATIONS
  419.  
  420.        The obvious next step is to decide what we mean by a declaration.
  421.        My  intent  here  is to have two kinds of declarations: variables
  422.        and  procedures/functions.    At  the  top  level,   only  global
  423.        declarations are allowed, just as in C.
  424.  
  425.        For now, there  can  only be variable declarations, identified by
  426.        the keyword VAR (abbreviated 'v'):
  427.  
  428.  
  429.             <top-level decls> ::= ( <data declaration> )*A6A6
  430.                                      - 7 -A*A*
  431. PA A
  432.  
  433.  
  434.  
  435.  
  436.  
  437.             <data declaration> ::= VAR <var-list>
  438.  
  439.  
  440.        Note that since there is only one variable type, there is no need
  441.        to  declare the type.  Later on, for full KISS, we can easily add
  442.        a type description.
  443.  
  444.        The procedure Prog becomes:
  445.  
  446.  
  447.        {--------------------------------------------------------------}
  448.        {  Parse and Translate a Program }
  449.  
  450.        procedure Prog;
  451.        begin
  452.           Match('p');
  453.           Header;
  454.           TopDecls;
  455.           Main;
  456.           Match('.');
  457.        end;
  458.        {--------------------------------------------------------------}
  459.  
  460.  
  461.        Now, add the two new procedures:
  462.  
  463.  
  464.        {--------------------------------------------------------------}
  465.        { Process a Data Declaration }
  466.  
  467.        procedure Decl;
  468.        begin
  469.           Match('v');
  470.           GetChar;
  471.        end;
  472.  
  473.  
  474.        {--------------------------------------------------------------}
  475.        { Parse and Translate Global Declarations }
  476.  
  477.        procedure TopDecls;
  478.        begin
  479.           while Look <> 'b' do
  480.              case Look of
  481.                'v': Decl;
  482.              else Abort('Unrecognized Keyword ''' + Look + '''');
  483.              end;
  484.        end;
  485.        {--------------------------------------------------------------}
  486.  
  487.  
  488.        Note that at this point, Decl  is  just  a stub.  It generates no
  489.        code, and it doesn't process a list ... every variable must occur
  490.        in a separate VAR statement.A*A*
  491.                                      - 8 -
  492. PA A
  493.  
  494.  
  495.  
  496.  
  497.  
  498.        OK,  now  we  can have any  number  of  data  declarations,  each
  499.        starting with a 'v' for VAR,  before  the BEGIN-block.  Try a few
  500.        cases and see what happens.
  501.  
  502.  
  503.        DECLARATIONS AND SYMBOLS
  504.  
  505.        That looks pretty good, but  we're still only generating the null
  506.        program  for  output.    A  real compiler would  issue  assembler
  507.        directives to allocate storage for  the  variables.    It's about
  508.        time we actually produced some code.
  509.  
  510.        With  a  little  extra  code,  that's  an  easy  thing to do from
  511.        procedure Decl.  Modify it as follows:
  512.  
  513.  
  514.        {--------------------------------------------------------------}
  515.        { Parse and Translate a Data Declaration }
  516.  
  517.        procedure Decl;
  518.        var Name: char;
  519.        begin
  520.           Match('v');
  521.           Alloc(GetName);
  522.        end;
  523.        {--------------------------------------------------------------}
  524.  
  525.  
  526.        The procedure Alloc just  issues  a  command  to the assembler to
  527.        allocate storage:
  528.  
  529.  
  530.        {--------------------------------------------------------------}
  531.        { Allocate Storage for a Variable }
  532.  
  533.        procedure Alloc(N: char);
  534.        begin
  535.           WriteLn(N, ':', TAB, 'DC 0');
  536.        end;
  537.        {--------------------------------------------------------------}
  538.  
  539.  
  540.        Give  this  one  a  whirl.    Try  an  input  that declares  some
  541.        variables, such as:
  542.  
  543.             pvxvyvzbe.
  544.  
  545.        See how the storage is allocated?    Simple, huh?  Note also that
  546.        the entry point, "MAIN," comes out in the right place.
  547.  
  548.        For the record, a "real" compiler would also have a  symbol table
  549.        to record the variables being used.  Normally,  the  symbol table
  550.        is necessary to record the type  of  each variable.  But since in
  551.        this case  all  variables  have  the  same  type, we don't need aA*A*
  552.                                      - 9 -
  553. PA A
  554.  
  555.  
  556.  
  557.  
  558.  
  559.        symbol  table  for  that reason.  As it turns out, we're going to
  560.        find a symbol  necessary  even without different types, but let's
  561.        postpone that need until it arises.
  562.  
  563.        Of course, we haven't really parsed the correct syntax for a data
  564.        declaration, since it involves a variable list.  Our version only
  565.        permits a single variable.  That's easy to fix, too.
  566.  
  567.        The BNF for <var-list> is
  568.  
  569.  
  570.             <var-list> ::= <ident> (, <ident>)*
  571.  
  572.  
  573.        Adding this syntax to Decl gives this new version:
  574.  
  575.  
  576.        {--------------------------------------------------------------}
  577.        { Parse and Translate a Data Declaration }
  578.  
  579.        procedure Decl;
  580.        var Name: char;
  581.        begin
  582.           Match('v');
  583.           Alloc(GetName);
  584.           while Look = ',' do begin
  585.              GetChar;
  586.              Alloc(GetName);
  587.           end;
  588.        end;
  589.        {--------------------------------------------------------------}
  590.  
  591.  
  592.        OK, now compile this code and give it  a  try.    Try a number of
  593.        lines of VAR declarations, try a list of several variables on one
  594.        line, and try combinations of the two.  Does it work?
  595.  
  596.  
  597.        INITIALIZERS
  598.  
  599.        As long as we're dealing with data declarations, one thing that's
  600.        always  bothered  me  about  Pascal  is  that  it  doesn't  allow
  601.        initializing  data items in the declaration.    That  feature  is
  602.        admittedly sort of a frill, and it  may  be  out  of  place  in a
  603.        language that purports to  be  a minimal language.  But it's also
  604.        SO easy to add that it seems a shame not  to  do  so.    The  BNF
  605.        becomes:
  606.  
  607.  
  608.             <var-list> ::= <var> ( <var> )*
  609.  
  610.             <var> ::= <ident> [ = <integer> ]ABAB
  611.                                     - 10 -A*A*
  612. PA A
  613.  
  614.  
  615.  
  616.  
  617.  
  618.        Change Alloc as follows:
  619.  
  620.  
  621.        {--------------------------------------------------------------}
  622.        { Allocate Storage for a Variable }
  623.  
  624.        procedure Alloc(N: char);
  625.        begin
  626.           Write(N, ':', TAB, 'DC ');
  627.           if Look = '=' then begin
  628.              Match('=');
  629.              WriteLn(GetNum);
  630.              end
  631.           else
  632.              WriteLn('0');
  633.        end;
  634.        {--------------------------------------------------------------}
  635.  
  636.  
  637.        There you are: an initializer with six added lines of Pascal.
  638.  
  639.        OK, try this  version  of  TINY  and verify that you can, indeed,
  640.        give the variables initial values.
  641.  
  642.        By golly, this thing is starting to look  real!    Of  course, it
  643.        still doesn't DO anything, but it looks good, doesn't it?
  644.  
  645.        Before leaving this section, I should point out  that  we've used
  646.        two versions of function GetNum.  One, the earlier one, returns a
  647.        character value, a single digit.  The other accepts a multi-digit
  648.        integer and returns an integer value.  Either one will work here,
  649.        since WriteLn will handle either type.  But there's no  reason to
  650.        limit ourselves  to  single-digit  values  here,  so  the correct
  651.        version to use is the one that returns an integer.  Here it is:
  652.  
  653.  
  654.        {--------------------------------------------------------------}
  655.        { Get a Number }
  656.  
  657.        function GetNum: integer;
  658.        var Val: integer;
  659.        begin
  660.           Val := 0;
  661.           if not IsDigit(Look) then Expected('Integer');
  662.           while IsDigit(Look) do begin
  663.              Val := 10 * Val + Ord(Look) - Ord('0');
  664.              GetChar;
  665.           end;
  666.           GetNum := Val;
  667.        end;
  668.        {--------------------------------------------------------------}ANAN
  669.                                     - 11 -A*A*
  670. PA A
  671.  
  672.  
  673.  
  674.  
  675.  
  676.        As a matter  of  fact,  strictly  speaking  we  should  allow for
  677.        expressions in the data field of the initializer, or at  the very
  678.        least  for  negative  values.  For  now,  let's  just  allow  for
  679.        negative values by changing the code for Alloc as follows:
  680.  
  681.  
  682.        {--------------------------------------------------------------}
  683.        { Allocate Storage for a Variable }
  684.  
  685.        procedure Alloc(N: char);
  686.        begin
  687.           if InTable(N) then Abort('Duplicate Variable Name ' + N);
  688.           ST[N] := 'v';
  689.           Write(N, ':', TAB, 'DC ');
  690.           if Look = '=' then begin
  691.              Match('=');
  692.              If Look = '-' then begin
  693.                 Write(Look);
  694.                 Match('-');
  695.              end;
  696.              WriteLn(GetNum);
  697.              end
  698.           else
  699.              WriteLn('0');
  700.        end;
  701.        {--------------------------------------------------------------}
  702.  
  703.  
  704.        Now  you should be able to  initialize  variables  with  negative
  705.        and/or multi-digit values.
  706.  
  707.  
  708.        THE SYMBOL TABLE
  709.  
  710.        There's one problem  with  the  compiler  as it stands so far: it
  711.        doesn't do anything to record a variable when we declare it.   So
  712.        the compiler is perfectly content to allocate storage for several
  713.        variables with the same name.  You can easily verify this with an
  714.        input like
  715.  
  716.  
  717.             pvavavabe.
  718.  
  719.  
  720.        Here we've declared the variable A three times.  As you  can see,
  721.        the compiler will  cheerfully  accept  that,  and  generate three
  722.        identical labels.  Not good.
  723.  
  724.        Later on,  when we start referencing variables, the compiler will
  725.        also let us reference variables  that don't exist.  The assembler
  726.        will  catch  both  of these error conditions, but it doesn't seem
  727.        friendly at all to pass such errors along to the assembler.   The
  728.        compiler should catch such things at the source language level.A6A6
  729.                                     - 12 -A*A*
  730. PA A
  731.  
  732.  
  733.  
  734.  
  735.  
  736.        So even though we don't need a symbol table to record data types,
  737.        we ought to install  one  just to check for these two conditions.
  738.        Since at this  point  we are still restricted to single-character
  739.        variable names, the symbol table can be trivial.  To  provide for
  740.        it, first add the following  declaration at the beginning of your
  741.        program:
  742.  
  743.  
  744.             var ST: array['A'..'Z'] of char;
  745.  
  746.  
  747.        and insert the following function:
  748.  
  749.  
  750.        {--------------------------------------------------------------}
  751.        { Look for Symbol in Table }
  752.  
  753.        function InTable(n: char): Boolean;
  754.        begin
  755.           InTable := ST[n] <> ' ';
  756.        end;
  757.        {--------------------------------------------------------------}
  758.  
  759.  
  760.        We  also  need  to initialize the  table  to  all  blanks.    The
  761.        following lines in Init will do the job:
  762.  
  763.  
  764.        var i: char;
  765.        begin
  766.           for i := 'A' to 'Z' do
  767.              ST[i] := ' ';
  768.           ...
  769.  
  770.  
  771.        Finally,  insert  the  following two lines at  the  beginning  of
  772.        Alloc:
  773.  
  774.  
  775.           if InTable(N) then Abort('Duplicate Variable Name ' + N);
  776.           ST[N] := 'v';
  777.  
  778.  
  779.        That  should  do  it.  The  compiler  will  now  catch  duplicate
  780.        declarations.  Later, we can  also  use  InTable  when generating
  781.        references to the variables.
  782.  
  783.  
  784.        EXECUTABLE STATEMENTS
  785.  
  786.        At this point, we can generate a null program that has  some data
  787.        variables  declared  and  possibly initialized.  But  so  far  we
  788.        haven't arranged to generate the first line of executable code.A6A6
  789.                                     - 13 -A*A*
  790. PA A
  791.  
  792.  
  793.  
  794.  
  795.  
  796.        Believe  it or not, though, we almost  have  a  usable  language!
  797.        What's missing is the executable code that must go into  the main
  798.        program.  But that code is just assignment statements and control
  799.        statements ... all stuff we have done before.   So  it  shouldn't
  800.        take us long to provide for them, as well.
  801.  
  802.        The BNF definition given earlier  for the main program included a
  803.        statement block, which we have so far ignored:
  804.  
  805.  
  806.             <main> ::= BEGIN <block> END
  807.  
  808.  
  809.        For now,  we  can  just  consider  a  block  to  be  a  series of
  810.        assignment statements:
  811.  
  812.  
  813.             <block> ::= (Assignment)*
  814.  
  815.  
  816.        Let's start things off by adding  a  parser for the block.  We'll
  817.        begin with a stub for the assignment statement:
  818.  
  819.  
  820.        {--------------------------------------------------------------}
  821.        { Parse and Translate an Assignment Statement }
  822.  
  823.        procedure Assignment;
  824.        begin
  825.           GetChar;
  826.        end;
  827.  
  828.  
  829.        {--------------------------------------------------------------}
  830.        { Parse and Translate a Block of Statements }
  831.  
  832.        procedure Block;
  833.        begin
  834.           while Look <> 'e' do
  835.              Assignment;
  836.        end;
  837.        {--------------------------------------------------------------}
  838.  
  839.  
  840.        Modify procedure Main to call Block as shown below:
  841.  
  842.  
  843.        {--------------------------------------------------------------}
  844.        { Parse and Translate a Main Program }
  845.  
  846.        procedure Main;
  847.        begin
  848.           Match('b');
  849.           Prolog;A*A*
  850.                                     - 14 -
  851. PA A
  852.  
  853.  
  854.  
  855.  
  856.  
  857.           Block;
  858.           Match('e');
  859.           Epilog;
  860.        end;
  861.        {--------------------------------------------------------------}
  862.  
  863.  
  864.        This version still won't generate any code for  the   "assignment
  865.        statements" ... all it does is to eat characters  until  it  sees
  866.        the 'e' for 'END.'  But it sets the stage for what is to follow.
  867.  
  868.        The  next  step,  of  course,  is  to  flesh out the code for  an
  869.        assignment statement.  This  is  something  we've done many times
  870.        before,  so  I  won't belabor it.  This time, though, I'd like to
  871.        deal with the code generation a little differently.  Up till now,
  872.        we've always just inserted the Emits that generate output code in
  873.        line with  the parsing routines.  A little unstructured, perhaps,
  874.        but it seemed the most straightforward approach, and made it easy
  875.        to see what kind of code would be emitted for each construct.
  876.  
  877.        However, I realize that most of you are using an  80x86 computer,
  878.        so  the 68000 code generated is of little use to you.  Several of
  879.        you have asked me if the CPU-dependent code couldn't be collected
  880.        into one spot  where  it  would  be easier to retarget to another
  881.        CPU.  The answer, of course, is yes.
  882.  
  883.        To  accomplish  this,  insert  the  following  "code  generation"
  884.        routines:
  885.  
  886.  
  887.        {---------------------------------------------------------------}
  888.        { Clear the Primary Register }
  889.  
  890.        procedure Clear;
  891.        begin
  892.           EmitLn('CLR D0');
  893.        end;
  894.  
  895.  
  896.        {---------------------------------------------------------------}
  897.        { Negate the Primary Register }
  898.  
  899.        procedure Negate;
  900.        begin
  901.           EmitLn('NEG D0');
  902.        end;
  903.  
  904.  
  905.        {---------------------------------------------------------------}
  906.        { Load a Constant Value to Primary Register }
  907.  
  908.        procedure LoadConst(n: integer);
  909.        begin
  910.           Emit('MOVE #');A*A*
  911.                                     - 15 -
  912. PA A
  913.  
  914.  
  915.  
  916.  
  917.  
  918.           WriteLn(n, ',D0');
  919.        end;
  920.  
  921.  
  922.        {---------------------------------------------------------------}
  923.        { Load a Variable to Primary Register }
  924.  
  925.        procedure LoadVar(Name: char);
  926.        begin
  927.           if not InTable(Name) then Undefined(Name);
  928.           EmitLn('MOVE ' + Name + '(PC),D0');
  929.        end;
  930.  
  931.  
  932.        {---------------------------------------------------------------}
  933.        { Push Primary onto Stack }
  934.  
  935.        procedure Push;
  936.        begin
  937.           EmitLn('MOVE D0,-(SP)');
  938.        end;
  939.  
  940.  
  941.        {---------------------------------------------------------------}
  942.        { Add Top of Stack to Primary }
  943.  
  944.        procedure PopAdd;
  945.        begin
  946.           EmitLn('ADD (SP)+,D0');
  947.        end;
  948.  
  949.  
  950.        {---------------------------------------------------------------}
  951.        { Subtract Primary from Top of Stack }
  952.  
  953.        procedure PopSub;
  954.        begin
  955.           EmitLn('SUB (SP)+,D0');
  956.           EmitLn('NEG D0');
  957.        end;
  958.  
  959.  
  960.        {---------------------------------------------------------------}
  961.        { Multiply Top of Stack by Primary }
  962.  
  963.        procedure PopMul;
  964.        begin
  965.           EmitLn('MULS (SP)+,D0');
  966.        end;
  967.  
  968.  
  969.        {---------------------------------------------------------------}
  970.        { Divide Top of Stack by Primary }A6A6
  971.                                     - 16 -A*A*
  972. PA A
  973.  
  974.  
  975.  
  976.  
  977.  
  978.        procedure PopDiv;
  979.        begin
  980.           EmitLn('MOVE (SP)+,D7');
  981.           EmitLn('EXT.L D7');
  982.           EmitLn('DIVS D0,D7');
  983.           EmitLn('MOVE D7,D0');
  984.        end;
  985.  
  986.  
  987.        {---------------------------------------------------------------}
  988.        { Store Primary to Variable }
  989.  
  990.        procedure Store(Name: char);
  991.        begin
  992.           if not InTable(Name) then Undefined(Name);
  993.           EmitLn('LEA ' + Name + '(PC),A0');
  994.           EmitLn('MOVE D0,(A0)')
  995.        end;
  996.        {---------------------------------------------------------------}
  997.  
  998.  
  999.        The  nice  part  of  this  approach,  of  course,  is that we can
  1000.        retarget  the compiler to a new CPU  simply  by  rewriting  these
  1001.        "code generator" procedures.  In  addition,  we  will  find later
  1002.        that we can improve the code quality by tweaking these routines a
  1003.        bit, without having to modify the compiler proper.
  1004.  
  1005.        Note that both LoadVar  and  Store check the symbol table to make
  1006.        sure that the variable is defined.  The  error  handler Undefined
  1007.        simply calls Abort:
  1008.  
  1009.  
  1010.        {--------------------------------------------------------------}
  1011.        { Report an Undefined Identifier }
  1012.  
  1013.        procedure Undefined(n: string);
  1014.        begin
  1015.           Abort('Undefined Identifier ' + n);
  1016.        end;
  1017.        {--------------------------------------------------------------}
  1018.  
  1019.  
  1020.        OK, we are now finally ready to begin processing executable code.
  1021.        We'll  do  that  by  replacing  the  stub  version  of  procedure
  1022.        Assignment.
  1023.  
  1024.        We've been down this  road  many times before, so this should all
  1025.        be familiar to you.    In fact, except for the changes associated
  1026.        with the code generation, we  could just copy the procedures from
  1027.        Part  VII.    Since we are making some changes, I won't just copy
  1028.        them, but we will go a little faster than usual.
  1029.  
  1030.        The BNF for the assignment statement is:A6A6
  1031.                                     - 17 -A*A*
  1032. PA A
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.             <assignment> ::= <ident> = <expression>
  1039.  
  1040.             <expression> ::= <first term> ( <addop> <term> )*
  1041.  
  1042.             <first term> ::= <first factor> <rest>
  1043.  
  1044.             <term> ::= <factor> <rest>
  1045.  
  1046.             <rest> ::= ( <mulop> <factor> )*
  1047.  
  1048.             <first factor> ::= [ <addop> ] <factor>
  1049.  
  1050.             <factor> ::= <var> | <number> | ( <expression> )
  1051.  
  1052.  
  1053.        This version of the BNF is  also  a bit different than we've used
  1054.        before ... yet another "variation on the theme of an expression."
  1055.        This particular version  has  what  I  consider  to  be  the best
  1056.        treatment  of  the  unary minus.  As you'll see later, it lets us
  1057.        handle   negative  constant  values  efficiently.    It's   worth
  1058.        mentioning  here  that  we  have  often  seen  the advantages  of
  1059.        "tweaking"  the  BNF  as we go, to help make the language easy to
  1060.        parse.    What  you're looking at here is a bit different:  we've
  1061.        tweaked  the  BNF  to make the CODE  GENERATION  more  efficient!
  1062.        That's a first for this series.
  1063.  
  1064.        Anyhow, the following code implements the BNF:
  1065.  
  1066.  
  1067.        {---------------------------------------------------------------}
  1068.        { Parse and Translate a Math Factor }
  1069.  
  1070.        procedure Expression; Forward;
  1071.  
  1072.        procedure Factor;
  1073.        begin
  1074.           if Look = '(' then begin
  1075.              Match('(');
  1076.              Expression;
  1077.              Match(')');
  1078.              end
  1079.           else if IsAlpha(Look) then
  1080.              LoadVar(GetName)
  1081.           else
  1082.              LoadConst(GetNum);
  1083.        end;
  1084.  
  1085.  
  1086.        {--------------------------------------------------------------}
  1087.        { Parse and Translate a Negative Factor }
  1088.  
  1089.        procedure NegFactor;
  1090.        begin
  1091.           Match('-');A*A*
  1092.                                     - 18 -
  1093. PA A
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.           if IsDigit(Look) then
  1100.              LoadConst(-GetNum)
  1101.           else begin
  1102.              Factor;
  1103.              Negate;
  1104.           end;
  1105.        end;
  1106.  
  1107.  
  1108.        {--------------------------------------------------------------}
  1109.        { Parse and Translate a Leading Factor }
  1110.  
  1111.        procedure FirstFactor;
  1112.        begin
  1113.           case Look of
  1114.             '+': begin
  1115.                     Match('+');
  1116.                     Factor;
  1117.                  end;
  1118.             '-': NegFactor;
  1119.           else  Factor;
  1120.           end;
  1121.        end;
  1122.  
  1123.  
  1124.        {--------------------------------------------------------------}
  1125.        { Recognize and Translate a Multiply }
  1126.  
  1127.        procedure Multiply;
  1128.        begin
  1129.           Match('*');
  1130.           Factor;
  1131.           PopMul;
  1132.        end;
  1133.  
  1134.  
  1135.        {-------------------------------------------------------------}
  1136.        { Recognize and Translate a Divide }
  1137.  
  1138.        procedure Divide;
  1139.        begin
  1140.           Match('/');
  1141.           Factor;
  1142.           PopDiv;
  1143.        end;
  1144.  
  1145.  
  1146.        {---------------------------------------------------------------}
  1147.        { Common Code Used by Term and FirstTerm }
  1148.  
  1149.        procedure Term1;
  1150.        begin
  1151.           while IsMulop(Look) do begin
  1152.              Push;A*A*
  1153.                                     - 19 -
  1154. PA A
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.              case Look of
  1161.               '*': Multiply;
  1162.               '/': Divide;
  1163.              end;
  1164.           end;
  1165.        end;
  1166.  
  1167.  
  1168.        {---------------------------------------------------------------}
  1169.        { Parse and Translate a Math Term }
  1170.  
  1171.        procedure Term;
  1172.        begin
  1173.           Factor;
  1174.           Term1;
  1175.        end;
  1176.  
  1177.  
  1178.        {---------------------------------------------------------------}
  1179.        { Parse and Translate a Leading Term }
  1180.  
  1181.        procedure FirstTerm;
  1182.        begin
  1183.           FirstFactor;
  1184.           Term1;
  1185.        end;
  1186.  
  1187.  
  1188.        {--------------------------------------------------------------}
  1189.        { Recognize and Translate an Add }
  1190.  
  1191.        procedure Add;
  1192.        begin
  1193.           Match('+');
  1194.           Term;
  1195.           PopAdd;
  1196.        end;
  1197.  
  1198.  
  1199.        {-------------------------------------------------------------}
  1200.        { Recognize and Translate a Subtract }
  1201.  
  1202.        procedure Subtract;
  1203.        begin
  1204.           Match('-');
  1205.           Term;
  1206.           PopSub;
  1207.        end;
  1208.  
  1209.  
  1210.        {---------------------------------------------------------------}
  1211.        { Parse and Translate an Expression }
  1212.  
  1213.        procedure Expression;A*A*
  1214.                                     - 20 -
  1215. PA A
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.        begin
  1222.           FirstTerm;
  1223.           while IsAddop(Look) do begin
  1224.              Push;
  1225.              case Look of
  1226.               '+': Add;
  1227.               '-': Subtract;
  1228.              end;
  1229.           end;
  1230.        end;
  1231.  
  1232.  
  1233.        {--------------------------------------------------------------}
  1234.        { Parse and Translate an Assignment Statement }
  1235.  
  1236.        procedure Assignment;
  1237.        var Name: char;
  1238.        begin
  1239.           Name := GetName;
  1240.           Match('=');
  1241.           Expression;
  1242.           Store(Name);
  1243.        end;
  1244.        {--------------------------------------------------------------}
  1245.  
  1246.  
  1247.        OK, if you've  got  all  this  code inserted, then compile it and
  1248.        check  it out.  You should  be  seeing  reasonable-looking  code,
  1249.        representing a complete program that will  assemble  and execute.
  1250.        We have a compiler!
  1251.  
  1252.  
  1253.        BOOLEANS
  1254.  
  1255.        The next step should also  be  familiar  to  you.    We  must add
  1256.        Boolean  expressions  and relational operations.    Again,  since
  1257.        we've already dealt with them more than once,  I  won't elaborate
  1258.        much on them, except  where  they  are  different from what we've
  1259.        done before.  Again, we won't just copy from other  files because
  1260.        I've changed a few things just a bit.  Most  of  the changes just
  1261.        involve encapsulating the machine-dependent parts as  we  did for
  1262.        the   arithmetic  operations.    I've  also  modified   procedure
  1263.        NotFactor  somewhat,  to  parallel  the structure of FirstFactor.
  1264.        Finally,  I  corrected  an  error  in  the  object code  for  the
  1265.        relational operators:  The Scc instruction I used  only  sets the
  1266.        low 8 bits of D0.  We want all 16 bits set for a logical true, so
  1267.        I've added an instruction to sign-extend the low byte.
  1268.  
  1269.        To begin, we're going to need some more recognizers:
  1270.  
  1271.  
  1272.        {--------------------------------------------------------------}
  1273.        { Recognize a Boolean Orop }A6A6
  1274.                                     - 21 -A*A*
  1275. PA A
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.        function IsOrop(c: char): boolean;
  1282.        begin
  1283.           IsOrop := c in ['|', '~'];
  1284.        end;
  1285.  
  1286.  
  1287.        {--------------------------------------------------------------}
  1288.        { Recognize a Relop }
  1289.  
  1290.        function IsRelop(c: char): boolean;
  1291.        begin
  1292.           IsRelop := c in ['=', '#', '<', '>'];
  1293.        end;
  1294.        {--------------------------------------------------------------}
  1295.  
  1296.  
  1297.        Also, we're going to need some more code generation routines:
  1298.  
  1299.  
  1300.        {---------------------------------------------------------------}
  1301.        { Complement the Primary Register }
  1302.  
  1303.        procedure NotIt;
  1304.        begin
  1305.           EmitLn('NOT D0');
  1306.        end;
  1307.        {---------------------------------------------------------------}
  1308.        .
  1309.        .
  1310.        .
  1311.        {---------------------------------------------------------------}
  1312.        { AND Top of Stack with Primary }
  1313.  
  1314.        procedure PopAnd;
  1315.        begin
  1316.           EmitLn('AND (SP)+,D0');
  1317.        end;
  1318.  
  1319.  
  1320.        {---------------------------------------------------------------}
  1321.        { OR Top of Stack with Primary }
  1322.  
  1323.        procedure PopOr;
  1324.        begin
  1325.           EmitLn('OR (SP)+,D0');
  1326.        end;
  1327.  
  1328.  
  1329.        {---------------------------------------------------------------}
  1330.        { XOR Top of Stack with Primary }
  1331.  
  1332.        procedure PopXor;
  1333.        begin
  1334.           EmitLn('EOR (SP)+,D0');A*A*
  1335.                                     - 22 -
  1336. PA A
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.        end;
  1343.  
  1344.  
  1345.        {---------------------------------------------------------------}
  1346.        { Compare Top of Stack with Primary }
  1347.  
  1348.        procedure PopCompare;
  1349.        begin
  1350.           EmitLn('CMP (SP)+,D0');
  1351.        end;
  1352.  
  1353.  
  1354.        {---------------------------------------------------------------}
  1355.        { Set D0 If Compare was = }
  1356.  
  1357.        procedure SetEqual;
  1358.        begin
  1359.           EmitLn('SEQ D0');
  1360.           EmitLn('EXT D0');
  1361.        end;
  1362.  
  1363.  
  1364.        {---------------------------------------------------------------}
  1365.        { Set D0 If Compare was != }
  1366.  
  1367.        procedure SetNEqual;
  1368.        begin
  1369.           EmitLn('SNE D0');
  1370.           EmitLn('EXT D0');
  1371.        end;
  1372.  
  1373.  
  1374.        {---------------------------------------------------------------}
  1375.        { Set D0 If Compare was > }
  1376.  
  1377.        procedure SetGreater;
  1378.        begin
  1379.           EmitLn('SLT D0');
  1380.           EmitLn('EXT D0');
  1381.        end;
  1382.  
  1383.  
  1384.        {---------------------------------------------------------------}
  1385.        { Set D0 If Compare was < }
  1386.  
  1387.        procedure SetLess;
  1388.        begin
  1389.           EmitLn('SGT D0');
  1390.           EmitLn('EXT D0');
  1391.        end;
  1392.        {---------------------------------------------------------------}ANAN
  1393.                                     - 23 -A*A*
  1394. PA A
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.        All of this  gives us the tools we need.  The BNF for the Boolean
  1401.        expressions is:
  1402.  
  1403.  
  1404.             <bool-expr> ::= <bool-term> ( <orop> <bool-term> )*
  1405.  
  1406.             <bool-term> ::= <not-factor> ( <andop> <not-factor> )*
  1407.  
  1408.             <not-factor> ::= [ '!' ] <relation>
  1409.  
  1410.             <relation> ::= <expression> [ <relop> <expression> ]
  1411.  
  1412.  
  1413.        Sharp-eyed readers might  note  that this syntax does not include
  1414.        the non-terminal  "bool-factor" used in earlier versions.  It was
  1415.        needed then because I also allowed for the Boolean constants TRUE
  1416.        and FALSE.   But  remember  that  in TINY there is no distinction
  1417.        made between Boolean and arithmetic  types ... they can be freely
  1418.        intermixed.   So there is really no  need  for  these  predefined
  1419.        values ... we can just use -1 and 0, respectively.
  1420.  
  1421.        In C terminology, we could always use the defines:
  1422.  
  1423.  
  1424.             #define TRUE -1
  1425.             #define FALSE 0
  1426.  
  1427.  
  1428.        (That is, if TINY had a  preprocessor.)   Later on, when we allow
  1429.        for  declarations  of  constants,  these  two   values   will  be
  1430.        predefined by the language.
  1431.  
  1432.        The reason that I'm harping on this is that  I've  already  tried
  1433.        the alternative, which is to  include TRUE and FALSE as keywords.
  1434.        The problem with that approach is that it  then  requires lexical
  1435.        scanning for EVERY variable name  in every expression.  If you'll
  1436.        recall,  I pointed out in Installment VII  that  this  slows  the
  1437.        compiler  down considerably.  As long as  keywords  can't  be  in
  1438.        expressions, we need to do the scanning only at the  beginning of
  1439.        every  new  statement  ...  quite  an improvement.  So using  the
  1440.        syntax above not only simplifies the parsing, but  speeds  up the
  1441.        scanning as well.
  1442.  
  1443.        OK, given that we're  all  satisfied  with  the syntax above, the
  1444.        corresponding code is shown below:
  1445.  
  1446.  
  1447.        {---------------------------------------------------------------}
  1448.        { Recognize and Translate a Relational "Equals" }
  1449.  
  1450.        procedure Equals;
  1451.        begin
  1452.           Match('=');
  1453.           Expression;A*A*
  1454.                                     - 24 -
  1455. PA A
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461.           PopCompare;
  1462.           SetEqual;
  1463.        end;
  1464.  
  1465.  
  1466.        {---------------------------------------------------------------}
  1467.        { Recognize and Translate a Relational "Not Equals" }
  1468.  
  1469.        procedure NotEquals;
  1470.        begin
  1471.           Match('#');
  1472.           Expression;
  1473.           PopCompare;
  1474.           SetNEqual;
  1475.        end;
  1476.  
  1477.  
  1478.        {---------------------------------------------------------------}
  1479.        { Recognize and Translate a Relational "Less Than" }
  1480.  
  1481.        procedure Less;
  1482.        begin
  1483.           Match('<');
  1484.           Expression;
  1485.           PopCompare;
  1486.           SetLess;
  1487.        end;
  1488.  
  1489.  
  1490.        {---------------------------------------------------------------}
  1491.        { Recognize and Translate a Relational "Greater Than" }
  1492.  
  1493.        procedure Greater;
  1494.        begin
  1495.           Match('>');
  1496.           Expression;
  1497.           PopCompare;
  1498.           SetGreater;
  1499.        end;
  1500.  
  1501.  
  1502.        {---------------------------------------------------------------}
  1503.        { Parse and Translate a Relation }
  1504.  
  1505.  
  1506.        procedure Relation;
  1507.        begin
  1508.           Expression;
  1509.           if IsRelop(Look) then begin
  1510.              Push;
  1511.              case Look of
  1512.               '=': Equals;
  1513.               '#': NotEquals;
  1514.               '<': Less;A*A*
  1515.                                     - 25 -
  1516. PA A
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.               '>': Greater;
  1523.              end;
  1524.           end;
  1525.        end;
  1526.  
  1527.  
  1528.        {---------------------------------------------------------------}
  1529.        { Parse and Translate a Boolean Factor with Leading NOT }
  1530.  
  1531.        procedure NotFactor;
  1532.        begin
  1533.           if Look = '!' then begin
  1534.              Match('!');
  1535.              Relation;
  1536.              NotIt;
  1537.              end
  1538.           else
  1539.              Relation;
  1540.        end;
  1541.  
  1542.  
  1543.        {---------------------------------------------------------------}
  1544.        { Parse and Translate a Boolean Term }
  1545.  
  1546.        procedure BoolTerm;
  1547.        begin
  1548.           NotFactor;
  1549.           while Look = '&' do begin
  1550.              Push;
  1551.              Match('&');
  1552.              NotFactor;
  1553.              PopAnd;
  1554.           end;
  1555.        end;
  1556.  
  1557.  
  1558.        {--------------------------------------------------------------}
  1559.        { Recognize and Translate a Boolean OR }
  1560.  
  1561.        procedure BoolOr;
  1562.        begin
  1563.           Match('|');
  1564.           BoolTerm;
  1565.           PopOr;
  1566.        end;
  1567.  
  1568.  
  1569.        {--------------------------------------------------------------}
  1570.        { Recognize and Translate an Exclusive Or }
  1571.  
  1572.        procedure BoolXor;
  1573.        begin
  1574.           Match('~');
  1575.           BoolTerm;A*A*
  1576.                                     - 26 -
  1577. PA A
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.           PopXor;
  1584.        end;
  1585.  
  1586.  
  1587.        {---------------------------------------------------------------}
  1588.        { Parse and Translate a Boolean Expression }
  1589.  
  1590.        procedure BoolExpression;
  1591.        begin
  1592.           BoolTerm;
  1593.           while IsOrOp(Look) do begin
  1594.              Push;
  1595.              case Look of
  1596.               '|': BoolOr;
  1597.               '~': BoolXor;
  1598.              end;
  1599.           end;
  1600.        end;
  1601.        {--------------------------------------------------------------}
  1602.  
  1603.  
  1604.        To tie it all together, don't forget to change the  references to
  1605.        Expression in  procedures Factor and Assignment so that they call
  1606.        BoolExpression instead.
  1607.  
  1608.        OK, if  you've  got  all  that typed in, compile it and give it a
  1609.        whirl.    First,  make  sure  you  can  still parse  an  ordinary
  1610.        arithmetic expression.  Then, try a Boolean one.    Finally, make
  1611.        sure  that you can assign the results of  relations.    Try,  for
  1612.        example:
  1613.  
  1614.             pvx,y,zbx=z>ye.
  1615.  
  1616.        which stands for:
  1617.  
  1618.             PROGRAM
  1619.             VAR X,Y,Z
  1620.             BEGIN
  1621.             X = Z > Y
  1622.             END.
  1623.  
  1624.  
  1625.        See how this assigns a Boolean value to X?
  1626.  
  1627.        CONTROL STRUCTURES
  1628.  
  1629.        We're almost home.   With  Boolean  expressions  in place, it's a
  1630.        simple  matter  to  add control structures.  For TINY, we'll only
  1631.        allow two kinds of them, the IF and the WHILE:
  1632.  
  1633.  
  1634.             <if> ::= IF <bool-expression> <block> [ ELSE <block>] ENDIF
  1635.  
  1636.             <while> ::= WHILE <bool-expression> <block> ENDWHILEA*A*
  1637.                                     - 27 -
  1638. PA A
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.        Once  again,  let  me  spell  out the decisions implicit in  this
  1645.        syntax, which departs strongly from that of C or Pascal.  In both
  1646.        of those languages, the "body" of an IF or WHILE is regarded as a
  1647.        single  statement.  If you intend to use a block of more than one
  1648.        statement, you have to build a compound statement using BEGIN-END
  1649.        (in Pascal) or  '{}' (in C).  In TINY (and KISS) there is no such
  1650.        thing as a compound statement  ... single or multiple they're all
  1651.        just blocks to these languages.
  1652.  
  1653.        In KISS, all the control structures will have explicit and unique
  1654.        keywords  bracketing  the  statement block, so there  can  be  no
  1655.        confusion as to where things begin  and  end.  This is the modern
  1656.        approach, used in such respected languages as Ada  and  Modula 2,
  1657.        and it completely eliminates the problem of the "dangling else."
  1658.  
  1659.        Note  that I could have chosen to use the same keyword END to end
  1660.        all  the constructs, as is done in Pascal.  (The closing '}' in C
  1661.        serves the same purpose.)  But this has always led  to confusion,
  1662.        which is why Pascal programmers tend to write things like
  1663.  
  1664.  
  1665.             end { loop }
  1666.  
  1667.        or   end { if }
  1668.  
  1669.  
  1670.        As I explained in  Part  V,  using  unique terminal keywords does
  1671.        increase  the  size  of the keyword list and therefore slows down
  1672.        the  scanning, but in this case it seems a small price to pay for
  1673.        the added insurance.   Better  to find the errors at compile time
  1674.        rather than run time.
  1675.  
  1676.        One last thought:  The two constructs above each  have  the  non-
  1677.        terminals
  1678.  
  1679.  
  1680.              <bool-expression> and <block>
  1681.  
  1682.  
  1683.        juxtaposed with no separating keyword.  In Pascal we would expect
  1684.        the keywords THEN and DO in these locations.
  1685.  
  1686.        I have no problem with leaving out these keywords, and the parser
  1687.        has no trouble either, ON CONDITION that we make no errors in the
  1688.        bool-expression part.  On  the  other hand, if we were to include
  1689.        these extra keywords we would get yet one more level of insurance
  1690.        at very little  cost,  and  I  have no problem with that, either.
  1691.        Use your best judgment as to which way to go.
  1692.  
  1693.        OK, with that bit of explanation let's proceed.  As  usual, we're
  1694.        going to need some new  code generation routines.  These generate
  1695.        the code for conditional and unconditional branches:ABAB
  1696.                                     - 28 -A*A*
  1697. PA A
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.        {---------------------------------------------------------------}
  1704.        { Branch Unconditional  }
  1705.  
  1706.        procedure Branch(L: string);
  1707.        begin
  1708.           EmitLn('BRA ' + L);
  1709.        end;
  1710.  
  1711.  
  1712.        {---------------------------------------------------------------}
  1713.        { Branch False }
  1714.  
  1715.        procedure BranchFalse(L: string);
  1716.        begin
  1717.           EmitLn('TST D0');
  1718.           EmitLn('BEQ ' + L);
  1719.        end;
  1720.        {--------------------------------------------------------------}
  1721.  
  1722.  
  1723.        Except for the encapsulation of  the code generation, the code to
  1724.        parse the control constructs is the same as you've seen before:
  1725.  
  1726.  
  1727.        {---------------------------------------------------------------}
  1728.        { Recognize and Translate an IF Construct }
  1729.  
  1730.        procedure Block; Forward;
  1731.  
  1732.  
  1733.        procedure DoIf;
  1734.        var L1, L2: string;
  1735.        begin
  1736.           Match('i');
  1737.           BoolExpression;
  1738.           L1 := NewLabel;
  1739.           L2 := L1;
  1740.           BranchFalse(L1);
  1741.           Block;
  1742.           if Look = 'l' then begin
  1743.              Match('l');
  1744.              L2 := NewLabel;
  1745.              Branch(L2);
  1746.              PostLabel(L1);
  1747.              Block;
  1748.           end;
  1749.           PostLabel(L2);
  1750.           Match('e');
  1751.        end;
  1752.  
  1753.  
  1754.        {--------------------------------------------------------------}
  1755.        { Parse and Translate a WHILE Statement }A6A6
  1756.                                     - 29 -A*A*
  1757. PA A
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.        procedure DoWhile;
  1764.        var L1, L2: string;
  1765.        begin
  1766.           Match('w');
  1767.           L1 := NewLabel;
  1768.           L2 := NewLabel;
  1769.           PostLabel(L1);
  1770.           BoolExpression;
  1771.           BranchFalse(L2);
  1772.           Block;
  1773.           Match('e');
  1774.           Branch(L1);
  1775.           PostLabel(L2);
  1776.        end;
  1777.        {--------------------------------------------------------------}
  1778.  
  1779.  
  1780.        To tie everything  together,  we need only modify procedure Block
  1781.        to recognize the "keywords" for the  IF  and WHILE.  As usual, we
  1782.        expand the definition of a block like so:
  1783.  
  1784.  
  1785.             <block> ::= ( <statement> )*
  1786.  
  1787.  
  1788.        where
  1789.  
  1790.  
  1791.             <statement> ::= <if> | <while> | <assignment>
  1792.  
  1793.  
  1794.        The corresponding code is:
  1795.  
  1796.  
  1797.        {--------------------------------------------------------------}
  1798.        { Parse and Translate a Block of Statements }
  1799.  
  1800.        procedure Block;
  1801.        begin
  1802.           while not(Look in ['e', 'l']) do begin
  1803.              case Look of
  1804.               'i': DoIf;
  1805.               'w': DoWhile;
  1806.              else Assignment;
  1807.              end;
  1808.           end;
  1809.        end;
  1810.        {--------------------------------------------------------------}
  1811.  
  1812.  
  1813.        OK,  add the routines I've given, compile and  test  them.    You
  1814.        should be able to parse the single-character versions  of  any of
  1815.        the control constructs.  It's looking pretty good!A6A6
  1816.                                     - 30 -A*A*
  1817. PA A
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.        As a matter  of  fact, except for the single-character limitation
  1824.        we've got a virtually complete version of TINY.  I call  it, with
  1825.        tongue planted firmly in cheek, TINY Version 0.1.
  1826.  
  1827.  
  1828.        LEXICAL SCANNING
  1829.  
  1830.        Of course, you know what's next:  We have to convert  the program
  1831.        so that  it can deal with multi-character keywords, newlines, and
  1832.        whitespace.   We have just gone through all  that  in  Part  VII.
  1833.        We'll use the distributed scanner  technique that I showed you in
  1834.        that  installment.    The  actual  implementation  is   a  little
  1835.        different because the way I'm handling newlines is different.
  1836.  
  1837.        To begin with, let's simply  allow for whitespace.  This involves
  1838.        only adding calls to SkipWhite at the end of the  three routines,
  1839.        GetName, GetNum, and Match.    A call to SkipWhite in Init primes
  1840.        the pump in case there are leading spaces.
  1841.  
  1842.        Next, we need to deal with  newlines.   This is really a two-step
  1843.        process,  since  the  treatment  of  the  newlines  with  single-
  1844.        character tokens is different from that for multi-character ones.
  1845.        We can eliminate some work by doing both  steps  at  once,  but I
  1846.        feel safer taking things one step at a time.
  1847.  
  1848.        Insert the new procedure:
  1849.  
  1850.  
  1851.        {--------------------------------------------------------------}
  1852.        { Skip Over an End-of-Line }
  1853.  
  1854.        procedure NewLine;
  1855.        begin
  1856.           while Look = CR do begin
  1857.              GetChar;
  1858.              if Look = LF then GetChar;
  1859.              SkipWhite;
  1860.           end;
  1861.        end;
  1862.        {--------------------------------------------------------------}
  1863.  
  1864.  
  1865.        Note that  we  have  seen  this  procedure  before in the form of
  1866.        Procedure Fin.  I've changed the name since this  new  one  seems
  1867.        more descriptive of the actual function.  I've  also  changed the
  1868.        code  to  allow  for multiple newlines and lines with nothing but
  1869.        white space.
  1870.  
  1871.        The next step is to insert calls to NewLine wherever we  decide a
  1872.        newline is permissible.  As I've pointed out before, this  can be
  1873.        very different in different languages.   In TINY, I've decided to
  1874.        allow them virtually anywhere.  This means that we need  calls to
  1875.        NewLine at the BEGINNING (not the end, as with SkipWhite)  of the
  1876.        procedures GetName, GetNum, and Match.A*A*
  1877.                                     - 31 -
  1878. PA A
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.        For procedures that have while loops, such as TopDecl, we  need a
  1885.        call  to NewLine at the beginning of the  procedure  AND  at  the
  1886.        bottom  of  each  loop.  That way, we can be assured that NewLine
  1887.        has just been called at the beginning of each  pass  through  the
  1888.        loop.
  1889.  
  1890.        If you've got all this done, try the program out and  verify that
  1891.        it will indeed handle white space and newlines.
  1892.  
  1893.        If it does, then we're  ready to deal with multi-character tokens
  1894.        and keywords.   To begin, add the additional declarations (copied
  1895.        almost verbatim from Part VII):
  1896.  
  1897.  
  1898.        {--------------------------------------------------------------}
  1899.        { Type Declarations }
  1900.  
  1901.        type Symbol = string[8];
  1902.  
  1903.             SymTab = array[1..1000] of Symbol;
  1904.  
  1905.             TabPtr = ^SymTab;
  1906.  
  1907.  
  1908.        {--------------------------------------------------------------}
  1909.        { Variable Declarations }
  1910.  
  1911.        var Look : char;             { Lookahead Character }
  1912.            Token: char;             { Encoded Token       }
  1913.            Value: string[16];       { Unencoded Token     }
  1914.  
  1915.            ST: Array['A'..'Z'] of char;
  1916.  
  1917.        {--------------------------------------------------------------}
  1918.        { Definition of Keywords and Token Types }
  1919.  
  1920.        const NKW =   9;
  1921.              NKW1 = 10;
  1922.  
  1923.        const KWlist: array[1..NKW] of Symbol =
  1924.                      ('IF', 'ELSE', 'ENDIF', 'WHILE', 'ENDWHILE',
  1925.                       'VAR', 'BEGIN', 'END', 'PROGRAM');
  1926.  
  1927.        const KWcode: string[NKW1] = 'xilewevbep';
  1928.        {--------------------------------------------------------------}
  1929.  
  1930.  
  1931.        Next, add the three procedures, also from Part VII:
  1932.  
  1933.  
  1934.        {--------------------------------------------------------------}
  1935.        { Table Lookup }
  1936.  
  1937.        function Lookup(T: TabPtr; s: string; n: integer): integer;A*A*
  1938.                                     - 32 -
  1939. PA A
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.        var i: integer;
  1946.            found: Boolean;
  1947.        begin
  1948.           found := false;
  1949.           i := n;
  1950.           while (i > 0) and not found do
  1951.              if s = T^[i] then
  1952.                 found := true
  1953.              else
  1954.                 dec(i);
  1955.           Lookup := i;
  1956.        end;
  1957.        {--------------------------------------------------------------}
  1958.        .
  1959.        .
  1960.        {--------------------------------------------------------------}
  1961.        { Get an Identifier and Scan it for Keywords }
  1962.  
  1963.        procedure Scan;
  1964.        begin
  1965.           GetName;
  1966.           Token := KWcode[Lookup(Addr(KWlist), Value, NKW) + 1];
  1967.        end;
  1968.        {--------------------------------------------------------------}
  1969.        .
  1970.        .
  1971.        {--------------------------------------------------------------}
  1972.        { Match a Specific Input String }
  1973.  
  1974.        procedure MatchString(x: string);
  1975.        begin
  1976.           if Value <> x then Expected('''' + x + '''');
  1977.        end;
  1978.        {--------------------------------------------------------------}
  1979.  
  1980.  
  1981.        Now, we have to make a  fairly  large number of subtle changes to
  1982.        the remaining procedures.  First,  we  must  change  the function
  1983.        GetName to a procedure, again as we did in Part VII:
  1984.  
  1985.  
  1986.        {--------------------------------------------------------------}
  1987.        { Get an Identifier }
  1988.  
  1989.        procedure GetName;
  1990.        begin
  1991.           NewLine;
  1992.           if not IsAlpha(Look) then Expected('Name');
  1993.           Value := '';
  1994.           while IsAlNum(Look) do begin
  1995.              Value := Value + UpCase(Look);
  1996.              GetChar;
  1997.           end;
  1998.           SkipWhite;A*A*
  1999.                                     - 33 -
  2000. PA A
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.        end;
  2007.        {--------------------------------------------------------------}
  2008.  
  2009.  
  2010.        Note that this procedure leaves its result in  the  global string
  2011.        Value.
  2012.  
  2013.        Next, we have to change every reference to GetName to reflect its
  2014.        new form. These occur in Factor, Assignment, and Decl:
  2015.  
  2016.  
  2017.        {---------------------------------------------------------------}
  2018.        { Parse and Translate a Math Factor }
  2019.  
  2020.        procedure BoolExpression; Forward;
  2021.  
  2022.        procedure Factor;
  2023.        begin
  2024.           if Look = '(' then begin
  2025.              Match('(');
  2026.              BoolExpression;
  2027.              Match(')');
  2028.              end
  2029.           else if IsAlpha(Look) then begin
  2030.              GetName;
  2031.              LoadVar(Value[1]);
  2032.              end
  2033.           else
  2034.              LoadConst(GetNum);
  2035.        end;
  2036.        {--------------------------------------------------------------}
  2037.        .
  2038.        .
  2039.        {--------------------------------------------------------------}
  2040.        { Parse and Translate an Assignment Statement }
  2041.  
  2042.        procedure Assignment;
  2043.        var Name: char;
  2044.        begin
  2045.           Name := Value[1];
  2046.           Match('=');
  2047.           BoolExpression;
  2048.           Store(Name);
  2049.        end;
  2050.        {---------------------------------------------------------------}
  2051.        .
  2052.        .
  2053.        {--------------------------------------------------------------}
  2054.        { Parse and Translate a Data Declaration }
  2055.  
  2056.        procedure Decl;
  2057.        begin
  2058.           GetName;
  2059.           Alloc(Value[1]);A*A*
  2060.                                     - 34 -
  2061. PA A
  2062.  
  2063.  
  2064.  
  2065.  
  2066.  
  2067.           while Look = ',' do begin
  2068.              Match(',');
  2069.              GetName;
  2070.              Alloc(Value[1]);
  2071.           end;
  2072.        end;
  2073.        {--------------------------------------------------------------}
  2074.  
  2075.  
  2076.        (Note that we're still  only  allowing  single-character variable
  2077.        names,  so we take the easy way out here and simply use the first
  2078.        character of the string.)
  2079.  
  2080.        Finally, we must make the changes to use Token instead of Look as
  2081.        the  test  character  and to call Scan at the appropriate places.
  2082.        Mostly, this  involves  deleting  calls  to  Match,  occasionally
  2083.        replacing calls to  Match  by calls to MatchString, and Replacing
  2084.        calls  to  NewLine  by  calls  to  Scan.    Here are the affected
  2085.        routines:
  2086.  
  2087.        {---------------------------------------------------------------}
  2088.        { Recognize and Translate an IF Construct }
  2089.  
  2090.        procedure Block; Forward;
  2091.  
  2092.  
  2093.        procedure DoIf;
  2094.        var L1, L2: string;
  2095.        begin
  2096.           BoolExpression;
  2097.           L1 := NewLabel;
  2098.           L2 := L1;
  2099.           BranchFalse(L1);
  2100.           Block;
  2101.           if Token = 'l' then begin
  2102.              L2 := NewLabel;
  2103.              Branch(L2);
  2104.              PostLabel(L1);
  2105.              Block;
  2106.           end;
  2107.           PostLabel(L2);
  2108.           MatchString('ENDIF');
  2109.        end;
  2110.  
  2111.  
  2112.        {--------------------------------------------------------------}
  2113.        { Parse and Translate a WHILE Statement }
  2114.  
  2115.        procedure DoWhile;
  2116.        var L1, L2: string;
  2117.        begin
  2118.           L1 := NewLabel;
  2119.           L2 := NewLabel;
  2120.           PostLabel(L1);A*A*
  2121.                                     - 35 -
  2122. PA A
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.           BoolExpression;
  2129.           BranchFalse(L2);
  2130.           Block;
  2131.           MatchString('ENDWHILE');
  2132.           Branch(L1);
  2133.           PostLabel(L2);
  2134.        end;
  2135.  
  2136.  
  2137.        {--------------------------------------------------------------}
  2138.        { Parse and Translate a Block of Statements }
  2139.  
  2140.        procedure Block;
  2141.        begin
  2142.           Scan;
  2143.           while not(Token in ['e', 'l']) do begin
  2144.              case Token of
  2145.               'i': DoIf;
  2146.               'w': DoWhile;
  2147.              else Assignment;
  2148.              end;
  2149.              Scan;
  2150.           end;
  2151.        end;
  2152.  
  2153.  
  2154.        {--------------------------------------------------------------}
  2155.        { Parse and Translate Global Declarations }
  2156.  
  2157.        procedure TopDecls;
  2158.        begin
  2159.           Scan;
  2160.           while Token <> 'b' do begin
  2161.              case Token of
  2162.                'v': Decl;
  2163.              else Abort('Unrecognized Keyword ' + Value);
  2164.              end;
  2165.              Scan;
  2166.           end;
  2167.        end;
  2168.  
  2169.  
  2170.        {--------------------------------------------------------------}
  2171.        { Parse and Translate a Main Program }
  2172.  
  2173.        procedure Main;
  2174.        begin
  2175.           MatchString('BEGIN');
  2176.           Prolog;
  2177.           Block;
  2178.           MatchString('END');
  2179.           Epilog;
  2180.        end;A6A6
  2181.                                     - 36 -A*A*
  2182. PA A
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.        {--------------------------------------------------------------}
  2189.        {  Parse and Translate a Program }
  2190.  
  2191.        procedure Prog;
  2192.        begin
  2193.           MatchString('PROGRAM');
  2194.           Header;
  2195.           TopDecls;
  2196.           Main;
  2197.           Match('.');
  2198.        end;
  2199.  
  2200.  
  2201.        {--------------------------------------------------------------}
  2202.        { Initialize }
  2203.  
  2204.        procedure Init;
  2205.        var i: char;
  2206.        begin
  2207.           for i := 'A' to 'Z' do
  2208.              ST[i] := ' ';
  2209.           GetChar;
  2210.           Scan;
  2211.        end;
  2212.        {--------------------------------------------------------------}
  2213.  
  2214.  
  2215.        That should do  it.    If  all  the changes got in correctly, you
  2216.        should now be parsing programs that look like programs.   (If you
  2217.        didn't  make  it  through all the  changes,  don't  despair.    A
  2218.        complete listing of the final form is given later.)
  2219.  
  2220.        Did it work?  If so, then we're just about home.  In fact, with a
  2221.        few minor  exceptions we've already got a compiler that's usable.
  2222.        There are still a few areas that need improvement.
  2223.  
  2224.  
  2225.        MULTI-CHARACTER VARIABLE NAMES
  2226.  
  2227.        One of those is  the  restriction  that  we still have, requiring
  2228.        single-character variable names.    Now that we can handle multi-
  2229.        character keywords, this one  begins  to  look  very much like an
  2230.        arbitrary  and  unnecessary  limitation.    And  indeed   it  is.
  2231.        Basically, its only virtue is  that it permits a trivially simple
  2232.        implementation  of  the   symbol   table.    But  that's  just  a
  2233.        convenience to the compiler writers, and needs to be eliminated.
  2234.  
  2235.        We've done this step before.  This time, as usual, I'm doing it a
  2236.        little differently.  I think  the approach used here keeps things
  2237.        just about as simple as possible.
  2238.  
  2239.        The natural  way  to  implement  a  symbol  table in Pascal is by
  2240.        declaring a record type, and making the symbol table an  array of
  2241.        such records.  Here, though, we don't really need  a  type  fieldA*A*
  2242.                                     - 37 -
  2243. PA A
  2244.  
  2245.  
  2246.  
  2247.  
  2248.  
  2249.        yet  (there is only one kind of entry allowed so far), so we only
  2250.        need an array of symbols.  This has the advantage that we can use
  2251.        the existing procedure Lookup to  search the symbol table as well
  2252.        as the  keyword  list.    As it turns out, even when we need more
  2253.        fields we can still use the same approach, simply by  storing the
  2254.        other fields in separate arrays.
  2255.  
  2256.        OK, here are the changes that  need  to  be made.  First, add the
  2257.        new typed constant:
  2258.  
  2259.  
  2260.              NEntry: integer = 0;
  2261.  
  2262.  
  2263.        Then change the definition of the symbol table as follows:
  2264.  
  2265.  
  2266.        const MaxEntry = 100;
  2267.  
  2268.        var ST   : array[1..MaxEntry] of Symbol;
  2269.  
  2270.  
  2271.        (Note that ST is _NOT_ declared as a SymTab.  That declaration is
  2272.        a phony one to get Lookup to work.  A SymTab  would  take  up too
  2273.        much RAM space, and so one is never actually allocated.)
  2274.  
  2275.        Next, we need to replace InTable:
  2276.  
  2277.  
  2278.        {--------------------------------------------------------------}
  2279.        { Look for Symbol in Table }
  2280.  
  2281.        function InTable(n: Symbol): Boolean;
  2282.        begin
  2283.           InTable := Lookup(@ST, n, MaxEntry) <> 0;
  2284.        end;
  2285.        {--------------------------------------------------------------}
  2286.  
  2287.  
  2288.        We also need a new procedure, AddEntry, that adds a new  entry to
  2289.        the table:
  2290.  
  2291.  
  2292.        {--------------------------------------------------------------}
  2293.        { Add a New Entry to Symbol Table }
  2294.  
  2295.        procedure AddEntry(N: Symbol; T: char);
  2296.        begin
  2297.           if InTable(N) then Abort('Duplicate Identifier ' + N);
  2298.           if NEntry = MaxEntry then Abort('Symbol Table Full');
  2299.           Inc(NEntry);
  2300.           ST[NEntry] := N;
  2301.           SType[NEntry] := T;
  2302.        end;A*A*
  2303.                                     - 38 -
  2304. PA A
  2305.  
  2306.  
  2307.  
  2308.  
  2309.  
  2310.        {--------------------------------------------------------------}
  2311.  
  2312.  
  2313.        This procedure is called by Alloc:
  2314.  
  2315.  
  2316.        {--------------------------------------------------------------}
  2317.        { Allocate Storage for a Variable }
  2318.  
  2319.        procedure Alloc(N: Symbol);
  2320.        begin
  2321.           if InTable(N) then Abort('Duplicate Variable Name ' + N);
  2322.           AddEntry(N, 'v');
  2323.        .
  2324.        .
  2325.        .
  2326.        {--------------------------------------------------------------}
  2327.  
  2328.  
  2329.        Finally, we must change all the routines that currently treat the
  2330.        variable name as a single character.  These include   LoadVar and
  2331.        Store (just change the  type  from  char  to string), and Factor,
  2332.        Assignment, and Decl (just change Value[1] to Value).
  2333.  
  2334.        One  last  thing:  change  procedure  Init to clear the array  as
  2335.        shown:
  2336.  
  2337.  
  2338.        {--------------------------------------------------------------}
  2339.        { Initialize }
  2340.  
  2341.        procedure Init;
  2342.        var i: integer;
  2343.        begin
  2344.           for i := 1 to MaxEntry do begin
  2345.              ST[i] := '';
  2346.              SType[i] := ' ';
  2347.           end;
  2348.           GetChar;
  2349.           Scan;
  2350.        end;
  2351.        {--------------------------------------------------------------}
  2352.  
  2353.  
  2354.        That should do it.  Try it out and verify  that  you can, indeed,
  2355.        use multi-character variable names.
  2356.  
  2357.  
  2358.        MORE RELOPS
  2359.  
  2360.        We still have one remaining single-character restriction: the one
  2361.        on relops.  Some of the relops are indeed single  characters, but
  2362.        others  require two.  These are '<=' and '>='.  I also prefer the
  2363.        Pascal '<>' for "not equals,"  instead of '#'.A*A*
  2364.                                     - 39 -
  2365. PA A
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.        If you'll recall, in Part VII I pointed out that the conventional
  2372.        way  to  deal  with  relops  is  to  include them in the list  of
  2373.        keywords, and let the  lexical  scanner  find  them.  But, again,
  2374.        this requires scanning throughout the expression parsing process,
  2375.        whereas so far we've been able to limit the use of the scanner to
  2376.        the beginning of a statement.
  2377.  
  2378.        I mentioned then that we can still get away with this,  since the
  2379.        multi-character relops are so few  and so limited in their usage.
  2380.        It's easy to just treat them as special cases and handle  them in
  2381.        an ad hoc manner.
  2382.  
  2383.        The changes required affect only the code generation routines and
  2384.        procedures Relation and friends.   First, we're going to need two
  2385.        more code generation routines:
  2386.  
  2387.  
  2388.        {---------------------------------------------------------------}
  2389.        { Set D0 If Compare was <= }
  2390.  
  2391.        procedure SetLessOrEqual;
  2392.        begin
  2393.           EmitLn('SGE D0');
  2394.           EmitLn('EXT D0');
  2395.        end;
  2396.  
  2397.  
  2398.        {---------------------------------------------------------------}
  2399.        { Set D0 If Compare was >= }
  2400.  
  2401.        procedure SetGreaterOrEqual;
  2402.        begin
  2403.           EmitLn('SLE D0');
  2404.           EmitLn('EXT D0');
  2405.        end;
  2406.        {---------------------------------------------------------------}
  2407.  
  2408.  
  2409.        Then, modify the relation parsing routines as shown below:
  2410.  
  2411.  
  2412.        {---------------------------------------------------------------}
  2413.        { Recognize and Translate a Relational "Less Than or Equal" }
  2414.  
  2415.        procedure LessOrEqual;
  2416.        begin
  2417.           Match('=');
  2418.           Expression;
  2419.           PopCompare;
  2420.           SetLessOrEqual;
  2421.        end;
  2422.  
  2423.  
  2424.        {---------------------------------------------------------------}A*A*
  2425.                                     - 40 -
  2426. PA A
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.        { Recognize and Translate a Relational "Not Equals" }
  2433.  
  2434.        procedure NotEqual;
  2435.        begin
  2436.           Match('>');
  2437.           Expression;
  2438.           PopCompare;
  2439.           SetNEqual;
  2440.        end;
  2441.  
  2442.  
  2443.        {---------------------------------------------------------------}
  2444.        { Recognize and Translate a Relational "Less Than" }
  2445.  
  2446.        procedure Less;
  2447.        begin
  2448.           Match('<');
  2449.           case Look of
  2450.             '=': LessOrEqual;
  2451.             '>': NotEqual;
  2452.           else begin
  2453.                   Expression;
  2454.                   PopCompare;
  2455.                   SetLess;
  2456.                end;
  2457.           end;
  2458.        end;
  2459.  
  2460.  
  2461.        {---------------------------------------------------------------}
  2462.        { Recognize and Translate a Relational "Greater Than" }
  2463.  
  2464.        procedure Greater;
  2465.        begin
  2466.           Match('>');
  2467.           if Look = '=' then begin
  2468.              Match('=');
  2469.              Expression;
  2470.              PopCompare;
  2471.              SetGreaterOrEqual;
  2472.              end
  2473.           else begin
  2474.              Expression;
  2475.              PopCompare;
  2476.              SetGreater;
  2477.           end;
  2478.        end;
  2479.        {---------------------------------------------------------------}
  2480.  
  2481.  
  2482.        That's all it takes.  Now  you  can  process all the relops.  Try
  2483.        it.ABAB
  2484.                                     - 41 -A*A*
  2485. PA A
  2486.  
  2487.  
  2488.  
  2489.  
  2490.  
  2491.        INPUT/OUTPUT
  2492.  
  2493.        We  now  have  a complete, working language, except for one minor
  2494.        embarassment: we have no way to get data in or out.  We need some
  2495.        I/O.
  2496.  
  2497.        Now, the convention these days, established in C and continued in
  2498.        Ada and Modula 2, is to leave I/O statements out of  the language
  2499.        itself,  and  just  include them in the subroutine library.  That
  2500.        would  be  fine, except that so far  we  have  no  provision  for
  2501.        subroutines.  Anyhow, with this approach you run into the problem
  2502.        of variable-length argument lists.  In Pascal, the I/O statements
  2503.        are built into the language because they are the  only  ones  for
  2504.        which  the  argument  list can have a variable number of entries.
  2505.        In C, we settle for kludges like scanf and printf, and  must pass
  2506.        the argument count to the called procedure.  In Ada and  Modula 2
  2507.        we must use the  awkward  (and SLOW!) approach of a separate call
  2508.        for each argument.
  2509.  
  2510.        So I think I prefer the  Pascal  approach of building the I/O in,
  2511.        even though we don't need to.
  2512.  
  2513.        As  usual,  for  this we need some more code generation routines.
  2514.        These turn out  to be the easiest of all, because all we do is to
  2515.        call library procedures to do the work:
  2516.  
  2517.  
  2518.        {---------------------------------------------------------------}
  2519.        { Read Variable to Primary Register }
  2520.  
  2521.        procedure ReadVar;
  2522.        begin
  2523.           EmitLn('BSR READ');
  2524.           Store(Value);
  2525.        end;
  2526.  
  2527.  
  2528.        {---------------------------------------------------------------}
  2529.        { Write Variable from Primary Register }
  2530.  
  2531.        procedure WriteVar;
  2532.        begin
  2533.           EmitLn('BSR WRITE');
  2534.        end;
  2535.        {--------------------------------------------------------------}
  2536.  
  2537.  
  2538.        The idea is that READ loads the value from input  to  the D0, and
  2539.        WRITE outputs it from there.
  2540.  
  2541.        These two procedures represent  our  first  encounter with a need
  2542.        for library procedures ... the components of a  Run  Time Library
  2543.        (RTL).    Of  course, someone (namely  us)  has  to  write  these
  2544.        routines, but they're not  part  of the compiler itself.  I won'tA*A*
  2545.                                     - 42 -
  2546. PA A
  2547.  
  2548.  
  2549.  
  2550.  
  2551.  
  2552.        even bother  showing the routines here, since these are obviously
  2553.        very much OS-dependent.   I  _WILL_  simply  say that for SK*DOS,
  2554.        they  are  particularly  simple ... almost trivial.  One reason I
  2555.        won't show them here is that  you  can add all kinds of fanciness
  2556.        to the things, for  example  by prompting in READ for the inputs,
  2557.        and by giving the user a chance to reenter a bad input.
  2558.  
  2559.        But that is really separate from compiler design, so for now I'll
  2560.        just assume that a library call TINYLIB.LIB exists.  Since we now
  2561.        need  it  loaded,  we need to add a statement to  include  it  in
  2562.        procedure Header:
  2563.  
  2564.  
  2565.        {--------------------------------------------------------------}
  2566.        { Write Header Info }
  2567.  
  2568.        procedure Header;
  2569.        begin
  2570.  
  2571.           WriteLn('WARMST', TAB, 'EQU $A01E');
  2572.           EmitLn('LIB TINYLIB');
  2573.        end;
  2574.        {--------------------------------------------------------------}
  2575.  
  2576.        That takes care of that part.  Now, we also need to recognize the
  2577.        read  and  write  commands.  We can do this by  adding  two  more
  2578.        keywords to our list:
  2579.  
  2580.  
  2581.        {--------------------------------------------------------------}
  2582.        { Definition of Keywords and Token Types }
  2583.  
  2584.        const NKW =   11;
  2585.              NKW1 = 12;
  2586.  
  2587.        const KWlist: array[1..NKW] of Symbol =
  2588.                      ('IF', 'ELSE', 'ENDIF', 'WHILE', 'ENDWHILE',
  2589.                       'READ',    'WRITE',    'VAR',    'BEGIN',   'END',
  2590.        'PROGRAM');
  2591.  
  2592.        const KWcode: string[NKW1] = 'xileweRWvbep';
  2593.        {--------------------------------------------------------------}
  2594.  
  2595.  
  2596.        (Note how I'm using upper case codes here to avoid  conflict with
  2597.        the 'w' of WHILE.)
  2598.  
  2599.        Next, we need procedures for processing the  read/write statement
  2600.        and its argument list:
  2601.  
  2602.  
  2603.        {--------------------------------------------------------------}
  2604.        { Process a Read Statement }A6A6
  2605.                                     - 43 -A*A*
  2606. PA A
  2607.  
  2608.  
  2609.  
  2610.  
  2611.  
  2612.        procedure DoRead;
  2613.        begin
  2614.           Match('(');
  2615.           GetName;
  2616.           ReadVar;
  2617.           while Look = ',' do begin
  2618.              Match(',');
  2619.              GetName;
  2620.              ReadVar;
  2621.           end;
  2622.           Match(')');
  2623.        end;
  2624.  
  2625.  
  2626.        {--------------------------------------------------------------}
  2627.        { Process a Write Statement }
  2628.  
  2629.        procedure DoWrite;
  2630.        begin
  2631.           Match('(');
  2632.           Expression;
  2633.           WriteVar;
  2634.           while Look = ',' do begin
  2635.              Match(',');
  2636.              Expression;
  2637.              WriteVar;
  2638.           end;
  2639.           Match(')');
  2640.        end;
  2641.        {--------------------------------------------------------------}
  2642.  
  2643.  
  2644.        Finally,  we  must  expand  procedure  Block  to  handle the  new
  2645.        statement types:
  2646.  
  2647.  
  2648.        {--------------------------------------------------------------}
  2649.        { Parse and Translate a Block of Statements }
  2650.  
  2651.        procedure Block;
  2652.        begin
  2653.           Scan;
  2654.           while not(Token in ['e', 'l']) do begin
  2655.              case Token of
  2656.               'i': DoIf;
  2657.               'w': DoWhile;
  2658.               'R': DoRead;
  2659.               'W': DoWrite;
  2660.              else Assignment;
  2661.              end;
  2662.              Scan;
  2663.           end;
  2664.        end;
  2665.        {--------------------------------------------------------------}A*A*
  2666.                                     - 44 -
  2667. PA A
  2668.  
  2669.  
  2670.  
  2671.  
  2672.  
  2673.        That's all there is to it.  _NOW_ we have a language!
  2674.  
  2675.  
  2676.        CONCLUSION
  2677.  
  2678.        At this point we have TINY completely defined.  It's not much ...
  2679.        actually a toy  compiler.    TINY  has  only one data type and no
  2680.        subroutines  ... but it's a complete,  usable  language.    While
  2681.        you're not likely to be able to write another compiler in  it, or
  2682.        do anything else very seriously, you could write programs to read
  2683.        some input, perform calculations,  and  output  the results.  Not
  2684.        too bad for a toy.
  2685.  
  2686.        Most importantly, we have a firm base upon which to build further
  2687.        extensions.  I know you'll be glad to hear this: this is the last
  2688.        time  I'll  start  over in building a parser ... from  now  on  I
  2689.        intend to just add features to  TINY  until it becomes KISS.  Oh,
  2690.        there'll be other times we will  need  to try things out with new
  2691.        copies  of  the  Cradle, but once we've found out how to do those
  2692.        things they'll be incorporated into TINY.
  2693.  
  2694.        What  will  those  features  be?    Well,  for starters  we  need
  2695.        subroutines and functions.    Then  we  need to be able to handle
  2696.        different types, including arrays, strings, and other structures.
  2697.        Then we need to deal with the idea of pointers.  All this will be
  2698.        upcoming in future installments.
  2699.  
  2700.        See you then.
  2701.  
  2702.        For references purposes, the complete listing of TINY Version 1.0
  2703.        is shown below:
  2704.  
  2705.  
  2706.        {--------------------------------------------------------------}
  2707.        program Tiny10;
  2708.  
  2709.        {--------------------------------------------------------------}
  2710.        { Constant Declarations }
  2711.  
  2712.        const TAB = ^I;
  2713.              CR  = ^M;
  2714.              LF  = ^J;
  2715.  
  2716.              LCount: integer = 0;
  2717.              NEntry: integer = 0;
  2718.  
  2719.  
  2720.        {--------------------------------------------------------------}
  2721.        { Type Declarations }
  2722.  
  2723.        type Symbol = string[8];
  2724.  
  2725.             SymTab = array[1..1000] of Symbol;A6A6
  2726.                                     - 45 -A*A*
  2727. PA A
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.             TabPtr = ^SymTab;
  2734.  
  2735.  
  2736.        {--------------------------------------------------------------}
  2737.        { Variable Declarations }
  2738.  
  2739.        var Look : char;             { Lookahead Character }
  2740.            Token: char;             { Encoded Token       }
  2741.            Value: string[16];       { Unencoded Token     }
  2742.  
  2743.  
  2744.        const MaxEntry = 100;
  2745.  
  2746.        var ST   : array[1..MaxEntry] of Symbol;
  2747.            SType: array[1..MaxEntry] of char;
  2748.  
  2749.  
  2750.        {--------------------------------------------------------------}
  2751.        { Definition of Keywords and Token Types }
  2752.  
  2753.        const NKW =   11;
  2754.              NKW1 = 12;
  2755.  
  2756.        const KWlist: array[1..NKW] of Symbol =
  2757.                      ('IF', 'ELSE', 'ENDIF', 'WHILE', 'ENDWHILE',
  2758.                       'READ',    'WRITE',    'VAR',    'BEGIN',   'END',
  2759.        'PROGRAM');
  2760.  
  2761.        const KWcode: string[NKW1] = 'xileweRWvbep';
  2762.  
  2763.  
  2764.        {--------------------------------------------------------------}
  2765.        { Read New Character From Input Stream }
  2766.  
  2767.        procedure GetChar;
  2768.        begin
  2769.           Read(Look);
  2770.        end;
  2771.  
  2772.        {--------------------------------------------------------------}
  2773.        { Report an Error }
  2774.  
  2775.        procedure Error(s: string);
  2776.        begin
  2777.           WriteLn;
  2778.           WriteLn(^G, 'Error: ', s, '.');
  2779.        end;
  2780.  
  2781.  
  2782.        {--------------------------------------------------------------}
  2783.        { Report Error and Halt }
  2784.  
  2785.        procedure Abort(s: string);
  2786.        beginA*A*
  2787.                                     - 46 -
  2788. PA A
  2789.  
  2790.  
  2791.  
  2792.  
  2793.  
  2794.           Error(s);
  2795.           Halt;
  2796.        end;
  2797.  
  2798.  
  2799.        {--------------------------------------------------------------}
  2800.        { Report What Was Expected }
  2801.  
  2802.        procedure Expected(s: string);
  2803.        begin
  2804.           Abort(s + ' Expected');
  2805.        end;
  2806.  
  2807.        {--------------------------------------------------------------}
  2808.        { Report an Undefined Identifier }
  2809.  
  2810.        procedure Undefined(n: string);
  2811.        begin
  2812.           Abort('Undefined Identifier ' + n);
  2813.        end;
  2814.  
  2815.  
  2816.        {--------------------------------------------------------------}
  2817.        { Recognize an Alpha Character }
  2818.  
  2819.        function IsAlpha(c: char): boolean;
  2820.        begin
  2821.           IsAlpha := UpCase(c) in ['A'..'Z'];
  2822.        end;
  2823.  
  2824.  
  2825.        {--------------------------------------------------------------}
  2826.        { Recognize a Decimal Digit }
  2827.  
  2828.        function IsDigit(c: char): boolean;
  2829.        begin
  2830.           IsDigit := c in ['0'..'9'];
  2831.        end;
  2832.  
  2833.  
  2834.        {--------------------------------------------------------------}
  2835.        { Recognize an AlphaNumeric Character }
  2836.  
  2837.        function IsAlNum(c: char): boolean;
  2838.        begin
  2839.           IsAlNum := IsAlpha(c) or IsDigit(c);
  2840.        end;
  2841.  
  2842.  
  2843.        {--------------------------------------------------------------}
  2844.        { Recognize an Addop }
  2845.  
  2846.        function IsAddop(c: char): boolean;
  2847.        beginA*A*
  2848.                                     - 47 -
  2849. PA A
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.           IsAddop := c in ['+', '-'];
  2856.        end;
  2857.  
  2858.  
  2859.        {--------------------------------------------------------------}
  2860.        { Recognize a Mulop }
  2861.  
  2862.        function IsMulop(c: char): boolean;
  2863.        begin
  2864.           IsMulop := c in ['*', '/'];
  2865.        end;
  2866.  
  2867.  
  2868.        {--------------------------------------------------------------}
  2869.        { Recognize a Boolean Orop }
  2870.  
  2871.        function IsOrop(c: char): boolean;
  2872.        begin
  2873.           IsOrop := c in ['|', '~'];
  2874.        end;
  2875.  
  2876.  
  2877.        {--------------------------------------------------------------}
  2878.        { Recognize a Relop }
  2879.  
  2880.        function IsRelop(c: char): boolean;
  2881.        begin
  2882.           IsRelop := c in ['=', '#', '<', '>'];
  2883.        end;
  2884.  
  2885.  
  2886.        {--------------------------------------------------------------}
  2887.        { Recognize White Space }
  2888.  
  2889.        function IsWhite(c: char): boolean;
  2890.        begin
  2891.           IsWhite := c in [' ', TAB];
  2892.        end;
  2893.  
  2894.  
  2895.        {--------------------------------------------------------------}
  2896.        { Skip Over Leading White Space }
  2897.  
  2898.        procedure SkipWhite;
  2899.        begin
  2900.           while IsWhite(Look) do
  2901.              GetChar;
  2902.        end;
  2903.  
  2904.  
  2905.        {--------------------------------------------------------------}
  2906.        { Skip Over an End-of-Line }
  2907.  
  2908.        procedure NewLine;A*A*
  2909.                                     - 48 -
  2910. PA A
  2911.  
  2912.  
  2913.  
  2914.  
  2915.  
  2916.        begin
  2917.           while Look = CR do begin
  2918.              GetChar;
  2919.              if Look = LF then GetChar;
  2920.              SkipWhite;
  2921.           end;
  2922.        end;
  2923.  
  2924.  
  2925.        {--------------------------------------------------------------}
  2926.        { Match a Specific Input Character }
  2927.  
  2928.        procedure Match(x: char);
  2929.        begin
  2930.           NewLine;
  2931.           if Look = x then GetChar
  2932.           else Expected('''' + x + '''');
  2933.           SkipWhite;
  2934.        end;
  2935.  
  2936.  
  2937.        {--------------------------------------------------------------}
  2938.        { Table Lookup }
  2939.  
  2940.        function Lookup(T: TabPtr; s: string; n: integer): integer;
  2941.        var i: integer;
  2942.            found: Boolean;
  2943.        begin
  2944.           found := false;
  2945.           i := n;
  2946.           while (i > 0) and not found do
  2947.              if s = T^[i] then
  2948.                 found := true
  2949.              else
  2950.                 dec(i);
  2951.           Lookup := i;
  2952.        end;
  2953.  
  2954.  
  2955.        {--------------------------------------------------------------}
  2956.        { Locate a Symbol in Table }
  2957.        { Returns the index of the entry.  Zero if not present. }
  2958.  
  2959.        function Locate(N: Symbol): integer;
  2960.        begin
  2961.           Locate := Lookup(@ST, n, MaxEntry);
  2962.        end;
  2963.  
  2964.  
  2965.        {--------------------------------------------------------------}
  2966.        { Look for Symbol in Table }
  2967.  
  2968.        function InTable(n: Symbol): Boolean;
  2969.        beginA*A*
  2970.                                     - 49 -
  2971. PA A
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.           InTable := Lookup(@ST, n, MaxEntry) <> 0;
  2978.        end;
  2979.  
  2980.  
  2981.        {--------------------------------------------------------------}
  2982.        { Add a New Entry to Symbol Table }
  2983.  
  2984.        procedure AddEntry(N: Symbol; T: char);
  2985.        begin
  2986.           if InTable(N) then Abort('Duplicate Identifier ' + N);
  2987.           if NEntry = MaxEntry then Abort('Symbol Table Full');
  2988.           Inc(NEntry);
  2989.           ST[NEntry] := N;
  2990.           SType[NEntry] := T;
  2991.        end;
  2992.  
  2993.  
  2994.        {--------------------------------------------------------------}
  2995.        { Get an Identifier }
  2996.  
  2997.        procedure GetName;
  2998.        begin
  2999.           NewLine;
  3000.           if not IsAlpha(Look) then Expected('Name');
  3001.           Value := '';
  3002.           while IsAlNum(Look) do begin
  3003.              Value := Value + UpCase(Look);
  3004.              GetChar;
  3005.           end;
  3006.           SkipWhite;
  3007.        end;
  3008.  
  3009.  
  3010.        {--------------------------------------------------------------}
  3011.        { Get a Number }
  3012.  
  3013.        function GetNum: integer;
  3014.        var Val: integer;
  3015.        begin
  3016.           NewLine;
  3017.           if not IsDigit(Look) then Expected('Integer');
  3018.           Val := 0;
  3019.           while IsDigit(Look) do begin
  3020.              Val := 10 * Val + Ord(Look) - Ord('0');
  3021.              GetChar;
  3022.           end;
  3023.           GetNum := Val;
  3024.           SkipWhite;
  3025.        end;
  3026.  
  3027.  
  3028.        {--------------------------------------------------------------}
  3029.        { Get an Identifier and Scan it for Keywords }A6A6
  3030.                                     - 50 -A*A*
  3031. PA A
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.        procedure Scan;
  3038.        begin
  3039.           GetName;
  3040.           Token := KWcode[Lookup(Addr(KWlist), Value, NKW) + 1];
  3041.        end;
  3042.  
  3043.  
  3044.        {--------------------------------------------------------------}
  3045.        { Match a Specific Input String }
  3046.  
  3047.        procedure MatchString(x: string);
  3048.        begin
  3049.           if Value <> x then Expected('''' + x + '''');
  3050.        end;
  3051.  
  3052.  
  3053.        {--------------------------------------------------------------}
  3054.        { Output a String with Tab }
  3055.  
  3056.        procedure Emit(s: string);
  3057.        begin
  3058.           Write(TAB, s);
  3059.        end;
  3060.  
  3061.  
  3062.        {--------------------------------------------------------------}
  3063.        { Output a String with Tab and CRLF }
  3064.  
  3065.        procedure EmitLn(s: string);
  3066.        begin
  3067.           Emit(s);
  3068.           WriteLn;
  3069.        end;
  3070.  
  3071.  
  3072.        {--------------------------------------------------------------}
  3073.        { Generate a Unique Label }
  3074.  
  3075.        function NewLabel: string;
  3076.        var S: string;
  3077.        begin
  3078.           Str(LCount, S);
  3079.           NewLabel := 'L' + S;
  3080.           Inc(LCount);
  3081.        end;
  3082.  
  3083.  
  3084.        {--------------------------------------------------------------}
  3085.        { Post a Label To Output }
  3086.  
  3087.        procedure PostLabel(L: string);
  3088.        begin
  3089.           WriteLn(L, ':');
  3090.        end;A*A*
  3091.                                     - 51 -
  3092. PA A
  3093.  
  3094.  
  3095.  
  3096.  
  3097.  
  3098.        {---------------------------------------------------------------}
  3099.        { Clear the Primary Register }
  3100.  
  3101.        procedure Clear;
  3102.        begin
  3103.           EmitLn('CLR D0');
  3104.        end;
  3105.  
  3106.  
  3107.        {---------------------------------------------------------------}
  3108.        { Negate the Primary Register }
  3109.  
  3110.        procedure Negate;
  3111.        begin
  3112.           EmitLn('NEG D0');
  3113.        end;
  3114.  
  3115.  
  3116.        {---------------------------------------------------------------}
  3117.        { Complement the Primary Register }
  3118.  
  3119.        procedure NotIt;
  3120.        begin
  3121.           EmitLn('NOT D0');
  3122.        end;
  3123.  
  3124.  
  3125.        {---------------------------------------------------------------}
  3126.        { Load a Constant Value to Primary Register }
  3127.  
  3128.        procedure LoadConst(n: integer);
  3129.        begin
  3130.           Emit('MOVE #');
  3131.           WriteLn(n, ',D0');
  3132.        end;
  3133.  
  3134.  
  3135.        {---------------------------------------------------------------}
  3136.        { Load a Variable to Primary Register }
  3137.  
  3138.        procedure LoadVar(Name: string);
  3139.        begin
  3140.           if not InTable(Name) then Undefined(Name);
  3141.           EmitLn('MOVE ' + Name + '(PC),D0');
  3142.        end;
  3143.  
  3144.  
  3145.        {---------------------------------------------------------------}
  3146.        { Push Primary onto Stack }
  3147.  
  3148.        procedure Push;
  3149.        begin
  3150.           EmitLn('MOVE D0,-(SP)');
  3151.        end;A*A*
  3152.                                     - 52 -
  3153. PA A
  3154.  
  3155.  
  3156.  
  3157.  
  3158.  
  3159.        {---------------------------------------------------------------}
  3160.        { Add Top of Stack to Primary }
  3161.  
  3162.        procedure PopAdd;
  3163.        begin
  3164.           EmitLn('ADD (SP)+,D0');
  3165.        end;
  3166.  
  3167.  
  3168.        {---------------------------------------------------------------}
  3169.        { Subtract Primary from Top of Stack }
  3170.  
  3171.        procedure PopSub;
  3172.        begin
  3173.           EmitLn('SUB (SP)+,D0');
  3174.           EmitLn('NEG D0');
  3175.        end;
  3176.  
  3177.  
  3178.        {---------------------------------------------------------------}
  3179.        { Multiply Top of Stack by Primary }
  3180.  
  3181.        procedure PopMul;
  3182.        begin
  3183.           EmitLn('MULS (SP)+,D0');
  3184.        end;
  3185.  
  3186.  
  3187.        {---------------------------------------------------------------}
  3188.        { Divide Top of Stack by Primary }
  3189.  
  3190.        procedure PopDiv;
  3191.        begin
  3192.           EmitLn('MOVE (SP)+,D7');
  3193.           EmitLn('EXT.L D7');
  3194.           EmitLn('DIVS D0,D7');
  3195.           EmitLn('MOVE D7,D0');
  3196.        end;
  3197.  
  3198.  
  3199.        {---------------------------------------------------------------}
  3200.        { AND Top of Stack with Primary }
  3201.  
  3202.        procedure PopAnd;
  3203.        begin
  3204.           EmitLn('AND (SP)+,D0');
  3205.        end;
  3206.  
  3207.  
  3208.        {---------------------------------------------------------------}
  3209.        { OR Top of Stack with Primary }
  3210.  
  3211.        procedure PopOr;
  3212.        beginA*A*
  3213.                                     - 53 -
  3214. PA A
  3215.  
  3216.  
  3217.  
  3218.  
  3219.  
  3220.           EmitLn('OR (SP)+,D0');
  3221.        end;
  3222.  
  3223.  
  3224.        {---------------------------------------------------------------}
  3225.        { XOR Top of Stack with Primary }
  3226.  
  3227.        procedure PopXor;
  3228.        begin
  3229.           EmitLn('EOR (SP)+,D0');
  3230.        end;
  3231.  
  3232.  
  3233.        {---------------------------------------------------------------}
  3234.        { Compare Top of Stack with Primary }
  3235.  
  3236.        procedure PopCompare;
  3237.        begin
  3238.           EmitLn('CMP (SP)+,D0');
  3239.        end;
  3240.  
  3241.  
  3242.        {---------------------------------------------------------------}
  3243.        { Set D0 If Compare was = }
  3244.  
  3245.        procedure SetEqual;
  3246.        begin
  3247.           EmitLn('SEQ D0');
  3248.           EmitLn('EXT D0');
  3249.        end;
  3250.  
  3251.  
  3252.        {---------------------------------------------------------------}
  3253.        { Set D0 If Compare was != }
  3254.  
  3255.        procedure SetNEqual;
  3256.        begin
  3257.           EmitLn('SNE D0');
  3258.           EmitLn('EXT D0');
  3259.        end;
  3260.  
  3261.  
  3262.        {---------------------------------------------------------------}
  3263.        { Set D0 If Compare was > }
  3264.  
  3265.        procedure SetGreater;
  3266.        begin
  3267.           EmitLn('SLT D0');
  3268.           EmitLn('EXT D0');
  3269.        end;
  3270.  
  3271.  
  3272.        {---------------------------------------------------------------}
  3273.        { Set D0 If Compare was < }A*A*
  3274.                                     - 54 -
  3275. PA A
  3276.  
  3277.  
  3278.  
  3279.  
  3280.  
  3281.        procedure SetLess;
  3282.        begin
  3283.           EmitLn('SGT D0');
  3284.           EmitLn('EXT D0');
  3285.        end;
  3286.  
  3287.  
  3288.        {---------------------------------------------------------------}
  3289.        { Set D0 If Compare was <= }
  3290.  
  3291.        procedure SetLessOrEqual;
  3292.        begin
  3293.           EmitLn('SGE D0');
  3294.           EmitLn('EXT D0');
  3295.        end;
  3296.  
  3297.  
  3298.        {---------------------------------------------------------------}
  3299.        { Set D0 If Compare was >= }
  3300.  
  3301.        procedure SetGreaterOrEqual;
  3302.        begin
  3303.           EmitLn('SLE D0');
  3304.           EmitLn('EXT D0');
  3305.        end;
  3306.  
  3307.  
  3308.        {---------------------------------------------------------------}
  3309.        { Store Primary to Variable }
  3310.  
  3311.        procedure Store(Name: string);
  3312.        begin
  3313.           if not InTable(Name) then Undefined(Name);
  3314.           EmitLn('LEA ' + Name + '(PC),A0');
  3315.           EmitLn('MOVE D0,(A0)')
  3316.        end;
  3317.  
  3318.  
  3319.        {---------------------------------------------------------------}
  3320.        { Branch Unconditional  }
  3321.  
  3322.        procedure Branch(L: string);
  3323.        begin
  3324.           EmitLn('BRA ' + L);
  3325.        end;
  3326.  
  3327.  
  3328.        {---------------------------------------------------------------}
  3329.        { Branch False }
  3330.  
  3331.        procedure BranchFalse(L: string);
  3332.        begin
  3333.           EmitLn('TST D0');
  3334.           EmitLn('BEQ ' + L);A*A*
  3335.                                     - 55 -
  3336. PA A
  3337.  
  3338.  
  3339.  
  3340.  
  3341.  
  3342.        end;
  3343.  
  3344.  
  3345.        {---------------------------------------------------------------}
  3346.        { Read Variable to Primary Register }
  3347.  
  3348.        procedure ReadVar;
  3349.        begin
  3350.           EmitLn('BSR READ');
  3351.           Store(Value[1]);
  3352.        end;
  3353.  
  3354.  
  3355.        { Write Variable from Primary Register }
  3356.  
  3357.        procedure WriteVar;
  3358.        begin
  3359.           EmitLn('BSR WRITE');
  3360.        end;
  3361.  
  3362.  
  3363.        {--------------------------------------------------------------}
  3364.        { Write Header Info }
  3365.  
  3366.        procedure Header;
  3367.        begin
  3368.           WriteLn('WARMST', TAB, 'EQU $A01E');
  3369.        end;
  3370.  
  3371.  
  3372.        {--------------------------------------------------------------}
  3373.        { Write the Prolog }
  3374.  
  3375.        procedure Prolog;
  3376.        begin
  3377.           PostLabel('MAIN');
  3378.        end;
  3379.  
  3380.  
  3381.        {--------------------------------------------------------------}
  3382.        { Write the Epilog }
  3383.  
  3384.        procedure Epilog;
  3385.        begin
  3386.           EmitLn('DC WARMST');
  3387.           EmitLn('END MAIN');
  3388.        end;
  3389.  
  3390.  
  3391.        {---------------------------------------------------------------}
  3392.        { Parse and Translate a Math Factor }
  3393.  
  3394.        procedure BoolExpression; Forward;A6A6
  3395.                                     - 56 -A*A*
  3396. PA A
  3397.  
  3398.  
  3399.  
  3400.  
  3401.  
  3402.        procedure Factor;
  3403.        begin
  3404.           if Look = '(' then begin
  3405.              Match('(');
  3406.              BoolExpression;
  3407.              Match(')');
  3408.              end
  3409.           else if IsAlpha(Look) then begin
  3410.              GetName;
  3411.              LoadVar(Value);
  3412.              end
  3413.           else
  3414.              LoadConst(GetNum);
  3415.        end;
  3416.  
  3417.  
  3418.        {--------------------------------------------------------------}
  3419.        { Parse and Translate a Negative Factor }
  3420.  
  3421.        procedure NegFactor;
  3422.        begin
  3423.           Match('-');
  3424.           if IsDigit(Look) then
  3425.              LoadConst(-GetNum)
  3426.           else begin
  3427.              Factor;
  3428.              Negate;
  3429.           end;
  3430.        end;
  3431.  
  3432.  
  3433.        {--------------------------------------------------------------}
  3434.        { Parse and Translate a Leading Factor }
  3435.  
  3436.        procedure FirstFactor;
  3437.        begin
  3438.           case Look of
  3439.             '+': begin
  3440.                     Match('+');
  3441.                     Factor;
  3442.                  end;
  3443.             '-': NegFactor;
  3444.           else  Factor;
  3445.           end;
  3446.        end;
  3447.  
  3448.  
  3449.        {--------------------------------------------------------------}
  3450.        { Recognize and Translate a Multiply }
  3451.  
  3452.        procedure Multiply;
  3453.        begin
  3454.           Match('*');
  3455.           Factor;A*A*
  3456.                                     - 57 -
  3457. PA A
  3458.  
  3459.  
  3460.  
  3461.  
  3462.  
  3463.           PopMul;
  3464.        end;
  3465.  
  3466.  
  3467.        {-------------------------------------------------------------}
  3468.        { Recognize and Translate a Divide }
  3469.  
  3470.        procedure Divide;
  3471.        begin
  3472.           Match('/');
  3473.           Factor;
  3474.           PopDiv;
  3475.        end;
  3476.  
  3477.  
  3478.        {---------------------------------------------------------------}
  3479.        { Common Code Used by Term and FirstTerm }
  3480.  
  3481.        procedure Term1;
  3482.        begin
  3483.           while IsMulop(Look) do begin
  3484.              Push;
  3485.              case Look of
  3486.               '*': Multiply;
  3487.               '/': Divide;
  3488.              end;
  3489.           end;
  3490.        end;
  3491.  
  3492.  
  3493.        {---------------------------------------------------------------}
  3494.        { Parse and Translate a Math Term }
  3495.  
  3496.        procedure Term;
  3497.        begin
  3498.           Factor;
  3499.           Term1;
  3500.        end;
  3501.  
  3502.  
  3503.        {---------------------------------------------------------------}
  3504.        { Parse and Translate a Leading Term }
  3505.  
  3506.        procedure FirstTerm;
  3507.        begin
  3508.           FirstFactor;
  3509.           Term1;
  3510.        end;
  3511.  
  3512.  
  3513.        {--------------------------------------------------------------}
  3514.        { Recognize and Translate an Add }
  3515.  
  3516.        procedure Add;A*A*
  3517.                                     - 58 -
  3518. PA A
  3519.  
  3520.  
  3521.  
  3522.  
  3523.  
  3524.        begin
  3525.           Match('+');
  3526.           Term;
  3527.           PopAdd;
  3528.        end;
  3529.  
  3530.  
  3531.        {-------------------------------------------------------------}
  3532.        { Recognize and Translate a Subtract }
  3533.  
  3534.        procedure Subtract;
  3535.        begin
  3536.           Match('-');
  3537.           Term;
  3538.           PopSub;
  3539.        end;
  3540.  
  3541.  
  3542.        {---------------------------------------------------------------}
  3543.        { Parse and Translate an Expression }
  3544.  
  3545.        procedure Expression;
  3546.        begin
  3547.           FirstTerm;
  3548.           while IsAddop(Look) do begin
  3549.              Push;
  3550.              case Look of
  3551.               '+': Add;
  3552.               '-': Subtract;
  3553.              end;
  3554.           end;
  3555.        end;
  3556.  
  3557.  
  3558.        {---------------------------------------------------------------}
  3559.        { Recognize and Translate a Relational "Equals" }
  3560.  
  3561.        procedure Equal;
  3562.        begin
  3563.           Match('=');
  3564.           Expression;
  3565.           PopCompare;
  3566.           SetEqual;
  3567.        end;
  3568.  
  3569.  
  3570.        {---------------------------------------------------------------}
  3571.        { Recognize and Translate a Relational "Less Than or Equal" }
  3572.  
  3573.        procedure LessOrEqual;
  3574.        begin
  3575.           Match('=');
  3576.           Expression;
  3577.           PopCompare;A*A*
  3578.                                     - 59 -
  3579. PA A
  3580.  
  3581.  
  3582.  
  3583.  
  3584.  
  3585.           SetLessOrEqual;
  3586.        end;
  3587.  
  3588.  
  3589.        {---------------------------------------------------------------}
  3590.        { Recognize and Translate a Relational "Not Equals" }
  3591.  
  3592.        procedure NotEqual;
  3593.        begin
  3594.           Match('>');
  3595.           Expression;
  3596.           PopCompare;
  3597.           SetNEqual;
  3598.        end;
  3599.  
  3600.  
  3601.        {---------------------------------------------------------------}
  3602.        { Recognize and Translate a Relational "Less Than" }
  3603.  
  3604.        procedure Less;
  3605.        begin
  3606.           Match('<');
  3607.           case Look of
  3608.             '=': LessOrEqual;
  3609.             '>': NotEqual;
  3610.           else begin
  3611.                   Expression;
  3612.                   PopCompare;
  3613.                   SetLess;
  3614.                end;
  3615.           end;
  3616.        end;
  3617.  
  3618.  
  3619.        {---------------------------------------------------------------}
  3620.        { Recognize and Translate a Relational "Greater Than" }
  3621.  
  3622.        procedure Greater;
  3623.        begin
  3624.           Match('>');
  3625.           if Look = '=' then begin
  3626.              Match('=');
  3627.              Expression;
  3628.              PopCompare;
  3629.              SetGreaterOrEqual;
  3630.              end
  3631.           else begin
  3632.              Expression;
  3633.              PopCompare;
  3634.              SetGreater;
  3635.           end;
  3636.        end;ABAB
  3637.                                     - 60 -A*A*
  3638. PA A
  3639.  
  3640.  
  3641.  
  3642.  
  3643.  
  3644.        {---------------------------------------------------------------}
  3645.        { Parse and Translate a Relation }
  3646.  
  3647.  
  3648.        procedure Relation;
  3649.        begin
  3650.           Expression;
  3651.           if IsRelop(Look) then begin
  3652.              Push;
  3653.              case Look of
  3654.               '=': Equal;
  3655.               '<': Less;
  3656.               '>': Greater;
  3657.              end;
  3658.           end;
  3659.        end;
  3660.  
  3661.  
  3662.        {---------------------------------------------------------------}
  3663.        { Parse and Translate a Boolean Factor with Leading NOT }
  3664.  
  3665.        procedure NotFactor;
  3666.        begin
  3667.           if Look = '!' then begin
  3668.              Match('!');
  3669.              Relation;
  3670.              NotIt;
  3671.              end
  3672.           else
  3673.              Relation;
  3674.        end;
  3675.  
  3676.  
  3677.        {---------------------------------------------------------------}
  3678.        { Parse and Translate a Boolean Term }
  3679.  
  3680.        procedure BoolTerm;
  3681.        begin
  3682.           NotFactor;
  3683.           while Look = '&' do begin
  3684.              Push;
  3685.              Match('&');
  3686.              NotFactor;
  3687.              PopAnd;
  3688.           end;
  3689.        end;
  3690.  
  3691.  
  3692.        {--------------------------------------------------------------}
  3693.        { Recognize and Translate a Boolean OR }
  3694.  
  3695.        procedure BoolOr;
  3696.        begin
  3697.           Match('|');A*A*
  3698.                                     - 61 -
  3699. PA A
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.           BoolTerm;
  3706.           PopOr;
  3707.        end;
  3708.  
  3709.  
  3710.        {--------------------------------------------------------------}
  3711.        { Recognize and Translate an Exclusive Or }
  3712.  
  3713.        procedure BoolXor;
  3714.        begin
  3715.           Match('~');
  3716.           BoolTerm;
  3717.           PopXor;
  3718.        end;
  3719.  
  3720.  
  3721.        {---------------------------------------------------------------}
  3722.        { Parse and Translate a Boolean Expression }
  3723.  
  3724.        procedure BoolExpression;
  3725.        begin
  3726.           BoolTerm;
  3727.           while IsOrOp(Look) do begin
  3728.              Push;
  3729.              case Look of
  3730.               '|': BoolOr;
  3731.               '~': BoolXor;
  3732.              end;
  3733.           end;
  3734.        end;
  3735.  
  3736.  
  3737.        {--------------------------------------------------------------}
  3738.        { Parse and Translate an Assignment Statement }
  3739.  
  3740.        procedure Assignment;
  3741.        var Name: string;
  3742.        begin
  3743.           Name := Value;
  3744.           Match('=');
  3745.           BoolExpression;
  3746.           Store(Name);
  3747.        end;
  3748.  
  3749.  
  3750.        {---------------------------------------------------------------}
  3751.        { Recognize and Translate an IF Construct }
  3752.  
  3753.        procedure Block; Forward;
  3754.  
  3755.  
  3756.        procedure DoIf;
  3757.        var L1, L2: string;
  3758.        beginA*A*
  3759.                                     - 62 -
  3760. PA A
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766.           BoolExpression;
  3767.           L1 := NewLabel;
  3768.           L2 := L1;
  3769.           BranchFalse(L1);
  3770.           Block;
  3771.           if Token = 'l' then begin
  3772.              L2 := NewLabel;
  3773.              Branch(L2);
  3774.              PostLabel(L1);
  3775.              Block;
  3776.           end;
  3777.           PostLabel(L2);
  3778.           MatchString('ENDIF');
  3779.        end;
  3780.  
  3781.  
  3782.        {--------------------------------------------------------------}
  3783.        { Parse and Translate a WHILE Statement }
  3784.  
  3785.        procedure DoWhile;
  3786.        var L1, L2: string;
  3787.        begin
  3788.           L1 := NewLabel;
  3789.           L2 := NewLabel;
  3790.           PostLabel(L1);
  3791.           BoolExpression;
  3792.           BranchFalse(L2);
  3793.           Block;
  3794.           MatchString('ENDWHILE');
  3795.           Branch(L1);
  3796.           PostLabel(L2);
  3797.        end;
  3798.  
  3799.  
  3800.        {--------------------------------------------------------------}
  3801.        { Process a Read Statement }
  3802.  
  3803.        procedure DoRead;
  3804.        begin
  3805.           Match('(');
  3806.           GetName;
  3807.           ReadVar;
  3808.           while Look = ',' do begin
  3809.              Match(',');
  3810.              GetName;
  3811.              ReadVar;
  3812.           end;
  3813.           Match(')');
  3814.        end;
  3815.  
  3816.  
  3817.        {--------------------------------------------------------------}
  3818.        { Process a Write Statement }A6A6
  3819.                                     - 63 -A*A*
  3820. PA A
  3821.  
  3822.  
  3823.  
  3824.  
  3825.  
  3826.        procedure DoWrite;
  3827.        begin
  3828.           Match('(');
  3829.           Expression;
  3830.           WriteVar;
  3831.           while Look = ',' do begin
  3832.              Match(',');
  3833.              Expression;
  3834.              WriteVar;
  3835.           end;
  3836.           Match(')');
  3837.        end;
  3838.  
  3839.  
  3840.        {--------------------------------------------------------------}
  3841.        { Parse and Translate a Block of Statements }
  3842.  
  3843.        procedure Block;
  3844.        begin
  3845.           Scan;
  3846.           while not(Token in ['e', 'l']) do begin
  3847.              case Token of
  3848.               'i': DoIf;
  3849.               'w': DoWhile;
  3850.               'R': DoRead;
  3851.               'W': DoWrite;
  3852.              else Assignment;
  3853.              end;
  3854.              Scan;
  3855.           end;
  3856.        end;
  3857.  
  3858.  
  3859.        {--------------------------------------------------------------}
  3860.        { Allocate Storage for a Variable }
  3861.  
  3862.        procedure Alloc(N: Symbol);
  3863.        begin
  3864.           if InTable(N) then Abort('Duplicate Variable Name ' + N);
  3865.           AddEntry(N, 'v');
  3866.           Write(N, ':', TAB, 'DC ');
  3867.           if Look = '=' then begin
  3868.              Match('=');
  3869.              If Look = '-' then begin
  3870.                 Write(Look);
  3871.                 Match('-');
  3872.              end;
  3873.              WriteLn(GetNum);
  3874.              end
  3875.           else
  3876.              WriteLn('0');
  3877.        end;ABAB
  3878.                                     - 64 -A*A*
  3879. PA A
  3880.  
  3881.  
  3882.  
  3883.  
  3884.  
  3885.        {--------------------------------------------------------------}
  3886.        { Parse and Translate a Data Declaration }
  3887.  
  3888.        procedure Decl;
  3889.        begin
  3890.           GetName;
  3891.           Alloc(Value);
  3892.           while Look = ',' do begin
  3893.              Match(',');
  3894.              GetName;
  3895.              Alloc(Value);
  3896.           end;
  3897.        end;
  3898.  
  3899.  
  3900.        {--------------------------------------------------------------}
  3901.        { Parse and Translate Global Declarations }
  3902.  
  3903.        procedure TopDecls;
  3904.        begin
  3905.           Scan;
  3906.           while Token <> 'b' do begin
  3907.              case Token of
  3908.                'v': Decl;
  3909.              else Abort('Unrecognized Keyword ' + Value);
  3910.              end;
  3911.              Scan;
  3912.           end;
  3913.        end;
  3914.  
  3915.  
  3916.        {--------------------------------------------------------------}
  3917.        { Parse and Translate a Main Program }
  3918.  
  3919.        procedure Main;
  3920.        begin
  3921.           MatchString('BEGIN');
  3922.           Prolog;
  3923.           Block;
  3924.           MatchString('END');
  3925.           Epilog;
  3926.        end;
  3927.  
  3928.  
  3929.        {--------------------------------------------------------------}
  3930.        {  Parse and Translate a Program }
  3931.  
  3932.        procedure Prog;
  3933.        begin
  3934.           MatchString('PROGRAM');
  3935.           Header;
  3936.           TopDecls;
  3937.           Main;
  3938.           Match('.');A*A*
  3939.                                     - 65 -
  3940. PA A
  3941.  
  3942.  
  3943.  
  3944.  
  3945.  
  3946.        end;
  3947.  
  3948.  
  3949.        {--------------------------------------------------------------}
  3950.        { Initialize }
  3951.  
  3952.        procedure Init;
  3953.        var i: integer;
  3954.        begin
  3955.           for i := 1 to MaxEntry do begin
  3956.              ST[i] := '';
  3957.              SType[i] := ' ';
  3958.           end;
  3959.           GetChar;
  3960.           Scan;
  3961.        end;
  3962.  
  3963.  
  3964.        {--------------------------------------------------------------}
  3965.        { Main Program }
  3966.  
  3967.        begin
  3968.           Init;
  3969.           Prog;
  3970.           if Look <> CR then Abort('Unexpected data after ''.''');
  3971.        end.
  3972.        {--------------------------------------------------------------}
  3973.  
  3974.  
  3975.  
  3976.        *****************************************************************
  3977.        *                                                               *
  3978.        *                        COPYRIGHT NOTICE                       *
  3979.        *                                                               *
  3980.        *   Copyright (C) 1989 Jack W. Crenshaw. All rights reserved.   *
  3981.        *                                                               *
  3982.        *****************************************************************ARAR
  3983.  
  3984.  
  3985.                                     - 66 -A*A*
  3986. @