diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-08 23:53:43 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-09 22:39:58 +0200 |
commit | 50ce0fb2a436eb5e765b2764c9468f648f18a4f8 (patch) | |
tree | 02756d7e728a8a2280d61d1e702f5740b424de6c /test/parser_test.c | |
parent | 35425aa5837543e4cc3fc82266dc2ae429cb2779 (diff) |
parser: parser boolean comparison expressions
After this commit, this is a valid expression:
1 || 2 && 3 > 4 < 5 >= 6 <= 7 == 8 != 9
Signed-off-by: Carlos Maniero <carlos@maniero.me>
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'test/parser_test.c')
-rw-r--r-- | test/parser_test.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/test/parser_test.c b/test/parser_test.c index 1371453..830fed7 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -244,6 +244,118 @@ test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_ return MUNIT_OK; } +static MunitResult +test_parse_boolean_expression(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, "1 + 3 > 3 / 2 - 7"); + parser_init(&parser, &lexer, scope); + + ast_node_t *ast_expression = parser_parse_expression(&parser); + assert_not_null(ast_expression); + + ast_node_t *exp1 = ast_expression; + { + + assert_int(AST_BINARY_OPERATION, ==, exp1->kind); + assert_int(AST_BINOP_GT, ==, exp1->data.binary_operation.kind); + + ast_node_t *exp1_left = exp1->data.binary_operation.left; + { + + assert_int(AST_BINARY_OPERATION, ==, exp1_left->kind); + assert_int(AST_BINOP_ADITION, ==, exp1_left->data.binary_operation.kind); + + assert_int(AST_LITERAL, ==, exp1_left->data.binary_operation.left->kind); + assert_int(exp1_left->data.binary_operation.left->data.literal.value.integer, ==, 1); + + assert_int(AST_LITERAL, ==, exp1_left->data.binary_operation.right->kind); + assert_int(exp1_left->data.binary_operation.right->data.literal.value.integer, ==, 3); + } + + ast_node_t *exp1_right = exp1->data.binary_operation.right; + { + + assert_int(AST_BINARY_OPERATION, ==, exp1_right->kind); + assert_int(AST_BINOP_SUBTRACTION, ==, exp1_right->data.binary_operation.kind); + + ast_node_t *exp2_left = exp1_right->data.binary_operation.left; + { + assert_int(AST_BINARY_OPERATION, ==, exp2_left->kind); + assert_int(AST_BINOP_DIVISION, ==, exp2_left->data.binary_operation.kind); + + assert_int(AST_LITERAL, ==, exp2_left->data.binary_operation.left->kind); + assert_int(exp2_left->data.binary_operation.left->data.literal.value.integer, ==, 3); + + assert_int(AST_LITERAL, ==, exp2_left->data.binary_operation.right->kind); + assert_int(exp2_left->data.binary_operation.right->data.literal.value.integer, ==, 2); + } + + assert_int(AST_LITERAL, ==, exp1_right->data.binary_operation.right->kind); + assert_int(exp1_right->data.binary_operation.right->data.literal.value.integer, ==, 7); + } + } + + ast_node_destroy(ast_expression); + scope_destroy(scope); + + return MUNIT_OK; +} + +static MunitResult +test_parse_all_boolean_expression(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, "1 || 2 && 3 > 4 < 5 >= 6 <= 7 == 8 != 9"); + parser_init(&parser, &lexer, scope); + + ast_node_t *ast_expression = parser_parse_expression(&parser); + assert_not_null(ast_expression); + + ast_node_t *exp1 = ast_expression; + assert_int(TYPE_BOOL, ==, exp1->result_type); + assert_int(AST_BINOP_NOT_EQUAL, ==, exp1->data.binary_operation.kind); + + ast_node_t *exp2 = exp1->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp2->result_type); + assert_int(AST_BINOP_EQUAL, ==, exp2->data.binary_operation.kind); + + ast_node_t *exp3 = exp2->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp3->result_type); + assert_int(AST_BINOP_LT_EQUAL, ==, exp3->data.binary_operation.kind); + + ast_node_t *exp4 = exp3->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp4->result_type); + assert_int(AST_BINOP_GT_EQUAL, ==, exp4->data.binary_operation.kind); + + ast_node_t *exp5 = exp4->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp5->result_type); + assert_int(AST_BINOP_LT, ==, exp5->data.binary_operation.kind); + + ast_node_t *exp6 = exp5->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp6->result_type); + assert_int(AST_BINOP_GT, ==, exp6->data.binary_operation.kind); + + ast_node_t *exp7 = exp6->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp7->result_type); + assert_int(AST_BINOP_AND, ==, exp7->data.binary_operation.kind); + + ast_node_t *exp8 = exp7->data.binary_operation.left; + assert_int(TYPE_BOOL, ==, exp8->result_type); + assert_int(AST_BINOP_OR, ==, exp8->data.binary_operation.kind); + + ast_node_destroy(ast_expression); + scope_destroy(scope); + + return MUNIT_OK; +} + void assert_string_view_equal(char *expected, string_view_t actual) { @@ -295,6 +407,8 @@ static MunitTest tests[] = { { "/test_parse_boolean", test_parse_boolean, 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_boolean_expression", test_parse_boolean_expression, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/test_parse_all_boolean_expression", test_parse_all_boolean_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 } }; |