aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 12:11:20 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 12:11:20 +0100
commit26a1adffd4ac7b4f1390ba0135ede850697c4dfa (patch)
treefb785b007b5e9decd9667c32782b2c17dd2f1f57 /src
parent306f057f985260d334a5e54405273f910120523c (diff)
downloadunja-26a1adffd4ac7b4f1390ba0135ede850697c4dfa.tar.gz
unja-26a1adffd4ac7b4f1390ba0135ede850697c4dfa.zip
add string equality operators
Diffstat (limited to 'src')
-rw-r--r--src/template.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/template.c b/src/template.c
index 9e739bc..a1439c4 100644
--- a/src/template.c
+++ b/src/template.c
@@ -100,7 +100,7 @@ mpc_parser_t *parser_init() {
" | <lexp> <spaces> \"!=\" <spaces> <lexp> "
" | <lexp> <spaces> \"==\" <spaces> <lexp> "
" | <lexp> ;"
- " print : /{{2}-? */ <lexp> / *-?}}/ ;"
+ " print : /{{2}-? */ <expression> / *-?}}/ ;"
" comment : \"{#\" /[^#][^#}]*/ \"#}\" ;"
" statement_open: /{\%-? */;"
" statement_close: / *-?\%}/;"
@@ -366,13 +366,28 @@ struct unja_object *eval_expression_value(mpc_ast_t* node, struct context *ctx)
return &null_object;
}
+struct unja_object *eval_string_infix_expression(struct unja_object *left, char *op, struct unja_object *right) {
+ struct unja_object *result;
+
+ if (strcmp(op, "+") == 0) {
+ result = make_string_object(left->string, right->string);
+ } else if (strcmp(op, "==") == 0) {
+ result = make_int_object(strcmp(left->string, right->string) == 0);
+ } else if(strcmp(op, "!=") == 0) {
+ result = make_int_object(strcmp(left->string, right->string) != 0);
+ } else {
+ errx(EXIT_FAILURE, "invalid string operator: %s", op);
+ }
+
+ object_free(left);
+ object_free(right);
+ return result;
+}
+
struct unja_object *eval_infix_expression(struct unja_object *left, char *op, struct unja_object *right) {
/* if operator is + and either left or right node is of type string: concat */
- if (op[0] == '+' && (left->type == OBJ_STRING && right->type == OBJ_STRING)) {
- struct unja_object *result = make_string_object(left->string, right->string);
- object_free(left);
- object_free(right);
- return result;
+ if (left->type == OBJ_STRING && right->type == OBJ_STRING) {
+ return eval_string_infix_expression(left, op, right);
}
int result;