[go: up one dir, main page]

File: tmpl-expr.c

package info (click to toggle)
template-glib 3.36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 792 kB
  • sloc: ansic: 6,565; yacc: 288; lex: 237; python: 12; javascript: 11; makefile: 9
file content (451 lines) | stat: -rw-r--r-- 10,451 bytes parent folder | download | duplicates (3)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* tmpl-expr.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tmpl-expr.h"
#include "tmpl-expr-private.h"
#include "tmpl-expr-parser-private.h"

static gpointer tmpl_expr_new     (TmplExprType  type);
static void     tmpl_expr_destroy (TmplExpr     *expr);

G_DEFINE_BOXED_TYPE (TmplExpr, tmpl_expr, tmpl_expr_ref, tmpl_expr_unref)

TmplExpr *
tmpl_expr_ref (TmplExpr *self)
{
  g_return_val_if_fail (self != NULL, NULL);
  g_return_val_if_fail (self->any.ref_count > 0, NULL);

  g_atomic_int_inc (&self->any.ref_count);

  return self;
}

void
tmpl_expr_unref (TmplExpr *self)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (self->any.ref_count > 0);

  if (g_atomic_int_dec_and_test (&self->any.ref_count))
    tmpl_expr_destroy (self);
}

static gpointer
tmpl_expr_new (TmplExprType type)
{
  TmplExpr *ret;

  ret = g_slice_new0 (TmplExpr);
  ret->any.type = type;
  ret->any.ref_count = 1;

  return ret;
}

static void
tmpl_expr_destroy (TmplExpr *self)
{
  g_assert (self != NULL);
  g_assert (self->any.ref_count == 0);

  /* Set fields to NULL to aid in debugging. */

  switch (self->any.type)
    {
    case TMPL_EXPR_ADD:
    case TMPL_EXPR_DIV:
    case TMPL_EXPR_EQ:
    case TMPL_EXPR_GT:
    case TMPL_EXPR_GTE:
    case TMPL_EXPR_LT:
    case TMPL_EXPR_LTE:
    case TMPL_EXPR_MUL:
    case TMPL_EXPR_NE:
    case TMPL_EXPR_SUB:
    case TMPL_EXPR_UNARY_MINUS:
    case TMPL_EXPR_AND:
    case TMPL_EXPR_OR:
    case TMPL_EXPR_INVERT_BOOLEAN:
    case TMPL_EXPR_ARGS:
      g_clear_pointer (&self->simple.left, tmpl_expr_unref);
      g_clear_pointer (&self->simple.right, tmpl_expr_unref);
      break;

    case TMPL_EXPR_USER_FN_CALL:
      g_clear_pointer (&self->user_fn_call.symbol, g_free);
      g_clear_pointer (&self->user_fn_call.params, tmpl_expr_unref);
      break;

    case TMPL_EXPR_ANON_FN_CALL:
      g_clear_pointer (&self->anon_fn_call.anon, tmpl_expr_unref);
      g_clear_pointer (&self->user_fn_call.params, tmpl_expr_unref);
      break;

    case TMPL_EXPR_GETATTR:
      g_clear_pointer (&self->getattr.attr, g_free);
      g_clear_pointer (&self->getattr.left, tmpl_expr_unref);
      break;

    case TMPL_EXPR_SETATTR:
      g_clear_pointer (&self->setattr.attr, g_free);
      g_clear_pointer (&self->setattr.left, tmpl_expr_unref);
      g_clear_pointer (&self->setattr.right, tmpl_expr_unref);
      break;

    case TMPL_EXPR_STMT_LIST:
      g_clear_pointer (&self->stmt_list.stmts, g_ptr_array_unref);
      break;

    case TMPL_EXPR_BOOLEAN:
    case TMPL_EXPR_NUMBER:
      break;

    case TMPL_EXPR_STRING:
      g_clear_pointer (&self->string.value, g_free);
      break;

    case TMPL_EXPR_IF:
    case TMPL_EXPR_WHILE:
      g_clear_pointer (&self->flow.condition, tmpl_expr_unref);
      g_clear_pointer (&self->flow.primary, tmpl_expr_unref);
      g_clear_pointer (&self->flow.secondary, tmpl_expr_unref);
      break;

    case TMPL_EXPR_SYMBOL_REF:
      g_clear_pointer (&self->sym_ref.symbol, g_free);
      break;

    case TMPL_EXPR_SYMBOL_ASSIGN:
      g_clear_pointer (&self->sym_assign.symbol, g_free);
      g_clear_pointer (&self->sym_assign.right, tmpl_expr_unref);
      break;

    case TMPL_EXPR_FN_CALL:
      g_clear_pointer (&self->fn_call.param, tmpl_expr_unref);
      break;

    case TMPL_EXPR_GI_CALL:
      g_clear_pointer (&self->gi_call.name, g_free);
      g_clear_pointer (&self->gi_call.object, tmpl_expr_unref);
      g_clear_pointer (&self->gi_call.params, tmpl_expr_unref);
      break;

    case TMPL_EXPR_REQUIRE:
      g_clear_pointer (&self->require.name, g_free);
      g_clear_pointer (&self->require.version, g_free);
      break;

    case TMPL_EXPR_FUNC:
      g_clear_pointer (&self->func.name, g_free);
      g_clear_pointer (&self->func.symlist, g_strfreev);
      g_clear_pointer (&self->func.list, tmpl_expr_unref);
      break;

    case TMPL_EXPR_NOP:
    case TMPL_EXPR_NULL:
      /* This should never happen,
       * but just log it if it does */
      g_return_if_reached ();

    default:
      g_assert_not_reached ();
    }

  g_slice_free (TmplExpr, self);
}

/**
 * tmpl_expr_new_stmt_list:
 * @stmts: (transfer full) (element-type TmplExpr): a #GPtrArray of
 *   #TmplExpr which will be evaluated in sequence.
 *
 * Creates a new statement list for which the last item will be
 * used as the "return value" from execution.
 *
 * Returns: (transfer full): a new #TmplExpr
 *
 * Since: 3.36
 */
TmplExpr *
tmpl_expr_new_stmt_list (GPtrArray *stmts)
{
  TmplExpr *ret;

  g_return_val_if_fail (stmts != NULL, NULL);

  g_ptr_array_set_free_func (stmts, (GDestroyNotify)tmpl_expr_unref);

  ret = tmpl_expr_new (TMPL_EXPR_STMT_LIST);
  ((TmplExprStmtList *)ret)->stmts = stmts;

  return ret;
}

TmplExpr *
tmpl_expr_new_boolean (gboolean value)
{
  TmplExpr *ret;

  ret = tmpl_expr_new (TMPL_EXPR_BOOLEAN);
  ((TmplExprBoolean *)ret)->value = !!value;

  return ret;
}

TmplExpr *
tmpl_expr_new_number (gdouble value)
{
  TmplExprNumber *ret;

  ret = tmpl_expr_new (TMPL_EXPR_NUMBER);
  ret->number = value;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_string (const gchar *str,
                      gssize       length)
{
  TmplExprString *ret;

  ret = tmpl_expr_new (TMPL_EXPR_STRING);

  if (length < 0)
    ret->value = g_strdup (str);
  else
    ret->value = g_strndup (str, length);

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_require (const gchar *typelib,
                       const gchar *version)
{
  TmplExprRequire *ret;

  ret = tmpl_expr_new (TMPL_EXPR_REQUIRE);
  ret->name = g_strdup (typelib);
  ret->version = g_strdup (version);

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_simple (TmplExprType  type,
                      TmplExpr     *left,
                      TmplExpr     *right)
{
  TmplExprSimple *ret;

  ret = tmpl_expr_new (type);
  ret->left = left;
  ret->right = right;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_invert_boolean (TmplExpr *left)
{
  TmplExprSimple *ret;

  ret = tmpl_expr_new (TMPL_EXPR_INVERT_BOOLEAN);
  ret->left = left;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_flow (TmplExprType  type,
                    TmplExpr     *condition,
                    TmplExpr     *primary,
                    TmplExpr     *secondary)
{
  TmplExprFlow *ret;

  ret = tmpl_expr_new (type);
  ret->condition = condition;
  ret->primary = primary;
  ret->secondary = secondary;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_getattr (TmplExpr    *left,
                       const gchar *attr)
{
  TmplExprGetattr *ret;

  ret = tmpl_expr_new (TMPL_EXPR_GETATTR);
  ret->left = left;
  ret->attr = g_strdup (attr);

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_setattr (TmplExpr    *left,
                       const gchar *attr,
                       TmplExpr    *right)
{
  TmplExprSetattr *ret;

  ret = tmpl_expr_new (TMPL_EXPR_SETATTR);
  ret->left = left;
  ret->attr = g_strdup (attr);
  ret->right = right;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_symbol_ref (const gchar *symbol)
{
  TmplExprSymbolRef *ret;

  ret = tmpl_expr_new (TMPL_EXPR_SYMBOL_REF);
  ret->symbol = g_strdup (symbol);

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_symbol_assign (const gchar *symbol,
                             TmplExpr    *right)
{
  TmplExprSymbolAssign *ret;

  ret = tmpl_expr_new (TMPL_EXPR_SYMBOL_ASSIGN);
  ret->symbol = g_strdup (symbol);
  ret->right = right;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_fn_call (TmplExprBuiltin  builtin,
                       TmplExpr        *param)
{
  TmplExprFnCall *ret;

  ret = tmpl_expr_new (TMPL_EXPR_FN_CALL);
  ret->builtin = builtin;
  ret->param = param;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_user_fn_call (const gchar *symbol,
                            TmplExpr    *params)
{
  TmplExprUserFnCall *ret;

  ret = tmpl_expr_new (TMPL_EXPR_USER_FN_CALL);
  ret->symbol = g_strdup (symbol);
  ret->params = params;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_gi_call (TmplExpr    *object,
                       const gchar *name,
                       TmplExpr    *params)
{
  TmplExprGiCall *ret;

  ret = tmpl_expr_new (TMPL_EXPR_GI_CALL);
  ret->object = object;
  ret->name = g_strdup (name);
  ret->params = params;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_from_string (const gchar  *str,
                       GError      **error)
{
  TmplExprParser parser = { 0 };
  TmplExpr *ret = NULL;

  g_return_val_if_fail (str != NULL, NULL);

  if (!tmpl_expr_parser_init (&parser, error))
      return NULL;

  if (tmpl_expr_parser_parse_string (&parser, str, error))
    ret = parser.ast, parser.ast = NULL;

  tmpl_expr_parser_destroy (&parser);

  return ret;
}

TmplExpr *
tmpl_expr_new_func (char      *name,
                    char     **symlist,
                    TmplExpr  *list)
{
  TmplExprFunc *ret;

  if (list == NULL)
    list = tmpl_expr_new_nop ();

  ret = tmpl_expr_new (TMPL_EXPR_FUNC);
  ret->name = name;
  ret->symlist = symlist;
  ret->list = list;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_anon_call (TmplExpr *func,
                         TmplExpr *params)
{
  TmplExprAnonFnCall *ret;

  g_return_val_if_fail (func != NULL, NULL);
  g_return_val_if_fail (func->any.type == TMPL_EXPR_FUNC, NULL);

  ret = tmpl_expr_new (TMPL_EXPR_ANON_FN_CALL);
  ret->anon = func;
  ret->params = params;

  return (TmplExpr *)ret;
}

TmplExpr *
tmpl_expr_new_nop (void)
{
  static TmplExpr interned = { .any.type = TMPL_EXPR_NOP, .any.ref_count = 1 };
  return tmpl_expr_ref (&interned);
}

TmplExpr *
tmpl_expr_new_null (void)
{
  static TmplExpr interned = { .any.type = TMPL_EXPR_NULL, .any.ref_count = 1 };
  return tmpl_expr_ref (&interned);
}