From ef07fab261cce781ca750c1288574d4001f14bcf Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 30 Apr 2023 01:48:18 +0200 Subject: 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 Co-authored-by: Carlos Maniero --- test/parser_test.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'test/parser_test.c') 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'"); -- cgit v1.2.3