Esempio n. 1
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack        = 0;
            var branchLabels = new List <LabelOperand>(Branches.Count);

            for (var i = 0; i < Branches.Count; i++)
            {
                branchLabels.Add(context.MakeLabel("ifBranch_" + i));
            }

            var branchElse = context.MakeLabel("ifElse");
            var branchEnd  = context.MakeLabel("ifEnd");

            context.PushScope();

            for (var i = 0; i < Branches.Count; i++)
            {
                var branch = Branches[i];

                context.Statement(branch.Condition);
                stack += branch.Condition.Compile(context);
                stack += context.JumpTrue(branchLabels[i]);
            }

            stack += context.Jump(branchElse);

            for (var i = 0; i < Branches.Count; i++)
            {
                var branch = Branches[i];

                stack += context.Bind(branchLabels[i]);
                stack += branch.Block.Compile(context);
                stack += context.Jump(branchEnd);
            }

            stack += context.Bind(branchElse);

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

            stack += context.Bind(branchEnd);

            context.PopScope();

            CheckStack(stack, 0);
            return(0);
        }
Esempio n. 2
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;
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var target = context.BreakLabel();
            if (target == null)
                throw new MondCompilerException(FileName, Line, "Unresolved jump");

            context.Jump(target);
            return 0;
        }
Esempio n. 7
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var branchLabels = new List<LabelOperand>(Branches.Count);

            for (var i = 0; i < Branches.Count; i++)
            {
                branchLabels.Add(context.MakeLabel("ifBranch_" + i));
            }

            var branchElse = context.MakeLabel("ifElse");
            var branchEnd = context.MakeLabel("ifEnd");

            for (var i = 0; i < Branches.Count; i++)
            {
                var branch = Branches[i];
                CompileCheck(context, branch.Condition, 1);
                context.JumpTrue(branchLabels[i]);
            }

            context.Jump(branchElse);

            for (var i = 0; i < Branches.Count; i++)
            {
                var branch = Branches[i];
                context.Bind(branchLabels[i]);
                CompileCheck(context, branch.Block, 0);
                context.Jump(branchEnd);
            }

            context.Bind(branchElse);

            if (Else != null)
                CompileCheck(context, Else.Block, 0);

            context.Bind(branchEnd);

            return 0;
        }
Esempio n. 8
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var target = context.BreakLabel();

            if (target == null)
            {
                throw new MondCompilerException(this, CompilerError.UnresolvedJump);
            }

            return(context.Jump(target));
        }
Esempio n. 9
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var target = context.ContinueLabel();

            if (target == null)
            {
                throw new MondCompilerException(FileName, Line, CompilerError.UnresolvedJump);
            }

            return(context.Jump(target));
        }
Esempio n. 10
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var sequenceContext = context as SequenceBodyContext;

            if (sequenceContext == null)
            {
                throw new MondCompilerException(FileName, Line, CompilerError.YieldInFun);
            }

            return(context.Jump(sequenceContext.SequenceBody.EndLabel));
        }
Esempio n. 11
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;
        }
Esempio n. 12
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;
        }
Esempio n. 13
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);
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        public override void CompileBody(FunctionContext context)
        {
            var stack = 0;

            EndLabel = context.MakeLabel("state_end");

            stack += context.Bind(context.Label);
            stack += context.Enter();

            // jump to state label
            stack += context.Load(State);
            stack += context.JumpTable(0, _stateLabels);
            stack += context.Jump(EndLabel);

            // first state
            stack += context.Bind(MakeStateLabel(context));

            // compile body
            stack += Block.Compile(context);

            // set enumerator.current to undefined
            // do this before EndLabel so we dont overwrite return values
            stack += context.LoadUndefined();
            stack += context.Load(Enumerable);
            stack += context.StoreField(context.String("current"));

            stack += context.Bind(EndLabel);

            // set state to end
            stack += context.Load(context.Number(-1));
            stack += context.Store(State);

            stack += context.LoadFalse();
            stack += context.Return();

            CheckStack(stack, 0);
        }
Esempio n. 16
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack  = Initializer?.Compile(context) ?? 1;
            var global = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal;

            var hasSlice = false;
            var headSize = 0;
            var tailSize = 0;

            foreach (var index in Indices)
            {
                if (index.IsSlice)
                {
                    if (hasSlice)
                    {
                        throw new InvalidOperationException($"Multiple slices in {nameof(DestructuredArrayExpression)}");
                    }

                    hasSlice = true;
                }
                else if (hasSlice)
                {
                    tailSize++;
                }
                else
                {
                    headSize++;
                }
            }

            var fixedSize = headSize + tailSize;

            stack += context.Dup();
            stack += context.LoadField(context.String("length"));
            stack += context.Call(0, new List <ImmediateOperand>());

            var inTail = false;
            var fixedI = 0;

            for (var i = 0; i < Indices.Count; i++)
            {
                var index    = Indices[i];
                var assign   = context.MakeLabel("arrayDestructureAssign");
                var destruct = context.MakeLabel("arrayDestructureIndex");

                if (index.IsSlice)
                {
                    inTail = true;
                }

                if (i < Indices.Count - 1)
                {
                    stack += context.Dup2(); // -> init.length(), init
                }
                stack += context.Load(context.Number(index.IsSlice ? fixedSize : fixedI));
                stack += context.BinaryOperation(TokenType.GreaterThan);
                stack += context.JumpTrue(destruct);
                stack += context.Drop(); // drops initializer
                stack += index.IsSlice ? context.NewArray(0) : context.LoadUndefined();
                stack += context.Jump(assign);

                stack += context.Bind(destruct);

                if (index.IsSlice)
                {
                    stack += context.Load(context.Number(fixedI));
                    stack += context.Load(context.Number(-tailSize - 1));
                    stack += context.LoadUndefined();
                    stack += context.Slice();
                }
                else
                {
                    stack += context.Load(context.Number(inTail ? fixedI - fixedSize : fixedI));
                    stack += context.LoadArray();
                }

                stack += context.Bind(assign);

                if (global)
                {
                    stack += context.LoadGlobal();
                    stack += context.StoreField(context.String(index.Name));
                }
                else
                {
                    if (!context.DefineIdentifier(index.Name, IsReadOnly))
                    {
                        throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, index.Name);
                    }

                    stack += context.Store(context.Identifier(index.Name));
                }

                if (!index.IsSlice)
                {
                    fixedI++;
                }
            }

            CheckStack(stack, 0);
            return(-1);
        }
Esempio n. 17
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack      = 0;
            var caseLabels = new List <LabelOperand>(Branches.Count);

            for (var i = 0; i < Branches.Count; i++)
            {
                caseLabels.Add(context.MakeLabel("caseBranch_" + i));
            }

            var caseDefault = context.MakeLabel("caseDefault");
            var caseEnd     = context.MakeLabel("caseEnd");

            stack += Expression.Compile(context);

            List <JumpTable> tables;
            List <JumpEntry> rest;
            var flattenedBranches = FlattenBranches(Branches, caseLabels);

            BuildTables(flattenedBranches, caseDefault, out tables, out rest);

            foreach (var table in tables)
            {
                var start  = table.Entries[0].Value;
                var labels = table.Entries.Select(e => e.Label).ToList();

                stack += context.Dup();
                stack += context.JumpTable(start, labels);
            }

            foreach (var entry in rest)
            {
                stack += context.Dup();
                stack += entry.Condition.Compile(context);
                stack += context.BinaryOperation(TokenType.EqualTo);
                stack += context.JumpTrue(entry.Label);
            }

            stack += context.Jump(caseDefault);

            context.PushLoop(null, caseEnd);

            for (var i = 0; i < Branches.Count; i++)
            {
                var branchStack = stack;
                var branch      = Branches[i];

                branchStack += context.Bind(caseLabels[i]);
                branchStack += context.Drop();
                branchStack += branch.Block.Compile(context);
                branchStack += context.Jump(caseEnd);

                CheckStack(branchStack, 0);
            }

            stack += context.Bind(caseDefault);
            stack += context.Drop();

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

            context.PopLoop();

            stack += context.Bind(caseEnd);

            CheckStack(stack, 0);
            return(stack);
        }
Esempio n. 18
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack      = 0;
            var caseLabels = new List <LabelOperand>(Branches.Count);

            var             caseEnd      = context.MakeLabel("caseEnd");
            LabelOperand    caseDefault  = null;
            BlockExpression defaultBlock = null;

            for (var i = 0; i < Branches.Count; i++)
            {
                var label = context.MakeLabel("caseBranch_" + i);
                caseLabels.Add(label);

                var conditions = Branches[i].Conditions;
                if (conditions.Any(c => c == null))
                {
                    caseDefault = label;

                    if (conditions.Count == 1)
                    {
                        defaultBlock = Branches[i].Block;
                    }
                }
            }

            var emptyDefault = caseDefault == null;

            if (emptyDefault)
            {
                caseDefault = context.MakeLabel("caseDefault");
            }

            context.PushScope();

            context.Statement(Expression);
            stack += Expression.Compile(context);

            var flattenedBranches = FlattenBranches(Branches, caseLabels, caseDefault);

            BuildTables(flattenedBranches, caseDefault, out var tables, out var rest);

            foreach (var table in tables)
            {
                var start  = table.Entries[0].Value;
                var labels = table.Entries.Select(e => e.Label).ToList();

                stack += context.Dup();
                stack += context.JumpTable(start, labels);
            }

            foreach (var entry in rest)
            {
                stack += context.Dup();
                stack += entry.Condition.Compile(context);
                stack += context.BinaryOperation(TokenType.EqualTo);
                stack += context.JumpTrue(entry.Label);
            }

            stack += context.Jump(caseDefault);

            context.PushLoop(null, caseEnd);

            for (var i = 0; i < Branches.Count; i++)
            {
                var branchStack = stack;
                var branch      = Branches[i];

                branchStack += context.Bind(caseLabels[i]);
                branchStack += context.Drop();
                branchStack += branch.Block.Compile(context);
                branchStack += context.Jump(caseEnd);

                CheckStack(branchStack, 0);
            }

            // only bind if we need a default block
            if (emptyDefault || defaultBlock != null)
            {
                stack += context.Bind(caseDefault);
            }

            // always drop the switch value
            stack += context.Drop();

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

            context.PopLoop();

            stack += context.Bind(caseEnd);

            context.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }
Esempio n. 19
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);
        }
Esempio n. 20
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);
        }