lshift and rshift for Nim.

Incomplete, should reject negative shift count.
This commit is contained in:
Simon Forman 2023-02-14 14:19:57 -08:00
parent c2eae95bb2
commit 23cf3f5b70
1 changed files with 18 additions and 0 deletions

View File

@ -512,6 +512,23 @@ proc joy_eval(sym: string, stack: JoyListType, expression: JoyListType, dictiona
let (b, s1) = pop_int(s0)
return (push_bool(b == a, s1), expression, dictionary)
of "lshift":
let (a, s0) = pop_int(stack)
let (b, s1) = pop_int(s0)
# I couldn't get toInt[int](a) to compile.
# > Nim Error: cannot instantiate: 'toInt[int]'; got 1 typeof(s) but expected 0
# So just convert to string and back to int, and hope for the best...
let n = parseInt($a)
let i = b shl n
return (push_int(i, s1), expression, dictionary)
of "rshift":
let (a, s0) = pop_int(stack)
let (b, s1) = pop_int(s0)
let n = parseInt($a)
let i = b shr n
return (push_int(i, s1), expression, dictionary)
of "branch":
return branch(stack, expression, dictionary)
of "clear":
@ -604,6 +621,7 @@ for line in defs_text.splitLines:
continue
add_def(line, d)
var s = empty_list.listVal
var exp: JoyListType
while true: