This commit is contained in:
Simon Forman 2023-02-07 17:44:32 -08:00
parent b57cfdf825
commit 0c8e49d591
1 changed files with 24 additions and 19 deletions

View File

@ -364,9 +364,9 @@ print_stack(JoyList el)
JoyList JoyList
parse_list(char **text) parse_list(char **text)
{ {
/* /*
Extract terms from the text until a closing bracket is found. * Extract terms from the text until a closing bracket is found.
*/ */
char *rest; char *rest;
ptrdiff_t diff; ptrdiff_t diff;
JoyList result = EMPTY_LIST; JoyList result = EMPTY_LIST;
@ -386,12 +386,12 @@ Extract terms from the text until a closing bracket is found.
/* Look for blanks or brackets. */ /* Look for blanks or brackets. */
rest = strpbrk(*text, " []"); rest = strpbrk(*text, " []");
/* /* rest now points to a space or '[' or ']' after a term, -or- it
rest now points to a space or '[' or ']' after a term, * is NULL if the rest of the string is a single term with no
-or- it is NULL if the rest of the string is a single term * spaces nor brackets. If that's the case then we're missing a
with no spaces nor brackets. If that's the case then we're * closing bracket!
missing a closing bracket! */
*/
if (NULL == rest) { if (NULL == rest) {
printf("Missing ']' bracket. C\n"); printf("Missing ']' bracket. C\n");
longjmp(jbuf, 1); longjmp(jbuf, 1);
@ -766,12 +766,23 @@ truthy(JoyListPtr stack, __attribute__((unused)) JoyListPtr expression)
*/ */
void
dispatch(char *sym, JoyListPtr stack, JoyListPtr expression)
{
const struct dict_entry *word = in_word_set(sym, strlen(sym));
if (!word) {
printf("Unknown: %s\n", sym);
longjmp(jbuf, 1);
}
/* longjmp() is as good as return, no need for else clause. */
word->func(stack, expression);
}
void void
joy(JoyListPtr stack, JoyListPtr expression) joy(JoyListPtr stack, JoyListPtr expression)
{ {
char *sym;
JoyTypePtr term; JoyTypePtr term;
const struct dict_entry *interned;
JoyList e = EMPTY_LIST; JoyList e = EMPTY_LIST;
JoyListPtr ePtr = &e; JoyListPtr ePtr = &e;
push_quote_onto_expression(*expression, ePtr); push_quote_onto_expression(*expression, ePtr);
@ -786,19 +797,13 @@ joy(JoyListPtr stack, JoyListPtr expression)
case joyList: case joyList:
push_thing(term, stack); push_thing(term, stack);
break; break;
case joySymbol: case joySymbol:
sym = term->value.symbol; dispatch(term->value.symbol, stack, expression);
interned = in_word_set(sym, strlen(sym));
if (!interned) {
printf("Unknown: %s\n", sym);
longjmp(jbuf, 1);
}
interned->func(stack, expression);
} }
} }
} }
int int
main(void) main(void)
{ {