From 26a1adffd4ac7b4f1390ba0135ede850697c4dfa Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Fri, 20 Mar 2020 12:11:20 +0100 Subject: add string equality operators --- src/template.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src') 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() { " | \"!=\" " " | \"==\" " " | ;" - " print : /{{2}-? */ / *-?}}/ ;" + " print : /{{2}-? */ / *-?}}/ ;" " 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; -- cgit v1.2.3