java.lang.Object
simula.compiler.syntaxClass.SyntaxClass
simula.compiler.syntaxClass.statement.Statement
simula.compiler.syntaxClass.statement.ForStatement
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-expressionThe 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 assignmentEach 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
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate class
Utility class ForListElement implementing a single value element.private class
Utility class: For-list Step until element.private class
Utility class: For-list While element. -
Field Summary
Modifier and TypeFieldDescriptionprivate final Token
Assignment operator := or :-private final VariableExpression
The control variableprivate final Statement
The statement after DO.private final Vector
<ForStatement.ForListElement> The list of ForList elements.Fields inherited from class simula.compiler.syntaxClass.SyntaxClass
lineNumber
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Perform semantic checking.void
Output Java code.private String
edControlVariableByName
(String classIdent, Type xType) Coding Utility: Edit control variable by name.private ForStatement.ForListElement
Check if this ForListElement is a single optimizable element.private ForStatement.ForListElement
Parse a for-list element.void
print
(int indent) Utility print method.void
printTree
(int indent) Utility print syntax tree method.toString()
Methods inherited from class simula.compiler.syntaxClass.statement.Statement
expectStatement
Methods inherited from class simula.compiler.syntaxClass.SyntaxClass
ASSERT_SEMANTICS_CHECKED, doDeclarationCoding, edIndent, edTreeIndent, IS_SEMANTICS_CHECKED, SET_SEMANTICS_CHECKED, toJavaCode
-
Field Details
-
controlVariable
The control variable -
assignmentOperator
Assignment operator := or :- -
forList
The list of ForList elements. -
doStatement
The statement after DO.
-
-
Constructor Details
-
ForStatement
ForStatement(int line) Create a new ForStatement.- Parameters:
line
- the source line number
-
-
Method Details
-
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 classSyntaxClass
-
doJavaCoding
public void doJavaCoding()Description copied from class:SyntaxClass
Output Java code.- Overrides:
doJavaCoding
in classStatement
-
getSingleOptimizableElement
Check if this ForListElement is a single optimizable element.- Returns:
- a single optimizable element or null
-
edControlVariableByName
Coding Utility: Edit control variable by name.- Parameters:
classIdent
- Java class identifierxType
- 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 classSyntaxClass
- 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 classSyntaxClass
- Parameters:
indent
- number of spaces leading the lines
-
toString
-