mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-12 15:20:49 -05:00
VM: return instead of exit on stack overflow/underflow
This commit is contained in:
parent
f1b5822a81
commit
d89f7e13cf
1 changed files with 20 additions and 6 deletions
26
vm.c
26
vm.c
|
@ -11,9 +11,8 @@ struct virtualMachine *initVM() {
|
|||
|
||||
void push(struct virtualMachine *vm, struct garbageObject *value) {
|
||||
if (vm->stackSize >= STACK_MAX) {
|
||||
fprintf(stderr, "ERROR: push(): Refusing to overflow the stack!\n");
|
||||
// FIXME: Must free memory before exiting
|
||||
exit(-1);
|
||||
fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n");
|
||||
return;
|
||||
}
|
||||
vm->stack[vm->stackSize++] = value;
|
||||
}
|
||||
|
@ -21,9 +20,8 @@ void push(struct virtualMachine *vm, struct garbageObject *value) {
|
|||
struct garbageObject *pop(struct virtualMachine *vm) {
|
||||
if (vm->stackSize < 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: pop(): Stack underflow - stack size cannot be negative!");
|
||||
// FIXME: Must free memory before exiting
|
||||
exit(-1);
|
||||
"ERROR: pop(): Stack size cannot be negative!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return vm->stack[--vm->stackSize];
|
||||
|
@ -33,5 +31,21 @@ struct garbageObject *initGarbage(struct virtualMachine *vm,
|
|||
enum garbageData type) {
|
||||
struct garbageObject *obj = malloc(sizeof(struct garbageObject));
|
||||
obj->type = type;
|
||||
obj->mark = 0;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void pushInt(struct virtualMachine *vm, int value) {
|
||||
struct garbageObject *obj = initGarbage(vm, GARBAGE_INT);
|
||||
obj->value = value;
|
||||
push(vm, obj);
|
||||
}
|
||||
|
||||
struct garbageObject *pushPair(struct virtualMachine *vm) {
|
||||
struct garbageObject *obj = initGarbage(vm, GARBAGE_PAIR);
|
||||
obj->tail = pop(vm);
|
||||
obj->head = pop(vm);
|
||||
|
||||
push(vm, obj);
|
||||
return obj;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue