Exemplo n.º 1
0
        public static ObjInstance newInstance(ObjClass klass)
        {
            ObjInstance instance = (ObjInstance)ALLOCATE_OBJ(ObjType.OBJ_INSTANCE);

            instance.klass = klass;
            Table.initTable(ref instance.fields);
            return(instance);
        }
Exemplo n.º 2
0
        private static void defineMethod(ObjString name)
        {
            Value_t  method = peek(0);
            ObjClass klass  = Object.AS_CLASS(peek(1));

            Table.tableSet(ref klass.methods, name, method);
            pop();
        }
Exemplo n.º 3
0
        public static ObjClass newClass(ObjString name)
        {
            ObjClass klass = (ObjClass)ALLOCATE_OBJ(ObjType.OBJ_CLASS);

            klass.name = name;
            Table.initTable(ref klass.methods);
            return(klass);
        }
Exemplo n.º 4
0
        private static bool callValue(Value_t callee, int argCount)
        {
            if (Value.IS_OBJ(callee))
            {
                ObjType _type = Object.OBJ_TYPE(callee);

                switch (Object.OBJ_TYPE(callee))
                {
                case ObjType.OBJ_BOUND_METHOD:
                {
                    ObjBoundMethod bound = Object.AS_BOUND_METHOD(callee);
                    vm.stack[vm.stackTop - argCount - 1] = bound.receiver;
                    return(call(bound.method, argCount));
                }

                case ObjType.OBJ_CLASS:
                {
                    ObjClass klass = Object.AS_CLASS(callee);
                    vm.stack[vm.stackTop - argCount - 1] = Value.OBJ_VAL(Object.newInstance(klass));
                    Value_t initializer = new Value_t();
                    if (Table.tableGet(ref klass.methods, vm.initString, ref initializer))
                    {
                        return(call(Object.AS_CLOSURE(initializer), argCount));
                    }
                    else if (argCount != 0)
                    {
                        runtimeError("Expected 0 arguments but got {0}.", argCount.ToString());
                        return(false);
                    }
                    vm.frames[vm.frameCount - 1]._ip_index += 2;         // HACK FIX
                    return(true);
                }

                case ObjType.OBJ_CLOSURE:
                    return(call(Object.AS_CLOSURE(callee), argCount));

                case ObjType.OBJ_NATIVE:
                {
                    NativeFn native = Object.AS_NATIVE(callee);
                    Value_t  result = native(argCount, vm.stackTop - argCount);
                    vm.stackTop -= argCount + 1;
                    push(result);

                    vm.frames[vm.frameCount - 1]._ip_index += 2;         // HACK FIX
                    return(true);
                }

                default:
                    // Non-callable object type.
                    break;
                }
            }

            runtimeError("Can only call functions and classes.");
            return(false);
        }
Exemplo n.º 5
0
        // Generics, <T>, doesn't seem to work with classes?
        static Obj allocateObject(int size, ObjType type)
        {
            Obj object_ = null;

            switch (type)
            {
            case ObjType.OBJ_STRING:
                object_ = new ObjString();
                break;

            case ObjType.OBJ_FUNCTION:
                object_ = new ObjFunction();
                break;

            case ObjType.OBJ_INSTANCE:
                object_ = new ObjInstance();
                break;

            case ObjType.OBJ_NATIVE:
                object_ = new ObjNative();
                break;

            case ObjType.OBJ_CLOSURE:
                object_ = new ObjClosure();
                break;

            case ObjType.OBJ_UPVALUE:
                object_ = new ObjUpvalue();
                break;

            case ObjType.OBJ_CLASS:
                object_ = new ObjClass();
                break;

            case ObjType.OBJ_BOUND_METHOD:
                object_ = new ObjBoundMethod();
                break;

            default:
                object_ = null;    // clox: (Obj*)reallocate(NULL, 0, size);
                break;
            }

            object_.type     = type;
            object_.isMarked = false;

            object_.next  = VM.vm.objects;
            VM.vm.objects = object_;

#if DEBUG_LOG_GC
            System.Console.WriteLine("{0} allocate {1} for {2}", object_._mem_id.ToString(), size.ToString(), type.ToString());
#endif
            return(object_);
        }
Exemplo n.º 6
0
        private static bool invokeFromClass(ObjClass klass, ObjString name, int argCount)
        {
            Value_t method = new Value_t();

            if (!Table.tableGet(ref klass.methods, name, ref method))
            {
                runtimeError("Undefined property '{0}'.", new string(name.chars, 0, name.chars.Length - 1));
                return(false);
            }

            return(call(Object.AS_CLOSURE(method), argCount));
        }
Exemplo n.º 7
0
        private static bool bindMethod(ObjClass klass, ObjString name)
        {
            Value_t method = new Value_t();

            if (!Table.tableGet(ref klass.methods, name, ref method))
            {
                runtimeError("Undefined property '{0}'.", new string(name.chars, 0, name.chars.Length - 1));
                return(false);
            }

            ObjBoundMethod bound = Object.newBoundMethod(peek(0), Object.AS_CLOSURE(method));

            pop();
            push(Value.OBJ_VAL(bound));
            return(true);
        }
Exemplo n.º 8
0
        public static InterpretResult run()
        {
            CallFrame frame = vm.frames[vm.frameCount - 1];

            frame._slot_offset = 0;

            for (;;)
            {
#if DEBUG_TRACE_EXECUTION
                System.Console.Write("          ");
                for (int slot = 0; slot < vm.stackTop; slot++)
                {
                    System.Console.Write("[ ");
                    Value.printValue(vm.stack[slot]);
                    System.Console.Write(" ]");
                }
                System.Console.WriteLine();

                Debug.disassembleInstruction(ref frame.closure.function.chunk, frame._ip_index);
#endif

                OpCode instruction;
                switch (instruction = (OpCode)READ_BYTE(ref frame))
                {
                case OpCode.OP_CONSTANT:
                {
                    Value_t constant = READ_CONSTANT(ref frame);
                    push(constant);
                    break;
                }

                case OpCode.OP_NIL:
                    push(Value.NIL_VAL());
                    break;

                case OpCode.OP_TRUE:
                    push(Value.BOOL_VAL(true));
                    break;

                case OpCode.OP_FALSE:
                    push(Value.BOOL_VAL(false));
                    break;

                case OpCode.OP_POP:
                    pop();
                    break;

                case OpCode.OP_GET_LOCAL:
                {
                    byte slot = READ_BYTE(ref frame);
                    push(frame.slots[frame._slot_offset + slot]);
                    break;
                }

                case OpCode.OP_SET_LOCAL:
                {
                    byte slot = READ_BYTE(ref frame);
                    frame.slots[frame._slot_offset + slot] = peek(0);
                    break;
                }

                case OpCode.OP_GET_GLOBAL:
                {
                    ObjString name  = READ_STRING(ref frame);
                    Value_t   value = new Value_t();
                    if (!Table.tableGet(ref vm.globals, name, ref value))
                    {
                        runtimeError("Undefined variable '{0}'.", new string(name.chars, 0, name.chars.Length - 1));
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    push(value);
                    break;
                }

                case OpCode.OP_DEFINE_GLOBAL:
                {
                    ObjString name = READ_STRING(ref frame);
                    Table.tableSet(ref vm.globals, name, peek(0));
                    pop();
                    break;
                }

                case OpCode.OP_SET_GLOBAL:
                {
                    ObjString name = READ_STRING(ref frame);
                    if (Table.tableSet(ref vm.globals, name, peek(0)))
                    {
                        Table.tableDelete(ref vm.globals, name);
                        runtimeError("Undefined variable '{0}'.", new string(name.chars, 0, name.chars.Length - 1));
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    break;
                }

                case OpCode.OP_GET_UPVALUE:
                {
                    byte slot = READ_BYTE(ref frame);
                    push(frame.closure.upvalues[slot]._value_src[frame.closure.upvalues[slot].location]);
                    break;
                }

                case OpCode.OP_SET_UPVALUE:
                {
                    byte slot = READ_BYTE(ref frame);
                    frame.closure.upvalues[slot]._value_src[frame.closure.upvalues[slot].location] = peek(0);
                    break;
                }

                case OpCode.OP_GET_PROPERTY:
                {
                    if (!Object.IS_INSTANCE(peek(0)))
                    {
                        runtimeError("Only instances have properties.");
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }

                    ObjInstance instance = Object.AS_INSTANCE(peek(0));
                    ObjString   name     = READ_STRING(ref frame);

                    Value_t value = new Value_t();
                    if (Table.tableGet(ref instance.fields, name, ref value))
                    {
                        pop();         // Instance.
                        push(value);
                        break;
                    }

                    if (!bindMethod(instance.klass, name))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    break;
                }

                case OpCode.OP_SET_PROPERTY:
                {
                    if (!Object.IS_INSTANCE(peek(1)))
                    {
                        runtimeError("Only instances have fields.");
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }

                    ObjInstance instance = Object.AS_INSTANCE(peek(1));
                    Table.tableSet(ref instance.fields, READ_STRING(ref frame), peek(0));

                    Value_t value = pop();
                    pop();
                    push(value);
                    break;
                }

                case OpCode.OP_GET_SUPER:
                {
                    ObjString name       = READ_STRING(ref frame);
                    ObjClass  superclass = Object.AS_CLASS(pop());
                    if (!bindMethod(superclass, name))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    break;
                }

                case OpCode.OP_EQUAL:
                {
                    Value_t b = pop();
                    Value_t a = pop();
                    push(Value.BOOL_VAL(Value.valuesEqual(a, b)));
                    break;
                }

                case OpCode.OP_ADD:
                {
                    if (Object.IS_STRING(peek(0)) && Object.IS_STRING(peek(1)))
                    {
                        concatenate();
                    }
                    else if (Value.IS_NUMBER(peek(0)) && Value.IS_NUMBER(peek(1)))
                    {
                        BINARY_OP(instruction);
                    }
                    else
                    {
                        runtimeError("Operands must be two numbers or two strings.");
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                }
                break;

                case OpCode.OP_GREATER:
                case OpCode.OP_LESS:
                case OpCode.OP_SUBTRACT:
                case OpCode.OP_MULTIPLY:
                case OpCode.OP_DIVIDE:
                    if (!BINARY_OP(instruction))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    break;

                case OpCode.OP_NOT:
                    push(Value.BOOL_VAL(isFalsey(pop())));
                    break;

                case OpCode.OP_NEGATE:
                    if (!Value.IS_NUMBER(peek(0)))
                    {
                        runtimeError("Operand must be a number.");
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }

                    push(Value.NUMBER_VAL(-Value.AS_NUMBER(pop())));
                    break;

                case OpCode.OP_PRINT:
                {
                    Value.printValue(pop());
                    System.Console.WriteLine();
                    break;
                }

                case OpCode.OP_JUMP:
                {
                    ushort offset = READ_SHORT(ref frame);
                    frame._ip_index += offset;
                    break;
                }

                case OpCode.OP_JUMP_IF_FALSE:
                {
                    ushort offset = READ_SHORT(ref frame);
                    if (isFalsey(peek(0)))
                    {
                        frame._ip_index += offset;
                    }
                    break;
                }

                case OpCode.OP_LOOP:
                {
                    ushort offset = READ_SHORT(ref frame);
                    frame._ip_index -= offset;
                    break;
                }

                case OpCode.OP_CALL:
                {
                    int argCount = READ_BYTE(ref frame);
                    _caller_ip_index = frame._ip_index;
                    if (!callValue(peek(argCount), argCount))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    frame = vm.frames[vm.frameCount - 1];
                    break;
                }

                case OpCode.OP_INVOKE:
                {
                    ObjString method   = READ_STRING(ref frame);
                    int       argCount = READ_BYTE(ref frame);

                    _caller_ip_index = frame._ip_index;
                    if (!invoke(method, argCount))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    frame = vm.frames[vm.frameCount - 1];
                    break;
                }

                case OpCode.OP_SUPER_INVOKE:
                {
                    ObjString method     = READ_STRING(ref frame);
                    int       argCount   = READ_BYTE(ref frame);
                    ObjClass  superclass = Object.AS_CLASS(pop());

                    _caller_ip_index = frame._ip_index;
                    if (!invokeFromClass(superclass, method, argCount))
                    {
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }
                    frame = vm.frames[vm.frameCount - 1];
                    break;
                }

                case OpCode.OP_CLOSURE:
                {
                    ObjFunction function = Object.AS_FUNCTION(READ_CONSTANT(ref frame));
                    ObjClosure  closure  = Object.newClosure(function);

                    for (int i = 0; i < closure.upvalueCount; i++)
                    {
                        byte isLocal = READ_BYTE(ref frame);
                        byte index   = READ_BYTE(ref frame);
                        if (isLocal != 0)
                        {
                            closure.upvalues[i] = captureUpvalue(frame._slot_offset + index);
                        }
                        else
                        {
                            closure.upvalues[i] = frame.closure.upvalues[index];
                        }
                    }

                    push(Value.OBJ_VAL(closure));         // cs push after modify?

                    break;
                }

                case OpCode.OP_CLOSE_UPVALUE:
                    closeUpvalues(vm.stackTop - 1);
                    pop();
                    break;

                case OpCode.OP_RETURN:
                {
                    Value_t result = pop();
                    closeUpvalues(frame._slot_offset);

                    vm.frameCount--;
                    if (vm.frameCount == 0)
                    {
                        pop();
                        return(InterpretResult.INTERPRET_OK);
                    }

                    vm.stackTop = frame._slot_offset;
                    push(result);

                    frame = vm.frames[vm.frameCount - 1];
                    break;
                }

                case OpCode.OP_CLASS:
                    push(Value.OBJ_VAL(Object.newClass(READ_STRING(ref frame))));
                    break;

                case OpCode.OP_INHERIT:
                {
                    Value_t superclass = peek(1);
                    if (!Object.IS_CLASS(superclass))
                    {
                        runtimeError("Superclass must be a class.");
                        return(InterpretResult.INTERPRET_RUNTIME_ERROR);
                    }

                    ObjClass subclass = Object.AS_CLASS(peek(0));
                    Table.tableAddAll(ref Object.AS_CLASS(superclass).methods, ref subclass.methods);
                    pop();         // Subclass.
                    break;
                }

                case OpCode.OP_METHOD:
                    defineMethod(READ_STRING(ref frame));
                    break;
                }

                vm.frames[vm.frameCount - 1]._ip_index = frame._ip_index; //Csharp reference updating workaround
            }
        }
Exemplo n.º 9
0
        private static void freeObject(ref Obj object_)
        {
#if DEBUG_LOG_GC
            System.Console.WriteLine("{0} free type {1}", object_._mem_id.ToString(), object_.type.ToString());
#endif
            switch (object_.type)
            {
            case ObjType.OBJ_BOUND_METHOD:
                object_._free();
                object_ = null;
                break;

            case ObjType.OBJ_CLASS:
            {
                ObjClass klass = (ObjClass)object_;
                Table.freeTable(ref klass.methods);
                //FREE<ObjClass>(ref object_);
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_CLOSURE:
            {
                ObjClosure closure = (ObjClosure)object_;
                FREE_ARRAY <ObjUpvalue>(typeof(ObjUpvalue), ref closure.upvalues, closure.upvalueCount);
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_FUNCTION:
            {
                ObjFunction function = (ObjFunction)object_;
                Chunk.freeChunk(ref function.chunk);         // the function's byte code
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_INSTANCE:
            {
                ObjInstance instance = (ObjInstance)object_;
                Table.freeTable(ref instance.fields);
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_NATIVE:
            {
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_STRING:
            {
                ObjString string_ = (ObjString)object_;
                FREE_ARRAY <char>(typeof(char), ref string_.chars, string_.length + 1);
                object_._free();
                object_ = null;
                break;
            }

            case ObjType.OBJ_UPVALUE:
                util.cHeap.values.remove(((ObjUpvalue)object_).location);
                object_._free();
                object_ = null;
                break;
            }
        }
Exemplo n.º 10
0
        private static void blackenObject(Obj object_)
        {
#if DEBUG_LOG_GC
            System.Console.Write("{0} blacken ", object_._mem_id.ToString());
            Value.printValue(Value.OBJ_VAL(object_));
            System.Console.WriteLine();
#endif

            switch (object_.type)
            {
            case ObjType.OBJ_BOUND_METHOD:
            {
                ObjBoundMethod bound = (ObjBoundMethod)object_;
                markValue(ref bound.receiver);
                markObject((Obj)bound.method);
                break;
            }

            case ObjType.OBJ_CLASS:
            {
                ObjClass klass = (ObjClass)object_;
                markObject((Obj)klass.name);
                Table.markTable(ref klass.methods);
                break;
            }

            case ObjType.OBJ_CLOSURE:
            {
                ObjClosure closure = (ObjClosure)object_;
                markObject((Obj)closure.function);
                for (int i = 0; i < closure.upvalueCount; i++)
                {
                    markObject((Obj)closure.upvalues[i]);
                }
                break;
            }

            case ObjType.OBJ_FUNCTION:
            {
                ObjFunction function = (ObjFunction)object_;
                markObject((Obj)function.name);
                markArray(ref function.chunk.constants);
                break;
            }

            case ObjType.OBJ_INSTANCE:
            {
                ObjInstance instance = (ObjInstance)object_;
                markObject((Obj)(instance.klass));
                Table.markTable(ref instance.fields);
                break;
            }

            case ObjType.OBJ_UPVALUE:
                markValue(ref ((ObjUpvalue)object_).closed);
                break;

            case ObjType.OBJ_NATIVE:
            case ObjType.OBJ_STRING:
                break;
            }
        }