Add paren support to reader
This commit is contained in:
parent
7b62b58c4f
commit
e0070bfb1e
23
lib/reader.c
23
lib/reader.c
@ -3,11 +3,12 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
static int parse_expression(
|
||||||
|
memory_pool_t *pool, const char *input, int len, expression_t **out);
|
||||||
|
|
||||||
static int parse_number(
|
static int parse_number(
|
||||||
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||||
{
|
{
|
||||||
(void)pool;
|
|
||||||
|
|
||||||
int value = 0, used = 0;
|
int value = 0, used = 0;
|
||||||
while (used < len && isdigit(input[used])) {
|
while (used < len && isdigit(input[used])) {
|
||||||
value *= 10;
|
value *= 10;
|
||||||
@ -24,13 +25,27 @@ static int parse_number(
|
|||||||
return used;
|
return used;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int parse_factor(
|
||||||
|
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||||
|
{
|
||||||
|
if (len > 1 && input[0] == '(') {
|
||||||
|
const int result = parse_expression(pool, input + 1, len - 1, out);
|
||||||
|
if (0 == result)
|
||||||
|
return 0;
|
||||||
|
const int used = result + 1;
|
||||||
|
return len >= used && input[used] == ')' ? used + 1 : 0;
|
||||||
|
} else {
|
||||||
|
return parse_number(pool, input, len, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int parse_term(
|
static int parse_term(
|
||||||
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||||
{
|
{
|
||||||
int result, used = 0;
|
int result, used = 0;
|
||||||
|
|
||||||
expression_t *expr;
|
expression_t *expr;
|
||||||
result = parse_number(pool, input, len, &expr);
|
result = parse_factor(pool, input, len, &expr);
|
||||||
if (0 >= result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
@ -48,7 +63,7 @@ static int parse_term(
|
|||||||
++used;
|
++used;
|
||||||
|
|
||||||
expression_t *term1;
|
expression_t *term1;
|
||||||
result = parse_number(pool, input + used, len - used, &term1);
|
result = parse_factor(pool, input + used, len - used, &term1);
|
||||||
if (0 >= result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
|
@ -141,6 +141,31 @@ input_1_plus_2_times_3_is_parsed_as_a_right_nested_application(void)
|
|||||||
ASSERT_EQUAL(3, operand1->application.operands[1]->number);
|
ASSERT_EQUAL(3, operand1->application.operands[1]->number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void paren_1_plus_2_times_3_close_paren_parses_as_left_nested(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
const expression_t *result = read_expression(&pool, "(1+2)*3", 7);
|
||||||
|
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_ADD, 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);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
TESTING_BEGIN();
|
TESTING_BEGIN();
|
||||||
@ -154,5 +179,6 @@ int main(void)
|
|||||||
RUN_TEST(input_1_plus_2_plus_3_is_parsed_as_a_left_nested_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_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);
|
RUN_TEST(input_1_plus_2_times_3_is_parsed_as_a_right_nested_application);
|
||||||
|
RUN_TEST(paren_1_plus_2_times_3_close_paren_parses_as_left_nested);
|
||||||
TESTING_END();
|
TESTING_END();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user