void WhileStatement(out Statement stmt) { stmt = new WhileStatement(); Expect(13); stmt.t = t; Expect(38); Expression(out (stmt as WhileStatement).condition); Expect(45); Statement(out (stmt as WhileStatement).body); }
void ReturnStatement(out Statement stmt) { Expect(28); stmt = new ReturnStatement(); stmt.t = t; if (StartOf(3)) { Expression(out (stmt as ReturnStatement).expr); } Expect(46); }
void Statement(out Statement stmt) { stmt = null; switch (la.kind) { case 7: { BlockStatement(out stmt); break; } case 1: case 2: case 3: case 4: case 26: case 27: case 36: case 38: case 40: case 41: { CallOrAssgStatement(out stmt); break; } case 28: { ReturnStatement(out stmt); break; } case 15: { IfStatement(out stmt); break; } case 13: { WhileStatement(out stmt); break; } case 12: { DoWhileStatement(out stmt); break; } case 9: { ForStatement(out stmt); break; } default: SynErr(54); break; } }
void ForStatement(out Statement stmt) { stmt = new ForStatement(); Expect(9); stmt.t = t; Expression(out (stmt as ForStatement).variable); Expect(29); Expression(out (stmt as ForStatement).initial); if (la.kind == 10) { Get(); (stmt as ForStatement).direction = ForDirection.Up; } else if (la.kind == 11) { Get(); (stmt as ForStatement).direction = ForDirection.Down; } else SynErr(55); Expression(out (stmt as ForStatement).final); Expect(12); Statement(out (stmt as ForStatement).body); }
void IfStatement(out Statement stmt) { stmt = new IfStatement(); Expect(15); stmt.t = t; Expect(38); Expression(out (stmt as IfStatement).condition); Expect(45); Statement(out (stmt as IfStatement).ifBranch); if (la.kind == 16) { Get(); Statement(out (stmt as IfStatement).elseBranch); } }
void BlockStatement(out Statement stmt) { BlockStatement blockStmt = new BlockStatement(); Statement containedStmt; Expect(7); blockStmt.t = t; while (StartOf(2)) { Statement(out containedStmt); blockStmt.AddStatement(containedStmt); } Expect(8); stmt = blockStmt; }
void CallOrAssgStatement(out Statement stmt) { Expression expr; stmt = null; Expression(out expr); if (la.kind == 29) { Get(); Expression rightSide; Expression(out rightSide); stmt = new AssignementStatement(expr, rightSide); } Expect(46); if (stmt == null) stmt = new CallStatement(expr); stmt.t = expr.t; }
/// <summary> /// Evaluates this node and all of its children /// </summary> /// <param name="scope">The scope of this statement</param> /// <returns></returns> public override Statement Evaluate(Scope scope) { // Evaluate condition condition = condition.Evaluate(scope); // Evaluate repetitive statement body = body.Evaluate(scope); // Perform dead code elimination if the value of the condition can be evaluated at compile time if (condition is ConstantExpression) { // If condition is always false, drop statement if (!(condition as ConstantExpression).IsTrue()) return null; } return this; }
/// <summary> /// Evaluates this node and all of its children /// </summary> /// <param name="scope">The scope of this statement</param> /// <returns></returns> public override Statement Evaluate(Scope scope) { // Evaluate the condition condition = condition.Evaluate(scope); // Evaluate the if branch ifBranch = ifBranch.Evaluate(scope); // Evaluate the else branch if it exists if (elseBranch != null) { elseBranch = elseBranch.Evaluate(scope); // Propagate return state if both branches return // This is the only case in which we can make sure code following the statement won't be reached returns = ifBranch.returns && elseBranch.returns; } // Perform dead code elimination if the value of the condition can be evaluated at compile time if (condition is ConstantExpression) { // If condition always holds, replace statement with the if branch if ((condition as ConstantExpression).IsTrue()) return ifBranch; else // If an else branch exists, replace statemnt with it, if not, drop statement if (elseBranch != null) return elseBranch; else return null; } return this; }
/// <summary> /// Evaluates this node and all of its children /// </summary> /// <param name="scope">The scope of this statement</param> /// <returns></returns> public override Statement Evaluate(Scope scope) { if (!(variable is AssignableExpression)) { // If variable is not assignable expression, issue error Compiler.Compiler.errors.SemErr(t.line, t.col, "Invalid variable for FOR statement"); return this; } // Evaluate all child nodes variable = variable.Evaluate(scope); initial = initial.Evaluate(scope); final = final.Evaluate(scope); body = body.Evaluate(scope); return this; }
/// <summary> /// Evaluates this node and all of its children /// </summary> /// <param name="scope">The scope of this statement</param> /// <returns></returns> public override Statement Evaluate(Scope scope) { // Evaluate condition expression condition = condition.Evaluate(scope); // Evaluate repetitive statement body = body.Evaluate(scope); // Perform dead code elimination if the value of the condition can be evaluated at compile time if (condition is ConstantExpression) { // If it is always false, replace the statement with the inner repetitive statement if (!(condition as ConstantExpression).IsTrue()) return body; } // Propagate return state - if inner statement returns, no code following this statement // will be reached returns = body.returns; return this; }
/// <summary> /// Adds a statement to the list of inner statements /// </summary> /// <param name="stmt">Statement to add</param> public void AddStatement(Statement stmt) { statements.Add(stmt); }