aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/template.c28
-rw-r--r--tests/test_template.c22
2 files changed, 34 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);
diff --git a/tests/test_template.c b/tests/test_template.c
index cb23dbf..4521d6c 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -89,6 +89,27 @@ TEST(expr_add) {
hashmap_free(ctx);
}
+
+TEST(expr_op_precedence) {
+ struct {
+ char *input;
+ char *expected_output;
+ } tests[] = {
+ {"{{ 5 * 2 + 1 }}.", "11."},
+ {"{{ 1 + 5 * 2 }}.", "11."},
+ {"{{ 10 / 2 + 1 }}.", "6."},
+ {"{{ 1 + 10 / 2 }}.", "6."},
+ };
+
+ struct hashmap *ctx = hashmap_new();
+ for (int i=0; i < ARRAY_SIZE(tests); i++) {
+ char *output = template_string(tests[i].input, ctx);
+ assert_str(output, tests[i].expected_output);
+ free(output);
+ }
+ hashmap_free(ctx);
+}
+
TEST(expr_subtract) {
char *input = "Hello {{ 5 - 5 }}.";
char *output = template_string(input, NULL);
@@ -211,6 +232,7 @@ TEST(if_block) {
{"{% if foobar %}1{% endif %}.", "."},
{"{% if name %}1{% endif %}.", "1."},
{"{% if age > 10 %}1{% endif %}.", "1."},
+ {"{% if 10+1 > 10 %}1{% endif %}.", "1."},
};
struct hashmap *ctx = hashmap_new();