diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-24 11:19:04 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-24 11:19:04 +0200 |
commit | 15e24115a2a117407157c993fe17b2d414dc0bf4 (patch) | |
tree | 0fd846846d82bca5ecfa9c40ffaa31bbf39159aa /src/main.c | |
parent | 55849f31e9ad0d2faadd7488ca25669737d74b8d (diff) | |
download | bdl-15e24115a2a117407157c993fe17b2d414dc0bf4.tar.gz bdl-15e24115a2a117407157c993fe17b2d414dc0bf4.zip |
Add proper parsing of return statements and functions params
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 34 |
1 files changed, 25 insertions, 9 deletions
@@ -491,16 +491,18 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
491 | return node->type; | 491 | return node->type; |
492 | } break; | 492 | } break; |
493 | case NODE_RETURN: { | 493 | case NODE_RETURN: { |
494 | Type type; | 494 | Type ret_type = cstr("("); |
495 | for (sz i = 0; i < array_size(node->elements); i++) { | 495 | for (sz i = 0; i < array_size(node->elements); i++) { |
496 | Node *expr = node->elements[i]; | 496 | Node *expr = node->elements[i]; |
497 | type = type_inference(a, expr, scope); | 497 | Type type = type_inference(a, expr, scope); |
498 | ret_type = str_concat(ret_type, type, a->storage); | ||
499 | if (i != array_size(node->elements) - 1) { | ||
500 | ret_type = str_concat(ret_type, cstr(","), a->storage); | ||
501 | } | ||
498 | } | 502 | } |
499 | // TODO: this is incorrect, must return the inferred type, but what | 503 | ret_type = str_concat(ret_type, cstr(")"), a->storage); |
500 | // to do if we have multiple? This only considers single return | 504 | node->type = ret_type; |
501 | // values for now... | 505 | return node->type; |
502 | node->type = type; | ||
503 | return type; | ||
504 | } break; | 506 | } break; |
505 | case NODE_FUN: { | 507 | case NODE_FUN: { |
506 | // TODO: we may want to do this and register a special table with | 508 | // TODO: we may want to do this and register a special table with |
@@ -523,6 +525,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
523 | 525 | ||
524 | node->type = cstr("nil"); | 526 | node->type = cstr("nil"); |
525 | scope = typescope_alloc(a, scope); | 527 | scope = typescope_alloc(a, scope); |
528 | Type param_type = cstr("("); | ||
526 | for (sz i = 0; i < array_size(node->func_params); i++) { | 529 | for (sz i = 0; i < array_size(node->func_params); i++) { |
527 | Node *param = node->func_params[i]; | 530 | Node *param = node->func_params[i]; |
528 | Str symbol = param->param_name->value.str; | 531 | Str symbol = param->param_name->value.str; |
@@ -531,12 +534,25 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
531 | type_inference(a, param->param_type, scope); | 534 | type_inference(a, param->param_type, scope); |
532 | param->type = param->param_name->type; | 535 | param->type = param->param_name->type; |
533 | typemap_insert(&scope->types, symbol, type, a->storage); | 536 | typemap_insert(&scope->types, symbol, type, a->storage); |
537 | param_type = str_concat(param_type, type, a->storage); | ||
538 | if (i != array_size(node->func_params) - 1) { | ||
539 | param_type = str_concat(param_type, cstr(","), a->storage); | ||
540 | } | ||
534 | } | 541 | } |
542 | param_type = str_concat(param_type, cstr(")"), a->storage); | ||
543 | node->fun_params = param_type; | ||
535 | 544 | ||
545 | Type ret_type = cstr("("); | ||
536 | for (sz i = 0; i < array_size(node->func_ret); i++) { | 546 | for (sz i = 0; i < array_size(node->func_ret); i++) { |
537 | Node *ret = node->func_ret[i]; | 547 | Node *expr = node->func_ret[i]; |
538 | ret->type = type_inference(a, ret, scope); | 548 | Type type = type_inference(a, expr, scope); |
549 | ret_type = str_concat(ret_type, type, a->storage); | ||
550 | if (i != array_size(node->func_ret) - 1) { | ||
551 | ret_type = str_concat(ret_type, cstr(","), a->storage); | ||
552 | } | ||
539 | } | 553 | } |
554 | ret_type = str_concat(ret_type, cstr(")"), a->storage); | ||
555 | node->fun_return = ret_type; | ||
540 | 556 | ||
541 | node->type = cstr("nil"); | 557 | node->type = cstr("nil"); |
542 | if (node->func_body->kind == NODE_BLOCK) { | 558 | if (node->func_body->kind == NODE_BLOCK) { |