From 35425aa5837543e4cc3fc82266dc2ae429cb2779 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Mon, 8 May 2023 23:53:42 -0300 Subject: parser: Ensure the expression types When assign a variable or returning a value it now ensures that the expression matches the expected type. To make this possible a %result_type% field was added to ast_node_t and this field is used whenever to make the comparison. Signed-off-by: Carlos Maniero Signed-off-by: Johnny Richard --- test/parser_test.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test') diff --git a/test/parser_test.c b/test/parser_test.c index 12b2cac..1371453 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -268,6 +268,21 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or 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."); + assert_parser_error("fn main(): i32 { let b: bool = 3; return 1; }", + "incompatible types: expected 'bool', actual 'i32'."); + assert_parser_error("fn main(): i32 { let b: i32 = true; return 1; }", + "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { let b: i32 = 1; b = true; return 1; }", + "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { let b: bool = true; let c: i32 = b; return 1; }", + "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { let b: i32 = 1; let c: bool = b; return 1; }", + "incompatible types: expected 'bool', actual 'i32'."); + assert_parser_error("fn main(): i32 { let b: bool = 1; b = 42; return 1; }", + "incompatible types: expected 'bool', actual 'i32'."); + assert_parser_error("fn main(): i32 { return true; }", "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { let a: bool = true; return a; }", + "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 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); -- cgit v1.2.3