aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-25 15:46:48 +0200
committerBad Diode <bd@badd10de.dev>2021-10-25 15:46:48 +0200
commitd54e595644fcaf6756d53d368213ad3129c49327 (patch)
treecda267ef5ea0676e82ddbb3ebb09de40d522846a /src/bytecode/compiler.h
parentad8c598e84bd1e5469e2487cc3e4d0ea784d0ff3 (diff)
downloadbdl-d54e595644fcaf6756d53d368213ad3129c49327.tar.gz
bdl-d54e595644fcaf6756d53d368213ad3129c49327.zip
Add initial `fun` declaration compilation
Diffstat (limited to 'src/bytecode/compiler.h')
-rwxr-xr-xsrc/bytecode/compiler.h57
1 files changed, 51 insertions, 6 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
index 4130269..2c7827f 100755
--- a/src/bytecode/compiler.h
+++ b/src/bytecode/compiler.h
@@ -14,7 +14,7 @@ Token next_token(Compiler *compiler);
14Token peek_token(const Compiler *compiler); 14Token peek_token(const Compiler *compiler);
15bool has_next_token(const Compiler *compiler); 15bool has_next_token(const Compiler *compiler);
16 16
17Object compile(Token *tokens); 17Chunk * compile(Token *tokens);
18 18
19Token 19Token
20peek_token(const Compiler *compiler) { 20peek_token(const Compiler *compiler) {
@@ -248,6 +248,49 @@ compile_declare_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) {
248} 248}
249 249
250void 250void
251compile_fun_op(Chunk *chunk, Compiler *compiler, Token start) {
252 Token name = peek_token(compiler);
253 if (name.type != TOKEN_SYMBOL) {
254 error_push((Error){
255 .type = ERR_TYPE_COMPILER,
256 .value = ERR_WRONG_ARG_TYPE,
257 .line = start.line,
258 .col = start.column,
259 });
260 return;
261 }
262 parse_tree(chunk, compiler);
263
264 // TODO: compile lambda expression.
265 Object fun = make_lambda(name.value);
266 // FIXME: skipping arguments for now. Assuming nil.
267 next_token(compiler);
268
269 // Compile body.
270 while (has_next_token(compiler)) {
271 Token tok = peek_token(compiler);
272 if (tok.type == TOKEN_EOF) {
273 error_push((Error){
274 .type = ERR_TYPE_COMPILER,
275 .value = ERR_UNBALANCED_PAREN,
276 .line = start.line,
277 .col = start.column,
278 });
279 return;
280 }
281 if (tok.type == TOKEN_RPAREN) {
282 next_token(compiler);
283 break;
284 }
285 parse_tree(fun.chunk, compiler);
286 }
287 add_code(fun.chunk, OP_RETURN, start.line, start.column);
288
289 emit_constant(chunk, start, fun);
290 add_code(chunk, OP_DEF, start.line, start.column);
291}
292
293void
251compile_if_op(Chunk *chunk, Compiler *compiler, Token start) { 294compile_if_op(Chunk *chunk, Compiler *compiler, Token start) {
252 Token tok = peek_token(compiler); 295 Token tok = peek_token(compiler);
253 if (tok.type == TOKEN_EOF) { 296 if (tok.type == TOKEN_EOF) {
@@ -332,6 +375,7 @@ parse_list(Chunk *chunk, Compiler *compiler, Token start) {
332 case TOKEN_NEWLINE: { compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); } break; 375 case TOKEN_NEWLINE: { compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); } break;
333 case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break; 376 case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break;
334 case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break; 377 case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break;
378 case TOKEN_FUN: { compile_fun_op(chunk, compiler, start); } break;
335 case TOKEN_IF: { compile_if_op(chunk, compiler, start); } break; 379 case TOKEN_IF: { compile_if_op(chunk, compiler, start); } break;
336 default: { 380 default: {
337 error_push((Error){ 381 error_push((Error){
@@ -415,19 +459,20 @@ parse_tree(Chunk *chunk, Compiler *compiler) {
415 return; 459 return;
416} 460}
417 461
418Object 462Chunk *
419compile(Token *tokens) { 463compile(Token *tokens) {
420 Object main = make_lambda((StringView){"main", sizeof("main")}); 464 Chunk *chunk = NULL;
465 chunk = NEW_CHUNK("main");
421 Compiler compiler = (Compiler){ 466 Compiler compiler = (Compiler){
422 .tokens = tokens, 467 .tokens = tokens,
423 .current = 0, 468 .current = 0,
424 }; 469 };
425 Token start_tok = peek_token(&compiler); 470 Token start_tok = peek_token(&compiler);
426 while (has_next_token(&compiler) && peek_token(&compiler).type != TOKEN_EOF) { 471 while (has_next_token(&compiler) && peek_token(&compiler).type != TOKEN_EOF) {
427 parse_tree(main.chunk, &compiler); 472 parse_tree(chunk, &compiler);
428 } 473 }
429 add_code(main.chunk, OP_RETURN, start_tok.line, start_tok.column); 474 add_code(chunk, OP_RETURN, start_tok.line, start_tok.column);
430 return main; 475 return chunk;
431} 476}
432 477
433#endif // BDL_COMPILER_H 478#endif // BDL_COMPILER_H