diff options
author | Bad Diode <bd@badd10de.dev> | 2022-02-01 18:36:52 +0100 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-02-01 18:36:52 +0100 |
commit | ee1a5de91c875fb66724dc21c02333bfebe2a812 (patch) | |
tree | d3eaa226816d295bb9dc48a2aed27044832ec413 /src/bytecode/main.c | |
parent | 3156265c7b2da8cc43fee996c0518ea274d39c8a (diff) | |
download | bdl-ee1a5de91c875fb66724dc21c02333bfebe2a812.tar.gz bdl-ee1a5de91c875fb66724dc21c02333bfebe2a812.zip |
Add new syntax to lexer and prepare refactor
Diffstat (limited to 'src/bytecode/main.c')
-rwxr-xr-x | src/bytecode/main.c | 214 |
1 files changed, 0 insertions, 214 deletions
diff --git a/src/bytecode/main.c b/src/bytecode/main.c deleted file mode 100755 index 4b80f71..0000000 --- a/src/bytecode/main.c +++ /dev/null | |||
@@ -1,214 +0,0 @@ | |||
1 | #include <assert.h> | ||
2 | #include <getopt.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | // | ||
8 | // Config. | ||
9 | // | ||
10 | |||
11 | #ifdef DEBUG | ||
12 | #define DEBUG_TRACE_EXECUTION | ||
13 | #endif | ||
14 | |||
15 | #include "vm.h" | ||
16 | #include "errors.c" | ||
17 | #include "chunk.c" | ||
18 | #include "objects.c" | ||
19 | #include "compiler.h" | ||
20 | #include "ops.h" | ||
21 | #include "debug.h" | ||
22 | #include "lexer.c" | ||
23 | #include "read_line.c" | ||
24 | #include "string_view.c" | ||
25 | |||
26 | static VM vm; | ||
27 | |||
28 | void | ||
29 | init(void) { | ||
30 | vm_init(&vm); | ||
31 | } | ||
32 | |||
33 | void | ||
34 | halt(void) { | ||
35 | vm_free(&vm); | ||
36 | } | ||
37 | |||
38 | void | ||
39 | process_source(const StringView *source) { | ||
40 | // Read tokens. | ||
41 | Token *tokens = tokenize(source); | ||
42 | if (errors_n != 0) { | ||
43 | array_free(tokens); | ||
44 | return; | ||
45 | } | ||
46 | |||
47 | // Compile chunk. | ||
48 | Chunk *main = compile(tokens); | ||
49 | if (errors_n != 0) { | ||
50 | chunk_free(main); | ||
51 | array_free(tokens); | ||
52 | return; | ||
53 | } | ||
54 | |||
55 | #ifdef DEBUG | ||
56 | disassemble_chunk(main); | ||
57 | #endif | ||
58 | |||
59 | // Interpret chunk. | ||
60 | Object main_proc = make_lambda(main); | ||
61 | CallFrame frame = (CallFrame){ | ||
62 | .closure = main_proc.closure, | ||
63 | }; | ||
64 | array_push(vm.frames, frame); | ||
65 | vm_interpret(&vm); | ||
66 | |||
67 | // Free resources. | ||
68 | chunk_free(main); | ||
69 | array_free(tokens); | ||
70 | } | ||
71 | |||
72 | #define REPL_PROMPT "bdl> " | ||
73 | |||
74 | void | ||
75 | run_repl(void) { | ||
76 | printf("BDL REPL (Press Ctrl-D or Ctrl-C to exit)\n"); | ||
77 | while (true) { | ||
78 | printf(REPL_PROMPT); | ||
79 | StringView sv = read_line(); | ||
80 | if (sv.start == NULL) { | ||
81 | return; | ||
82 | } | ||
83 | process_source(&sv); | ||
84 | |||
85 | // Check if there were any errors. | ||
86 | if (errors_n != 0 && !supress_errors) { | ||
87 | for (size_t i = 0; i < errors_n; i++) { | ||
88 | Error err = errors[i]; | ||
89 | for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) { | ||
90 | putchar(' '); | ||
91 | } | ||
92 | printf("|\n"); | ||
93 | for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) { | ||
94 | putchar(' '); | ||
95 | } | ||
96 | printf("%s\n", error_msgs[err.value]); | ||
97 | } | ||
98 | errors_n = 0; | ||
99 | continue; | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | |||
104 | void | ||
105 | run_file(char *file_name) { | ||
106 | FILE *file = fopen(file_name, "r"); | ||
107 | if (!file) { | ||
108 | fprintf(stderr, "error: couldn't open input file: %s\n", file_name); | ||
109 | exit(EXIT_FAILURE); | ||
110 | } | ||
111 | |||
112 | // Read entire file into memory. | ||
113 | fseek(file, 0, SEEK_END); | ||
114 | size_t file_size = ftell(file); | ||
115 | fseek(file, 0, SEEK_SET); | ||
116 | |||
117 | char *source = malloc(file_size + 1); | ||
118 | fread(source, 1, file_size, file); | ||
119 | source[file_size] = 0; | ||
120 | |||
121 | StringView sv = (StringView){ | ||
122 | .start = source, | ||
123 | .n = file_size, | ||
124 | }; | ||
125 | |||
126 | process_source(&sv); | ||
127 | |||
128 | // Check if there were any errors. | ||
129 | if (errors_n != 0 && !supress_errors) { | ||
130 | report_errors(file_name); | ||
131 | } | ||
132 | |||
133 | free(source); | ||
134 | fclose(file); | ||
135 | } | ||
136 | |||
137 | #define STDIN_BUF_CAP 16 | ||
138 | |||
139 | void | ||
140 | run_stdin(void) { | ||
141 | size_t buf_size = 0; | ||
142 | char *source = NULL; | ||
143 | array_init(source, STDIN_BUF_CAP); | ||
144 | |||
145 | char c; | ||
146 | while ((c = getchar()) != EOF) { | ||
147 | array_push(source, c); | ||
148 | buf_size++; | ||
149 | } | ||
150 | |||
151 | StringView sv = (StringView){ | ||
152 | .start = source, | ||
153 | .n = buf_size, | ||
154 | }; | ||
155 | |||
156 | process_source(&sv); | ||
157 | |||
158 | // Check if there were any errors. | ||
159 | if (errors_n != 0 && !supress_errors) { | ||
160 | report_errors("stdin"); | ||
161 | } | ||
162 | |||
163 | array_free(source); | ||
164 | } | ||
165 | |||
166 | #ifndef BIN_NAME | ||
167 | #define BIN_NAME "bdl" | ||
168 | #endif | ||
169 | |||
170 | void | ||
171 | print_usage(void) { | ||
172 | printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME); | ||
173 | printf("\n"); | ||
174 | printf("\t-i\tInteractive mode (REPL).\n"); | ||
175 | printf("\n"); | ||
176 | } | ||
177 | |||
178 | int | ||
179 | main(int argc, char *argv[]) { | ||
180 | init(); | ||
181 | |||
182 | int option; | ||
183 | while ((option = getopt(argc, argv, "i")) != -1) { | ||
184 | switch (option) { | ||
185 | case 'i': { | ||
186 | // Interactive mode. | ||
187 | run_repl(); | ||
188 | halt(); | ||
189 | return EXIT_SUCCESS; | ||
190 | } break; | ||
191 | default: { | ||
192 | print_usage(); | ||
193 | return EXIT_FAILURE; | ||
194 | } break; | ||
195 | } | ||
196 | } | ||
197 | |||
198 | // Run from stdin. | ||
199 | if (optind == argc) { | ||
200 | run_stdin(); | ||
201 | halt(); | ||
202 | return EXIT_SUCCESS; | ||
203 | } | ||
204 | |||
205 | // Run from file. | ||
206 | while (optind < argc) { | ||
207 | char *file_name = argv[optind]; | ||
208 | run_file(file_name); | ||
209 | optind++; | ||
210 | } | ||
211 | |||
212 | halt(); | ||
213 | return EXIT_SUCCESS; | ||
214 | } | ||