aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-20 14:30:59 +0200
committerBad Diode <bd@badd10de.dev>2024-06-20 14:41:10 +0200
commitd8bdb8be74904ee7b12d3f61dc3afe222aec82ff (patch)
treeb496399cae78397d0ec2696654b86b44cf7232a3
parenteca9cb305b08e0e292d01008797b9300d47f72d5 (diff)
downloadbdl-d8bdb8be74904ee7b12d3f61dc3afe222aec82ff.tar.gz
bdl-d8bdb8be74904ee7b12d3f61dc3afe222aec82ff.zip
Add parsing of funcalls
-rw-r--r--src/parser.c10
-rw-r--r--tests/functions.bad4
2 files changed, 12 insertions, 2 deletions
diff --git a/src/parser.c b/src/parser.c
index dbab4af..a8b9c47 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -183,6 +183,7 @@ typedef struct Node {
183 struct Node **elements; 183 struct Node **elements;
184 struct Node **statements; 184 struct Node **statements;
185 struct Node **expressions; 185 struct Node **expressions;
186 struct Node **arguments;
186 struct { 187 struct {
187 struct Node *func_name; 188 struct Node *func_name;
188 struct Node **func_params; 189 struct Node **func_params;
@@ -953,6 +954,13 @@ parse_symbol(Parser *parser) {
953 parse_symbol(parser); 954 parse_symbol(parser);
954 node->next = array_pop(parser->nodes); 955 node->next = array_pop(parser->nodes);
955 } 956 }
957 } else if (parse_match(parser, TOK_LPAREN)) {
958 node->kind = NODE_FUNCALL;
959 while (!parse_match(parser, TOK_RPAREN) && !parser->panic) {
960 parse_expr(parser, PREC_LOW);
961 array_push(node->arguments, array_pop(parser->nodes),
962 parser->storage);
963 }
956 } else { 964 } else {
957 node = node_alloc(parser, NODE_SYMBOL, prev); 965 node = node_alloc(parser, NODE_SYMBOL, prev);
958 if (!node) return; 966 if (!node) return;
@@ -987,6 +995,7 @@ graph_node(Node *node) {
987 case NODE_STRUCT: 995 case NODE_STRUCT:
988 case NODE_STRUCT_LIT: print("| Name: %s", node->value.sym); break; 996 case NODE_STRUCT_LIT: print("| Name: %s", node->value.sym); break;
989 case NODE_SYMBOL: 997 case NODE_SYMBOL:
998 case NODE_FUNCALL:
990 case NODE_ARR_TYPE: 999 case NODE_ARR_TYPE:
991 case NODE_TYPE: { 1000 case NODE_TYPE: {
992 if (node->is_ptr) { 1001 if (node->is_ptr) {
@@ -1032,6 +1041,7 @@ graph_node(Node *node) {
1032 graph_node(next); 1041 graph_node(next);
1033 } 1042 }
1034 } break; 1043 } break;
1044 case NODE_FUNCALL:
1035 case NODE_RETURN: 1045 case NODE_RETURN:
1036 case NODE_BLOCK: 1046 case NODE_BLOCK:
1037 case NODE_STRUCT_LIT: 1047 case NODE_STRUCT_LIT:
diff --git a/tests/functions.bad b/tests/functions.bad
index 0d2e2d0..fa28d75 100644
--- a/tests/functions.bad
+++ b/tests/functions.bad
@@ -3,7 +3,7 @@ fun add_two(a: int, b: int): int a + b
3 3
4; Functions return the result of the last expression implicitly. 4; Functions return the result of the last expression implicitly.
5fun add_three(a: int, b: int, c: int): int { 5fun add_three(a: int, b: int, c: int): int {
6 a + b + c 6 add_two(a, b) + c
7} 7}
8 8
9; Functions that don't return anything must explicitly state it as returning 9; Functions that don't return anything must explicitly state it as returning
@@ -14,7 +14,7 @@ fun foo(): nil {
14 14
15; An alternate form for empty return values. 15; An alternate form for empty return values.
16fun bar(): () { 16fun bar(): () {
17 write("hello world") 17 foo()
18} 18}
19 19
20; We support multiple return values, in this case the "return" keyword must be 20; We support multiple return values, in this case the "return" keyword must be