public void visitForLoop(ForLoopNode node) { IdentifierNode identifier = (IdentifierNode)node.getChildren()[0]; string variableName = identifier.getVariableName(); if (!variableAlreadyDeclared(variableName)) { throw new SemanticException("The control variable '" + variableName + "' in for loop has not been declared."); } this.forLoopControlVariables.Push(variableName); node.getChildren()[2].accept(this); this.forLoopControlVariables.Pop(); }
public void visitForLoop(ForLoopNode forLoopNode) { IdentifierNode identifier = (IdentifierNode)forLoopNode.getChildren()[0]; string controlVariable = identifier.getVariableName(); RangeOperatorNode rangeNode = (RangeOperatorNode)forLoopNode.getChildren()[1]; StatementListNode forStatements = (StatementListNode)forLoopNode.getChildren()[2]; rangeNode.getChildren()[0].accept(this); int begin = popInt(); rangeNode.getChildren()[1].accept(this); int end = popInt(); for (int i = begin; i <= end; i++) { this.symbolTable.updateVariable(controlVariable, i); foreach (INode child in forStatements.getChildren()) { child.accept(this); } } }
public void visitForLoop(ForLoopNode node) { RangeOperatorNode rangeOperatorNode = (RangeOperatorNode)node.getChildren()[1]; this.typeStack = new Stack <MiniPLTokenType>(); INode leftExpression = rangeOperatorNode.getChildren()[0]; this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER); accessChildren(leftExpression); if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { throw new SemanticException("Expected an integer. Wrong type in the left hand side of range operator."); } INode rightExpression = rangeOperatorNode.getChildren()[1]; this.typeStack.Clear(); this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER); accessChildren(rightExpression); if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { throw new SemanticException("Expected an integer. Wrong type in the right hand side of range operator."); } }