reality-engine/src/arch/linux/main.c

72 lines
1.3 KiB
C

#include "../../compiler.h"
#include "../../vm.h"
#define MAX_SRC_SIZE 16384
void compileFile(const char *path, VM *vm) {
FILE *f = fopen(path, "rb");
if (!f) {
perror("fopen");
exit(1);
}
static char source[MAX_SRC_SIZE + 1];
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
if (len >= MAX_SRC_SIZE) {
perror("source is larget than buffer");
exit(1);
}
size_t read = fread(source, 1, len, f);
source[read] = '\0';
fclose(f);
compile(source, vm);
}
static void repl(VM *vm) {
char line[1024];
for (;;) {
printf("> ");
if (!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}
/* reset the code counter to 0 */
vm->cp = 0;
vm->sp = 0;
vm->pc = 0;
vm->mp = 0;
compile(line, vm);
while (step_vm(vm));
}
exit(0);
}
int main(int argc, char **argv) {
VM vm = {0};
vm.frames_size = FRAMES_SIZE;
vm.return_stack_size = STACK_SIZE;
vm.stack_size = STACK_SIZE;
vm.memory_size = MEMORY_SIZE;
if (argc == 1) {
repl(&vm);
} else if (argc == 2) {
compileFile(argv[1], &vm);
} else {
fprintf(stderr, "Usage: %s <file.zrl>\n", argv[0]);
return 64;
}
bool running = true;
while (running) {
running = step_vm(&vm);
}
return 0;
}