Module Simula

Class ForStatement


public final class ForStatement extends Statement
For Statement.
 
 Simula Standard: 4.4 For-statement
 
  for-statement = FOR variable :- reference-list DO statement
                | FOR variable := value-list DO statement
               
      reference-list = reference-list-element { , reference-list-element }
 
          reference-list-element = reference-expression [ WHILE Boolean-expression ]
 
      value-list = value-list-element { , value-list-element }
 
          value-list-element = value-expression [ WHILE Boolean-expression ]
                             | arithmetic-expression STEP arithmetic-expression UNTIL arithmetic-expression

 
The Implementation of the for-statement is a bit tricky. The basic idea is to create a ForList iterator that iterates over a set of ForElt iterators. The following subclasses of ForElt are defined:
                - SingleElt<T>    for basic types T control variable
                - SingleTValElt   for Text type control variable
                - StepUntil       for numeric types
                - WhileElt<T>     for basic types T control variable
                - WhileTValElt    representing For t:= <TextExpr> while <Cond>
                                  With text value assignment
 
Each of which deliver a boolean value 'CB' used to indicate whether this for-element is exhausted. All parameters to these classes are transferred 'by name'. This is done to ensure that all expressions are evaluated in the right order. The assignment to the 'control variable' is done within the various for-elements when the 'next' method is invoked. To get a full overview of all the details you are encouraged to study the generated code together with the 'FRAMEWORK for for-list iteration' found in the runtime class RTS_RTObject.

Example, the following for-statement:

           for i:=1,6,13 step 6 until 66,i+1 while i < 80 do j:=j+i;
 
Is compiled to:
           for(boolean CB:new ForList(
               new SingleElt<Number>(...)
              ,new SingleElt<Number>(...)
              ,new StepUntil(...)
              ,new WhileElt<Number>(...)
           )) { if(!CB) continue;
                j=j+i;
              }
              
Another example with control variable of type Text:
           for t:="one",other while k < 7 do <statement>
 
Where 'other' is a text procedure, is compiled to:
           for(boolean CB:new ForList(
               new SingleTValElt(...)
              ,new WhileTValElt(...)
            )) { if(!CB) continue;
                 … // Statement
               }
 

Optimized For-Statement

However; most of the for-statements with only one for-list element are optimized.

Single for step-until statements are optimized when the step-expression is constant. I.e. the following for-statements:

           for i:=<expr-1> step 1  until <expr-2> do <statements>
           for i:=<expr-1> step -1 until <expr-2> do <statements>
           for i:=<expr-1> step 6  until <expr-2> do <statements>
           for i:=<expr-1> step -6 until <expr-2> do <statements>
 
are compiled to:
           for(i = <expr-1>; i <= <expr-2>; i++) { <statements> }
           for(i = <expr-1>; i >= <expr-2>; i--) { <statements> }
           for(i = <expr-1>; i <= <expr-2>; i=i+6) { <statements> }
           for(i = <expr-1>; i >= <expr-2>; i=i-6) { <statements> }
 
The other kinds of single elements are optimized in these ways:
           for i:=<expr> do <statements>
           for i:=<expr> while <cond> do <statements>
 
are compiled to:
           i = <expr>; { <statements> }
           
           i = <expr>;
           While( <cond> ) {
                  <statements>;
                  i = <expr>;
           }
 
Link to GitHub: Source File.
Author:
SIMULA Standards Group, Øystein Myhre Andersen
  • Field Details

    • controlVariable

      private final VariableExpression controlVariable
      The control variable
    • assignmentOperator

      private final Token assignmentOperator
      Assignment operator := or :-
    • forList

      private final Vector<ForStatement.ForListElement> forList
      The list of ForList elements.
    • doStatement

      private final Statement doStatement
      The statement after DO.
  • Constructor Details

    • ForStatement

      ForStatement(int line)
      Create a new ForStatement.
      Parameters:
      line - the source line number
  • Method Details

    • parseForListElement

      private ForStatement.ForListElement parseForListElement()
      Parse a for-list element.
      Returns:
      the resulting ForListElement
    • doChecking

      public void doChecking()
      Description copied from class: SyntaxClass
      Perform semantic checking.

      This must be redefined in every subclass.

      Overrides:
      doChecking in class SyntaxClass
    • doJavaCoding

      public void doJavaCoding()
      Description copied from class: SyntaxClass
      Output Java code.
      Overrides:
      doJavaCoding in class Statement
    • getSingleOptimizableElement

      private ForStatement.ForListElement getSingleOptimizableElement()
      Check if this ForListElement is a single optimizable element.
      Returns:
      a single optimizable element or null
    • edControlVariableByName

      private String edControlVariableByName(String classIdent, Type xType)
      Coding Utility: Edit control variable by name.
      Parameters:
      classIdent - Java class identifier
      xType - control variable's type
      Returns:
      the resulting Java source code for this ForListElement
    • print

      public void print(int indent)
      Description copied from class: SyntaxClass
      Utility print method.
      Overrides:
      print in class SyntaxClass
      Parameters:
      indent - number of spaces leading the line
    • printTree

      public void printTree(int indent)
      Description copied from class: SyntaxClass
      Utility print syntax tree method.
      Overrides:
      printTree in class SyntaxClass
      Parameters:
      indent - number of spaces leading the lines
    • toString

      public String toString()
      Overrides:
      toString in class Object