diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-30 01:48:18 +0200 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-30 01:55:29 +0200 |
commit | ef07fab261cce781ca750c1288574d4001f14bcf (patch) | |
tree | 9b4da44aee7cc64cec448adeee31b38e12d29e6d /test | |
parent | 88a08db927629032d6d4c662e00f0dce2c112ce4 (diff) |
parser: Registry identifiers on scope
We are parsing variables/functions and checking if they are defined on
scope. Otherwise we fail the parsing with a nice message.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Co-authored-by: Carlos Maniero <carlosmaniero@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 2 | ||||
-rw-r--r-- | test/parser_test.c | 18 | ||||
-rw-r--r-- | test/scope_test.c | 14 |
3 files changed, 22 insertions, 12 deletions
diff --git a/test/Makefile b/test/Makefile index 6d8bfb7..9572d0f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -27,7 +27,7 @@ vector_test: munit.o ../build/vector.o vector_test.o lexer_test: munit.o ../build/string_view.o ../build/lexer.o lexer_test.o $(CC) $? $(CFLAGS) -o $@ -parser_test: munit.o ../build/string_view.o ../build/vector.o ../build/lexer.o ../build/ast.o ../build/parser.o parser_test.o +parser_test: munit.o ../build/string_view.o ../build/scope.o ../build/vector.o ../build/lexer.o ../build/ast.o ../build/parser.o parser_test.o $(CC) $? $(CFLAGS) -o $@ scope_test: munit.o ../build/string_view.o ../build/vector.o ../build/ast.o ../build/scope.o scope_test.o diff --git a/test/parser_test.c b/test/parser_test.c index 40c0198..0c54e50 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -19,6 +19,7 @@ #include "lexer.h" #include "munit.h" #include "parser.h" +#include "scope.h" #include "string.h" #include "vector.h" @@ -43,8 +44,9 @@ assert_parser_error(char *src, char *error_msg) parser_t parser; lexer_t lexer; + scope_t *scope = scope_new(); make_lexer_from_static_src(&lexer, src); - parser_init(&parser, &lexer); + parser_init(&parser, &lexer, scope); ast_node_t *ast_function = ast_node_new(); @@ -54,6 +56,7 @@ assert_parser_error(char *src, char *error_msg) assert_string_equal(error_msg, parser.errors[0].message); ast_node_destroy(ast_function); + scope_destroy(scope); } static MunitResult @@ -61,9 +64,10 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture) { parser_t parser; lexer_t lexer; + scope_t *scope = scope_new(); make_lexer_from_static_src(&lexer, "main(): i32 { \nreturn 42;\n }"); - parser_init(&parser, &lexer); + parser_init(&parser, &lexer, scope); ast_node_t *ast_function = ast_node_new(); bool parsed = parser_parse_function_declaration(&parser, ast_function); @@ -85,6 +89,7 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture) assert_int(42, ==, ast_literal->data.literal.value.integer); ast_node_destroy(ast_function); + scope_destroy(scope); return MUNIT_OK; } @@ -94,9 +99,10 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or { parser_t parser; lexer_t lexer; + scope_t *scope = scope_new(); make_lexer_from_static_src(&lexer, "main(): i32 { \nvariable : i32 = 42; \nreturn variable;\n }"); - parser_init(&parser, &lexer); + parser_init(&parser, &lexer, scope); ast_node_t *ast_function = ast_node_new(); bool parsed = parser_parse_function_declaration(&parser, ast_function); @@ -124,6 +130,7 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or assert_string_view_equal("variable", ast_literal->data.identifier.name); ast_node_destroy(ast_function); + scope_destroy(scope); return MUNIT_OK; } @@ -133,9 +140,10 @@ test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_ { parser_t parser; lexer_t lexer; + scope_t *scope = scope_new(); make_lexer_from_static_src(&lexer, "1 + 3 * 3 / 2 - 1"); - parser_init(&parser, &lexer); + parser_init(&parser, &lexer, scope); ast_node_t *ast_expression = ast_node_new(); bool parsed = parser_parse_expression(&parser, ast_expression); @@ -185,6 +193,7 @@ test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_ } ast_node_destroy(ast_expression); + scope_destroy(scope); return MUNIT_OK; } @@ -210,6 +219,7 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or assert_parser_error("main(): i32 { return; }", "unexpected '; (;)' token"); assert_parser_error("main(): i32 { return 42;", "expected '}' but got end of file"); assert_parser_error("main(): beff { return 42; }", "type 'beff' is not defined"); + assert_parser_error("main(): i32 { return b; }", "identifier 'b' not defined"); // FIXME: once function calls are implemented, this error should inform that // neither a variable or function call was found. assert_parser_error("main(): i32 { oxi 42; }", "expected ':' but got 'TOKEN_NUMBER'"); diff --git a/test/scope_test.c b/test/scope_test.c index b8ee30e..c71a38f 100644 --- a/test/scope_test.c +++ b/test/scope_test.c @@ -31,24 +31,24 @@ test_scope(const MunitParameter params[], void *user_data_or_fixture) scope_t *scope = scope_new(); - assert_null(scope_get(scope, &first_id)); - assert_null(scope_get(scope, &second_id)); + assert_null(scope_get(scope, first_id.name)); + assert_null(scope_get(scope, second_id.name)); scope_push(scope, &first_id, &first_id_node_1); scope_push(scope, &second_id, &second_id_node); - assert_ptr_equal((void *)scope_get(scope, &first_id), (void *)&first_id_node_1); - assert_ptr_equal((void *)scope_get(scope, &second_id), (void *)&second_id_node); + assert_ptr_equal((void *)scope_get(scope, first_id.name), (void *)&first_id_node_1); + assert_ptr_equal((void *)scope_get(scope, second_id.name), (void *)&second_id_node); scope_enter(scope); scope_push(scope, &first_id, &first_id_node_2); - assert_ptr_equal((void *)scope_get(scope, &first_id), (void *)&first_id_node_2); - assert_ptr_equal((void *)scope_get(scope, &second_id), (void *)&second_id_node); + assert_ptr_equal((void *)scope_get(scope, first_id.name), (void *)&first_id_node_2); + assert_ptr_equal((void *)scope_get(scope, second_id.name), (void *)&second_id_node); scope_leave(scope); - assert_ptr_equal((void *)scope_get(scope, &first_id), (void *)&first_id_node_1); + assert_ptr_equal((void *)scope_get(scope, first_id.name), (void *)&first_id_node_1); scope_destroy(scope); |