reality-engine/src/main.c

66 lines
1.2 KiB
C

#include "parser.h"
#include "compiler.h"
#include "debug.h"
#include "vm.h"
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
uint32_t pc = 1; /* Program counter */
Code *code;
void mainloop() {
pc = step_vm(code->memory, code->size, pc);
if (pc == 0) {
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop(); /* this should "kill" the app. */
#else
core_dump(code->memory, code->size);
exit(0);
#endif
}
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: zre input.zre");
return EXIT_FAILURE;
}
char *buffer;
int length = 0;
int sp = open(argv[1], O_RDONLY);
if (sp) {
length = lseek(sp, (size_t)0, SEEK_END);
lseek(sp, (size_t)0, SEEK_SET);
buffer = malloc(length);
if (buffer) {
read(sp, buffer, length * sizeof(char));
}
close(sp);
}
new_TokenMap();
code = compile(buffer);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainloop, 0, 1);
#else
while (1) {
mainloop();
}
#endif
}