-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
194 lines (165 loc) · 5.43 KB
/
main.c
File metadata and controls
194 lines (165 loc) · 5.43 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "common.h"
#include "ast.h"
#include "value.h"
#include "vm.h"
#include "token.h"
#include "parse.h"
/* reason the parser failed (used for repl */
enum brd_compiler_status {
BRD_REPL_SUCCESS,
BRD_REPL_TOKEN,
BRD_REPL_PARSER,
};
static char *
brd_read_file(const char *file_name)
{
FILE *file;
char *contents;
size_t length;
file = fopen(file_name, "rb");
if (file == NULL) {
return NULL;
}
if (0 != fseek(file, 0, SEEK_END)) {
return NULL;
}
length = ftell(file);
fseek(file, 0, SEEK_SET);
contents = malloc(length + 1);
contents[fread(contents, sizeof(char), length, file)] = '\0';
fclose(file);
return contents;
}
static int
brd_parse_and_compile_repl(char *code)
{
/* return true upon success */
struct brd_token_list tokens;
struct brd_node *program;
brd_token_list_init(&tokens);
if (!brd_token_list_tokenize(&tokens, code)) {
fprintf(stderr, "Error: %s%s\n", error_message, bad_character);
brd_token_list_destroy(&tokens);
return BRD_REPL_TOKEN;
}
program = brd_parse_program(&tokens);
brd_token_list_destroy(&tokens);
if (program == NULL) {
return BRD_REPL_PARSER;
}
brd_node_compile(program);
brd_node_destroy(program);
/* overwrite the last pop so that it can later be saved into "_" */
vm.bc_length -= sizeof(enum brd_bytecode);
*(enum brd_bytecode *)(vm.bytecode + vm.bc_length - 1) = BRD_VM_RETURN;
return BRD_REPL_SUCCESS;
}
static void
brd_repl(void)
{
char code[1024], extra[512];
int empty_line;
enum brd_compiler_status compiler_status;
printf("Welcome to the repl!\n");
for (;;) {
start_loop:
printf(">>> ");
if (fgets(code, sizeof(code), stdin) == NULL) {
break;
}
empty_line = true;
for (int i = 0; code[i] != '\0'; i++) {
if (!isspace(code[i])) {
empty_line = false;
break;
}
}
if (empty_line) {
continue;
}
compiler_status = brd_parse_and_compile_repl(code);
while (compiler_status != BRD_REPL_SUCCESS) {
if (compiler_status == BRD_REPL_TOKEN) {
goto start_loop;
}
printf(" <| ");
if (fgets(extra, sizeof(extra), stdin) == NULL) {
printf("\nKeyboard Interrupt\n");
clearerr(stdin);
goto start_loop;
}
strcat(code, extra);
compiler_status = brd_parse_and_compile_repl(code);
}
brd_vm_run();
if (brd_stack_peek(&vm.stack)->vtype != BRD_VAL_UNIT) {
struct brd_value *val = brd_stack_pop(&vm.stack);
brd_value_map_set(&vm.frame[0].locals, "_", val);
brd_value_debug(val);
printf("\n");
}
}
printf("\nGoodbye!\n");
}
static void
brd_parse_and_compile(char *code)
{
struct brd_token_list tokens;
struct brd_node *program;
brd_token_list_init(&tokens);
if (!brd_token_list_tokenize(&tokens, code)) {
fprintf(stderr, "Error: %s%s\n", error_message, bad_character);
brd_token_list_destroy(&tokens);
exit(EXIT_FAILURE);
}
program = brd_parse_program(&tokens);
brd_token_list_destroy(&tokens);
if (program == NULL) {
fprintf(stderr, "Error: %s on line %d\n", error_message, line_number);
exit(EXIT_FAILURE);
}
brd_node_compile(program);
brd_node_destroy(program);
}
static void
brd_run_file(char *file_name)
{
char *code;
code = brd_read_file(file_name);
if (code == NULL) {
fprintf(stderr, "Unable to open file %s\n", file_name);
exit(EXIT_FAILURE);
}
brd_parse_and_compile(code);
free(code);
brd_vm_run();
}
const char *help =
"bread usage:\n"
"\n"
" bread Run the bread REPL\n"
" bread --help Print this message and exit\n"
" bread [ file ... ] Run the given files\n"
" bread [ file ... ] - Run the given files, then start a REPL\n"
"\n"
;
int
main(int argc, char **argv)
{
brd_vm_init();
if (argc == 1) {
brd_repl();
} else {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-") == 0) {
brd_repl();
break;
} else if (strcmp(argv[i], "--help") == 0) {
printf("%s", help);
} else {
brd_run_file(argv[i]);
}
}
}
brd_vm_destroy();
}