Пример #1
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var start = context.MakeLabel("forStart");
            var increment = context.MakeLabel("forContinue");
            var end = context.MakeLabel("forEnd");

            if (Initializer != null)
                CompileCheck(context, Initializer, 0);

            context.Bind(start);
            if (Condition != null)
                CompileCheck(context, Condition, 1);
            context.JumpFalse(end);

            context.PushLoop(increment, end);
            CompileCheck(context, Block, 0);
            context.PopLoop();

            context.Bind(increment);
            if (Increment != null)
                CompileCheck(context, Increment, 0);
            context.Jump(start);

            context.Bind(end);

            return 0;
        }
Пример #2
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack = 0;
            var start = context.MakeLabel("whileStart");
            var end   = context.MakeLabel("whileEnd");

            var boolExpression = Condition as BoolExpression;
            var isInfinite     = boolExpression != null && boolExpression.Value;

            stack += context.Bind(start);

            if (!isInfinite)
            {
                stack += Condition.Compile(context);
                stack += context.JumpFalse(end);
            }

            context.PushLoop(start, end);
            stack += Block.Compile(context);
            context.PopLoop();

            stack += context.Jump(start);
            stack += context.Bind(end);

            CheckStack(stack, 0);
            return(stack);
        }
Пример #3
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var enumerator = context.DefineInternal("enumerator", true);

            var stack = 0;
            var start = context.MakeLabel("foreachStart");
            var end   = context.MakeLabel("foreachEnd");

            // set enumerator
            stack += Expression.Compile(context);
            stack += context.LoadField(context.String("getEnumerator"));
            stack += context.Call(0);
            stack += context.Store(enumerator);

            // loop while moveNext returns true
            stack += context.Bind(start);
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("moveNext"));
            stack += context.Call(0);
            stack += context.JumpFalse(end);

            // loop body
            context.PushScope();
            context.PushLoop(start, end);

            if (!context.DefineIdentifier(Identifier))
            {
                throw new MondCompilerException(FileName, Line, CompilerError.IdentifierAlreadyDefined, Identifier);
            }

            var identifier = context.Identifier(Identifier);

            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("current"));
            stack += context.Store(identifier);

            stack += Block.Compile(context);
            stack += context.Jump(start);

            context.PopLoop();
            context.PopScope();

            // after loop
            stack += context.Bind(end);
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("dispose"));
            stack += context.Call(0);
            stack += context.Drop();

            CheckStack(stack, 0);
            return(stack);
        }
Пример #4
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var falseLabel = context.MakeLabel("ternaryFalse");
            var endLabel = context.MakeLabel("ternaryEnd");

            CompileCheck(context, Condition, 1);
            context.JumpFalse(falseLabel);
            CompileCheck(context, IfTrue, 1);
            context.Jump(endLabel);
            context.Bind(falseLabel);
            CompileCheck(context, IfFalse, 1);
            context.Bind(endLabel);

            return 1;
        }
Пример #5
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var start = context.MakeLabel("whileStart");
            var end = context.MakeLabel("whileEnd");

            context.Bind(start);
            CompileCheck(context, Condition, 1);
            context.JumpFalse(end);
            context.PushLoop(start, end);
            CompileCheck(context, Block, 0);
            context.PopLoop();
            context.Jump(start);
            context.Bind(end);

            return 0;
        }
Пример #6
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack     = 0;
            var start     = context.MakeLabel("forStart");
            var increment = context.MakeLabel("forContinue");
            var end       = context.MakeLabel("forEnd");

            context.PushScope();

            if (Initializer != null)
            {
                stack += Initializer.Compile(context);
            }

            context.Bind(start);
            if (Condition != null)
            {
                stack += Condition.Compile(context);
                stack += context.JumpFalse(end);
            }

            context.PushLoop(increment, end);
            stack += Block.Compile(context);
            context.PopLoop();

            stack += context.Bind(increment);

            if (Increment != null)
            {
                stack += Increment.Compile(context);
            }

            stack += context.Jump(start);

            stack += context.Bind(end);

            context.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }
Пример #7
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack      = 0;
            var falseLabel = context.MakeLabel("ternaryFalse");
            var endLabel   = context.MakeLabel("ternaryEnd");

            stack += Condition.Compile(context);
            stack += context.JumpFalse(falseLabel);
            CheckStack(IfTrue.Compile(context), 1);
            stack += context.Jump(endLabel);
            stack += context.Bind(falseLabel);
            CheckStack(IfFalse.Compile(context), 1);
            stack += context.Bind(endLabel);

            CheckStack(stack, 0);
            return(1);
        }
Пример #8
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack = 0;
            var start = context.MakeLabel("whileStart");
            var cont  = context.MakeLabel("whileContinue");
            var brk   = context.MakeLabel("whileBreak");
            var end   = context.MakeLabel("whileEnd");

            var boolExpression = Condition as BoolExpression;
            var isInfinite     = boolExpression != null && boolExpression.Value;

            var containsFunction = new LoopContainsFunctionVisitor();

            Block.Accept(containsFunction);

            var loopContext = containsFunction.Value ? new LoopContext(context) : context;

            context.PushScope();

            stack += context.Bind(start); // continue (without function)

            if (!isInfinite)
            {
                context.Statement(Condition);
                stack += Condition.Compile(context);
                stack += context.JumpFalse(end);
            }

            loopContext.PushLoop(containsFunction.Value ? cont : start, containsFunction.Value ? brk : end);

            if (containsFunction.Value)
            {
                stack += loopContext.Enter();
            }

            stack += Block.Compile(loopContext);

            if (containsFunction.Value)
            {
                stack += loopContext.Bind(cont); // continue (with function)
                stack += loopContext.Leave();
            }

            loopContext.PopLoop();

            stack += context.Jump(start);

            if (containsFunction.Value)
            {
                stack += context.Bind(brk); // break (with function)
                stack += context.Leave();
            }

            stack += context.Bind(end); // break (without function)

            context.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }
Пример #9
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack     = 0;
            var start     = context.MakeLabel("forStart");
            var increment = context.MakeLabel("forContinue");
            var brk       = context.MakeLabel("forBreak");
            var end       = context.MakeLabel("forEnd");

            var containsFunction = new LoopContainsFunctionVisitor();

            Block.Accept(containsFunction);

            context.PushScope();

            if (Initializer != null)
            {
                var hasCode = true;

                if (Initializer.Statements.Count > 0)
                {
                    if (Initializer.Statements[0] is VarExpression initializerVar &&
                        initializerVar.Declarations.All(d => d.Initializer == null))
                    {
                        hasCode = false;
                    }
                }

                if (hasCode)
                {
                    context.Statement(Initializer);
                }

                stack += Initializer.Compile(context);
            }

            // loop body
            context.Bind(start);

            if (Condition != null)
            {
                context.Statement(Condition);
                stack += Condition.Compile(context);
                stack += context.JumpFalse(end);
            }

            var loopContext = containsFunction.Value ? new LoopContext(context) : context;

            loopContext.PushLoop(increment, containsFunction.Value ? brk : end);

            if (containsFunction.Value)
            {
                stack += loopContext.Enter();
            }

            stack += Block.Compile(loopContext);

            stack += context.Bind(increment); // continue

            if (containsFunction.Value)
            {
                stack += loopContext.Leave();
            }

            loopContext.PopLoop();

            if (Increment != null)
            {
                context.Statement(Increment);
                stack += Increment.Compile(context);
            }

            stack += context.Jump(start);

            if (containsFunction.Value)
            {
                stack += context.Bind(brk); // break (with function)
                stack += context.Leave();
            }

            stack += context.Bind(end); // break (without function)

            context.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }