This commit is contained in:
Simon Forman 2023-03-20 16:33:11 -07:00
parent 290dec513d
commit 242c98edc4
1 changed files with 21 additions and 2 deletions

View File

@ -560,6 +560,7 @@ text_to_expression(char *str)
return top; return top;
} }
/* /*
In order to return two "pointers" I'm going to just OR them In order to return two "pointers" I'm going to just OR them
into one u64 value. It might be conceptually cleaner to define 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("pop") { stack = pop(stack); }
else MATCH("dup") { stack = dup(stack); } else MATCH("dup") { stack = dup(stack); }
else MATCH("stack") { stack = cons(stack, stack); } else MATCH("stack") { stack = cons(stack, stack); }
// first, rest, swap, ... else MATCH("swap") { stack = swap(stack); }
// first, rest, ...
//else MATCH("") { stack = (stack); } //else MATCH("") { stack = (stack); }
CHECK_ERROR CHECK_ERROR
//print_str(symbol);print_endl(); //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 u32
joy(u32 stack, u32 expression) joy(u32 stack, u32 expression)
{ {
@ -670,7 +689,7 @@ main()
print_endl(); 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 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"); //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); print_joy_list(expression);