branch combinator.

This commit is contained in:
Simon Forman 2023-02-04 19:19:48 -08:00
parent 21f99aac96
commit c3e786aed9
2 changed files with 29 additions and 11 deletions

View File

@ -164,8 +164,7 @@ pop_any(JoyListPtr stack)
mpz_t *
pop_int(JoyListPtr stack)
{
JoyList node;
node = pop_any(stack);
JoyList node = pop_any(stack);
switch (node->head->kind) {
case joyInt:
return &(node->head->value.i);
@ -176,6 +175,23 @@ pop_int(JoyListPtr stack)
}
int
pop_bool(JoyListPtr stack)
{
JoyList node = pop_any(stack);
/* TODO: Look for just the singletons? */
switch (node->head->kind) {
case joyTrue:
return 1;
case joyFalse:
return 0;
default:
printf("Not a Boolean.\n");
exit(1);
}
}
JoyList
pop_list(JoyListPtr stack)
{
@ -536,13 +552,7 @@ cmp_joyfunc(JoyListPtr stack, JoyListPtr expression)
b = pop_int(stack);
a = pop_int(stack);
hmm = mpz_cmp(*a, *b);
if (hmm > 0) {
push_quote(G, expression);
} else if (hmm < 0) {
push_quote(L, expression);
} else {
push_quote(E, expression);
}
push_quote(((hmm > 0) ? G : (hmm < 0) ? L : E), expression);
}
@ -553,7 +563,16 @@ i_joyfunc(JoyListPtr stack, JoyListPtr expression)
}
void branch(JoyListPtr stack, JoyListPtr expression) {stack = expression;}
void
branch(JoyListPtr stack, JoyListPtr expression)
{
JoyList T, F;
T = pop_list_node(stack);
F = pop_list_node(stack);
push_quote((pop_bool(stack) ? T : F), expression);
}
void clear(JoyListPtr stack, JoyListPtr expression) {stack = expression;}
void div_joyfunc(JoyListPtr stack, JoyListPtr expression) {stack = expression;}
void mod(JoyListPtr stack, JoyListPtr expression) {stack = expression;}

View File

@ -15,7 +15,6 @@ cons
dip
dup
first
i
loop
pop
rest