public override void Visit(ReadStmt readStmt) { string userInput = Console.ReadLine(); Symbol inputSymbol = SymbolTable.GetSymbol(readStmt.Variable.IdentifierName); switch (readStmt.Variable.Type) { case ExprType.IntType: inputSymbol.Value = int.Parse(userInput); break; case ExprType.StringType: inputSymbol.Value = userInput; break; case ExprType.BoolType: userInput = userInput.ToLower(); if (userInput == "false" || userInput == "0") inputSymbol.Value = false; else inputSymbol.Value = true; break; } }
public void Visit(ReadStmt node) { Symbol variable = SymbolTable [node.Children [0].Name]; string userInput = Console.ReadLine ().Trim (); if (variable.Type == "Int") { try { Int32.Parse (userInput); } catch (Exception e) { throw new RuntimeError ("Expected input value " + userInput + " to be integer", 0, 0); } } SymbolTable [variable.Name].Value = userInput; }
public void Visit(ReadStmt node) { try { if (!SymbolTable.ContainsKey (node.Children [0].Name)) { throw new SemanticError ("Variable " + node.Children [0].Name + " must be declared" + " before it can be assigned a value", node.Children [0].Row, node.Children [0].Column); } } catch (SemanticError error) { Errors.Add (error); TypeStack.Clear (); } }
public override void Visit(ReadStmt readStmt) { readStmt.Variable.Accept(this); }
private Statement Stmt() { switch ((Token.Types)currentToken.Type) { case Token.Types.Var: VarDeclStmt varDeclStmt = new VarDeclStmt ("VarDecl", currentToken.Row, currentToken.Column); Match (Token.Types.Var); varDeclStmt.AddChild (IdentifierNameStmt ()); Match (Token.Types.Colon); varDeclStmt.AddChild (Type ()); if ((Token.Types)currentToken.Type == Token.Types.Assign) { Match (Token.Types.Assign); varDeclStmt.AddChild (Expr ()); return varDeclStmt; } else if ((Token.Types)currentToken.Type == Token.Types.Semicolon) { return varDeclStmt; } throw new SyntaxError ("Expected Assign, got: " + currentToken.Type, currentToken.Row, currentToken.Column); case Token.Types.Identifier: AssignmentStmt assignmentStmt = new AssignmentStmt ("AssignmentStmt", currentToken.Row, currentToken.Column); assignmentStmt.AddChild (IdentifierNameStmt ()); Match (Token.Types.Assign); assignmentStmt.AddChild (Expr ()); return assignmentStmt; case Token.Types.For: ForStmt forStmt = new ForStmt ("ForStmt", currentToken.Row, currentToken.Column); Match (Token.Types.For); forStmt.AddChild (IdentifierNameStmt ()); Match (Token.Types.In); forStmt.AddChild (Expr ()); Match (Token.Types.Range); forStmt.AddChild (Expr ()); Match (Token.Types.Do); forStmt.AddChild (Stmts ()); Match (Token.Types.End); Match (Token.Types.For); return forStmt; case Token.Types.Read: ReadStmt readStmt = new ReadStmt ("ReadStmt", currentToken.Row, currentToken.Column); Match (Token.Types.Read); readStmt.AddChild (IdentifierNameStmt ()); return readStmt; case Token.Types.Print: PrintStmt printStmt = new PrintStmt ("PrintStmt", currentToken.Row, currentToken.Column); Match (Token.Types.Print); printStmt.AddChild (Expr ()); return printStmt; case Token.Types.Assert: AssertStmt assertStmt = new AssertStmt ("AssertStmt", currentToken.Row, currentToken.Column); Match (Token.Types.Assert); Match (Token.Types.LeftParenthesis); assertStmt.AddChild (Expr ()); Match (Token.Types.RightParenthesis); return assertStmt; default: throw new SyntaxError ("invalid start symbol for statement " + currentToken.Lexeme, currentToken.Row, currentToken.Column); } }
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)); }