Refactor parsing routines
This commit is contained in:
parent
bb622a0766
commit
5ea066b75e
76
lib/reader.c
76
lib/reader.c
@ -3,46 +3,44 @@
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static int parse_expression(
|
||||
memory_pool_t *pool, const char *input, int remaining,
|
||||
expression_t *out);
|
||||
|
||||
static int parse_number(
|
||||
memory_pool_t *pool, const char *input, int remaining, expression_t *out)
|
||||
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||
{
|
||||
(void)pool;
|
||||
|
||||
int value = 0, used = 0;
|
||||
while (remaining > 0 && isdigit(*input)) {
|
||||
while (used < len && isdigit(input[used])) {
|
||||
value *= 10;
|
||||
value += *input - '0';
|
||||
value += input[used] - '0';
|
||||
++used;
|
||||
--remaining;
|
||||
++input;
|
||||
}
|
||||
if (0 == used)
|
||||
return 0;
|
||||
|
||||
out->is_number = true;
|
||||
out->number = value;
|
||||
expression_t *number = allocate_expression(pool);
|
||||
number->is_number = true;
|
||||
number->number = value;
|
||||
*out = number;
|
||||
return used;
|
||||
}
|
||||
|
||||
static int parse_application(
|
||||
memory_pool_t *pool, const char *input, int remaining, expression_t *out)
|
||||
static int parse_expression(
|
||||
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
expression_t *operand0 = allocate_expression(pool);
|
||||
result = parse_number(pool, input, remaining, operand0);
|
||||
if (0 == result)
|
||||
expression_t *number;
|
||||
result = parse_number(pool, input, len, &number);
|
||||
if (0 >= result)
|
||||
return 0;
|
||||
used += result;
|
||||
remaining -= result;
|
||||
input += result;
|
||||
if (used >= len) {
|
||||
*out = number;
|
||||
return used;
|
||||
}
|
||||
|
||||
if (remaining <= 0)
|
||||
return 0;
|
||||
operator_t operator;
|
||||
switch (*input) {
|
||||
switch (input[used]) {
|
||||
case '+':
|
||||
operator= OPERATOR_ADD;
|
||||
break;
|
||||
@ -53,41 +51,27 @@ static int parse_application(
|
||||
return 0;
|
||||
}
|
||||
++used;
|
||||
--remaining;
|
||||
++input;
|
||||
|
||||
expression_t *operand1 = allocate_expression(pool);
|
||||
result = parse_expression(pool, input, remaining, operand1);
|
||||
if (0 == result)
|
||||
expression_t *subexpr;
|
||||
result = parse_expression(pool, input + used, len - used, &subexpr);
|
||||
if (0 >= result)
|
||||
return 0;
|
||||
used += result;
|
||||
remaining -= result;
|
||||
input += result;
|
||||
|
||||
out->is_number = false;
|
||||
out->application.operator= operator;
|
||||
out->application.operands[0] = operand0;
|
||||
out->application.operands[1] = operand1;
|
||||
expression_t *expr = allocate_expression(pool);
|
||||
expr->is_number = false;
|
||||
expr->application.operator= operator;
|
||||
expr->application.operands[0] = number;
|
||||
expr->application.operands[1] = subexpr;
|
||||
|
||||
*out = expr;
|
||||
return used;
|
||||
}
|
||||
|
||||
static int parse_expression(
|
||||
memory_pool_t *pool, const char *input, int remaining, expression_t *out)
|
||||
{
|
||||
int result = parse_application(pool, input, remaining, out);
|
||||
if (0 != result)
|
||||
return result;
|
||||
else
|
||||
return parse_number(pool, input, remaining, out);
|
||||
}
|
||||
|
||||
const expression_t *
|
||||
read_expression(memory_pool_t *pool, const char *input, int len)
|
||||
{
|
||||
expression_t *expression = allocate_expression(pool);
|
||||
if (NULL == expression)
|
||||
return NULL;
|
||||
parse_expression(pool, input, len, expression);
|
||||
expression_t *expression;
|
||||
parse_expression(pool, input, len, &expression);
|
||||
return expression;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user