public void AddStatement(Statement s) { AddChild(s); }
private StatementSequence ToStatementSequence(Statement s) { if (s is StatementSequence) { return (StatementSequence)s; } else { StatementSequence seq = new StatementSequence(); seq.AddStatement(s); return seq; } }
public virtual void Visit(Statement node) { }
void Stmt(out Statement stmt) { Expression exp = null; stmt = null; int sl = la.line, sc = la.col; StatementSequence stmtSeq; Token bf; switch (la.kind) { case 1: { AssignStmt(out stmt); stmt.AddSequencePoint(sl,sc, t.line,t.col+t.val.Length); break; } case 13: { Get(); stmt = new Skip();stmt.AddSequencePoint(t); break; } case 3: { BlockStmt(out stmt); break; } case 18: { IfStmt(out stmt); break; } case 22: { WhileStmt(out stmt); break; } case 15: { ReadStmt(out stmt); stmt.AddSequencePoint(sl,sc, t.line,t.col+t.val.Length); break; } case 14: { Get(); Expr(out exp); stmt = new Write(exp); stmt.AddSequencePoint(sl,sc, t.line,t.col+t.val.Length); break; } case 25: { CallProc(out stmt); stmt.AddSequencePoint(sl,sc, t.line,t.col+t.val.Length); break; } case 6: { Get(); bf = t; StmtSeq(out stmtSeq); Expect(9); stmt = stmtSeq; stmt.AddSequencePoint(bf); stmt.AddSequencePoint(t); break; } default: SynErr(52); break; } }
void WhileStmt(out Statement whileStmt) { Expression exp = null; StatementSequence whileBranch = null; Statement branchStmt = null; whileStmt = null; Expect(22); int sl = t.line; int sc = t.col; Token tok = t; Expr(out exp); if (!ExpectBool(exp, tok, true)) { return; } Expect(23); int el = t.line; int ec = t.col+t.val.Length; if (Options.BookVersion) { Stmt(out branchStmt); whileBranch = ToStatementSequence(branchStmt); } else if (StartOf(1)) { StmtSeq(out whileBranch); Expect(24); whileBranch.AddSequencePoint(t); } else SynErr(54); whileStmt = new While.AST.Statements.While((TypedExpression<bool>)exp, whileBranch); whileStmt.AddSequencePoint(sl,sc,el,ec); }
void ReadStmt(out Statement stmt) { stmt = null; Expect(15); if (la.kind == 1) { Get(); stmt = new Read(new Variable(t.val)); } else if (la.kind == 6) { Get(); Expect(1); stmt = new Read(new Variable(t.val)); Expect(9); } else SynErr(55); }
void IfStmt(out Statement ifStmt) { StatementSequence ifBranch = null; StatementSequence elseBranch = null; Statement tmpStmt = null; Expression exp = null; ifStmt = null; Expect(18); int sl = t.line; int sc = t.col; Token tok = t; Expr(out exp); if (!ExpectBool(exp, tok, true)) { return; } Expect(19); int el = t.line; int ec = t.col+t.val.Length; if (Options.BookVersion) { Stmt(out tmpStmt); ifBranch = ToStatementSequence(tmpStmt); if (la.kind == 20) { Get(); Stmt(out tmpStmt); elseBranch = ToStatementSequence(tmpStmt); } } else if (StartOf(1)) { StmtSeq(out ifBranch); if (la.kind == 20) { Get(); StmtSeq(out elseBranch); } Expect(21); ifBranch.AddSequencePoint(t); if (elseBranch != null) { elseBranch.AddSequencePoint(t); } } else SynErr(53); ifStmt = new If((TypedExpression<bool>)exp, ifBranch, elseBranch); ifStmt.AddSequencePoint(sl,sc,el,ec); }
void CallProc(out Statement callStmt) { Expression exp; List<Expression> expressions = new List<Expression>(); Expect(25); Token callToken = t; Token exprToken = null; Expect(1); string proc = t.val; Expect(6); if (StartOf(2)) { exprToken = la; Expr(out exp); expressions.Add(exp); ExpectIntArg(exp, exprToken); while (la.kind == 12) { Get(); exprToken = la; Expr(out exp); expressions.Add(exp); ExpectIntArg(exp, exprToken); } } Expect(9); callStmt = new Call(proc, expressions, callToken, exprToken); }
void BlockStmt(out Statement block) { Expect(3); if (Options.BookVersion) { errors.SemErr(t.line, t.col, "Variable declarations are only allowed when using the /coursesyntax switch. Type 'wc.exe /help' for more information"); While.Environment.Exit(1); } VariableDeclarationSequence vars = new VariableDeclarationSequence(); SymbolTable.PushScope(); int sl = t.line; int sc = t.col; int el = t.line; int ec = t.col+t.val.Length; if (la.kind == 16) { VarDecStmt(out vars); } StatementSequence statements; StmtSeq(out statements); Expect(4); block = new Block(vars, statements); block.AddSequencePoint(sl,sc,el,ec); block.AddSequencePoint(t); SymbolTable.PopScope(); }
void AssignStmt(out Statement assign) { Expression exp; Variable var; assign = null; Expect(1); var = new Variable(t.val); if (!SymbolTable.IsInScope(t.val) && !Options.BookVersion) { errors.SemErr(t.line, t.col, string.Format("Assignment to undeclared variable '{0}'",t.val)); } Expect(17); Token tok = t; Expr(out exp); if (!ExpectInt(exp, tok, true)) { return; } assign = new Assign(var, (TypedExpression<int>)exp); }