Skip to content

Commit 9de6301

Browse files
committed
Merge branch 'filterx-operator-plus' into tip
2 parents 74cdb6c + de85823 commit 9de6301

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

lib/filterx/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ filterxinclude_HEADERS = \
1212
lib/filterx/expr-setattr.h \
1313
lib/filterx/expr-get-subscript.h \
1414
lib/filterx/expr-set-subscript.h \
15+
lib/filterx/expr-plus.h \
1516
lib/filterx/expr-variable.h \
1617
lib/filterx/expr-comparison.h \
1718
lib/filterx/filterx-object.h \
@@ -54,6 +55,7 @@ filterx_sources = \
5455
lib/filterx/expr-setattr.c \
5556
lib/filterx/expr-get-subscript.c \
5657
lib/filterx/expr-set-subscript.c \
58+
lib/filterx/expr-plus.c \
5759
lib/filterx/expr-variable.c \
5860
lib/filterx/expr-comparison.c \
5961
lib/filterx/filterx-object.c \

lib/filterx/expr-plus.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "expr-plus.h"
2+
#include "object-string.h"
3+
#include "filterx-eval.h"
4+
#include "scratch-buffers.h"
5+
6+
typedef struct FilterXOperatorPlus
7+
{
8+
FilterXBinaryOp super;
9+
} FilterXOperatorPlus;
10+
11+
static FilterXObject *
12+
_eval(FilterXExpr *s)
13+
{
14+
FilterXOperatorPlus *self = (FilterXOperatorPlus *) s;
15+
16+
FilterXObject *lhs_object = filterx_expr_eval(self->super.lhs);
17+
if (!lhs_object)
18+
{
19+
return NULL;
20+
}
21+
22+
FilterXObject *rhs_object = filterx_expr_eval(self->super.rhs);
23+
if (!rhs_object)
24+
{
25+
filterx_object_unref(lhs_object);
26+
return NULL;
27+
}
28+
29+
if (filterx_object_is_type(lhs_object, &FILTERX_TYPE_NAME(string)) &&
30+
filterx_object_is_type(rhs_object, &FILTERX_TYPE_NAME(string)))
31+
{
32+
gsize lhs_len, rhs_len;
33+
const gchar *lhs_value = filterx_string_get_value(lhs_object, &lhs_len);
34+
const gchar *rhs_value = filterx_string_get_value(rhs_object, &rhs_len);
35+
GString *buffer = scratch_buffers_alloc();
36+
37+
g_string_append_len(buffer, lhs_value, lhs_len);
38+
g_string_append_len(buffer, rhs_value, rhs_len);
39+
/* FIXME: support taking over the already allocated space */
40+
return filterx_string_new(buffer->str, buffer->len);
41+
}
42+
43+
filterx_eval_push_error("operator+ only works on strings", s, NULL);
44+
filterx_object_unref(lhs_object);
45+
filterx_object_unref(rhs_object);
46+
return NULL;
47+
}
48+
49+
FilterXExpr *
50+
filterx_operator_plus_new(FilterXExpr *lhs, FilterXExpr *rhs)
51+
{
52+
FilterXOperatorPlus *self = g_new0(FilterXOperatorPlus, 1);
53+
filterx_binary_op_init_instance(&self->super, lhs, rhs);
54+
self->super.super.eval = _eval;
55+
return &self->super.super;
56+
}

lib/filterx/expr-plus.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef FILTERX_EXPR_PLUS_H_INCLUDED
2+
#define FILTERX_EXPR_PLUS_H_INCLUDED
3+
4+
#include "filterx-expr.h"
5+
6+
FilterXExpr *filterx_operator_plus_new(FilterXExpr *lhs, FilterXExpr *rhs);
7+
8+
#endif

lib/filterx/filterx-grammar.ym

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "filterx/expr-literal-generator.h"
5353
#include "filterx/expr-shorthand.h"
5454
#include "filterx/expr-regexp.h"
55+
#include "filterx/expr-plus.h"
5556

5657
#include "template/templates.h"
5758

@@ -299,6 +300,7 @@ expr
299300
| expr KW_STR_GT expr { $$ = filterx_comparison_new($1, $3, FCMPX_STRING_BASED | FCMPX_GT); }
300301
| expr KW_TAV_EQ expr { $$ = filterx_comparison_new($1, $3, FCMPX_TYPE_AND_VALUE_BASED | FCMPX_EQ); }
301302
| expr KW_TAV_NE expr { $$ = filterx_comparison_new($1, $3, FCMPX_TYPE_AND_VALUE_BASED | FCMPX_NE ); }
303+
| expr '+' expr { $$ = filterx_operator_plus_new($1, $3); }
302304
| '(' expr ')' { $$ = $2; }
303305
| ternary { $$ = $1; }
304306
| KW_ISSET '(' expr ')' { $$ = filterx_isset_new($3); }

0 commit comments

Comments
 (0)