Fixed issues with '>', thanks Nim...

This commit is contained in:
Mattia Giambirtone 2022-08-15 19:52:06 +02:00
parent 0861135e7f
commit 70a5f9dcd3
2 changed files with 38 additions and 21 deletions

View File

@ -80,6 +80,22 @@ proc getInf*(self: PeonVM, positive: bool): PeonObject {.inline.} =
proc getNan*(self: PeonVM): PeonObject {.inline.} = self.cache[5] proc getNan*(self: PeonVM): PeonObject {.inline.} = self.cache[5]
# Thanks to nim's *genius* idea of making x !> y a template
# for y < x (which by itself is fine) together with the fact
# that the order of evaluation of templates with the same
# expression is fucking stupid (see https://nim-lang.org/docs/manual.html#order-of-evaluation
# and https://github.com/nim-lang/Nim/issues/10425 and try not to
# bang your head against the nearest wall), we need a custom operator
# that preserves the natural order of evaluation
proc `!>`[T](a, b: T): auto {.inline.} =
b < a
proc `!>=`[T](a, b: T): auto {.inline, used.} =
b <= a
# Stack primitives. Note: all stack accessing that goes # Stack primitives. Note: all stack accessing that goes
# through the get(c)/set(c)/peek(c) wrappers is frame-relative, # through the get(c)/set(c)/peek(c) wrappers is frame-relative,
# meaning that the index is added to the current stack frame's # meaning that the index is added to the current stack frame's
@ -268,7 +284,7 @@ proc constReadString(self: PeonVM, size: int, idx: int): PeonObject =
## chunk's constant table and ## chunk's constant table and
## returns a Peon object. Assumes ## returns a Peon object. Assumes
## the constant is a string ## the constant is a string
result = PeonObject(kind: String, str: self.chunk.consts[idx..< idx + size].fromBytes()) result = PeonObject(kind: String, str: self.chunk.consts[idx..<idx + size].fromBytes())
proc constReadFloat32(self: PeonVM, idx: int): PeonObject = proc constReadFloat32(self: PeonVM, idx: int): PeonObject =
@ -303,16 +319,16 @@ proc dispatch*(self: PeonVM) =
when DEBUG_TRACE_VM: when DEBUG_TRACE_VM:
echo &"IP: {self.ip}" echo &"IP: {self.ip}"
echo &"Instruction: {OpCode(self.chunk.code[self.ip])}" echo &"Instruction: {OpCode(self.chunk.code[self.ip])}"
if self.calls.len() > 0: if self.calls.len() !> 0:
echo &"Call Stack: {self.calls}" echo &"Call Stack: {self.calls}"
if self.operands.len() > 0: if self.operands.len() !> 0:
echo &"Operand Stack: {self.operands}" echo &"Operand Stack: {self.operands}"
if self.frames.len() > 0: if self.frames.len() !> 0:
echo &"Current Frame: {self.calls[self.frames[^1]..^1]}" echo &"Current Frame: {self.calls[self.frames[^1]..^1]}"
echo &"Frames: {self.frames}" echo &"Frames: {self.frames}"
if self.closedOver.len() > 0: if self.closedOver.len() !> 0:
echo &"Closure Array: {self.closedOver}" echo &"Closure Array: {self.closedOver}"
if self.results.len() > 0: if self.results.len() !> 0:
echo &"Results: {self.results}" echo &"Results: {self.results}"
discard readLine stdin discard readLine stdin
instruction = OpCode(self.readByte()) instruction = OpCode(self.readByte())
@ -390,7 +406,7 @@ proc dispatch*(self: PeonVM) =
# TODO: Use the frame's initial size once # TODO: Use the frame's initial size once
# we have more control over the # we have more control over the
# memory # memory
#[while argc > 0: #[while argc !> 0:
dec(argc) dec(argc)
self.pushc(self.getNil()) self.pushc(self.getNil())
]# ]#
@ -400,7 +416,7 @@ proc dispatch*(self: PeonVM) =
# in a hidden function, so this # in a hidden function, so this
# will also exit the VM if we're # will also exit the VM if we're
# at the end of the program # at the end of the program
while self.calls.len() > self.frames[^1] + 2: while self.calls.len() !> self.frames[^1] + 2:
# Discards the function's local variables, # Discards the function's local variables,
# if there is any # if there is any
discard self.popc() discard self.popc()
@ -437,7 +453,7 @@ proc dispatch*(self: PeonVM) =
# Stores/updates the value of a closed-over # Stores/updates the value of a closed-over
# variable # variable
let idx = self.readLong().int let idx = self.readLong().int
if idx > self.closedOver.high(): if idx !> self.closedOver.high():
# Note: we *peek* the stack, but we # Note: we *peek* the stack, but we
# don't pop! # don't pop!
self.closedOver.add(self.peek()) self.closedOver.add(self.peek())
@ -468,7 +484,7 @@ proc dispatch*(self: PeonVM) =
# Pops a peon object off the # Pops a peon object off the
# operand stack and prints it. # operand stack and prints it.
# Used in interactive REPL mode # Used in interactive REPL mode
if self.frames.len() > 1: if self.frames.len() !> 1:
discard self.pop() discard self.pop()
continue continue
echo self.pop() echo self.pop()
@ -599,7 +615,7 @@ proc dispatch*(self: PeonVM) =
of LessThanInt64: of LessThanInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long < self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long < self.pop().long))
of GreaterThanInt64: of GreaterThanInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long > self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long !> self.pop().long))
of EqualInt64: of EqualInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long == self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long == self.pop().long))
of NotEqualInt64: of NotEqualInt64:
@ -607,7 +623,7 @@ proc dispatch*(self: PeonVM) =
of LessThanUInt64: of LessThanUInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long < self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long < self.pop().long))
of GreaterThanUInt64: of GreaterThanUInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long > self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long !> self.pop().long))
of EqualUInt64: of EqualUInt64:
self.push(PeonObject(kind: Bool, boolean: self.pop().long == self.pop().long)) self.push(PeonObject(kind: Bool, boolean: self.pop().long == self.pop().long))
of NotEqualUInt64: of NotEqualUInt64:
@ -615,7 +631,7 @@ proc dispatch*(self: PeonVM) =
of LessThanInt32: of LessThanInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().`int` < self.pop().`int`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`int` < self.pop().`int`))
of GreaterThanInt32: of GreaterThanInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().`int` > self.pop().`int`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`int` !> self.pop().`int`))
of EqualInt32: of EqualInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().`int` == self.pop().`int`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`int` == self.pop().`int`))
of NotEqualInt32: of NotEqualInt32:
@ -623,7 +639,7 @@ proc dispatch*(self: PeonVM) =
of LessThanUInt32: of LessThanUInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().uInt < self.pop().uInt)) self.push(PeonObject(kind: Bool, boolean: self.pop().uInt < self.pop().uInt))
of GreaterThanUInt32: of GreaterThanUInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().uInt > self.pop().uInt)) self.push(PeonObject(kind: Bool, boolean: self.pop().uInt !> self.pop().uInt))
of EqualUInt32: of EqualUInt32:
self.push(PeonObject(kind: Bool, boolean: self.pop().uInt == self.pop().uInt)) self.push(PeonObject(kind: Bool, boolean: self.pop().uInt == self.pop().uInt))
of NotEqualUInt32: of NotEqualUInt32:
@ -631,7 +647,7 @@ proc dispatch*(self: PeonVM) =
of LessThanInt16: of LessThanInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().short < self.pop().short)) self.push(PeonObject(kind: Bool, boolean: self.pop().short < self.pop().short))
of GreaterThanInt16: of GreaterThanInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().short > self.pop().short)) self.push(PeonObject(kind: Bool, boolean: self.pop().short !> self.pop().short))
of EqualInt16: of EqualInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().short == self.pop().short)) self.push(PeonObject(kind: Bool, boolean: self.pop().short == self.pop().short))
of NotEqualInt16: of NotEqualInt16:
@ -639,7 +655,7 @@ proc dispatch*(self: PeonVM) =
of LessThanUInt16: of LessThanUInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().uShort < self.pop().uShort)) self.push(PeonObject(kind: Bool, boolean: self.pop().uShort < self.pop().uShort))
of GreaterThanUInt16: of GreaterThanUInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().uShort > self.pop().uShort)) self.push(PeonObject(kind: Bool, boolean: self.pop().uShort !> self.pop().uShort))
of EqualUInt16: of EqualUInt16:
self.push(PeonObject(kind: Bool, boolean: self.pop().uShort == self.pop().uShort)) self.push(PeonObject(kind: Bool, boolean: self.pop().uShort == self.pop().uShort))
of NotEqualUInt16: of NotEqualUInt16:
@ -647,7 +663,7 @@ proc dispatch*(self: PeonVM) =
of LessThanInt8: of LessThanInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().tiny < self.pop().tiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().tiny < self.pop().tiny))
of GreaterThanInt8: of GreaterThanInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().tiny > self.pop().tiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().tiny !> self.pop().tiny))
of EqualInt8: of EqualInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().tiny == self.pop().tiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().tiny == self.pop().tiny))
of NotEqualInt8: of NotEqualInt8:
@ -655,7 +671,7 @@ proc dispatch*(self: PeonVM) =
of LessThanUInt8: of LessThanUInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny < self.pop().uTiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny < self.pop().uTiny))
of GreaterThanUInt8: of GreaterThanUInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny > self.pop().uTiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny !> self.pop().uTiny))
of EqualUInt8: of EqualUInt8:
self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny == self.pop().uTiny)) self.push(PeonObject(kind: Bool, boolean: self.pop().uTiny == self.pop().uTiny))
of NotEqualUInt8: of NotEqualUInt8:
@ -663,7 +679,7 @@ proc dispatch*(self: PeonVM) =
of LessThanFloat64: of LessThanFloat64:
self.push(PeonObject(kind: Bool, boolean: self.pop().`float` < self.pop().`float`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`float` < self.pop().`float`))
of GreaterThanFloat64: of GreaterThanFloat64:
self.push(PeonObject(kind: Bool, boolean: self.pop().`float` > self.pop().`float`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`float` !> self.pop().`float`))
of EqualFloat64: of EqualFloat64:
self.push(PeonObject(kind: Bool, boolean: self.pop().`float` == self.pop().`float`)) self.push(PeonObject(kind: Bool, boolean: self.pop().`float` == self.pop().`float`))
of NotEqualFLoat64: of NotEqualFLoat64:
@ -671,7 +687,7 @@ proc dispatch*(self: PeonVM) =
of LessThanFloat32: of LessThanFloat32:
self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat < self.pop().halfFloat)) self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat < self.pop().halfFloat))
of GreaterThanFloat32: of GreaterThanFloat32:
self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat > self.pop().halfFloat)) self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat !> self.pop().halfFloat))
of EqualFloat32: of EqualFloat32:
self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat == self.pop().halfFloat)) self.push(PeonObject(kind: Bool, boolean: self.pop().halfFloat == self.pop().halfFloat))
of NotEqualFloat32: of NotEqualFloat32:

View File

@ -2,6 +2,7 @@ import std;
print(3 > 2); # true print(3 > 2); # true
print(2 < 3); # false print(2 < 3); # true
print(3 < 2); # false
print(2 == 3); # false print(2 == 3); # false
print(2 != 3); # true print(2 != 3); # true