-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
72 lines (62 loc) · 1.71 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "src/compiler/chunk.h"
#include "src/common/common.h"
#include "src/common/os.h"
#include "src/inc/ini.h"
#include "src/vm/debug.h"
#include "src/vm/vm.h"
static void repl(VM* vm) {
printf("REPL for CLox version %s\n", vm->config.version);
vm->currentModule = newModule(vm, emptyString(vm));
char line[1024];
for (;;) {
printf("> ");
if (!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}
interpret(vm, line);
}
}
static void runFile(VM* vm, const char* filePath) {
ObjString* path = newString(vm, filePath);
vm->currentModule = newModule(vm, path);
char* source = readFile(filePath);
InterpretResult result = interpret(vm, source);
free(source);
if (result == INTERPRET_COMPILE_ERROR) exit(65);
if (result == INTERPRET_RUNTIME_ERROR) exit(70);
}
static void runScript(VM* vm, const char* path, const char* script) {
char scriptPath[UINT8_MAX];
if (strlen(path) + strlen(script) > UINT8_MAX) {
printf("file path/name too long...");
exit(74);
}
int length = sprintf_s(scriptPath, UINT8_MAX, "%s%s", path, script);
runFile(vm, scriptPath);
}
int main(int argc, char* argv[]) {
VM vm;
initVM(&vm);
runAtStartup();
atexit(runAtExit);
if (strlen(vm.config.script) > 0) {
runScript(&vm, vm.config.path, vm.config.script);
}
else if (argc == 1) {
repl(&vm);
}
else if (argc == 2) {
runFile(&vm, argv[1]);
}
else {
fprintf(stderr, "Usage: clox [path]\n");
exit(64);
}
uv_run(vm.eventLoop, UV_RUN_DEFAULT);
freeVM(&vm);
return 0;
}