Node head is pointer now.

This commit is contained in:
Simon Forman 2023-02-04 08:12:30 -08:00
parent 2e4381b2c0
commit 71c25e7bbd
2 changed files with 35 additions and 29 deletions

View File

@ -56,9 +56,10 @@ JoyList
push_integer_from_str(char *str, JoyList tail) push_integer_from_str(char *str, JoyList tail)
{ {
JoyList el = newJoyList; JoyList el = newJoyList;
el->head.kind = joyInt; el->head = newJoyType;
mpz_init_set_str(el->head.value.i, str, 10); el->head->kind = joyInt;
GC_register_finalizer(el->head.value.i, my_callback, NULL, NULL, NULL); mpz_init_set_str(el->head->value.i, str, 10);
GC_register_finalizer(el->head->value.i, my_callback, NULL, NULL, NULL);
el->tail = tail; el->tail = tail;
return el; return el;
} }
@ -100,7 +101,7 @@ void
print_list(JoyList el) print_list(JoyList el)
{ {
while (NULL != el) { while (NULL != el) {
print_node(el->head); print_node(*(el->head));
el = el->tail; el = el->tail;
if (NULL != el) { if (NULL != el) {
printf(" "); printf(" ");
@ -123,11 +124,12 @@ make_non_list_node(char *text, size_t size)
char *sym; char *sym;
const struct dict_entry *interned; const struct dict_entry *interned;
JoyList node = newJoyList; JoyList node = newJoyList;
node->head = newJoyType;
interned = in_word_set(text, size); interned = in_word_set(text, size);
if (interned) { if (interned) {
node->head.kind = joySymbol; node->head->kind = joySymbol;
node->head.value.symbol = interned->name; node->head->value.symbol = interned->name;
return node; return node;
} }
@ -136,22 +138,22 @@ make_non_list_node(char *text, size_t size)
if (!strncmp(sym, FALSE, 6)) { /* I know it's wrong to hardcode the length here. Sorry. */ if (!strncmp(sym, FALSE, 6)) { /* I know it's wrong to hardcode the length here. Sorry. */
/* If head was a pointer we could reuse Boolean singletons... */ /* If head was a pointer we could reuse Boolean singletons... */
node->head.kind = joyFalse; node->head->kind = joyFalse;
node->head.value.boolean = 0; node->head->value.boolean = 0;
} else if (!strncmp(sym, TRUE, 5)) { /* I know it's wrong to hardcode the length here. Sorry. */ } else if (!strncmp(sym, TRUE, 5)) { /* I know it's wrong to hardcode the length here. Sorry. */
node->head.kind = joyTrue; node->head->kind = joyTrue;
node->head.value.boolean = 1; node->head->value.boolean = 1;
} else if (mpz_init_set_str(node->head.value.i, sym, 10)) { } else if (mpz_init_set_str(node->head->value.i, sym, 10)) {
/* Non-zero (-1) return value means the string is not an int. */ /* Non-zero (-1) return value means the string is not an int. */
mpz_clear(node->head.value.i); mpz_clear(node->head->value.i);
node->head.kind = joySymbol; node->head->kind = joySymbol;
node->head.value.symbol = sym; node->head->value.symbol = sym;
} else { } else {
node->head.kind = joyInt; node->head->kind = joyInt;
GC_register_finalizer(node->head.value.i, my_callback, NULL, NULL, NULL); GC_register_finalizer(node->head->value.i, my_callback, NULL, NULL, NULL);
} }
return node; return node;
@ -162,8 +164,9 @@ JoyList
make_list_node(JoyList el) make_list_node(JoyList el)
{ {
JoyList node = newJoyList; JoyList node = newJoyList;
node->head.kind = joyList; node->head = newJoyType;
node->head.value.el = el; node->head->kind = joyList;
node->head->value.el = el;
return node; return node;
} }
@ -318,9 +321,9 @@ mpz_t *
pop_int(JoyListPtr stack) { pop_int(JoyListPtr stack) {
JoyList node; JoyList node;
node = pop_any(stack); node = pop_any(stack);
switch (node->head.kind) { switch (node->head->kind) {
case joyInt: case joyInt:
return &(node->head.value.i); return &(node->head->value.i);
default: default:
printf("Not an integer.\n"); printf("Not an integer.\n");
exit(1); exit(1);
@ -331,9 +334,10 @@ pop_int(JoyListPtr stack) {
JoyList JoyList
newIntNode(void) { newIntNode(void) {
JoyList node = newJoyList; JoyList node = newJoyList;
node->head.kind = joyInt; node->head = newJoyType;
mpz_init(node->head.value.i); node->head->kind = joyInt;
GC_register_finalizer(node->head.value.i, my_callback, NULL, NULL, NULL); mpz_init(node->head->value.i);
GC_register_finalizer(node->head->value.i, my_callback, NULL, NULL, NULL);
return node; return node;
} }
@ -346,7 +350,7 @@ name(JoyListPtr stack, __attribute__((unused)) JoyListPtr expression) \
a = pop_int(stack); \ a = pop_int(stack); \
b = pop_int(stack); \ b = pop_int(stack); \
node = newIntNode(); \ node = newIntNode(); \
mpz_ ## name(node->head.value.i, *a, *b); \ mpz_ ## name(node->head->value.i, *a, *b); \
node->tail = *stack; \ node->tail = *stack; \
*stack = node; \ *stack = node; \
} }
@ -373,9 +377,9 @@ void truthy(JoyListPtr stack, JoyListPtr expression) {stack = expression;}
void void
push_thing(JoyType *term, JoyListPtr stack) { push_thing(JoyTypePtr term, JoyListPtr stack) {
JoyList node = newJoyList; JoyList node = newJoyList;
node->head = *term; /* Copies data, right? */ node->head = term;
node->tail = *stack; node->tail = *stack;
*stack = node; *stack = node;
} }
@ -389,7 +393,7 @@ joy(JoyListPtr stack, JoyListPtr expression)
const struct dict_entry *interned; const struct dict_entry *interned;
while (*expression) { while (*expression) {
term = &((*expression)->head); term = (*expression)->head;
*expression = (*expression)->tail; *expression = (*expression)->tail;
switch (term->kind) { switch (term->kind) {
case joyInt: case joyInt:

View File

@ -41,18 +41,20 @@ typedef struct {
} value; } value;
} JoyType; } JoyType;
typedef JoyType* JoyTypePtr;
struct list_node { struct list_node {
JoyType head; /* Should this be a pointer? */ JoyTypePtr head;
JoyList tail; JoyList tail;
}; };
#define EMPTY_LIST (JoyList)NULL #define EMPTY_LIST (JoyList)NULL
#define newJoyList GC_malloc(sizeof(struct list_node)) #define newJoyList GC_malloc(sizeof(struct list_node))
#define newJoyType GC_malloc(sizeof(JoyType))
typedef void (*JoyFunc)(struct list_node**, struct list_node**); typedef void (*JoyFunc)(JoyListPtr, JoyListPtr);
void add(JoyListPtr stack, JoyListPtr expression); void add(JoyListPtr stack, JoyListPtr expression);