-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
102 lines (91 loc) · 2.39 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
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef COMMON_H
#include "common.h"
#endif
#ifndef ERROR_C
#include "error.c"
#endif
#ifndef TOKEN_C
#include "token.c"
#endif
#ifndef PARSER_C
#include "parser.c"
#endif
#ifndef SCANNER_C
#include "scanner.c"
#endif
#define BUFFER_MAX_LEN 65536
#define BUFFER_MAX_LINES 4096
#define MAX_TOKENS 1024
int read_file(char *restrict buf, const size_t n, const char *restrict path)
{
FILE *file = fopen(path, "rb");
if (!file) {
fprintf(stderr, "Could not find file \"%s\".\n", path);
exit(ERR_FILE);
}
size_t sz = fsize(file);
size_t count = (n <= sz ? n - 1 : sz);
size_t bytes_read = fread(buf, sizeof(char), count, file);
if (bytes_read < count) {
fprintf(stderr, "Could not read file \"%s\".\n", path);
fclose(file);
exit(ERR_FILE);
}
buf[bytes_read] = '\0';
fclose(file);
return (int)bytes_read;
}
void print(Expr *e)
{
static char *buf = NULL;
if (e) {
arr_reset(buf);
buf = expr_sprint(buf, e);
printf("%.*s\n", arr_count(buf), buf);
}
}
Expr *eval(Buffer *restrict b, Scanner *restrict s, Parser *restrict p, Token *restrict ts)
{
return parse(p, scan(s, b, ts));
}
void eval_file(Buffer *restrict b, Scanner *restrict s, Parser *restrict p,
Token *restrict ts, const char *restrict path)
{
// TODO use arr instead?
// b->name = path;
b->len = read_file(b->head, BUFFER_MAX_LEN, path);
Expr *e = eval(b, s, p, ts);
if (had_error) {
exit(ERR_COMPILE);
}
print(e);
}
void repl(Buffer *restrict b, Scanner *restrict s, Parser *restrict p, Token *restrict ts)
{
b->name = "repl";
for (;;) {
fputs(ANSI_BOLD "loxy> " ANSI_RESET, stdout);
if (!fgets(b->head, BUFFER_MAX_LEN, stdin)) {
fputs(ANSI_RESET "\n", stdout);
break;
}
if (b->head[0] != '\n') {
print(eval(b, s, p, ts));
}
buffer_reset(b);
had_error = false;
}
}
int main(int argc, const char *argv[])
{
Buffer b;
buffer_init(&b, BUFFER_MAX_LEN, BUFFER_MAX_LINES);
Scanner *s = malloc(sizeof(Scanner));
Parser *p = malloc(sizeof(Parser));
Token *ts = malloc(sizeof(Token) * MAX_TOKENS);
switch (argc) {
case 1: repl(&b, s, p, ts); break;
case 2: eval_file(&b, s, p, ts, argv[1]); break;
default: fputs("Usage: loxy [path]\n", stderr); return ERR_USAGE;
}
}