예제 #1
0
        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));
        }
예제 #2
0
        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));
        }
예제 #3
0
        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));
        }
예제 #4
0
        public override AST.Node ToAST(Env env)
        {
            /// The controlling expression shall have integer type.
            e = expr.ToASTExpr(env);
            if (!e.Type.IsInteger && e.Type.Kind != TKind.ENUM)
            {
                throw new ETypeError(Pos, "the controlling expression of switch statement shall have integer type");
            }

            /// Integer promotions are performed on the controlling expression.
            e = e.IntPromote();

            env.PushSwitch(this);

            /// Semantic check the statment.
            AST.Node s = stmt.ToAST(env);

            env.PopBreakable();

            return(new AST.Switch(breakLabel, cases, defaultLabel, e, s));
        }