mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-13 15:50:50 -05:00
51 lines
1.2 KiB
C
51 lines
1.2 KiB
C
#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");
|
|
return;
|
|
}
|
|
vm->stack[vm->stackSize++] = value;
|
|
}
|
|
|
|
struct garbageObject *pop(struct virtualMachine *vm) {
|
|
if (vm->stackSize < 0) {
|
|
fprintf(stderr,
|
|
"ERROR: pop(): Stack size cannot be negative!");
|
|
return NULL;
|
|
}
|
|
|
|
return vm->stack[--vm->stackSize];
|
|
}
|
|
|
|
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;
|
|
}
|