Implement 5.52 translate Scheme to C

This commit is contained in:
2021-06-12 19:49:53 -04:00
parent 15057b52d4
commit 90a1f8a573
12 changed files with 808 additions and 199 deletions

41
shared/scm2c/stack.c Normal file
View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct stack proc_stack;
stack* create_stack(void) {
stack* s = malloc(sizeof(stack));
if (!s) exit(-1);
s->first = NULL;
return s;
}
void save(void *d, stack *s) {
stack_elem* elem = malloc(sizeof(stack_elem));
elem->value = d;
if (!s->first) {
elem->next = NULL;
} else {
elem->next = s->first;
}
s->first = elem;
}
void* restore(stack *s) {
void *r;
stack_elem *se;
if (!s->first) {
printf("stack empty!\n");
exit(-1);
}
se = s->first;
r = s->first->value;
s->first = se->next;
free(se);
return r;
}