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 | |
parent | 55849f31e9ad0d2faadd7488ca25669737d74b8d (diff) | |
download | bdl-15e24115a2a117407157c993fe17b2d414dc0bf4.tar.gz bdl-15e24115a2a117407157c993fe17b2d414dc0bf4.zip |
Add proper parsing of return statements and functions params
-rw-r--r-- | src/main.c | 34 | ||||
-rw-r--r-- | src/parser.c | 8 | ||||
-rw-r--r-- | tests/semantics.bad | 17 |
3 files changed, 41 insertions, 18 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) { |
diff --git a/src/parser.c b/src/parser.c index b82579b..1283854 100644 --- a/src/parser.c +++ b/src/parser.c | |||
@@ -196,6 +196,8 @@ typedef struct Node { | |||
196 | bool is_ptr; | 196 | bool is_ptr; |
197 | struct Scope *scope; | 197 | struct Scope *scope; |
198 | Str type; | 198 | Str type; |
199 | Str fun_params; | ||
200 | Str fun_return; | ||
199 | } Node; | 201 | } Node; |
200 | 202 | ||
201 | // | 203 | // |
@@ -1030,6 +1032,12 @@ graph_node(Node *node) { | |||
1030 | if (node->type.size > 0) { | 1032 | if (node->type.size > 0) { |
1031 | print("| Type: %s", node->type); | 1033 | print("| Type: %s", node->type); |
1032 | } | 1034 | } |
1035 | if (node->fun_params.size > 0) { | ||
1036 | print("| Params: %s", node->fun_params); | ||
1037 | } | ||
1038 | if (node->fun_return.size > 0) { | ||
1039 | print("| Return: %s", node->fun_return); | ||
1040 | } | ||
1033 | println("\"];"); | 1041 | println("\"];"); |
1034 | 1042 | ||
1035 | switch (node->kind) { | 1043 | switch (node->kind) { |
diff --git a/tests/semantics.bad b/tests/semantics.bad index 47c4b88..5071d8b 100644 --- a/tests/semantics.bad +++ b/tests/semantics.bad | |||
@@ -90,16 +90,16 @@ | |||
90 | ; 1 + 1 + c | 90 | ; 1 + 1 + c |
91 | ; } | 91 | ; } |
92 | 92 | ||
93 | ; fun testmatches(): nil { | 93 | ; fun testmatches(num: int): int { |
94 | ; match (num) { | 94 | ; ; match (num) { |
95 | ; case 1 = 23 | 95 | ; ; case 1 = 23 |
96 | ; case 2 = num | 96 | ; ; case 2 = num |
97 | ; else = num | 97 | ; ; else = num |
98 | ; } | 98 | ; ; } |
99 | 99 | ||
100 | ; cond { | 100 | ; cond { |
101 | ; case 1 == 1 = 23 | 101 | ; 1 == 1 = 23 |
102 | ; case 2 != 1 = num | 102 | ; 2 != 1 = num |
103 | ; else = num | 103 | ; else = num |
104 | ; } | 104 | ; } |
105 | ; } | 105 | ; } |
@@ -109,7 +109,6 @@ | |||
109 | ; let c = 1 | 109 | ; let c = 1 |
110 | ; 1 + 1 + c | 110 | ; 1 + 1 + c |
111 | ; } | 111 | ; } |
112 | |||
113 | fun foo(a: int b: str): (f32, f64) { | 112 | fun foo(a: int b: str): (f32, f64) { |
114 | return(1.0, 2.0) | 113 | return(1.0, 2.0) |
115 | } | 114 | } |