public void visitVarDeclaration(VarDeclarationNode node) { IdentifierNode identifier = (IdentifierNode)node.getChildren()[0]; string variableName = identifier.getVariableName(); if (this.forLoopControlVariables.Count > 0) { throw new SemanticException("Declaring variables inside for loop is not allowed."); } if (variableAlreadyDeclared(variableName)) { throw new SemanticException("Variable '" + variableName + "' already declared."); } TypeNode typeNode = (TypeNode)node.getChildren()[1]; MiniPLTokenType type = (MiniPLTokenType)typeNode.getValue(); if (type == MiniPLTokenType.TYPE_IDENTIFIER_INTEGER) { this.symbolTable.addVariable(variableName, 0); } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_STRING) { this.symbolTable.addVariable(variableName, ""); } else if (type == MiniPLTokenType.TYPE_IDENTIFIER_BOOL) { this.symbolTable.addVariable(variableName, false); } else { throw new Exception("Unknown type usage in semantic analyzer."); } }
public void visitVarDeclaration(VarDeclarationNode node) { IdentifierNode identifier = (IdentifierNode)node.getChildren()[0]; string variableName = identifier.getVariableName(); TypeNode typeNode = (TypeNode)node.getChildren()[1]; MiniPLTokenType type = (MiniPLTokenType)typeNode.getValue(); typeCheck(typeNode, type); }