From 242c98edc4ab44e8de2e3888df91412965fe9bfe Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 20 Mar 2023 16:33:11 -0700 Subject: [PATCH] swap --- implementations/uvm-ncc/joy.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/implementations/uvm-ncc/joy.c b/implementations/uvm-ncc/joy.c index a03d5ff..ca6a01b 100644 --- a/implementations/uvm-ncc/joy.c +++ b/implementations/uvm-ncc/joy.c @@ -560,6 +560,7 @@ text_to_expression(char *str) return top; } + /* In order to return two "pointers" I'm going to just OR them into one u64 value. It might be conceptually cleaner to define @@ -583,7 +584,8 @@ joy_eval(char *symbol, u32 stack, u32 expression) else MATCH("pop") { stack = pop(stack); } else MATCH("dup") { stack = dup(stack); } else MATCH("stack") { stack = cons(stack, stack); } - // first, rest, swap, ... + else MATCH("swap") { stack = swap(stack); } + // first, rest, ... //else MATCH("") { stack = (stack); } CHECK_ERROR //print_str(symbol);print_endl(); @@ -618,6 +620,23 @@ dup(u32 stack) } +u32 +swap(u32 stack) +{ + u32 tos = pop_any(stack); + CHECK_ERROR + stack = tail(stack); + u32 second = pop_any(stack); + CHECK_ERROR + stack = tail(stack); + stack = cons(tos, stack); + CHECK_ERROR + stack = cons(second, stack); + CHECK_ERROR + return stack; +} + + u32 joy(u32 stack, u32 expression) { @@ -670,7 +689,7 @@ main() print_endl(); */ - u32 expression = text_to_expression("1 2 3 stack dup swaack"); + u32 expression = text_to_expression("1 2 3 stack dup swaack swap"); //u32 expression = text_to_expression("1 2 3 clear 4 5 6"); //u32 expression = text_to_expression(" 1[2[true 3][aa[aa bb] aa bb cc]bob]false[]bob 3[4] ga[]ry"); print_joy_list(expression);