Make + and - left-associative
This commit is contained in:
parent
b491a1a782
commit
9932aa7ef1
22
lib/reader.c
22
lib/reader.c
@ -29,15 +29,14 @@ static int parse_expression(
|
|||||||
{
|
{
|
||||||
int result, used = 0;
|
int result, used = 0;
|
||||||
|
|
||||||
expression_t *number;
|
expression_t *expr;
|
||||||
result = parse_number(pool, input, len, &number);
|
result = parse_number(pool, input, len, &expr);
|
||||||
if (0 >= result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
if (used >= len) {
|
|
||||||
*out = number;
|
while (len > used) {
|
||||||
return used;
|
const expression_t *term0 = expr;
|
||||||
}
|
|
||||||
|
|
||||||
operator_t operator;
|
operator_t operator;
|
||||||
switch (input[used]) {
|
switch (input[used]) {
|
||||||
@ -52,17 +51,18 @@ static int parse_expression(
|
|||||||
}
|
}
|
||||||
++used;
|
++used;
|
||||||
|
|
||||||
expression_t *subexpr;
|
expression_t *term1;
|
||||||
result = parse_expression(pool, input + used, len - used, &subexpr);
|
result = parse_number(pool, input + used, len - used, &term1);
|
||||||
if (0 >= result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
|
|
||||||
expression_t *expr = allocate_expression(pool);
|
expr = allocate_expression(pool);
|
||||||
expr->is_number = false;
|
expr->is_number = false;
|
||||||
expr->application.operator= operator;
|
expr->application.operator= operator;
|
||||||
expr->application.operands[0] = number;
|
expr->application.operands[0] = term0;
|
||||||
expr->application.operands[1] = subexpr;
|
expr->application.operands[1] = term1;
|
||||||
|
}
|
||||||
|
|
||||||
*out = expr;
|
*out = expr;
|
||||||
return used;
|
return used;
|
||||||
|
@ -64,7 +64,7 @@ input_6_minus_2_with_no_spaces_is_read_as_a_subtract_application(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
input_1_plus_2_plus_3_is_parsed_as_a_right_nested_application(void)
|
input_1_plus_2_plus_3_is_parsed_as_a_left_nested_application(void)
|
||||||
{
|
{
|
||||||
init_memory_pool(&pool);
|
init_memory_pool(&pool);
|
||||||
const expression_t *result = read_expression(&pool, "1+2+3", 5);
|
const expression_t *result = read_expression(&pool, "1+2+3", 5);
|
||||||
@ -74,19 +74,19 @@ input_1_plus_2_plus_3_is_parsed_as_a_right_nested_application(void)
|
|||||||
|
|
||||||
const expression_t *operand0 = result->application.operands[0];
|
const expression_t *operand0 = result->application.operands[0];
|
||||||
ASSERT_NOT_NULL(operand0);
|
ASSERT_NOT_NULL(operand0);
|
||||||
ASSERT_TRUE(operand0->is_number);
|
ASSERT_FALSE(operand0->is_number);
|
||||||
ASSERT_EQUAL(1, operand0->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];
|
const expression_t *operand1 = result->application.operands[1];
|
||||||
ASSERT_NOT_NULL(operand1);
|
ASSERT_NOT_NULL(operand1);
|
||||||
ASSERT_FALSE(operand1->is_number);
|
ASSERT_TRUE(operand1->is_number);
|
||||||
ASSERT_EQUAL(OPERATOR_ADD, operand1->application.operator);
|
ASSERT_EQUAL(3, operand1->number);
|
||||||
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)
|
int main(void)
|
||||||
@ -99,6 +99,6 @@ int main(void)
|
|||||||
RUN_TEST(input_1_plus_2_with_no_spaces_is_read_as_an_add_application);
|
RUN_TEST(input_1_plus_2_with_no_spaces_is_read_as_an_add_application);
|
||||||
RUN_TEST(
|
RUN_TEST(
|
||||||
input_6_minus_2_with_no_spaces_is_read_as_a_subtract_application);
|
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_right_nested_application);
|
RUN_TEST(input_1_plus_2_plus_3_is_parsed_as_a_left_nested_application);
|
||||||
TESTING_END();
|
TESTING_END();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user