Class 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-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_SingleELTfor 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 Summary
FieldsModifier and TypeFieldDescription(package private) int
Assignment operator := or :-(package private) VariableExpression
The control variable(package private) Statement
The statement after DO.private ObjectList
<ForListElement> The list of ForList elements.Fields inherited from class simula.compiler.syntaxClass.SyntaxClass
CHECKED, lineNumber, OBJECT_SEQU
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Default constructor used by Attribute File I/O(package private)
ForStatement
(int line) Create a new ForStatement. -
Method Summary
Modifier and TypeMethodDescriptionvoid
buildByteCode
(CodeBuilder codeBuilder) Build Java ByteCode.void
Perform semantic checking.void
Output Java code.(package private) String
edControlVariableByName
(String classIdent, Type xType) Coding Utility: Edit control variable by name.private ForListElement
Parse a for-list element.private ForListElement
Check if this ForListElement is a single optimizable element.void
print
(int indent) Utility print method.void
Utility print syntax tree method.static ForStatement
Read and return an object.toString()
void
Write a SyntaxClass object to a AttributeOutputStream.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, setLineNumber, toJavaCode
-
Field Details
-
controlVariable
VariableExpression controlVariableThe control variable -
assignmentOperator
int assignmentOperatorAssignment operator := or :- -
forList
The list of ForList elements. -
doStatement
Statement doStatementThe 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
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
-
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
Description copied from class:SyntaxClass
Utility print syntax tree method.- Overrides:
printTree
in classSyntaxClass
- Parameters:
indent
- number of spaces leading the lineshead
- the head of the tree.
-
toString
-
buildByteCode
Description copied from class:Statement
Build Java ByteCode.- Overrides:
buildByteCode
in classStatement
- Parameters:
codeBuilder
- the codeBuilder to use.
-
writeObject
Description copied from class:SyntaxClass
Write a SyntaxClass object to a AttributeOutputStream.- Overrides:
writeObject
in classSyntaxClass
- Parameters:
oupt
- the AttributeOutputStream to write to.- Throws:
IOException
- if something went wrong.
-
readObject
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.
-