VM: added definitions for init, push, and pop

This commit is contained in:
Andrew Scott 2024-08-19 15:45:23 -04:00
parent d33e6c8e99
commit 8d7a24bfda
Signed by: a
GPG key ID: 7CD5A5977E4931C1

37
vm.c Normal file
View file

@ -0,0 +1,37 @@
#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;
}