diff --git a/common.nim b/common.nim index 72a01eb..4491ae3 100644 --- a/common.nim +++ b/common.nim @@ -13,12 +13,10 @@ # limitations under the License. -import tables -import strutils import meta/tokenobject import types/japlvalue -import types/string import types/function +import tables const FRAMES_MAX* = 400 # TODO: Inspect why the VM crashes if this exceeds 400 diff --git a/types/function.nim b/types/function.nim index 962c3ba..864a6c6 100644 --- a/types/function.nim +++ b/types/function.nim @@ -24,7 +24,6 @@ import strformat import ../memory import ../meta/opcode import japlvalue -import tables type diff --git a/types/japlvalue.nim b/types/japlvalue.nim index f7b4d1d..5d10893 100644 --- a/types/japlvalue.nim +++ b/types/japlvalue.nim @@ -114,7 +114,7 @@ proc typeName*(obj: ptr Obj): string = result = "object" - +# TODO migrate to operations proc bool*(obj: ptr Obj): bool = ## Returns wheter the object should ## be considered a falsey value @@ -127,7 +127,7 @@ proc bool*(obj: ptr Obj): bool = else: result = false - +# TODO migrate to operations proc eq*(a: ptr Obj, b: ptr Obj): bool = ## Compares two objects for equality @@ -137,7 +137,7 @@ proc eq*(a: ptr Obj, b: ptr Obj): bool = else: result = a.kind == b.kind - +# TODO migrate to operations proc hash*(self: ptr Obj): uint32 = # TODO: Make this actually useful result = 2166136261u32 @@ -263,7 +263,7 @@ func toFloat*(value: Value): float = ## Converts a JAPL float to a nim float result = value.floatValue - +# TODO ambiguous naming: conflict with toString(value: Value) that does JAPL->JAPL func toStr*(value: Value): string = ## Converts a JAPL string into a nim string var strObj = cast[ptr String](value.obj) diff --git a/types/operations.nim b/types/operations.nim index 06f362e..4a5b3dc 100644 --- a/types/operations.nim +++ b/types/operations.nim @@ -43,7 +43,7 @@ func stringify*(value: Value): string = of ValueType.Minf: result = "-inf" -func bool*(value: Value): bool = +func isFalsey*(value: Value): bool = case value.kind: of ValueType.Bool: result = not value.toBool() @@ -56,11 +56,11 @@ func bool*(value: Value): bool = of ObjectType.Exception: result = cast[ptr JaplException](value.obj).isFalsey() of ObjectType.Class: - result = cast[ptr JaplException](value.obj).isFalsey() + result = cast[ptr JaplException](value.obj).isFalsey() # TODO Class of ObjectType.Module: - result = cast[ptr JaplException](value.obj).isFalsey() + result = cast[ptr JaplException](value.obj).isFalsey() # TODO Module of ObjectType.BaseObject: - result = cast[ptr JaplException](value.obj).isFalsey() + result = cast[ptr JaplException](value.obj).isFalsey() # TODO BaseObject of ValueType.Integer: result = value.toInt() == 0 of ValueType.Double: diff --git a/util/debug.nim b/util/debug.nim index b43de49..004cc67 100644 --- a/util/debug.nim +++ b/util/debug.nim @@ -18,7 +18,6 @@ import ../meta/opcode import ../types/japlvalue import ../types/operations -import ../common import strformat diff --git a/vm.nim b/vm.nim index 75645b7..21a8224 100644 --- a/vm.nim +++ b/vm.nim @@ -252,14 +252,14 @@ proc run(self: var VM, repl: bool): InterpretResult = template readConstant: Value = ## Reads a constant from the current ## frame's constant table - frame.function.chunk.consts.values[int(readByte())] + frame.function.chunk.consts[int(readByte())] template readLongConstant: Value = ## Reads a long constant from the ## current frame's constant table var arr = [readByte(), readByte(), readByte()] var idx: int copyMem(idx.addr, unsafeAddr(arr), sizeof(arr)) - frame.function.chunk.consts.values[idx] + frame.function.chunk.consts[idx] template binOp(op, check) = ## Performs binary operations on types, ## this will be soon ditched in favor @@ -332,7 +332,7 @@ proc run(self: var VM, repl: bool): InterpretResult = var rightVal {.inject.} = self.pop() var leftVal {.inject.} = self.pop() if isInt(leftVal) and isInt(rightVal): - self.push(Value(kind: INTEGER, intValue: `op`(leftVal.toInt(), rightVal.toInt()))) + self.push(Value(kind: ValueType.Integer, intValue: `op`(leftVal.toInt(), rightVal.toInt()))) else: self.error(newTypeError(&"unsupported binary operator for objects of type '{leftVal.typeName()}' and '{rightVal.typeName()}'")) return RUNTIME_ERROR @@ -340,7 +340,7 @@ proc run(self: var VM, repl: bool): InterpretResult = ## Handles unary bitwise operators var leftVal {.inject.} = self.pop() if isInt(leftVal): - self.push(Value(kind: INTEGER, intValue: `op`(leftVal.toInt()))) + self.push(Value(kind: ValueType.Integer, intValue: `op`(leftVal.toInt()))) else: self.error(newTypeError(&"unsupported unary operator for object of type '{leftVal.typeName()}'")) return RUNTIME_ERROR @@ -466,15 +466,15 @@ proc run(self: var VM, repl: bool): InterpretResult = of OpCode.Inf: self.push(Value(kind: ValueType.Inf)) of OpCode.Not: - self.push(Value(kind: BOOL, boolValue: isFalsey(self.pop()))) + self.push(Value(kind: ValueType.Bool, boolValue: isFalsey(self.pop()))) of OpCode.Equal: var a = self.pop() var b = self.pop() if a.isFloat() and b.isInt(): - b = Value(kind: DOUBLE, floatValue: float b.toInt()) + b = Value(kind: ValueType.Double, floatValue: float b.toInt()) elif b.isFloat() and a.isInt(): - a = Value(kind: DOUBLE, floatValue: float a.toInt()) - self.push(Value(kind: BOOL, boolValue: valuesEqual(a, b))) + a = Value(kind: ValueType.Double, floatValue: float a.toInt()) + self.push(Value(kind: ValueType.Bool, boolValue: eq(a, b))) of OpCode.Less: binOp(`<`, isNum) of OpCode.Greater: @@ -486,7 +486,7 @@ proc run(self: var VM, repl: bool): InterpretResult = if not self.sliceRange(): return RUNTIME_ERROR of OpCode.DefineGlobal: - if frame.function.chunk.consts.values.len > 255: + if frame.function.chunk.consts.len > 255: var constant = readLongConstant().toStr() self.globals[constant] = self.peek(0) else: @@ -494,7 +494,7 @@ proc run(self: var VM, repl: bool): InterpretResult = self.globals[constant] = self.peek(0) discard self.pop() # This will help when we have a custom GC of OpCode.GetGlobal: - if frame.function.chunk.consts.values.len > 255: + if frame.function.chunk.consts.len > 255: var constant = readLongConstant().toStr() if constant notin self.globals: self.error(newReferenceError(&"undefined name '{constant}'")) @@ -509,7 +509,7 @@ proc run(self: var VM, repl: bool): InterpretResult = else: self.push(self.globals[constant]) of OpCode.SetGlobal: - if frame.function.chunk.consts.values.len > 255: + if frame.function.chunk.consts.len > 255: var constant = readLongConstant().toStr() if constant notin self.globals: self.error(newReferenceError(&"assignment to undeclared name '{constant}'")) @@ -525,7 +525,7 @@ proc run(self: var VM, repl: bool): InterpretResult = self.globals[constant] = self.peek(0) of OpCode.DeleteGlobal: # This OpCode, as well as DeleteLocal, is currently unused due to potential issues with the GC - if frame.function.chunk.consts.values.len > 255: + if frame.function.chunk.consts.len > 255: var constant = readLongConstant().toStr() if constant notin self.globals: self.error(newReferenceError(&"undefined name '{constant}'")) @@ -659,8 +659,8 @@ proc interpret*(self: var VM, source: string, repl: bool = false, file: string): # to the vm if compiled == nil: return COMPILE_ERROR - self.push(Value(kind: OBJECT, obj: compiled)) - discard self.callValue(Value(kind: OBJECT, obj: compiled), 0) + self.push(Value(kind: ValueType.Object, obj: compiled)) + discard self.callValue(Value(kind: ValueType.Object, obj: compiled), 0) when DEBUG_TRACE_VM: echo "==== VM debugger starts ====\n" try: