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 FOR_Element iterators. The following subclasses of FOR_Element are defined:

               - FOR_SingleELT    for basic types T control variable
               - FOR_SingleTValElt   for Text type control variable
               - FOR_StepUntil       for numeric types
               - FOR_WhileElt     for basic types T control variable
               - FOR_WhileTValElt    representing For t:=  while 
                                     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 FOR_SingleELT(...)
             ,new FOR_SingleELT(...)
             ,new FOR_StepUntil(...)
             ,new FOR_WhileElt(...)
          )) { if(!CB) continue;
               j=j+i;
             }
             

Another example with control variable of type Text:

          for t:="one",other while k < 7 do 

Where 'other' is a text procedure, is compiled to:

          for(boolean CB:new ForList(
              new FOR_SingleTValElt(...)
             ,new FOR_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:= step 1  until  do 
          for i:= step -1 until  do 
          for i:= step 6  until  do 
          for i:= step -6 until  do 

are compiled to:

          for(i = ; i <= ; i++) {  }
          for(i = ; i >= ; i--) {  }
          for(i = ; i <= ; i=i+6) {  }
          for(i = ; i >= ; i=i-6) {  }

The other kinds of single elements are optimized in these ways:

          for i:= do 
          for i:= while  do 

are compiled to:

          i = ; {  }
          
          i = ;
          While(  ) {
                 ;
                 i = ;
          }

Link to GitHub: Source File.

Author:
SIMULA Standards Group, Øystein Myhre Andersen
  • Field Details

    • controlVariable

      VariableExpression controlVariable
      The control variable
    • assignmentOperator

      int assignmentOperator
      Assignment operator := or :-
    • forList

      private ObjectList<ForListElement> forList
      The list of ForList elements.
    • doStatement

      Statement doStatement
      The statement after DO.
  • Constructor Details

    • ForStatement

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

      private ForStatement()
      Default constructor used by Attribute File I/O
  • Method Details

    • expectForListElement

      private ForListElement expectForListElement()
      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 ForListElement getSingleOptimizableElement()
      Check if this ForListElement is a single optimizable element.
      Returns:
      a single optimizable element or null
    • edControlVariableByName

      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, Object head)
      Description copied from class: SyntaxClass
      Utility print syntax tree method.
      Overrides:
      printTree in class SyntaxClass
      Parameters:
      indent - number of spaces leading the lines
      head - the head of the tree.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • buildByteCode

      public void buildByteCode(CodeBuilder codeBuilder)
      Description copied from class: Statement
      Build Java ByteCode.
      Overrides:
      buildByteCode in class Statement
      Parameters:
      codeBuilder - the codeBuilder to use.
    • writeObject

      public void writeObject(AttributeOutputStream oupt) throws IOException
      Description copied from class: SyntaxClass
      Write a SyntaxClass object to a AttributeOutputStream.
      Overrides:
      writeObject in class SyntaxClass
      Parameters:
      oupt - the AttributeOutputStream to write to.
      Throws:
      IOException - if something went wrong.
    • readObject

      public static ForStatement readObject(AttributeInputStream inpt) throws IOException
      Read and return an object.
      Parameters:
      inpt - the AttributeInputStream to read from
      Returns:
      the object read from the stream.
      Throws:
      IOException - if something went wrong.