Allow whitespace in expressions
This commit is contained in:
39
lib/reader.c
39
lib/reader.c
@@ -6,6 +6,15 @@
|
||||
static int parse_expression(
|
||||
memory_pool_t *pool, const char *input, int len, expression_t **out);
|
||||
|
||||
static int parse_whitespace(const char *input, int len)
|
||||
{
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (!isspace(input[i]))
|
||||
return i;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static int parse_number(
|
||||
memory_pool_t *pool, const char *input, int len, expression_t **out)
|
||||
{
|
||||
@@ -29,10 +38,19 @@ 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);
|
||||
int result, used = 1;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
result = parse_expression(pool, input + used, len - used, out);
|
||||
if (0 == result)
|
||||
return 0;
|
||||
const int used = result + 1;
|
||||
used += result;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
return len >= used && input[used] == ')' ? used + 1 : 0;
|
||||
} else {
|
||||
return parse_number(pool, input, len, out);
|
||||
@@ -53,6 +71,9 @@ static int parse_term(
|
||||
while (len > used) {
|
||||
const expression_t *term0 = expr;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
operator_t operator;
|
||||
if ('*' == input[used])
|
||||
operator= OPERATOR_MULTIPLY;
|
||||
@@ -62,6 +83,9 @@ static int parse_term(
|
||||
break;
|
||||
++used;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
expression_t *term1;
|
||||
result = parse_factor(pool, input + used, len - used, &term1);
|
||||
if (0 >= result)
|
||||
@@ -84,8 +108,11 @@ static int parse_expression(
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
expression_t *expr;
|
||||
result = parse_term(pool, input, len, &expr);
|
||||
result = parse_term(pool, input + used, len - used, &expr);
|
||||
if (0 >= result)
|
||||
return 0;
|
||||
used += result;
|
||||
@@ -93,6 +120,9 @@ static int parse_expression(
|
||||
while (len > used) {
|
||||
const expression_t *term0 = expr;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
operator_t operator;
|
||||
if ('+' == input[used])
|
||||
operator= OPERATOR_ADD;
|
||||
@@ -102,6 +132,9 @@ static int parse_expression(
|
||||
break;
|
||||
++used;
|
||||
|
||||
result = parse_whitespace(input + used, len - used);
|
||||
used += result;
|
||||
|
||||
expression_t *term1;
|
||||
result = parse_term(pool, input + used, len - used, &term1);
|
||||
if (0 >= result)
|
||||
|
||||
Reference in New Issue
Block a user