diff --git a/vm.c b/vm.c index 0e9a1ac..b955cd2 100644 --- a/vm.c +++ b/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; }