From 3065f54e3a122dd3d8c2deffdec72ec48ea4f165 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 00:20:01 -0300 Subject: parser: Fixes boolean binary operation precedence The comparators && and || should have precedence over others comparators (> < >= <= == !=). Signed-off-by: Carlos Maniero --- test/parser_test.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 43eb27b..8565dc8 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -320,35 +320,35 @@ test_parse_all_boolean_expression(const MunitParameter params[], void *user_data ast_node_t *exp1 = ast_expression; assert_int(TYPE_BOOL, ==, exp1->result_type); - assert_int(AST_BINOP_NOT_EQUAL, ==, exp1->data.binary_operation.kind); + assert_int(AST_BINOP_AND, ==, exp1->data.binary_operation.kind); ast_node_t *exp2 = exp1->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp2->result_type); - assert_int(AST_BINOP_EQUAL, ==, exp2->data.binary_operation.kind); + assert_int(AST_BINOP_OR, ==, exp2->data.binary_operation.kind); - ast_node_t *exp3 = exp2->data.binary_operation.left; + ast_node_t *exp3 = exp1->data.binary_operation.right; assert_int(TYPE_BOOL, ==, exp3->result_type); - assert_int(AST_BINOP_LT_EQUAL, ==, exp3->data.binary_operation.kind); + assert_int(AST_BINOP_NOT_EQUAL, ==, exp3->data.binary_operation.kind); ast_node_t *exp4 = exp3->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp4->result_type); - assert_int(AST_BINOP_GT_EQUAL, ==, exp4->data.binary_operation.kind); + assert_int(AST_BINOP_EQUAL, ==, exp4->data.binary_operation.kind); ast_node_t *exp5 = exp4->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp5->result_type); - assert_int(AST_BINOP_LT, ==, exp5->data.binary_operation.kind); + assert_int(AST_BINOP_LT_EQUAL, ==, exp5->data.binary_operation.kind); ast_node_t *exp6 = exp5->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp6->result_type); - assert_int(AST_BINOP_GT, ==, exp6->data.binary_operation.kind); + assert_int(AST_BINOP_GT_EQUAL, ==, exp6->data.binary_operation.kind); ast_node_t *exp7 = exp6->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp7->result_type); - assert_int(AST_BINOP_AND, ==, exp7->data.binary_operation.kind); + assert_int(AST_BINOP_LT, ==, exp7->data.binary_operation.kind); ast_node_t *exp8 = exp7->data.binary_operation.left; assert_int(TYPE_BOOL, ==, exp8->result_type); - assert_int(AST_BINOP_OR, ==, exp8->data.binary_operation.kind); + assert_int(AST_BINOP_GT, ==, exp8->data.binary_operation.kind); ast_node_destroy(ast_expression); scope_destroy(scope); -- cgit v1.2.3 From 75639fbf01bd6ae1212521b6cf822025eb8b598d Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 16:07:39 -0300 Subject: namespaces: Add a namespace structure that represents a file We have been always parsing a single function. Since we want to have multiple functions in a near future, this patch introduces an namespace that represents an entire file. To ensure a function is defined inside a namespace, a helper function was created. Today our ast_node structure is highly exposed, and this is something that Johnny and I have been discussed. So then, this is a first step to try to protected the code generation from our ast tree. Signed-off-by: Carlos Maniero --- test/parser_test.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 8565dc8..78e2e23 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -64,17 +64,15 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture) lexer_t lexer; scope_t *scope = scope_new(); - make_lexer_from_static_src(&lexer, "fn main(): i32 { \nreturn 42;\n }"); + make_lexer_from_static_src(&lexer, "fn add(): i32 { return 2; } fn main(): i32 { \nreturn 42;\n }"); parser_init(&parser, &lexer, scope); - ast_node_t *ast_function = parser_parse_function_declaration(&parser); + ast_node_t *ast_ns = parser_parse_ns(&parser); - assert_not_null(ast_function); + assert_not_null(ast_ns); - char actual[5]; + ast_node_t *ast_function = ast_node_ns_get_function_node_by_name(ast_ns, "main"); - string_view_to_str(&ast_function->data.function.identifier.name, actual); - assert_string_equal("main", actual); - assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); + assert_not_null(ast_function); ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 0); -- cgit v1.2.3 From 6f187a71cbe3aa4ebb32ba287c75562d96c7a3f4 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 17:49:36 -0300 Subject: tests: Replace parse function with parse ns for error handling It was necessary to test function calls errors. Signed-off-by: Carlos Maniero --- test/parser_test.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 78e2e23..ebb917c 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -48,9 +48,9 @@ assert_parser_error(char *src, char *error_msg) make_lexer_from_static_src(&lexer, src); parser_init(&parser, &lexer, scope); - ast_node_t *ast_function = parser_parse_function_declaration(&parser); + ast_node_t *ast_ns = parser_parse_ns(&parser); - assert_false(ast_function != NULL); + assert_false(ast_ns != NULL); assert_int(1, ==, parser.errors_len); assert_string_equal(error_msg, parser.errors[0].message); @@ -394,7 +394,7 @@ assert_string_view_equal(char *expected, string_view_t actual) static MunitResult test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or_fixture) { - assert_parser_error("main(): i32 { return 42; }", "expected 'fn' but got 'TOKEN_NAME'"); + assert_parser_error("main(): i32 { return 42; }", "Unexpected token 'main'"); assert_parser_error("fn (): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); assert_parser_error("fn main): i32 { return 42; }", "expected '(' but got ')'"); assert_parser_error("fn main(: i32 { return 42; }", "expected ')' but got ':'"); @@ -427,8 +427,9 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or "incompatible types: expected 'bool', actual 'i32'."); assert_parser_error("fn main(): i32 { if true { return true; } \nreturn 42;\n }", "incompatible types: expected 'i32', actual 'bool'."); - // FIXME: once function calls are implemented, this error should inform that - // neither a variable or function call was found. + assert_parser_error("fn main(): i32 { return fn_404(); }", "identifier 'fn_404' not defined"); + assert_parser_error("fn my_bool_fn(): bool { return true; } fn main(): i32 { return my_bool_fn(); }", + "incompatible types: expected 'i32', actual 'bool'."); assert_parser_error("fn main(): i32 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); return MUNIT_OK; -- cgit v1.2.3 From 5042a4ffc1363d6f0f99a3afd79f76cf2da738d6 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 22:24:14 -0300 Subject: gas: implement function calls For now function calls are following the C's calling convention, which means they are using the following registers to pass functions' arguments: rdi, rsi, rdx, rcx, r8, r9 If a function has more then 6 parameters, the compilation will fail. To enable function with more than 6 parameters we will need to save the extra arguments on stack. Naming: parameters: function parameters are the variables a function receives. arguments: Arguments are the values passed to a function when calling it. Calling mechanism: When a function is called, all the expressions passed as argument are evaluated, after the evaluation, the result is stored on the register that represents its argument position, the first argument will be stored on rdi, the second on rsi and so on. Receiving mechanism: When a function starts, the first thing it does is store all the registers onto the stack. So rdi will be stored on -8(rbp), rsi on -16(rbp) and so on. And, a ref_entry is created making the relationship parameter-stack_offset. Signed-off-by: Carlos Maniero --- test/parser_test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index ebb917c..44ffd57 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -104,7 +104,7 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or char actual[5]; - string_view_to_str(&ast_function->data.function.identifier.name, actual); + string_view_to_str(&ast_function->data.function.prototype.identifier.name, actual); assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); @@ -141,7 +141,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_true(ast_function != NULL); - assert_string_view_equal("my_bool_fn", ast_function->data.function.identifier.name); + assert_string_view_equal("my_bool_fn", ast_node_function_declaration_name(ast_function)); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); @@ -397,7 +397,7 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or assert_parser_error("main(): i32 { return 42; }", "Unexpected token 'main'"); assert_parser_error("fn (): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); assert_parser_error("fn main): i32 { return 42; }", "expected '(' but got ')'"); - assert_parser_error("fn main(: i32 { return 42; }", "expected ')' but got ':'"); + assert_parser_error("fn main(: i32 { return 42; }", "expected 'TOKEN_NAME' but got ':'"); assert_parser_error("fn main() i32 { return 42; }", "expected ':' but got 'TOKEN_NAME'"); assert_parser_error("fn main(): { return 42; }", "expected 'TOKEN_NAME' but got '{'"); assert_parser_error("fn main(): i32 return 42; }", "expected '{' but got 'return'"); -- cgit v1.2.3 From ea7f65fe1250be8f49edcaaedd3410aed1401648 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Thu, 11 May 2023 15:00:10 -0300 Subject: gas: implement recursion and late evaluation Until now the below code was not valid for pipac. fn main(): i32 { return fib(13); } fn fib(n: i32): i32 { if n <= 1 { return n; } return fib(n - 1) + fib(n - 2); } Pipa's parser was adding a function to scope after they were fully parsed which means that fib's self-reference would not work. Also, functions were required to follow the be called in the order they are declared for the same scope reason so, the main function was required to be defined after fib. And how it is working now? When a TOKEN_NAME is not found in the scope, instead of returning an error, an unknown token is created as placeholder. The parser stores the node reference and the token it was trying to parse. During type checks, if the parser detects an unknown node, instead of returning an error, it stores in that node what was the expected type. After the NS is fully parsed a reevaluation is made on those unknown nodes by setting the lexer back on the node's token position and parsing the TOKEN_NAME again. Ps: There is a typo on the unknown token. It will be addressed in another commit since this issue was not introduced by this change. Signed-off-by: Carlos Maniero --- test/parser_test.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 44ffd57..acc238f 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -430,6 +430,8 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or assert_parser_error("fn main(): i32 { return fn_404(); }", "identifier 'fn_404' not defined"); assert_parser_error("fn my_bool_fn(): bool { return true; } fn main(): i32 { return my_bool_fn(); }", "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { return my_bool_fn(); } fn my_bool_fn(): bool { return true; }", + "incompatible types: expected 'i32', actual 'bool'."); assert_parser_error("fn main(): i32 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); return MUNIT_OK; -- cgit v1.2.3