From 23cf3f5b7037cb0a8aaad997d4f0c2e7e2d3d8bb Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 14 Feb 2023 14:19:57 -0800 Subject: [PATCH] lshift and rshift for Nim. Incomplete, should reject negative shift count. --- implementations/Nim/joy.nim | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/implementations/Nim/joy.nim b/implementations/Nim/joy.nim index 7bd03f3..8570b98 100644 --- a/implementations/Nim/joy.nim +++ b/implementations/Nim/joy.nim @@ -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: