summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-08 23:53:42 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-09 21:46:51 +0200
commit35425aa5837543e4cc3fc82266dc2ae429cb2779 (patch)
tree1d7dec87dc17785162c20206371818f5e28fd99b /test
parent3842de0e22d72075f06bd8cc44b8744e86c21725 (diff)
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 <carlos@maniero.me> Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'test')
-rw-r--r--test/parser_test.c15
1 files changed, 15 insertions, 0 deletions
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'");