public override void Visit(DeclarationStmt declarationStmt) { IdentifierExpr id = declarationStmt.Identifier; if (declarationStmt.AssignmentExpr != null) { declarationStmt.AssignmentExpr.Accept(this); SymbolTable.GetSymbol(id.IdentifierName).Value = declarationStmt.AssignmentExpr.ExprValue; } }
public override void Visit(DeclarationStmt declarationStmt) { IdentifierExpr id = declarationStmt.Identifier; if (!SymbolTable.AddSymbol(id.IdentifierName, declarationStmt.Type.Type)) { Errors.AddError(String.Format("Already declared variable {0} at line {1} column {2}.", id.IdentifierName, id.Line, id.Column), ErrorTypes.SemanticError); } if (declarationStmt.AssignmentExpr != null) { Expression assignment = declarationStmt.AssignmentExpr; assignment.Accept(this); if (assignment.Type == ExprType.VoidType) return; if (assignment.Type != declarationStmt.Type.Type) { Errors.AddError(String.Format("Can't assign expression of type {0} in variable {1} of type {2} at line {3} column {4}.", assignment.Type, id.IdentifierName, declarationStmt.Type, declarationStmt.Line, declarationStmt.Column), ErrorTypes.SemanticError); } } declarationStmt.Identifier.Type = declarationStmt.Type.Type; }
private Statement ParseStatement() { if (Accept(Token.Types.KwVar)) { DeclarationStmt declaration = new DeclarationStmt(AcceptedToken.Line, AcceptedToken.Column); Token id = Match(Token.Types.Identifier); declaration.Identifier = new IdentifierExpr(id.Line, id.Column, id.Content); Match(Token.Types.Colon); TypeNode typeNode = ParseType(); declaration.Type = typeNode; if (Accept(Token.Types.OpAssignment)) { declaration.AssignmentExpr = ParseExpression(); } return declaration; } else if (Accept(Token.Types.Identifier)) { AssignmentStmt statement = new AssignmentStmt(AcceptedToken.Line, AcceptedToken.Column); statement.Identifier = new IdentifierExpr(AcceptedToken.Line, AcceptedToken.Column, AcceptedToken.Content); Match(Token.Types.OpAssignment); statement.AssignmentExpr = ParseExpression(); return statement; } else if (Accept(Token.Types.KwFor)) { ForStmt statement = new ForStmt(AcceptedToken.Line, AcceptedToken.Column); Token idToken = Match(Token.Types.Identifier); statement.LoopVar = new IdentifierExpr(idToken.Line, idToken.Column, idToken.Content); Match(Token.Types.KwIn); statement.StartExpr = ParseExpression(); Match(Token.Types.OpRange); statement.EndExpr = ParseExpression(); Match(Token.Types.KwDo); statement.Body = ParseStatements(new StmtList(CurrentToken.Line, CurrentToken.Column)); Match(Token.Types.KwEnd); Match(Token.Types.KwFor); return statement; } else if (Accept(Token.Types.KwRead)) { ReadStmt statement = new ReadStmt(AcceptedToken.Line, AcceptedToken.Column); Token idToken = Match(Token.Types.Identifier); statement.Variable = new IdentifierExpr(idToken.Line, idToken.Column, idToken.Content); return statement; } else if (Accept(Token.Types.KwPrint)) { PrintStmt statement = new PrintStmt(AcceptedToken.Line, AcceptedToken.Column); statement.PrintExpr = ParseExpression(); return statement; } else if (Accept(Token.Types.KwAssert)) { AssertStmt statement = new AssertStmt(AcceptedToken.Line, AcceptedToken.Column); Match(Token.Types.LParen); statement.AssertExpr = ParseExpression(); Match(Token.Types.RParen); return statement; } throw new ParserException(String.Format("Expected statement, got {0} instead at line {1} column {2}.", CurrentToken.Type, CurrentToken.Line, CurrentToken.Column)); }
public void Visit(DeclarationStmt declarationStmt) { TreeLevel++; PrintNode("Declaration"); PrintNode("Name " + declarationStmt.Identifier.IdentifierName, true); declarationStmt.Type.Accept(this); if (declarationStmt.AssignmentExpr != null) declarationStmt.AssignmentExpr.Accept(this); TreeLevel--; }