diff --git a/include/lexer.h b/include/lexer.h index 8fd95eb..d87441c 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -7,7 +7,7 @@ // For identifing typedef enum { NODE_NUMBER, - NODE_OPERATOR, + NODE_BINARY_OP, } ASTNodeType; // For classify operators @@ -41,10 +41,12 @@ typedef struct ASTNode { ASTNodeType type; union { double number; - struct ASTNode *left; - struct ASTNode *right; - Operator op; - } operator; + struct { + struct ASTNode *left; + struct ASTNode *right; + Operator op; + } binary; + } data; } ASTNode; // I prefer ot have a dynamic array for storing the "tokens" diff --git a/test/test_ASTNodeArray.c b/test/test_ASTNodeArray.c index 4138ea3..6d81a97 100644 --- a/test/test_ASTNodeArray.c +++ b/test/test_ASTNodeArray.c @@ -9,20 +9,21 @@ static void test_array_push(void **state) { (void) state; + // We use 2 to force resize and checking anything wrong with malloc ASTNodeArray arr = ASTNodeArray_init(2); ASTNode node1 = { .type = NODE_NUMBER, - .operator = { .number = 90 } + .data = { .number = 90 } }; ASTNode node2 = { .type = NODE_NUMBER, - .operator = { .number = 80 } + .data = { .number = 80 } }; ASTNode node3 = { .type = NODE_NUMBER, - .operator = { .number = 70 } + .data = { .number = 70 } }; assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK); @@ -35,11 +36,44 @@ static void test_array_push(void **state) { assert_int_equal(ASTNodeArray_len(&arr), 3); } +static void test_array_pop(void **state) { + (void) state; + ASTNodeArray arr = ASTNodeArray_init(2); + ASTNode node1 = { + .type = NODE_NUMBER, + .data = { .number = 90 } + }; + + ASTNode node2 = { + .type = NODE_NUMBER, + .data = { .number = 80 } + }; + + ASTNode node3 = { + .type = NODE_NUMBER, + .data = { .number = 70 } + }; + + assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK); + assert_int_equal(ASTNodeArray_len(&arr), 1); + + assert_int_equal(ASTNodeArray_push(&arr, node2), ARRAY_OK); + assert_int_equal(ASTNodeArray_len(&arr), 2); + + assert_int_equal(ASTNodeArray_push(&arr, node3), ARRAY_OK); + assert_int_equal(ASTNodeArray_len(&arr), 3); + + ASTNode node4; + assert_int_equal(ASTNodeArray_pop(&arr, 1, &node4), ARRAY_OK); + assert_int_equal(node4.type, NODE_NUMBER); + assert_int_equal(node4.data.number, 80); +} int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_array_push), + cmocka_unit_test(test_array_pop), }; return cmocka_run_group_tests(tests, NULL, NULL);