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 <ctype.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
static int parse_expression(
|
|
||||||
memory_pool_t *pool, const char *input, int remaining,
|
|
||||||
expression_t *out);
|
|
||||||
|
|
||||||
static int parse_number(
|
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;
|
(void)pool;
|
||||||
|
|
||||||
int value = 0, used = 0;
|
int value = 0, used = 0;
|
||||||
while (remaining > 0 && isdigit(*input)) {
|
while (used < len && isdigit(input[used])) {
|
||||||
value *= 10;
|
value *= 10;
|
||||||
value += *input - '0';
|
value += input[used] - '0';
|
||||||
++used;
|
++used;
|
||||||
--remaining;
|
|
||||||
++input;
|
|
||||||
}
|
}
|
||||||
|
if (0 == used)
|
||||||
|
return 0;
|
||||||
|
|
||||||
out->is_number = true;
|
expression_t *number = allocate_expression(pool);
|
||||||
out->number = value;
|
number->is_number = true;
|
||||||
|
number->number = value;
|
||||||
|
*out = number;
|
||||||
return used;
|
return used;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_application(
|
static int parse_expression(
|
||||||
memory_pool_t *pool, const char *input, int remaining, 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 *operand0 = allocate_expression(pool);
|
expression_t *number;
|
||||||
result = parse_number(pool, input, remaining, operand0);
|
result = parse_number(pool, input, len, &number);
|
||||||
if (0 == result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
remaining -= result;
|
if (used >= len) {
|
||||||
input += result;
|
*out = number;
|
||||||
|
return used;
|
||||||
|
}
|
||||||
|
|
||||||
if (remaining <= 0)
|
|
||||||
return 0;
|
|
||||||
operator_t operator;
|
operator_t operator;
|
||||||
switch (*input) {
|
switch (input[used]) {
|
||||||
case '+':
|
case '+':
|
||||||
operator= OPERATOR_ADD;
|
operator= OPERATOR_ADD;
|
||||||
break;
|
break;
|
||||||
@ -53,41 +51,27 @@ static int parse_application(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
++used;
|
++used;
|
||||||
--remaining;
|
|
||||||
++input;
|
|
||||||
|
|
||||||
expression_t *operand1 = allocate_expression(pool);
|
expression_t *subexpr;
|
||||||
result = parse_expression(pool, input, remaining, operand1);
|
result = parse_expression(pool, input + used, len - used, &subexpr);
|
||||||
if (0 == result)
|
if (0 >= result)
|
||||||
return 0;
|
return 0;
|
||||||
used += result;
|
used += result;
|
||||||
remaining -= result;
|
|
||||||
input += result;
|
|
||||||
|
|
||||||
out->is_number = false;
|
expression_t *expr = allocate_expression(pool);
|
||||||
out->application.operator= operator;
|
expr->is_number = false;
|
||||||
out->application.operands[0] = operand0;
|
expr->application.operator= operator;
|
||||||
out->application.operands[1] = operand1;
|
expr->application.operands[0] = number;
|
||||||
|
expr->application.operands[1] = subexpr;
|
||||||
|
|
||||||
|
*out = expr;
|
||||||
return used;
|
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 *
|
const expression_t *
|
||||||
read_expression(memory_pool_t *pool, const char *input, int len)
|
read_expression(memory_pool_t *pool, const char *input, int len)
|
||||||
{
|
{
|
||||||
expression_t *expression = allocate_expression(pool);
|
expression_t *expression;
|
||||||
if (NULL == expression)
|
parse_expression(pool, input, len, &expression);
|
||||||
return NULL;
|
|
||||||
parse_expression(pool, input, len, expression);
|
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user