From e2e0ed950bb147ebca3b9ac879268feeb185e20b Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 3 May 2023 23:17:50 -0300 Subject: parser: Introduce statement keywords This commit introduces a few changes in pipalang syntax. Now, both functions and variables requires keywords to be defined. before: main(): i32 { a: i32 = 2; return a; } now: fn main(): i32 { let a: i32 = 2; return a; } Signed-off-by: Carlos Maniero Reviewed-by: Johnny Richard --- test/parser_test.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 2d749da..a1614b9 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -64,7 +64,7 @@ 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, "main(): i32 { \nreturn 42;\n }"); + make_lexer_from_static_src(&lexer, "fn main(): i32 { \nreturn 42;\n }"); parser_init(&parser, &lexer, scope); ast_node_t *ast_function = parser_parse_function_declaration(&parser); @@ -98,7 +98,7 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or lexer_t lexer; scope_t *scope = scope_new(); - make_lexer_from_static_src(&lexer, "main(): i32 { \nvariable : i32 = 42; \nreturn variable;\n }"); + make_lexer_from_static_src(&lexer, "fn main(): i32 { \nlet variable: i32 = 42; \nreturn variable;\n }"); parser_init(&parser, &lexer, scope); ast_node_t *ast_function = parser_parse_function_declaration(&parser); @@ -203,31 +203,30 @@ 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("(): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); - assert_parser_error("main): i32 { return 42; }", "expected '(' but got ')'"); - assert_parser_error("main(: i32 { return 42; }", "expected ')' but got ':'"); - assert_parser_error("main() i32 { return 42; }", "expected ':' but got 'TOKEN_NAME'"); - assert_parser_error("main(): { return 42; }", "expected 'TOKEN_NAME' but got '{'"); - assert_parser_error("main(): i32 return 42; }", "expected '{' but got 'return'"); - assert_parser_error("main(): i32 { 42; }", "unexpected token 'TOKEN_NUMBER' value='42'"); - 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"); - assert_parser_error("main(): i32 { b = 1; return b; }", "trying to assign 'b' before defining it."); + assert_parser_error("main(): i32 { return 42; }", "expected 'fn' but got 'TOKEN_NAME'"); + 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 ':' 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'"); + assert_parser_error("fn main(): i32 { 42; }", "unexpected token 'TOKEN_NUMBER' value='42'"); + assert_parser_error("fn main(): i32 { return; }", "unexpected '; (;)' token"); + assert_parser_error("fn main(): i32 { return 42;", "expected '}' but got end of file"); + assert_parser_error("fn main(): beff { return 42; }", "type 'beff' is not defined"); + assert_parser_error("fn main(): i32 { return b; }", "identifier 'b' not defined"); + assert_parser_error("fn main(): i32 { b = 1; return b; }", "trying to assign 'b' before defining it."); // 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; }", "unexpected token 'TOKEN_NAME' value='oxi'"); + assert_parser_error("fn main(): i32 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); return MUNIT_OK; } static MunitTest tests[] = { - { "/test_parse_function", test_parse_function, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_function", test_parse_function, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_basic_syntax_errors", test_parse_basic_syntax_errors, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_arithmetic_expression", test_parse_arithmetic_expression, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/test_parse_arithmetic_expression", test_parse_arithmetic_expression, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_variable_definition", test_parse_variable_definition, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; -- cgit v1.2.3