Support * and / in reader
This commit is contained in:
56
lib/reader.c
56
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user