public override AST.Node ToAST(Env env) { /// An iteration statement is a block. env.PushBlockScope(); /// The controlling expression should have scalar type. AST.Expr e = expr.ToASTExpr(env); if (!e.Type.IsScalar) { throw new ETypeError(Pos, "the controlling expression of iteration statement should have scalar type"); } /// The loop body is also a block. env.PushBlockScope(); /// Add this loop to the environemnt. env.PushLoop(this); var b = body.ToASTCompoundStmt(env); env.PopBreakable(); env.PopScope(); env.PopScope(); return(new AST.While(breakLabel, continueLabel, secondPlusLabel, firstLabel, e, b)); }
public override AST.Node ToAST(Env env) { /// An iteration statement is a block. env.PushBlockScope(); /// Check the expressions. /// An omitted expression-2 is replaced by a nonzero constant. var i = initExpr != null?initExpr.ToAST(env) : (initDecl != null ? initDecl.ToAST(env) : null); var p = pred != null?pred.ToASTExpr(env) : new AST.ConstIntExpr(TInt.Instance, 1, env.ASTEnv); var q = iter != null?iter.ToAST(env) : null; /// The controlling expression of a iteration statement shall have scalar type. if (!p.Type.IsScalar) { throw new ETypeError(Pos, "the controlling expression of iteration statement should have scalar type"); } /// Add this loop to the environment. env.PushLoop(this); var b = body.ToASTCompoundStmt(env); env.PopBreakable(); env.PopScope(); return(new AST.For(breakLabel, continueLabel, secondPlusLabel, firstLabel, i, p, q, b)); }
public override AST.Node ToAST(Env env) { env.PushBlockScope(); env.PushBlockScope(); env.PushLoop(this); var b = body.ToASTCompoundStmt(env); env.PopBreakable(); env.PopScope(); AST.Expr e = expr.ToASTExpr(env); if (!e.Type.IsScalar) { throw new ETypeError(Pos, "the controlling expression of iteration statement should be have scalar type"); } env.PopScope(); return(new AST.Do(breakLabel, continueLabel, secondPlusLabel, firstLabel, e, b)); }
public AST.CompoundStmt ToASTCompoundStmt(Env env) { env.PushBlockScope(); LinkedList <AST.Node> results = new LinkedList <AST.Node>(); foreach (var stmt in stmts) { results.AddLast(stmt.ToAST(env)); } env.PopScope(); return(new AST.CompoundStmt(results)); }
/// <summary> /// A selection statement is a block whose scope is a strict subset /// of the scope of its enclosing block. /// /// The controlling expression of an if statement shall have scalar type. /// </summary> /// <param name="env"></param> /// <returns></returns> public override AST.Node ToAST(Env env) { env.PushBlockScope(); AST.Expr e = expr.ToASTExpr(env); if (!e.Type.IsScalar) { throw new ETypeError(Pos, string.Format("expecting scalar type, given {0}", e.Type)); } AST.Node t = then.ToAST(env); AST.Node o = other != null?other.ToAST(env) : null; env.PopScope(); var labels = env.AllocIfLabel(); return(new AST.If(e, t, o, labels.Item1, labels.Item2)); }