-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
89 lines (72 loc) · 1.73 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <clocks/chunk.h>
#include <clocks/common.h>
#include <clocks/debug.h>
#include <clocks/vm.h>
#include <linenoise/linenoise.h>
static void repl()
{
linenoiseHistoryLoad(".clocks_history");
char* line = NULL;
while ((line = linenoise("clocks > ")) != NULL)
{
linenoiseHistoryAdd(line);
linenoiseHistorySave(".clocks_history");
interpret(line);
}
free(line);
}
static char* read_file(const char* path)
{
FILE* file = fopen(path, "rb");
if (file == NULL)
{
fprintf(stderr, "Could not open file \"%s\".\n", path);
exit(74);
}
fseek(file, 0L, SEEK_END);
const size_t file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
char* buffer = (char*)malloc(file_size + 1);
if (buffer == NULL)
{
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
exit(74);
}
const size_t bytes_read = fread(buffer, sizeof(char), file_size, file);
if (bytes_read < file_size)
{
fprintf(stderr, "Could not read file \"%s\".\n", path);
exit(74);
}
buffer[bytes_read] = '\0';
fclose(file);
return buffer;
}
static void run_file(const char* path)
{
char* source = read_file(path);
const InterpretResult result = interpret(source);
free(source);
if (result == InterpretCompileError)
exit(65);
if (result == InterpretRuntimeError)
exit(70);
}
int main(int argc, const char* argv[])
{
init_vm();
if (argc == 1)
repl();
else if (argc == 2)
run_file(argv[1]);
else
{
fprintf(stderr, "Usage: clocks [path]\n");
exit(64);
}
free_vm();
return 0;
}