Exemplo n.º 1
0
        public void Run(List <byte> instructions, List <Object> constants, List <BuiltIn> builtIns)
        {
            internalState = InitializeState(instructions, constants, builtIns);

            while (internalState.CurrentFrame.InstructionPointer < internalState.CurrentFrame.Closure.Instructions.Count)
            {
                if (internalState.Stack.Top != default(Object) && internalState.Stack.Top.Kind == ObjectKind.Error)
                {
                    break;
                }

                internalState.Opcode = internalState.CurrentFrame.Closure.Instructions[internalState.CurrentFrame.InstructionPointer];
                internalState.CurrentFrame.InstructionPointer++;

                switch (internalState.Opcode)
                {
                case 1:      // Opcode.Constant
                    ExecuteConstantOperation();
                    break;

                case 3:      // Opcode.Pop
                    internalState.Stack.Pop();
                    break;

                case 2:      // Opcode.Add
                case 4:      // Opcode.Subtract
                case 5:      // Opcode.Multiply
                case 6:      // Opcode.Divide
                case 9:      // Opcode.Equal
                case 10:     // Opcode.NotEqual
                case 11:     // Opcode.GreaterThan
                    ExecuteBinaryOperation(internalState.Opcode);
                    break;

                case 7:      // Opcode.True
                case 8:      // Opcode.False
                    ExecuteBooleanOperation(internalState.Opcode);
                    break;

                case 12:     // Opcode.Minus
                    ExecuteMinusOperation();
                    break;

                case 13:     // Opcode.Bang
                    ExecuteBangOperation();
                    break;

                case 14:     // Opcode.Jump
                    ExecuteJumpOperation();
                    break;

                case 15:     // Opcode.JumpNotTruthy
                    ExecuteJumpNotTruthyOperation();
                    break;

                case 16:     // Opcode.Null
                    ExecuteNullOperation();
                    break;

                case 17:     // Opcode.SetGlobal
                    ExecuteSetGlobalOperation();
                    break;

                case 18:     // Opcode.GetGlobal
                    ExecuteGetGlobalOperation();
                    break;

                case 19:     // Opcode.Array
                    ExecuteArrayOperation();
                    break;

                case 20:     // Opcode.Hash
                    ExecuteHashOperation();
                    break;

                case 21:     // Opcode.Index
                    ExecuteIndexOperation();
                    break;

                case 22:     // Opcode.Call
                    ExecuteCallOperation();
                    break;

                case 23:     // Opcode.Return
                    ExecuteReturnOperation();
                    break;

                case 24:     // Opcode.ReturnValue
                    ExecuteReturnValueOperation();
                    break;

                case 25:     // Opcode.SetLocal
                    ExecuteSetLocalOperation();
                    break;

                case 26:     // Opcode.GetLocal
                    ExecuteGetLocalOperation();
                    break;

                case 27:     // Opcode.GetBuiltIn
                    ExecuteGetBuiltInOperation();
                    break;

                case 28:     // Opcode.Closure
                    ExecuteClosureOperation();
                    break;

                case 29:     // Opcode.GetFree
                    ExecuteGetFreeOperation();
                    break;

                case 30:     // Opcode.GetCurrentClosure
                    ExecuteGetCurrentClosureOperation();
                    break;
                }
            }
        }
Exemplo n.º 2
0
 public VirtualMachine()
 {
     internalState = new VirtualMachineState {
         Globals = new List <Object>()
     };
 }