-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
65 lines (53 loc) · 1.59 KB
/
main.c
File metadata and controls
65 lines (53 loc) · 1.59 KB
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
#include "vm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *read_file(const char *path) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Could not open file '%s'.\n", path);
return NULL;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);
char *buffer = malloc((size_t)size + 1);
size_t bytes_read = fread(buffer, 1, (size_t)size, file);
buffer[bytes_read] = '\0';
fclose(file);
return buffer;
}
static int run_string(const char *source, bool jit) {
lisa_vm vm;
lisa_vm_init(&vm);
vm.jit_enabled = jit;
lisa_interpret_result result = lisa_interpret(&vm, source);
lisa_vm_free(&vm);
if (result == INTERPRET_COMPILE_ERROR) return 65;
if (result == INTERPRET_RUNTIME_ERROR) return 70;
return 0;
}
static int run_file(const char *path, bool jit) {
char *source = read_file(path);
if (source == NULL) return 74;
int result = run_string(source, jit);
free(source);
return result;
}
int main(int argc, char *argv[]) {
bool jit = true;
int argi = 1;
if (argi < argc && strcmp(argv[argi], "--no-jit") == 0) {
jit = false;
argi++;
}
if (argi < argc && argi == argc - 1 && strcmp(argv[argi], "-e") != 0) {
return run_file(argv[argi], jit);
}
if (argi + 1 < argc && strcmp(argv[argi], "-e") == 0) {
return run_string(argv[argi + 1], jit);
}
fprintf(stderr, "Usage: lisa [--no-jit] <file.lisa>\n");
fprintf(stderr, " lisa [--no-jit] -e \"<expression>\"\n");
return 64;
}