NAME
     statement - generate assembly codes for statements.
     condition - generate assembly codes for a 'condition'.
     write_to_display - generates assembly codes for 'write'.
     expression - controls the main flow of an expression.
     term - called from expression to perform multiplication or divition.
     factor - called from the term. It pushes a token to the stack.

SYNOPSIS
     #include "getsym.h"
     statement.c

     void
     statement(void);

     void 
     condition(void);

     void
     write_to_display(void);

     void
     expressio(void);

     void
     term(void);

     void 
     factor(void);

DESCRIPTION
     The file "statement.c" contains all the functions mentioned above. It
     controls the main flow of a statement. Statement is defined as: 

                  _______     ____     ___________
          ------>| ident |-->| := |-->| expression |-------------------------->
           |      -------     ----     ------------                          |
           |	 ------      -----------                                     |
          ----->| ident |-->| paramlist |------------------------------------
           |	 ------      -----------                                     |
           |                                                                 | 
           |	 ------      -----------            -----                    |
          ----->| begin |-->| statement |--------->| end |-------------------
           |	 ------   ^  -----------      |     -----                    |
           |		  |    ---            |	                             | 
           |		   ---| ; |-----------                               |
           |		       ---                                           |
           |	 ----     -----------     ------     -----------             |
          ----->| if |-->| condition |-->| then |-->| statement |------------
           |	 ----     -----------     ------     -----------          |  |
           |				    ------------------------------   |
           |			           |		                     |
           |			           |		                     |
           |			           |	  -----      -----------     |
           |			            ---> | else |-->| statement |----|
           |					  ------     -----------     |
           |								     |
           |	 -------     -----------     ----     -----------            |
          ----->| while |-->| condition |-->| do |-->| statement |-----------
           |	 -------     -----------     ----     -----------            |
           |     -------               -------                               |
          ----->| write |------------>| ident |------------------------------
                  -------      ^        -------        |
                               |         ---           |
           		       ---------| , |-----------
                                         --- 

     statement is called with the first token read from the previous
     function. So, it uses a switch to jump according to the token. It 
     is implemented like:

          switch(tok.attr) {
	     case IDENTIFIER: ..... /* expression or paramlist       */
	     case RWORD: .....      /* begin or if or while or write */ 
	  }

     Inside the 'case IDENTIFIER', there is two possibilities: either it
     can call expression or it can process a function calling. (The
     expression part is described later) When a LPAREN('(') follows the
     identifier immeadiately, statement assumes that a function calling
     should be prepared. The function calling is implemented as:

		 ___     ____________      ___
          ----->| ( |-->| expression |--->| ) |---------> 
		 ---  ^  ------------  |   ---
                      |      ___       |
		       -----| , |------
			     ---

     When:

          hanoi(1, 2, 3, 5);

     is encountered, the variables are passed using code_generator. The
     ultimate BR(Base Register) and SP(Stack Pointer) is calculated like 
     this:

	  BR = BR + Present Frame's variable numbers + 1
	  SP = SP + Present Frame's variable numbers

     code_generator uses straight 'store' to pass the variables(in a 
     sense it is not variable passing! :( ). This is implemented like:

	  gen_store_param(br_counter + countData(Table) + 1)

     gen_store_param is declared in code_generator.c like:

	  gen_store_param(int br_counter)
	  {
	      fprintf(outfile, "   store  r3, %d(BR) \n", br_counter);
	  }

     So,  ultimately you get assembly codes for 'hanoi(1, 2, 3, 5)':

	  loadi    r3, 1
          store    r3, 2(BR)  # 1st parameter is passed 
	  loadi    r3, 2
	  store    r3, 3(BR)  # 2nd parameter is passed
	  loadi    r3, 3
	  store    r3, 4(BR)  # 3rd parameter is passed
	  load      r3, 0
	  store    r3, 5(BR)  # 4th parameter is passed

     And don't forget to increase the SP(Stack Pointer) and BR(Base
     Register) following the below equation:

	  BR = BR + Present Frame's variable numbers + 1
	  SP = SP + Present Frame's variable numbers

     So, you will get assembly codes like: 

	  addi     sp, 1
	  addi     br, 2
	  call     hanoi

     And when you return you have to decrease the SP, BR to their original
     value by following the equation:

	  BR = BR - Present Frame's variable numbers - 1
	  SP = SP - Present Frame's variable numbers

     So, you will get assembly codes like:

	  subi     sp, 1
	  subi     br, 2


     condition is implemented like:

		 ____________  
          ----->| expression |--------------------------
		 ------------  |    |    |    |    |    |
                               v    v    v    v    v    v
                               =    <>   <    >    <=   >=
			       |    |    |    |    |    |
	        ____________   |    |    |    |    |    |
         <-----| expression |<--------------------------
	        ------------ 

     It uses two call to expression. After the first call to the
     expression, the result is saved in the register 'r1'. The result 
     of the second call to the expression is saved in the register is
     saved in the register 'r3'. So, condition is achieved comparing
     two registers like:

	  cmpr   r1, r3

     
     write_to_display writes a variable to the display. It uses register
     'r0' to load the appropiate variables and writing them to the 
     display. Point to remember that it always writes an extra newline
     character after each displayed variable.

     expression is main function that controls the flow of an expression.
     It is implemented like:

		                  _______
          ---------------------->| term  |------------------> 
		        ^         -------            |
	        	|           ___              |
                        |<---------| + |-------------
	                |           ---              |	
		        |	    ---              |
			 ----------| - |-------------
				    ---
     
     So, it calls the term immediately, which in turn calls factor. Actual
     assembly codes are actually produced after the 'term' finishes it's
     job of producing codes for multiplication or division. Here, how it
     is actually implemented:

          term();
          sign = tok.value;
	  while(tok.value == PLUS || tok.value == MINUS)
	  {
	    term();
	    gen_code(sign);
	    sign = tok.value;
	  }

     For example:

	  x - y + z;

     expression is encountered. Since, it calls term immeadiately and
     term calls factor immeadiately, the equation is marked like this:

	  x - y + z;                x - y + z;
	    ^            ---->        ^ 
	    |                         |
	  tok.value                  sign = tok.value

     at this condition it enters the while loop. In the while loop term
     is called(which in return calls factor) and the equation gets 
     marked again like:

	  x - y + z;
	    ^   ^
	    |   |
	  sign  tok.value

     At this position, assembly codes are produced for the sign by:

	  gen_code(sign);

     In this way, '-' is calculated before '+'. You can simply put it
     this way that: it performs calculation from the left to the right.

	  
     term performs the multiplication and division before addition or
     subtraction. It is implemented like:

		                  _______
          ---------------------->| factor |------------------> 
		        ^         -------            |
	        	|           ___              |
                        |<---------| * |-------------
	                |           ---              |	
		        |	   -----             |
			 ---------| div |------------
				   -----


     So, it calls factor immeadiately. Here, how it is actually 
     implemented.

          factor();
          sign = tok.value;
	  while(tok.value == TIMES || tok.value == DIV)
	  {
	    factor();
	    gen_code(sign);
	    sign = tok.value;
	  }

     For example:

	  x div y * z;

     expression is encountered. Since, it calls factor immeadiately
     the equation gets marked like this:

	  x div y * z;                x div y * z;
	     ^            ---->          ^ 
	     |                           |
	  tok.value                     sign = tok.value

     at this condition it enters the while loop. In the while loop term
     is called(which in return calls factor) and the equation gets 
     marked again like:

	  x div y * z;
	     ^    ^
	     |    |
	   sign  tok.value

     At this position, assembly codes are produced for the sign by:

	  gen_code(sign);

     In this way, 'div' is calculated before '*'. You can simply put it
     this way that: it performs calculation from the left to the right.


     factor is implemented like:

		                      _______
          ---------------------------| ident |--------------------> 
           |  ___   |    ^             -------             |
	    -| - |--   	|             ________            |
              ---       |<-----------| number |-----------
	                |             --------            |
		        |	                          | 
			|   ___      ____________    ___  |
			 --| ( |----| expression |--| ) |-
			    ---      ------------    ---

     Here, actually no code_generation is done. It simply puts the read
     token in top of the token:

		       STACK                          STACK
		       -----                          -----

          top -----> 4 |    |           top -----> 5 |     |
		        ----                          -----
		     3 | x  |                      4 | tok |
			----     --->                 -----
		     2 | 3  |                      3 |  x  |
			----                          -----
		     1 | z  |                      2 |  3  |


     However, when it encounters a LPAREN('(') it calls expression again.
     A question remains: how '-b' is performed? I used the tok.sline
     attribute, to store the fact that the token should be considered
     minus like:

          tok.sline = tok.sline * (-1); /* same, tok.sline = -tok.sline */

     code_generator looks at the stack and checks whether it has a minus
     tok.sline. If it has a minus tok.sline, it is multiplied with -1
     before using with any other variable.

FILES
     statement.c
     getsym.h

WARNING
     The following variables are declared as extern variables:

          extern  TOKEN   tok;
          extern  FILE    *infile;
          extern  FILE    *outfile;
          extern  int     label;
          extern  int     procedure_flag;
          extern  struct  list    *Table;
          extern  struct  list    *procedure_table;

          extern  TOKEN   stack[100];
          extern  int     top;

     These variables should be declared somewhere before explicitly used.
     Here, these variables were declared in main.c.

SEE ALSO
     code_generator(gen_code, gen_store_param), table. 

AUTHOR
     Chakma Junan <jchakma@yahoo.com> 
     Information and Computer Sciences
     Toyohashi University of Technology.
     (It was originially implemented by: ohno@edu.tut.ac.jp)

     Last Updated: Tue Feb 15 00:27:44 JST 2000
