|
| 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 | +} |
0 commit comments