marCsweep/vm.c

38 lines
989 B
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include "vm.h"
struct virtualMachine *initVM() {
struct virtualMachine *vm = malloc(sizeof(struct virtualMachine));
vm->stackSize = 0;
return vm;
}
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);
}
vm->stack[vm->stackSize++] = 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);
}
return vm->stack[--vm->stackSize];
}
struct garbageObject *initGarbage(struct virtualMachine *vm,
enum garbageData type) {
struct garbageObject *obj = malloc(sizeof(struct garbageObject));
obj->type = type;
return obj;
}