aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-24 12:04:06 +0200
committerBad Diode <bd@badd10de.dev>2021-10-24 12:04:06 +0200
commit8f9a84345c147da5d398331548753d1e350ce846 (patch)
treed9dba538e0c7bc5c2a2e16e0bcbf59167121b9da /src/bytecode/compiler.h
parent6e27b20d10306d53cd838ef375fe80571dfe91ff (diff)
downloadbdl-8f9a84345c147da5d398331548753d1e350ce846.tar.gz
bdl-8f9a84345c147da5d398331548753d1e350ce846.zip
Add globals and OP_DEF operation
Diffstat (limited to 'src/bytecode/compiler.h')
-rwxr-xr-xsrc/bytecode/compiler.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
index ec51942..8f3fa81 100755
--- a/src/bytecode/compiler.h
+++ b/src/bytecode/compiler.h
@@ -177,6 +177,52 @@ compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) {
177} 177}
178 178
179void 179void
180compile_define_op(Chunk *chunk, Visitor *vs, Token start) {
181 Token name = peek_token(vs);
182 if (name.type != TOKEN_SYMBOL) {
183 error_push((Error){
184 .type = ERR_TYPE_COMPILER,
185 .value = ERR_WRONG_ARG_TYPE,
186 .line = start.line,
187 .col = start.column,
188 });
189 return;
190 }
191 parse_tree(chunk, vs);
192 Token expr = peek_token(vs);
193 if (name.type == TOKEN_EOF || expr.type == TOKEN_EOF) {
194 error_push((Error){
195 .type = ERR_TYPE_COMPILER,
196 .value = ERR_UNBALANCED_PAREN,
197 .line = start.line,
198 .col = start.column,
199 });
200 return;
201 }
202 if (name.type == TOKEN_RPAREN || expr.type == TOKEN_RPAREN) {
203 error_push((Error){
204 .type = ERR_TYPE_COMPILER,
205 .value = ERR_NOT_ENOUGH_ARGS,
206 .line = start.line,
207 .col = start.column,
208 });
209 return;
210 }
211 parse_tree(chunk, vs);
212 if (peek_token(vs).type != TOKEN_RPAREN) {
213 error_push((Error){
214 .type = ERR_TYPE_COMPILER,
215 .value = ERR_TOO_MANY_ARGS,
216 .line = start.line,
217 .col = start.column,
218 });
219 return;
220 }
221 next_token(vs);
222 add_code(chunk, OP_DEF, start.line, start.column);
223}
224
225void
180parse_list(Chunk *chunk, Visitor *vs, Token start) { 226parse_list(Chunk *chunk, Visitor *vs, Token start) {
181 if (!has_next_token(vs)) { 227 if (!has_next_token(vs)) {
182 error_push((Error){ 228 error_push((Error){
@@ -205,6 +251,7 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) {
205 case TOKEN_PRINT: { compile_list_unary_op(chunk, vs, start, OP_PRINT); } break; 251 case TOKEN_PRINT: { compile_list_unary_op(chunk, vs, start, OP_PRINT); } break;
206 case TOKEN_DISPLAY: { compile_list_unary_op(chunk, vs, start, OP_DISPLAY); } break; 252 case TOKEN_DISPLAY: { compile_list_unary_op(chunk, vs, start, OP_DISPLAY); } break;
207 case TOKEN_NEWLINE: { compile_list_simple_op(chunk, vs, start, OP_NEWLINE); } break; 253 case TOKEN_NEWLINE: { compile_list_simple_op(chunk, vs, start, OP_NEWLINE); } break;
254 case TOKEN_DEF: { compile_define_op(chunk, vs, start); } break;
208 default: { 255 default: {
209 error_push((Error){ 256 error_push((Error){
210 .type = ERR_TYPE_COMPILER, 257 .type = ERR_TYPE_COMPILER,
@@ -219,6 +266,9 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) {
219void 266void
220parse_tree(Chunk *chunk, Visitor *vs) { 267parse_tree(Chunk *chunk, Visitor *vs) {
221 Token tok = next_token(vs); 268 Token tok = next_token(vs);
269 if (errors_n != 0) {
270 return;
271 }
222 switch (tok.type) { 272 switch (tok.type) {
223 case TOKEN_FIXNUM: { 273 case TOKEN_FIXNUM: {
224 parse_fixnum(chunk, tok); 274 parse_fixnum(chunk, tok);