summaryrefslogtreecommitdiff
path: root/src/lib/eolian/database_expr_api.c
blob: 2d3f81c73d873b9e1a55b4cff6314c29e1af1638 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <Eina.h>

#include "eolian_database.h"

EAPI Eolian_Value
eolian_expression_eval(const Eolian_Expression *expr, Eolian_Expression_Mask m)
{
   Eolian_Value err;
   err.type = EOLIAN_EXPR_UNKNOWN;
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, err);
   return database_expr_eval(NULL, (Eolian_Expression *)expr, m, NULL, NULL);
}

static void
_append_char_escaped(Eina_Strbuf *buf, char c)
{
   switch (c)
     {
      case '\'': eina_strbuf_append(buf, "\\\'"); break;
      case '\"': eina_strbuf_append(buf, "\\\""); break;
      case '\?': eina_strbuf_append(buf, "\\\?"); break;
      case '\\': eina_strbuf_append(buf, "\\\\"); break;
      case '\a': eina_strbuf_append(buf, "\\a"); break;
      case '\b': eina_strbuf_append(buf, "\\b"); break;
      case '\f': eina_strbuf_append(buf, "\\f"); break;
      case '\n': eina_strbuf_append(buf, "\\n"); break;
      case '\r': eina_strbuf_append(buf, "\\r"); break;
      case '\t': eina_strbuf_append(buf, "\\t"); break;
      case '\v': eina_strbuf_append(buf, "\\v"); break;
      default:
         if ((c < 32) || (c > 126))
           eina_strbuf_append_printf(buf, "\\x%X", (unsigned char)c);
         else
           eina_strbuf_append_char(buf, c);
         break;
     }
}

static void
_number_to_str(const Eolian_Value *v, Eina_Strbuf *buf)
{
   switch (v->type)
     {
      case EOLIAN_EXPR_INT:
        eina_strbuf_append_printf(buf, "%d", v->value.i); break;
      case EOLIAN_EXPR_UINT:
        eina_strbuf_append_printf(buf, "%uU", v->value.u); break;
      case EOLIAN_EXPR_LONG:
        eina_strbuf_append_printf(buf, "%ldL", v->value.l); break;
      case EOLIAN_EXPR_ULONG:
        eina_strbuf_append_printf(buf, "%luUL", v->value.ul); break;
      case EOLIAN_EXPR_LLONG:
        eina_strbuf_append_printf(buf, "%ldLL", (long)v->value.ll); break;
      case EOLIAN_EXPR_ULLONG:
        eina_strbuf_append_printf(buf, "%luULL", (unsigned long)v->value.ull);
        break;
      case EOLIAN_EXPR_FLOAT:
        eina_strbuf_append_printf(buf, "%ff", v->value.f); break;
      case EOLIAN_EXPR_DOUBLE:
        eina_strbuf_append_printf(buf, "%f", v->value.d); break;
      default:
        break;
     }
}

EAPI Eina_Stringshare *
eolian_expression_value_to_literal(const Eolian_Value *val)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(val, NULL);
   switch (val->type)
     {
      case EOLIAN_EXPR_BOOL:
        return eina_stringshare_add(val->value.b ? "EINA_TRUE"
                                                 : "EINA_FALSE");
      case EOLIAN_EXPR_NULL:
        return eina_stringshare_add("NULL");
      case EOLIAN_EXPR_CHAR:
        {
           char c = val->value.c;
           Eina_Strbuf *buf = eina_strbuf_new();
           const char *ret;
           eina_strbuf_append_char(buf, '\'');
           _append_char_escaped(buf, c);
           eina_strbuf_append_char(buf, '\'');
           ret = eina_stringshare_add(eina_strbuf_string_get(buf));
           eina_strbuf_free(buf);
           return ret;
        }
      case EOLIAN_EXPR_STRING:
        {
           const char *ret;
           char *c = (char*)val->value.s;
           Eina_Strbuf *buf = eina_strbuf_new();
           eina_strbuf_append_char(buf, '\"');
           while (*c) _append_char_escaped(buf, *(c++));
           eina_strbuf_append_char(buf, '\"');
           ret = eina_stringshare_add(eina_strbuf_string_get(buf));
           eina_strbuf_free(buf);
           return ret;
        }
      case EOLIAN_EXPR_INT:
      case EOLIAN_EXPR_UINT:
      case EOLIAN_EXPR_LONG:
      case EOLIAN_EXPR_ULONG:
      case EOLIAN_EXPR_LLONG:
      case EOLIAN_EXPR_ULLONG:
      case EOLIAN_EXPR_FLOAT:
      case EOLIAN_EXPR_DOUBLE:
        {
           const char *ret;
           Eina_Strbuf *buf = eina_strbuf_new();
           _number_to_str(val, buf);
           ret = eina_stringshare_add(eina_strbuf_string_get(buf));
           eina_strbuf_free(buf);
           return ret;
        }
      default:
        return NULL;
     }
}

static const char *_binops[] = {
    "+", "-", "*", "/", "%",
    "==", "!=", ">", "<", ">=", "<=",
    "&&", "||",
    "&", "|", "^", "<<", ">>"
};

static const char *_unops[] = {
    "-", "+", "!", "~"
};

static Eina_Bool
_expr_serialize(const Eolian_Expression *expr, Eina_Strbuf *buf, Eina_Bool outer)
{
   switch (expr->type)
     {
      case EOLIAN_EXPR_UNKNOWN:
        return EINA_FALSE;
      case EOLIAN_EXPR_INT:
      case EOLIAN_EXPR_UINT:
      case EOLIAN_EXPR_LONG:
      case EOLIAN_EXPR_ULONG:
      case EOLIAN_EXPR_LLONG:
      case EOLIAN_EXPR_ULLONG:
      case EOLIAN_EXPR_FLOAT:
      case EOLIAN_EXPR_DOUBLE:
      case EOLIAN_EXPR_STRING:
      case EOLIAN_EXPR_CHAR:
        {
           Eolian_Value v;
           v.type = expr->type;
           v.value = expr->value;
           const char *x = eolian_expression_value_to_literal(&v);
           if (!x)
             return EINA_FALSE;
           eina_strbuf_append(buf, x);
           eina_stringshare_del(x);
           break;
        }
      case EOLIAN_EXPR_NULL:
        eina_strbuf_append(buf, "null");
        break;
      case EOLIAN_EXPR_BOOL:
        eina_strbuf_append(buf, expr->value.b ? "true" : "false");
        break;
      case EOLIAN_EXPR_NAME:
        {
           eina_strbuf_append(buf, expr->value.s);
           break;
        }
      case EOLIAN_EXPR_UNARY:
        eina_strbuf_append(buf, _unops[expr->unop]);
        _expr_serialize(expr->expr, buf, EINA_FALSE);
        break;
      case EOLIAN_EXPR_BINARY:
        if (!outer)
          eina_strbuf_append_char(buf, '(');
        _expr_serialize(expr->lhs, buf, EINA_FALSE);
        eina_strbuf_append_printf(buf, " %s ", _binops[expr->binop]);
        _expr_serialize(expr->rhs, buf, EINA_FALSE);
        if (!outer)
          eina_strbuf_append_char(buf, ')');
        break;
      default:
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

EAPI Eina_Stringshare *
eolian_expression_serialize(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, NULL);
   Eina_Strbuf *buf = eina_strbuf_new();
   if (!_expr_serialize(expr, buf, EINA_TRUE))
     {
        eina_strbuf_free(buf);
        return NULL;
     }
   const char *ret = eina_stringshare_add(eina_strbuf_string_get(buf));
   eina_strbuf_free(buf);
   return ret;
}

EAPI Eolian_Expression_Type
eolian_expression_type_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, EOLIAN_EXPR_UNKNOWN);
   return expr->type;
}

EAPI Eolian_Binary_Operator
eolian_expression_binary_operator_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, EOLIAN_BINOP_INVALID);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type == EOLIAN_EXPR_BINARY,
                                   EOLIAN_BINOP_INVALID);
   return expr->binop;
}

EAPI const Eolian_Expression *
eolian_expression_binary_lhs_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type == EOLIAN_EXPR_BINARY, NULL);
   return expr->lhs;
}

EAPI const Eolian_Expression *
eolian_expression_binary_rhs_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type == EOLIAN_EXPR_BINARY, NULL);
   return expr->rhs;
}

EAPI Eolian_Unary_Operator
eolian_expression_unary_operator_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, EOLIAN_UNOP_INVALID);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type == EOLIAN_EXPR_UNARY,
                                   EOLIAN_UNOP_INVALID);
   return expr->unop;
}

EAPI const Eolian_Expression *
eolian_expression_unary_expression_get(const Eolian_Expression *expr)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type == EOLIAN_EXPR_UNARY, NULL);
   return expr->expr;
}

EAPI Eolian_Value
eolian_expression_value_get(const Eolian_Expression *expr)
{
   Eolian_Value v;
   v.type = EOLIAN_EXPR_UNKNOWN;
   EINA_SAFETY_ON_NULL_RETURN_VAL(expr, v);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(expr->type != EOLIAN_EXPR_UNKNOWN
                                && expr->type != EOLIAN_EXPR_BINARY
                                && expr->type != EOLIAN_EXPR_UNARY, v);
   v.type  = expr->type;
   v.value = expr->value;
   return v;
}