Beispiel #1
0
        public override void Compile(ByteCode bc)
        {
            m_Exp1.Compile(bc);

            if (m_Operator == Operator.Or)
            {
                var i = bc.Emit_Jump(OpCode.JtOrPop, -1);
                m_Exp2.Compile(bc);
                i.NumVal = bc.GetJumpPointForNextInstruction();
                return;
            }

            if (m_Operator == Operator.And)
            {
                var i = bc.Emit_Jump(OpCode.JfOrPop, -1);
                m_Exp2.Compile(bc);
                i.NumVal = bc.GetJumpPointForNextInstruction();
                return;
            }


            if (m_Exp2 != null)
            {
                m_Exp2.Compile(bc);
            }

            bc.Emit_Operator(OperatorToOpCode(m_Operator));

            if (ShouldInvertBoolean(m_Operator))
            {
                bc.Emit_Operator(OpCode.Not);
            }
        }
        public override void Compile(ByteCode bc)
        {
            bc.PushSourceRef(m_RefFor);

            Loop L = new Loop()
            {
                Scope = m_StackFrame
            };

            bc.LoopTracker.Loops.Push(L);

            m_End.Compile(bc);
            bc.Emit_ToNum(3);
            m_Step.Compile(bc);
            bc.Emit_ToNum(2);
            m_Start.Compile(bc);
            bc.Emit_ToNum(1);

            int start   = bc.GetJumpPointForNextInstruction();
            var jumpend = bc.Emit_Jump(OpCode.JFor, -1);

            bc.Emit_Enter(m_StackFrame);
            //bc.Emit_SymStorN(m_VarName);

            bc.Emit_Store(m_VarName, 0, 0);

            m_InnerBlock.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_RefEnd);

            bc.Emit_Debug("..end");
            bc.Emit_Leave(m_StackFrame);
            bc.Emit_Incr(1);
            bc.Emit_Jump(OpCode.Jump, start);

            bc.LoopTracker.Loops.Pop();

            int exitpoint = bc.GetJumpPointForNextInstruction();

            foreach (Instruction i in L.BreakJumps)
            {
                i.NumVal = exitpoint;
            }

            jumpend.NumVal = exitpoint;
            bc.Emit_Pop(3);

            bc.PopSourceRef();
        }
        public override void Compile(ByteCode bc)
        {
            var         endJumps  = new List <Instruction>();
            Instruction lastIfJmp = null;

            foreach (var ifblock in _ifs)
            {
                using (bc.EnterSource(ifblock.Source))
                {
                    if (lastIfJmp != null)
                    {
                        lastIfJmp.NumVal = bc.GetJumpPointForNextInstruction();
                    }

                    ifblock.Exp.Compile(bc);
                    lastIfJmp = bc.Emit_Jump(OpCode.Jf, -1);
                    bc.Emit_Enter(ifblock.StackFrame);
                    ifblock.Block.Compile(bc);
                }

                using (bc.EnterSource(_end))
                {
                    bc.Emit_Leave(ifblock.StackFrame);
                }

                endJumps.Add(bc.Emit_Jump(OpCode.Jump, -1));
            }

            lastIfJmp.NumVal = bc.GetJumpPointForNextInstruction();

            if (_else != null)
            {
                using (bc.EnterSource(_else.Source))
                {
                    bc.Emit_Enter(_else.StackFrame);
                    _else.Block.Compile(bc);
                }

                using (bc.EnterSource(_end))
                {
                    bc.Emit_Leave(_else.StackFrame);
                }
            }

            foreach (var endjmp in endJumps)
            {
                endjmp.NumVal = bc.GetJumpPointForNextInstruction();
            }
        }
        public override void Compile(ByteCode bc)
        {
            bc.PushSourceRef(_refFor);

            var l = new Loop
            {
                Scope = _stackFrame
            };

            bc.LoopTracker.Loops.Push(l);

            _end.Compile(bc);
            bc.Emit_ToNum(3);
            _step.Compile(bc);
            bc.Emit_ToNum(2);
            _start.Compile(bc);
            bc.Emit_ToNum(1);

            int start   = bc.GetJumpPointForNextInstruction();
            var jumpend = bc.Emit_Jump(OpCode.JFor, -1);

            bc.Emit_Enter(_stackFrame);

            bc.Emit_Store(_varName, 0, 0);

            _innerBlock.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(_refEnd);

            bc.Emit_Leave(_stackFrame);
            bc.Emit_Incr(1);
            bc.Emit_Jump(OpCode.Jump, start);

            bc.LoopTracker.Loops.Pop();

            int exitpoint = bc.GetJumpPointForNextInstruction();

            foreach (var i in l.BreakJumps)
            {
                i.NumVal = exitpoint;
            }

            jumpend.NumVal = exitpoint;
            bc.Emit_Pop(3);

            bc.PopSourceRef();
        }
Beispiel #5
0
        public override void Compile(ByteCode bc)
        {
            Loop L = new Loop()
            {
                Scope = m_StackFrame
            };

            bc.PushSourceRef(m_Repeat);

            bc.LoopTracker.Loops.Push(L);

            int start = bc.GetJumpPointForNextInstruction();

            bc.Emit_Enter(m_StackFrame);
            m_Block.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_Until);
            bc.Emit_Debug("..end");

            m_Condition.Compile(bc);
            bc.Emit_Leave(m_StackFrame);
            bc.Emit_Jump(OpCode.Jf, start);

            bc.LoopTracker.Loops.Pop();

            int exitpoint = bc.GetJumpPointForNextInstruction();

            foreach (Instruction i in L.BreakJumps)
            {
                i.NumVal = exitpoint;
            }

            bc.PopSourceRef();
        }
        public override void Compile(ByteCode bc)
        {
            var l = new Loop
            {
                Scope = _stackFrame
            };

            bc.PushSourceRef(_repeat);

            bc.LoopTracker.Loops.Push(l);

            int start = bc.GetJumpPointForNextInstruction();

            bc.Emit_Enter(_stackFrame);
            _block.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(_until);

            _condition.Compile(bc);
            bc.Emit_Leave(_stackFrame);
            bc.Emit_Jump(OpCode.Jf, start);

            bc.LoopTracker.Loops.Pop();

            int exitPoint = bc.GetJumpPointForNextInstruction();

            foreach (var i in l.BreakJumps)
            {
                i.NumVal = exitPoint;
            }

            bc.PopSourceRef();
        }
Beispiel #7
0
        public override void Compile(ByteCode bc)
        {
            Loop L = new Loop()
            {
                Scope = m_StackFrame
            };


            bc.LoopTracker.Loops.Push(L);

            bc.PushSourceRef(m_Start);

            int start = bc.GetJumpPointForNextInstruction();

            m_Condition.Compile(bc);
            var jumpend = bc.Emit_Jump(OpCode.Jf, -1);

            bc.Emit_Enter(m_StackFrame);

            m_Block.Compile(bc);

            bc.PopSourceRef();
            bc.Emit_Debug("..end");
            bc.PushSourceRef(m_End);

            bc.Emit_Leave(m_StackFrame);
            bc.Emit_Jump(OpCode.Jump, start);

            bc.LoopTracker.Loops.Pop();

            int exitpoint = bc.GetJumpPointForNextInstruction();

            foreach (int i in L.BreakJumps)
            {
                bc.SetNumVal(i, exitpoint);
            }

            bc.SetNumVal(jumpend, exitpoint);

            bc.PopSourceRef();
        }
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            string funcName = friendlyName ?? $"<{_begin.FormatLocation(bc.Script)}>";

            bc.PushSourceRef(_begin);

            var I = bc.Emit_Jump(OpCode.Jump, -1);

            var meta   = bc.Emit_Meta(funcName, OpCodeMetadataType.FunctionEntrypoint);
            int metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(_stackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            int entryPoint = bc.GetJumpPointForLastInstruction();

            if (_usesGlobalEnv)
            {
                bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
                bc.Emit_Store(_env, 0, 0);
                bc.Emit_Pop();
            }

            if (_paramNames.Length > 0)
            {
                bc.Emit_Args(_paramNames);
            }

            _statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(_end);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            I.NumVal    = bc.GetJumpPointForNextInstruction();
            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;

            bc.PopSourceRef();

            return(entryPoint);
        }
Beispiel #9
0
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            string funcName = friendlyName ?? ("<" + this.m_Begin.FormatLocation(bc.Script, true) + ">");

            bc.PushSourceRef(m_Begin);

            Instruction I = bc.Emit_Jump(OpCode.Jump, -1);

            Instruction meta   = bc.Emit_FuncMeta(funcName);
            int         metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(m_StackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            int entryPoint = bc.GetJumpPointForLastInstruction();

            if (m_GlobalEnv != null)
            {
                bc.Emit_Literal(DynValue.NewTable(m_GlobalEnv));
                bc.Emit_Store(m_Env, 0, 0);
                bc.Emit_Pop();
            }

            if (m_ParamNames.Length > 0)
            {
                bc.Emit_Args(m_ParamNames);
            }

            m_Statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_End);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            I.NumVal    = bc.GetJumpPointForNextInstruction();
            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;

            bc.PopSourceRef();

            return(entryPoint);
        }
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            string funcName = friendlyName ?? ("<" + this.m_Begin.FormatLocation(bc.Script, true) + ">");

            bc.PushSourceRef(m_Begin);

            int I = bc.Emit_Jump(OpCode.Jump, -1);

            int meta = bc.Emit_Meta(funcName, OpCodeMetadataType.FunctionEntrypoint);

            bc.Emit_BeginFn(m_StackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            int entryPoint = bc.GetJumpPointForLastInstruction();

            if (m_UsesGlobalEnv)
            {
                bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
                bc.Emit_Store(m_Env, 0, 0);
                bc.Emit_Pop();
            }

            if (m_ParamNames.Length > 0)
            {
                bc.Emit_Args(m_ParamNames);
            }

            m_Statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_End);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            bc.SetNumVal(I, bc.GetJumpPointForNextInstruction());
            bc.SetNumVal(meta, bc.GetJumpPointForLastInstruction() - meta);

            bc.PopSourceRef();

            return(entryPoint);
        }
Beispiel #11
0
 public void CompileBreak(ByteCode bc)
 {
     bc.Emit_Exit(Scope);
     BreakJumps.Add(bc.Emit_Jump(OpCode.Jump, -1));
 }
        public override void Compile(ByteCode bc)
        {
            //for var_1, ···, var_n in explist do block end

            bc.PushSourceRef(m_RefFor);

            Loop L = new Loop()
            {
                Scope = m_StackFrame
            };

            bc.LoopTracker.Loops.Push(L);

            // get iterator tuple
            m_RValues.Compile(bc);

            // prepares iterator tuple - stack : iterator-tuple
            bc.Emit_IterPrep();

            // loop start - stack : iterator-tuple
            int start = bc.GetJumpPointForNextInstruction();

            bc.Emit_Enter(m_StackFrame);

            // expand the tuple - stack : iterator-tuple, f, var, s
            bc.Emit_ExpTuple(0);

            // calls f(s, var) - stack : iterator-tuple, iteration result
            bc.Emit_Call(2, "for..in");

            // perform assignment of iteration result- stack : iterator-tuple, iteration result
            for (int i = 0; i < m_NameExps.Length; i++)
            {
                m_NameExps[i].CompileAssignment(bc, 0, i);
            }

            // pops  - stack : iterator-tuple
            bc.Emit_Pop();

            // repushes the main iterator var - stack : iterator-tuple, main-iterator-var
            bc.Emit_Load(m_Names[0]);

            // updates the iterator tuple - stack : iterator-tuple, main-iterator-var
            bc.Emit_IterUpd();

            // checks head, jumps if nil - stack : iterator-tuple, main-iterator-var
            var endjump = bc.Emit_Jump(OpCode.JNil, -1);

            // executes the stuff - stack : iterator-tuple
            m_Block.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_RefEnd);

            // loop back again - stack : iterator-tuple
            bc.Emit_Leave(m_StackFrame);
            bc.Emit_Jump(OpCode.Jump, start);

            bc.LoopTracker.Loops.Pop();

            int exitpointLoopExit = bc.GetJumpPointForNextInstruction();

            bc.Emit_Leave(m_StackFrame);

            int exitpointBreaks = bc.GetJumpPointForNextInstruction();

            bc.Emit_Pop();

            foreach (Instruction i in L.BreakJumps)
            {
                i.NumVal = exitpointBreaks;
            }

            endjump.NumVal = exitpointLoopExit;

            bc.PopSourceRef();
        }
 public override void Compile(ByteCode bc)
 {
     _jump = bc.Emit_Jump(OpCode.Jump, _labelAddress);
 }
Beispiel #14
0
 public override void Compile(ByteCode bc)
 {
     m_Jump = bc.Emit_Jump(OpCode.Jump, m_LabelAddress);
     m_bc   = bc;
 }