aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-19 20:05:39 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-19 20:05:39 +0100
commitfd6465aad222a00d4c805043b7c0cf3b2d0f3c6b (patch)
treee40554a08d2037af60b8efbc4ecd616c09dd1dd1 /src
parent3353eb18a8ff2480c089ab083c474bf2a2a62b96 (diff)
downloadunja-fd6465aad222a00d4c805043b7c0cf3b2d0f3c6b.tar.gz
unja-fd6465aad222a00d4c805043b7c0cf3b2d0f3c6b.zip
add operator precedence
Diffstat (limited to 'src')
-rw-r--r--src/template.c28
1 files changed, 12 insertions, 16 deletions
diff --git a/src/template.c b/src/template.c
index 3d336d0..7b0d65b 100644
--- a/src/template.c
+++ b/src/template.c
@@ -92,9 +92,9 @@ mpc_parser_t *parser_init() {
" string : '\"' /([^\"])*/ '\"' ;"
" op : '+' | '-' | '*' | '/' | '>' | '<';"
" factor : '(' <expression> ')' | <symbol> | <number> | <string> ;"
- " term : <spaces> <factor> <spaces> (('*' | '/' | '%') <spaces> <factor> <spaces>)* ;"
- " expression: <term> (('+' | '-') <term>)* ;"
- " print : /{{2}-?/ <expression> /-?}}/ ;"
+ " term : <factor> (<spaces> ('*' | '/' | '%') <spaces> <factor>)* ;"
+ " expression: <term> (<spaces> ('+' | '-' | '>' | '<') <spaces> <term>)* ;"
+ " print : /{{2}-? */ <expression> / *-?}}/ ;"
" comment : \"{#\" /[^#][^#}]*/ \"#}\" ;"
" statement_open: /{\%-? */;"
" statement_close: / *-?\%}/;"
@@ -385,24 +385,24 @@ struct unja_object *eval_infix_expression(struct unja_object *left, char *op, st
struct unja_object *eval_expression(mpc_ast_t* expr, struct context *ctx) {
/* singular term */
- if (strstr(expr->tag, "term")) {
- return eval_expression_value(expr->children[1], ctx);
+ if (expr->children_num == 0 || strstr(expr->tag, "string|")) {
+ return eval_expression_value(expr, ctx);
}
/* otherwise: with operator */
int offset = 0;
- struct object *result;
- mpc_ast_t *left_node = expr->children[0]->children[1];
- struct unja_object *left = eval_expression_value(left_node, ctx);
+ struct unja_object *result;
+ mpc_ast_t *left_node = expr->children[0];
+ struct unja_object *left = eval_expression(left_node, ctx);
while (offset < expr->children_num - 1) {
- mpc_ast_t *op = expr->children[offset+1];
- mpc_ast_t *right_node = expr->children[offset+2]->children[1];
- struct unja_object *right = eval_expression_value(right_node, ctx);
+ mpc_ast_t *op = expr->children[offset+2];
+ mpc_ast_t *right_node = expr->children[offset+4];
+ struct unja_object *right = eval_expression(right_node, ctx);
result = eval_infix_expression(left, op->contents, right);
left = result;
- offset += 2;
+ offset += 4;
}
return result;
@@ -412,10 +412,6 @@ struct unja_object *eval_expression(mpc_ast_t* expr, struct context *ctx) {
int eval(struct buffer *buf, mpc_ast_t* t, struct context *ctx) {
static int trim_whitespace = 0;
- #if DEBUG
- printf("Node: %s = %s\n", t->tag, t->contents);
- #endif
-
// maybe eat whitespace going backward
if (t->children_num > 0 && strstr(t->children[0]->contents, "-")) {
buf->string = trim_trailing_whitespace(buf->string);