From 9988f6dcb8da782fccf8dc7906b08243966dc1a6 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 24 Oct 2024 20:24:22 +0100 Subject: [PATCH] Support * and / in reader --- lib/reader.c | 56 ++++++++++++++++++++++++++++++++++++-------- tests/reader_tests.c | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 10 deletions(-) diff --git a/lib/reader.c b/lib/reader.c index 5e94282..201abc6 100644 --- a/lib/reader.c +++ b/lib/reader.c @@ -24,7 +24,7 @@ static int parse_number( return used; } -static int parse_expression( +static int parse_term( memory_pool_t *pool, const char *input, int len, expression_t **out) { int result, used = 0; @@ -39,16 +39,12 @@ static int parse_expression( const expression_t *term0 = expr; operator_t operator; - switch (input[used]) { - case '+': - operator= OPERATOR_ADD; + if ('*' == input[used]) + operator = OPERATOR_MULTIPLY; + else if ('/' == input[used]) + operator = OPERATOR_DIVIDE; + else break; - case '-': - operator= OPERATOR_SUBTRACT; - break; - default: - return 0; - } ++used; expression_t *term1; @@ -68,6 +64,46 @@ static int parse_expression( return used; } +static int parse_expression( + memory_pool_t *pool, const char *input, int len, expression_t **out) +{ + int result, used = 0; + + expression_t *expr; + result = parse_term(pool, input, len, &expr); + if (0 >= result) + return 0; + used += result; + + while (len > used) { + const expression_t *term0 = expr; + + operator_t operator; + if ('+' == input[used]) + operator = OPERATOR_ADD; + else if ('-' == input[used]) + operator = OPERATOR_SUBTRACT; + else + break; + ++used; + + expression_t *term1; + result = parse_term(pool, input + used, len - used, &term1); + if (0 >= result) + return 0; + used += result; + + expr = allocate_expression(pool); + expr->is_number = false; + expr->application.operator= operator; + expr->application.operands[0] = term0; + expr->application.operands[1] = term1; + } + + *out = expr; + return used; +} + const expression_t * read_expression(memory_pool_t *pool, const char *input, int len) { diff --git a/tests/reader_tests.c b/tests/reader_tests.c index 36ab9f5..6b984c3 100644 --- a/tests/reader_tests.c +++ b/tests/reader_tests.c @@ -89,6 +89,58 @@ input_1_plus_2_plus_3_is_parsed_as_a_left_nested_application(void) ASSERT_EQUAL(3, operand1->number); } +static void +input_1_times_2_times_3_is_parsed_as_a_left_nested_application(void) +{ + init_memory_pool(&pool); + const expression_t *result = read_expression(&pool, "1*2*3", 5); + ASSERT_NOT_NULL(result); + ASSERT_FALSE(result->is_number); + ASSERT_EQUAL(OPERATOR_MULTIPLY, result->application.operator); + + const expression_t *operand0 = result->application.operands[0]; + ASSERT_NOT_NULL(operand0); + ASSERT_FALSE(operand0->is_number); + ASSERT_EQUAL(OPERATOR_MULTIPLY, operand0->application.operator); + ASSERT_NOT_NULL(operand0->application.operands[0]); + ASSERT_TRUE(operand0->application.operands[0]->is_number); + ASSERT_EQUAL(1, operand0->application.operands[0]->number); + ASSERT_NOT_NULL(operand0->application.operands[1]); + ASSERT_TRUE(operand0->application.operands[1]->is_number); + ASSERT_EQUAL(2, operand0->application.operands[1]->number); + + const expression_t *operand1 = result->application.operands[1]; + ASSERT_NOT_NULL(operand1); + ASSERT_TRUE(operand1->is_number); + ASSERT_EQUAL(3, operand1->number); +} + +static void +input_1_plus_2_times_3_is_parsed_as_a_right_nested_application(void) +{ + init_memory_pool(&pool); + const expression_t *result = read_expression(&pool, "1+2*3", 5); + ASSERT_NOT_NULL(result); + ASSERT_FALSE(result->is_number); + ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); + + const expression_t *operand0 = result->application.operands[0]; + ASSERT_NOT_NULL(operand0); + ASSERT_TRUE(operand0->is_number); + ASSERT_EQUAL(1, operand0->number); + + const expression_t *operand1 = result->application.operands[1]; + ASSERT_NOT_NULL(operand1); + ASSERT_FALSE(operand1->is_number); + ASSERT_EQUAL(OPERATOR_MULTIPLY, operand1->application.operator); + ASSERT_NOT_NULL(operand1->application.operands[0]); + ASSERT_TRUE(operand1->application.operands[0]->is_number); + ASSERT_EQUAL(2, operand1->application.operands[0]->number); + ASSERT_NOT_NULL(operand1->application.operands[1]); + ASSERT_TRUE(operand1->application.operands[1]->is_number); + ASSERT_EQUAL(3, operand1->application.operands[1]->number); +} + int main(void) { TESTING_BEGIN(); @@ -100,5 +152,7 @@ int main(void) RUN_TEST( input_6_minus_2_with_no_spaces_is_read_as_a_subtract_application); RUN_TEST(input_1_plus_2_plus_3_is_parsed_as_a_left_nested_application); + RUN_TEST(input_1_times_2_times_3_is_parsed_as_a_left_nested_application); + RUN_TEST(input_1_plus_2_times_3_is_parsed_as_a_right_nested_application); TESTING_END(); }