made less/greater implementation consistent

This commit is contained in:
prod2 2022-01-27 06:09:04 +01:00
parent 215d1539de
commit 6cc2406d5f
3 changed files with 21 additions and 24 deletions

View File

@ -647,13 +647,13 @@ proc parseFunct(comp: Compiler) =
comp.endScope()
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over
# get konvalue functions:
let konFunct = newNdFunction(functII)
# get ndvalue functions:
let ndFunct = newNdFunction(functII)
# end of function declaration:
comp.patchJump(jumpOverBody)
# put the fn object on the stack
comp.writeConstant(konFunct)
comp.writeConstant(ndFunct)
tkFunct.genRule(parseFunct, nop, pcNone)

View File

@ -141,14 +141,16 @@ proc divide*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
return natError(&"Attempt to divide types {val.ndType} and {right.ndType}.")
return natOk
proc `<`*(val: NdValue, right: NdValue): NdValue =
proc less*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
return toNdValue(val.floatValue < right.floatValue)
val = toNdValue(val.floatValue < right.floatValue)
else:
return ndError(&"Attempt to compare types {val.ndType} and {right.ndType}.")
return natError(&"Attempt to compare types {val.ndType} and {right.ndType}.")
return natOk
proc `>`*(val: NdValue, right: NdValue): NdValue =
proc greater*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
return toNdValue(val.floatValue > right.floatValue)
val = toNdValue(val.floatValue > right.floatValue)
else:
return ndError(&"Attempt to compare types {val.ndType} and {right.ndType}.")
return natError(&"Attempt to compare types {val.ndType} and {right.ndType}.")
return natOk

25
vm.nim
View File

@ -46,18 +46,6 @@ proc run*(chunk: Chunk): InterpretResult =
for i in countup(1, amt):
discard stack.pop()
proc pushSafe(stack: var seq[NdValue], val: NdValue): bool =
## DEPRECATED
## returns if the value is not a runtime error
## prints the error if it is a runtime error
## pushes it to the stack if it is not an error
if val.isError():
runtimeError($val)
false
else:
stack.add(val)
true
proc readUI8(): int =
result = ip[].int
ip = ip.padd(1)
@ -72,6 +60,7 @@ proc run*(chunk: Chunk): InterpretResult =
template frameBottom: int = frames[frames.high].stackBottom
while true:
{.computedgoto.} # See https://nim-lang.org/docs/manual.html#pragmas-computedgoto-pragma
@ -108,7 +97,7 @@ proc run*(chunk: Chunk): InterpretResult =
let val: NdValue = readConstant()
stack.add(val)
of opNegate:
let res = stack.peek().negate
let res = stack.peek().negate()
if not res.ok:
runtimeError(res.msg)
break
@ -152,10 +141,16 @@ proc run*(chunk: Chunk): InterpretResult =
of opEqual:
stack.add(toNdValue(stack.pop().equal(stack.pop())))
of opLess:
if not stack.pushSafe(stack.pop() > stack.pop()):
let right = stack.pop()
let res = stack.peek().less(right)
if not res.ok:
runtimeError(res.msg)
break
of opGreater:
if not stack.pushSafe(stack.pop() < stack.pop()):
let right = stack.pop()
let res = stack.peek().greater(right)
if not res.ok:
runtimeError(res.msg)
break
of opPrint:
echo $stack.peek()