mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-12 15:20:49 -05:00
VM: added definitions for init, push, and pop
This commit is contained in:
parent
d33e6c8e99
commit
8d7a24bfda
1 changed files with 37 additions and 0 deletions
37
vm.c
Normal file
37
vm.c
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue