64 lines
1.1 KiB
C
64 lines
1.1 KiB
C
#include "compiler.h"
|
|
#include "debug.h"
|
|
#include "parser.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
|
|
|
|
VM vm = {0};
|
|
|
|
void mainloop() {
|
|
if (!step_vm(&vm)) {
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_cancel_main_loop(); /* this should "kill" the app. */
|
|
#else
|
|
core_dump(&vm);
|
|
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);
|
|
}
|
|
|
|
initTokenMap();
|
|
compile(vm.memory, buffer);
|
|
/* demo_add_compile(vm.memory); */
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten_set_main_loop(mainloop, 0, 1);
|
|
#else
|
|
while (1) {
|
|
mainloop();
|
|
}
|
|
#endif
|
|
}
|