public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; Expr expr; Int32 current = _expression.Parse(src, begin, out expr); if (current == -1) { expr = null; current = begin; } if (!Parser.IsSEMICOLON(src[current])) { return -1; } current++; stmt = new ExprStmt(expr); return current; }
public WhileStmt(Expr cond, Stmt body) { this.cond = cond; this.body = body; }
public SwitchStmt(Expr expr, Stmt stmt) { this.expr = expr; this.stmt = stmt; }
public LabeledStmt(String label, Stmt stmt) { this.label = label; this.stmt = stmt; }
public IfStmt(Expr cond, Stmt stmt) { this.cond = cond; this.stmt = stmt; }
public CaseStmt(Option<Expr> expr, Stmt stmt) { this.expr = expr; this.stmt = stmt; }
public IfElseStmt(Expr cond, Stmt true_stmt, Stmt false_stmt) { this.cond = cond; this.true_stmt = true_stmt; this.false_stmt = false_stmt; }
public ForStmt(Option<Expr> init, Option<Expr> cond, Option<Expr> loop, Stmt body) { this.init = init; this.cond = cond; this.loop = loop; this.body = body; }
public DoWhileStmt(Stmt body, Expr cond) { this.body = body; this.cond = cond; }
public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; Int32 current = _labeled_statement.Parse(src, begin, out stmt); if (current != -1) { return current; } CompoundStmt compound_stmt; current = _compound_statement.Parse(src, begin, out compound_stmt); if (current != -1) { stmt = compound_stmt; return current; } current = _expression_statement.Parse(src, begin, out stmt); if (current != -1) { return current; } current = _selection_statement.Parse(src, begin, out stmt); if (current != -1) { return current; } current = _iteration_statement.Parse(src, begin, out stmt); if (current != -1) { return current; } current = _jump_statement.Parse(src, begin, out stmt); if (current != -1) { return current; } return -1; }
public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; Int32 current; Expr expr; if (Parser.IsKeyword(src[begin], KeywordVal.SWITCH)) { // switch current = begin + 1; current = Parser.ParseParenExpr(src, current, out expr); if (current == -1) { return -1; } current = _statement.Parse(src, current, out stmt); if (current == -1) { return -1; } stmt = new SwitchStmt(expr, stmt); return current; } else if (Parser.IsKeyword(src[begin], KeywordVal.IF)) { // if current = begin + 1; current = Parser.ParseParenExpr(src, current, out expr); if (current == -1) { return -1; } Stmt true_stmt; current = _statement.Parse(src, current, out true_stmt); if (current == -1) { return -1; } if (!Parser.IsKeyword(src[current], KeywordVal.ELSE)) { stmt = new IfStmt(expr, true_stmt); return current; } current++; Stmt false_stmt; current = _statement.Parse(src, current, out false_stmt); if (current == -1) { return -1; } stmt = new IfElseStmt(expr, true_stmt, false_stmt); return current; } else { return -1; } }
public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; Int32 current; if (Parser.IsKeyword(src[begin], KeywordVal.DEFAULT)) { current = begin + 1; // match ':' if (!Parser.EatOperator(src, ref current, OperatorVal.COLON)) { return -1; } // match statement current = _statement.Parse(src, current, out stmt); if (current == -1) { return -1; } stmt = new CaseStmt(new None<Expr>(), stmt); return current; } else if (Parser.IsKeyword(src[begin], KeywordVal.CASE)) { current = begin + 1; // match expr Expr expr; current = _constant_expression.Parse(src, current, out expr); if (current == -1) { return -1; } // match ':' if (!Parser.EatOperator(src, ref current, OperatorVal.COLON)) { return -1; } // match statement current = _statement.Parse(src, current, out stmt); if (current == -1) { return -1; } stmt = new CaseStmt(new Some<Expr>(expr), stmt); return current; } else if (src[begin].type == TokenType.IDENTIFIER) { String label = ((TokenIdentifier)src[begin]).val; current = begin + 1; // match ':' if (!Parser.EatOperator(src, ref current, OperatorVal.COLON)) { return -1; } // match statement current = _statement.Parse(src, current, out stmt); if (current == -1) { return -1; } stmt = new LabeledStmt(label, stmt); return current; } else { return -1; } }
public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; if (src[begin].type != TokenType.KEYWORD) { return -1; } KeywordVal val = ((TokenKeyword)src[begin]).val; Int32 current = begin + 1; switch (val) { case KeywordVal.GOTO: if (src[current].type != TokenType.IDENTIFIER) { return -1; } stmt = new GotoStmt(((TokenIdentifier)src[current]).val); current++; break; case KeywordVal.CONTINUE: // current++; stmt = new ContStmt(); break; case KeywordVal.BREAK: // current++; stmt = new BreakStmt(); break; case KeywordVal.RETURN: Int32 saved = current; Expr expr; current = _expression.Parse(src, current, out expr); if (current == -1) { current = saved; stmt = new ReturnStmt(null); } else { stmt = new ReturnStmt(expr); } break; default: return -1; } if (!Parser.IsSEMICOLON(src[current])) { stmt = null; return -1; } current++; return current; }
public static Int32 Parse(List<Token> src, Int32 begin, out Stmt stmt) { stmt = null; Int32 current; if (Parser.IsKeyword(src[begin], KeywordVal.WHILE)) { // while current = begin + 1; Expr cond; current = Parser.ParseParenExpr(src, current, out cond); if (current == -1) { return -1; } Stmt body; current = _statement.Parse(src, current, out body); if (current == -1) { return -1; } stmt = new WhileStmt(cond, body); return current; } else if (Parser.IsKeyword(src[begin], KeywordVal.DO)) { // do current = begin + 1; Stmt body; current = _statement.Parse(src, current, out body); if (current == -1) { return -1; } Expr cond; current = Parser.ParseParenExpr(src, current, out cond); if (current == -1) { return -1; } stmt = new DoWhileStmt(body, cond); return current; } else if (Parser.IsKeyword(src[begin], KeywordVal.FOR)) { // for current = begin + 1; // match '(' if (!Parser.EatOperator(src, ref current, OperatorVal.LPAREN)) { return -1; } // match init Option<Expr> init_opt; // Expr init; Int32 saved = current; current = Parser.ParseOption(src, current, out init_opt, _expression.Parse); //current = _expression.Parse(src, current, out init); //if (current == -1) { // init = null; // init_opt = new None<Expr>(); // current = saved; //} else { // init_opt = new Some<Expr>(init); //} // match ';' if (!Parser.EatOperator(src, ref current, OperatorVal.SEMICOLON)) { return -1; } // match cond Option<Expr> cond_opt; current = Parser.ParseOption(src, current, out cond_opt, _expression.Parse); //Expr cond; //saved = current; //current = _expression.Parse(src, current, out cond); //if (current == -1) { // init = null; // current = saved; //} // match ';' if (!Parser.EatOperator(src, ref current, OperatorVal.SEMICOLON)) { return -1; } // match loop Option<Expr> loop_opt; current = Parser.ParseOption(src, current, out loop_opt, _expression.Parse); //Expr loop; //saved = current; //current = _expression.Parse(src, current, out loop); //if (current == -1) { // init = null; // current = saved; //} // match ')' if (!Parser.EatOperator(src, ref current, OperatorVal.RPAREN)) { return -1; } Stmt body; current = _statement.Parse(src, current, out body); if (current == -1) { return -1; } stmt = new ForStmt(init_opt, cond_opt, loop_opt, body); return current; } else { return -1; } }