Example #1
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack = 0;
            var start = context.MakeLabel("doWhileStart");
            var cont  = context.MakeLabel("doWhileContinue");
            var brk   = context.MakeLabel("doWhileBreak");
            var end   = context.MakeLabel("doWhileEnd");

            var containsFunction = new LoopContainsFunctionVisitor();

            Block.Accept(containsFunction);

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

            // body
            loopContext.PushScope();
            loopContext.PushLoop(cont, containsFunction.Value ? brk : end);

            stack += loopContext.Bind(start);

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

            stack += Block.Compile(loopContext);
            loopContext.PopLoop();

            // condition check
            stack += context.Bind(cont); // continue

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

            context.Statement(Condition);
            stack += Condition.Compile(context);
            stack += context.JumpTrue(start);

            if (containsFunction.Value)
            {
                stack += context.Jump(end);

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

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

            loopContext.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }
Example #2
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);
        }
Example #3
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack = 0;
            var start = context.MakeLabel("foreachStart");
            var cont  = context.MakeLabel("foreachContinue");
            var brk   = context.MakeLabel("foreachBreak");
            var end   = context.MakeLabel("foreachEnd");

            var containsFunction = new LoopContainsFunctionVisitor();

            Block.Accept(containsFunction);

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

            // set enumerator
            context.Statement(Expression);
            stack += Expression.Compile(context);
            stack += context.LoadField(context.String("getEnumerator"));
            stack += context.Call(0, new List <ImmediateOperand>());
            stack += context.Store(enumerator);

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

            // loop body
            loopContext.PushScope();
            loopContext.PushLoop(containsFunction.Value ? cont : start, containsFunction.Value ? brk : end);

            IdentifierOperand identifier;

            if (DestructureExpression != null)
            {
                identifier = context.DefineInternal(Identifier, true);
            }
            else
            {
                // create the loop variable outside of the loop context (but inside of its scope!)
                if (!context.DefineIdentifier(Identifier))
                {
                    throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, Identifier);
                }

                identifier = context.Identifier(Identifier);
            }
            stack += loopContext.Bind(start); // continue (without function)

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

            // loop while moveNext returns true
            context.Statement(InToken, InToken);
            stack += loopContext.Load(enumerator);
            stack += loopContext.LoadField(context.String("moveNext"));
            stack += loopContext.Call(0, new List <ImmediateOperand>());
            stack += loopContext.JumpFalse(containsFunction.Value ? brk : end);

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

            if (DestructureExpression != null)
            {
                stack += loopContext.Load(identifier);
                stack += DestructureExpression.Compile(loopContext);
            }

            stack += Block.Compile(loopContext);

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

            stack += loopContext.Jump(start);

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

            loopContext.PopLoop();
            loopContext.PopScope();

            // after loop
            stack += context.Bind(end); // break (without function)
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("dispose"));
            stack += context.Call(0, new List <ImmediateOperand>());
            stack += context.Drop();

            CheckStack(stack, 0);
            return(stack);
        }
Example #4
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);
        }