summaryrefslogtreecommitdiff
path: root/src/libaudcore/tuple_compiler.c
blob: 74584c15a336bb89a9df11e1933b026a41c461d5 (plain)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
 * tuple_compiler.c
 * Copyright (c) 2007 Matti 'ccr' Hämäläinen
 * Copyright (c) 2011-2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

/*
 * TODO:
 * - Unicode/UTF-8 support in format strings. using any non-ASCII
 *   characters in Tuplez format strings WILL cause things go boom
 *   at the moment!
 *
 * - implement definitions (${=foo,"baz"} ${=foo,1234})
 * - implement functions
 * - implement handling of external expressions
 * - evaluation context: how local variables should REALLY work?
 *   currently there is just a single context, is a "global" context needed?
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <glib.h>

#include "audstrings.h"
#include "tuple_compiler.h"

#define MAX_STR   (256)
#define TUPLEZ_MAX_VARS (4)

#define GET_VAR(c, i)  (& g_array_index ((c), TupleEvalVar, (i)))

#define tuple_error(ctx, ...) fprintf (stderr, "Tuple compiler: " __VA_ARGS__)

enum {
    OP_RAW = 0,   /* plain text */
    OP_FIELD,   /* a field/variable */
    OP_EXISTS,
    OP_EQUALS,
    OP_NOT_EQUALS,
    OP_GT,
    OP_GTEQ,
    OP_LT,
    OP_LTEQ,
    OP_IS_EMPTY
};

enum {
    TUPLE_VAR_FIELD = 0,
    TUPLE_VAR_CONST
};

struct _TupleEvalNode {
    int opcode;   /* operator, see OP_ enums */
    int var[TUPLEZ_MAX_VARS]; /* tuple variable references */
    char *text;   /* raw text, if any (OP_RAW) */
    struct _TupleEvalNode *children, *next, *prev; /* children of this struct, and pointer to next node. */
};

typedef struct {
    char *name;
    int type;     /* Type of variable, see VAR_* */
    int defvali;
    TupleValueType ctype; /* Type of constant/def value */

    int fieldidx;   /* if >= 0: Index # of "pre-defined" Tuple fields */
    bool_t fieldread, fieldvalid;
    char * fieldstr;
} TupleEvalVar;

/* Initialize an evaluation context
 */
TupleEvalContext * tuple_evalctx_new(void)
{
  return g_array_new (FALSE, TRUE, sizeof (TupleEvalVar));
}


/* "Reset" the evaluation context
 */
void tuple_evalctx_reset(TupleEvalContext *ctx)
{
  for (int i = 0; i < ctx->len; i ++)
  {
    TupleEvalVar * var = GET_VAR (ctx, i);

    var->fieldread = FALSE;
    var->fieldvalid = FALSE;
    str_unref (var->fieldstr);
    var->fieldstr = NULL;
  }
}


/* Free an evaluation context and associated data
 */
void tuple_evalctx_free(TupleEvalContext *ctx)
{
  for (int i = 0; i < ctx->len; i ++)
  {
    TupleEvalVar * var = GET_VAR (ctx, i);

    str_unref (var->name);
    str_unref (var->fieldstr);
  }

  g_array_free (ctx, TRUE);
}


/* note: may invalidate TupleEvalVar pointers due to reallocation */
static int tuple_evalctx_add_var (TupleEvalContext * ctx, const char * name,
 const int type, const TupleValueType ctype)
{
  int field = -1;

  if (type == TUPLE_VAR_FIELD)
  {
    field = tuple_field_by_name (name);
    if (field < 0)
      return -1;
  }

  int i = ctx->len;
  g_array_set_size (ctx, i + 1);

  TupleEvalVar * var = GET_VAR (ctx, i);

  var->name = str_get (name);
  var->type = type;
  var->fieldidx = field;
  var->ctype = ctype;

  switch (type) {
    case TUPLE_VAR_FIELD:
      var->ctype = tuple_field_get_type (field);
      break;

    case TUPLE_VAR_CONST:
      if (ctype == TUPLE_INT)
        var->defvali = atoi (name);
      break;
  }

  return i;
}


static void tuple_evalnode_insert(TupleEvalNode **nodes, TupleEvalNode *node)
{
  if (*nodes) {
    node->prev = (*nodes)->prev;
    (*nodes)->prev->next = node;
    (*nodes)->prev = node;
    node->next = NULL;
  } else {
    *nodes = node;
    node->prev = node;
    node->next = NULL;
  }
}


void tuple_evalnode_free(TupleEvalNode *expr)
{
  TupleEvalNode *curr = expr, *next;

  while (curr) {
    next = curr->next;

    str_unref (curr->text);

    if (curr->children)
      tuple_evalnode_free(curr->children);

    g_slice_free (TupleEvalNode, curr);

    curr = next;
  }
}


static TupleEvalNode *tuple_compiler_pass1(int *level, TupleEvalContext *ctx, const char **expression);


static bool_t tc_get_item(TupleEvalContext *ctx,
    const char **str, char *buf, gssize max,
    char endch, bool_t *literal, char *errstr, const char *item)
{
  gssize i = 0;
  const char *s = *str;
  char tmpendch;

  if (*s == '"') {
    if (*literal == FALSE) {
      tuple_error(ctx, "Literal string value not allowed in '%s'.\n", item);
      return FALSE;
    }
    s++;
    *literal = TRUE;
    tmpendch = '"';
  } else {
    *literal = FALSE;
    tmpendch = endch;
  }

  if (*literal == FALSE) {
    while (*s != '\0' && *s != tmpendch && (g_ascii_isalnum(*s) || *s == '-') && i < (max - 1)) {
      buf[i++] = *(s++);
    }

    if (*s != tmpendch && *s != '}' && !g_ascii_isalnum(*s) && *s != '-') {
      tuple_error(ctx, "Invalid field '%s' in '%s'.\n", *str, item);
      return FALSE;
    } else if (*s != tmpendch) {
      tuple_error(ctx, "Expected '%c' in '%s'.\n", tmpendch, item);
      return FALSE;
    }
  } else {
    while (*s != '\0' && *s != tmpendch && i < (max - 1)) {
      if (*s == '\\') s++;
      buf[i++] = *(s++);
    }
  }
  buf[i] = '\0';

  if (*literal) {
    if (*s == tmpendch)
      s++;
    else {
      tuple_error(ctx, "Expected literal string end ('%c') in '%s'.\n", tmpendch, item);
      return FALSE;
    }
  }

  if (*s != endch) {
    tuple_error(ctx, "Expected '%c' after %s in '%s'\n", endch, errstr, item);
    return FALSE;
  } else {
    *str = s;
    return TRUE;
  }
}


static int tc_get_variable(TupleEvalContext *ctx, char *name, int type)
{
  TupleValueType ctype = TUPLE_UNKNOWN;

  if (g_ascii_isdigit(name[0])) {
    ctype = TUPLE_INT;
    type = TUPLE_VAR_CONST;
  } else
    ctype = TUPLE_STRING;

  if (type != TUPLE_VAR_CONST) {
    for (int i = 0; i < ctx->len; i ++)
      if (! strcmp (GET_VAR (ctx, i)->name, name))
        return i;
  }

  return tuple_evalctx_add_var(ctx, name, type, ctype);
}


static bool_t tc_parse_construct(TupleEvalContext *ctx, TupleEvalNode **res,
 const char *item, const char **c, int *level, int opcode)
{
  char tmps1[MAX_STR], tmps2[MAX_STR];
  bool_t literal1 = TRUE, literal2 = TRUE;

  (*c)++;
  if (tc_get_item(ctx, c, tmps1, MAX_STR, ',', &literal1, "tag1", item)) {
    (*c)++;
    if (tc_get_item(ctx, c, tmps2, MAX_STR, ':', &literal2, "tag2", item)) {
      TupleEvalNode *tmp = g_slice_new0 (TupleEvalNode);
      (*c)++;

      tmp->opcode = opcode;
      if ((tmp->var[0] = tc_get_variable(ctx, tmps1, literal1 ? TUPLE_VAR_CONST : TUPLE_VAR_FIELD)) < 0) {
        tuple_evalnode_free(tmp);
        tuple_error(ctx, "Invalid variable '%s' in '%s'.\n", tmps1, item);
        return FALSE;
      }
      if ((tmp->var[1] = tc_get_variable(ctx, tmps2, literal2 ? TUPLE_VAR_CONST : TUPLE_VAR_FIELD)) < 0) {
        tuple_evalnode_free(tmp);
        tuple_error(ctx, "Invalid variable '%s' in '%s'.\n", tmps2, item);
        return FALSE;
      }
      tmp->children = tuple_compiler_pass1(level, ctx, c);
      tuple_evalnode_insert(res, tmp);
    } else
      return FALSE;
  } else
    return FALSE;

  return TRUE;
}


/* Compile format expression into TupleEvalNode tree.
 * A "simple" straight compilation is sufficient in first pass, later
 * passes can perform subexpression removal and other optimizations.
 */
static TupleEvalNode *tuple_compiler_pass1(int *level, TupleEvalContext *ctx, const char **expression)
{
  TupleEvalNode *res = NULL, *tmp = NULL;
  const char *c = *expression, *item;
  char tmps1[MAX_STR];
  bool_t literal, end = FALSE;

  (*level)++;

  while (*c != '\0' && !end) {
    tmp = NULL;
    if (*c == '}') {
      c++;
      (*level)--;
      end = TRUE;
    } else if (*c == '$') {
      /* Expression? */
      item = c++;
      if (*c == '{') {
        int opcode;
        const char *expr = ++c;

        switch (*c) {
          case '?': c++;
            /* Exists? */
            literal = FALSE;
            if (tc_get_item(ctx, &c, tmps1, MAX_STR, ':', &literal, "tag", item)) {
              c++;
              tmp = g_slice_new0 (TupleEvalNode);
              tmp->opcode = OP_EXISTS;
              if ((tmp->var[0] = tc_get_variable(ctx, tmps1, TUPLE_VAR_FIELD)) < 0) {
                tuple_error(ctx, "Invalid variable '%s' in '%s'.\n", tmps1, expr);
                goto ret_error;
              }
              tmp->children = tuple_compiler_pass1(level, ctx, &c);
              tuple_evalnode_insert(&res, tmp);
            } else
              goto ret_error;
            break;

          case '=': c++;
            if (*c != '=') {
              /* Definition */
              literal = FALSE;
              if (tc_get_item(ctx, &c, tmps1, MAX_STR, ',', &literal, "variable", item)) {
                c++;
                if (*c == '"') {
                  /* String */
                  c++;
                } else if (g_ascii_isdigit(*c)) {
                  /* Integer */
                }

                tuple_error(ctx, "Definitions are not yet supported!\n");
                goto ret_error;
              } else
                goto ret_error;
            } else {
              /* Equals? */
              if (!tc_parse_construct(ctx, &res, item, &c, level, OP_EQUALS))
                goto ret_error;
            }
            break;

          case '!': c++;
            if (*c != '=') goto ext_expression;
            if (!tc_parse_construct(ctx, &res, item, &c, level, OP_NOT_EQUALS))
              goto ret_error;
            break;

          case '<': c++;
            if (*c == '=') {
              opcode = OP_LTEQ;
              c++;
            } else
              opcode = OP_LT;

            if (!tc_parse_construct(ctx, &res, item, &c, level, opcode))
              goto ret_error;
            break;

          case '>': c++;
            if (*c == '=') {
              opcode = OP_GTEQ;
              c++;
            } else
              opcode = OP_GT;

            if (!tc_parse_construct(ctx, &res, item, &c, level, opcode))
              goto ret_error;
            break;

          case '(': c++;
            if (!strncmp(c, "empty)?", 7)) {
              c += 7;
              literal = FALSE;
              if (tc_get_item(ctx, &c, tmps1, MAX_STR, ':', &literal, "tag", item)) {
                c++;
                tmp = g_slice_new0 (TupleEvalNode);
                tmp->opcode = OP_IS_EMPTY;
                if ((tmp->var[0] = tc_get_variable(ctx, tmps1, TUPLE_VAR_FIELD)) < 0) {
                  tuple_error(ctx, "Invalid variable '%s' in '%s'.\n", tmps1, expr);
                  goto ret_error;
                }
                tmp->children = tuple_compiler_pass1(level, ctx, &c);
                tuple_evalnode_insert(&res, tmp);
              } else
                goto ret_error;
            } else
              goto ext_expression;
            break;

          default:
          ext_expression:
            /* Get expression content */
            c = expr;
            literal = FALSE;
            if (tc_get_item(ctx, &c, tmps1, MAX_STR, '}', &literal, "field", item)) {
              /* FIXME!! FIX ME! Check for external expressions */

              /* I HAS A FIELD - A field. You has it. */
              tmp = g_slice_new0 (TupleEvalNode);
              tmp->opcode = OP_FIELD;
              if ((tmp->var[0] = tc_get_variable(ctx, tmps1, TUPLE_VAR_FIELD)) < 0) {
                tuple_error(ctx, "Invalid variable '%s' in '%s'.\n", tmps1, expr);
                goto ret_error;
              }
              tuple_evalnode_insert(&res, tmp);
              c++;

            } else
              goto ret_error;
        }
      } else {
        tuple_error(ctx, "Expected '{', got '%c' in '%s'.\n", *c, c);
        goto ret_error;
      }

    } else if (*c == '%') {
      /* Function? */
      item = c++;
      if (*c == '{') {
        gssize i = 0;
        c++;

        while (*c != '\0' && (g_ascii_isalnum(*c) || *c == '-') && *c != '}' && *c != ':' && i < (MAX_STR - 1))
          tmps1[i++] = *(c++);
        tmps1[i] = '\0';

        if (*c == ':') {
          c++;
        } else if (*c == '}') {
          c++;
        } else if (*c == '\0') {
          tuple_error(ctx, "Expected '}' or function arguments in '%s'\n", item);
          goto ret_error;
        }
      } else {
        tuple_error(ctx, "Expected '{', got '%c' in '%s'.\n", *c, c);
        goto ret_error;
      }
    } else {
      /* Parse raw/literal text */
      gssize i = 0;
      while (*c != '\0' && *c != '$' && *c != '%' && *c != '}' && i < (MAX_STR - 1)) {
        if (*c == '\\') c++;
        tmps1[i++] = *(c++);
      }
      tmps1[i] = '\0';

      tmp = g_slice_new0 (TupleEvalNode);
      tmp->opcode = OP_RAW;
      tmp->text = str_get (tmps1);
      tuple_evalnode_insert(&res, tmp);
    }
  }

  if (*level <= 0) {
    tuple_error(ctx, "Syntax error! Uneven/unmatched nesting of elements in '%s'!\n", c);
    goto ret_error;
  }

  *expression = c;
  return res;

ret_error:
  tuple_evalnode_free(tmp);
  tuple_evalnode_free(res);
  return NULL;
}


TupleEvalNode *tuple_formatter_compile(TupleEvalContext *ctx, const char *expr)
{
  int level = 0;
  const char *tmpexpr = expr;
  TupleEvalNode *res1;

  res1 = tuple_compiler_pass1(&level, ctx, &tmpexpr);

  if (level != 1) {
    tuple_error(ctx, "Syntax error! Uneven/unmatched nesting of elements! (%d)\n", level);
    tuple_evalnode_free(res1);
    return NULL;
  }

  return res1;
}


/* Fetch a tuple field value.  Return TRUE if found. */
static bool_t tf_get_fieldval (TupleEvalVar * var, const Tuple * tuple)
{
  if (var->type != TUPLE_VAR_FIELD || var->fieldidx < 0)
    return FALSE;

  if (var->fieldread)
    return var->fieldvalid;

  if (tuple_get_value_type (tuple, var->fieldidx) != var->ctype) {
    var->fieldread = TRUE;
    var->fieldvalid = FALSE;
    return FALSE;
  }

  if (var->ctype == TUPLE_INT)
    var->defvali = tuple_get_int (tuple, var->fieldidx);
  else if (var->ctype == TUPLE_STRING)
    var->fieldstr = tuple_get_str (tuple, var->fieldidx);

  var->fieldread = TRUE;
  var->fieldvalid = TRUE;
  return TRUE;
}


/* Fetch string or int value of given variable, whatever type it might be.
 * Return VAR_* type for the variable.
 */
static TupleValueType tf_get_var (char * * tmps, int * tmpi, TupleEvalVar *
 var, const Tuple * tuple)
{
  TupleValueType type = TUPLE_UNKNOWN;
  *tmps = NULL;
  *tmpi = 0;

  switch (var->type) {
    case TUPLE_VAR_CONST:
      switch (var->ctype) {
        case TUPLE_STRING: *tmps = var->name; break;
        case TUPLE_INT: *tmpi = var->defvali; break;
        default: /* Cannot happen */ break;
      }
      type = var->ctype;
      break;

    case TUPLE_VAR_FIELD:
      if (tf_get_fieldval (var, tuple)) {
        type = var->ctype;
        if (type == TUPLE_INT)
          * tmpi = var->defvali;
        else if (type == TUPLE_STRING)
          * tmps = var->fieldstr;
      }
      break;
  }

  return type;
}


/* Evaluate tuple in given TupleEval expression in given
 * context and return resulting string.
 */
static bool_t tuple_formatter_eval_do (TupleEvalContext * ctx, TupleEvalNode *
 expr, const Tuple * tuple, GString * out)
{
  TupleEvalNode *curr = expr;
  TupleEvalVar *var0, *var1;
  TupleValueType type0, type1;
  int tmpi0, tmpi1;
  char tmps[MAX_STR], *tmps0, *tmps1, *tmps2;
  bool_t result;
  int resulti;

  if (!expr) return FALSE;

  while (curr) {
    const char *str = NULL;

    switch (curr->opcode) {
      case OP_RAW:
        str = curr->text;
        break;

      case OP_FIELD:
        var0 = GET_VAR (ctx, curr->var[0]);

        switch (var0->type) {
          case TUPLE_VAR_FIELD:
            if (tf_get_fieldval (var0, tuple)) {
              switch (var0->ctype) {
                case TUPLE_STRING:
                  str = var0->fieldstr;
                  break;

                case TUPLE_INT:
                  str_itoa (var0->defvali, tmps, sizeof (tmps));
                  str = tmps;
                  break;

                default:
                  str = NULL;
              }
            }
            break;
        }
        break;

      case OP_EQUALS:
      case OP_NOT_EQUALS:
      case OP_LT: case OP_LTEQ:
      case OP_GT: case OP_GTEQ:
        var0 = GET_VAR (ctx, curr->var[0]);
        var1 = GET_VAR (ctx, curr->var[1]);

        type0 = tf_get_var(&tmps0, &tmpi0, var0, tuple);
        type1 = tf_get_var(&tmps1, &tmpi1, var1, tuple);
        result = FALSE;

        if (type0 != TUPLE_UNKNOWN && type1 != TUPLE_UNKNOWN) {
          if (type0 == type1) {
            if (type0 == TUPLE_STRING)
              resulti = strcmp(tmps0, tmps1);
            else
              resulti = tmpi0 - tmpi1;
          } else {
            if (type0 == TUPLE_INT)
              resulti = tmpi0 - atoi(tmps1);
            else
              resulti = atoi(tmps0) - tmpi1;
          }

          switch (curr->opcode) {
            case OP_EQUALS:     result = (resulti == 0); break;
            case OP_NOT_EQUALS: result = (resulti != 0); break;
            case OP_LT:         result = (resulti <  0); break;
            case OP_LTEQ:       result = (resulti <= 0); break;
            case OP_GT:         result = (resulti >  0); break;
            case OP_GTEQ:       result = (resulti >= 0); break;
          default:            result = FALSE;
          }
        }

        if (result && ! tuple_formatter_eval_do (ctx, curr->children, tuple, out))
          return FALSE;
        break;

      case OP_EXISTS:
        if (tf_get_fieldval (GET_VAR (ctx, curr->var[0]), tuple))
        {
          if (! tuple_formatter_eval_do (ctx, curr->children, tuple, out))
            return FALSE;
        }
        break;

      case OP_IS_EMPTY:
        var0 = GET_VAR (ctx, curr->var[0]);

        if (tf_get_fieldval (var0, tuple)) {
          switch (var0->ctype) {
          case TUPLE_INT:
            result = (var0->defvali == 0);
            break;

          case TUPLE_STRING:
            result = TRUE;
            tmps2 = var0->fieldstr;

            while (result && tmps2 && *tmps2 != '\0') {
              if (g_ascii_isspace (* tmps2))
                tmps2 ++;
              else
                result = FALSE;
            }
            break;

          default:
            result = TRUE;
          }
        } else
          result = TRUE;

        if (result && ! tuple_formatter_eval_do (ctx, curr->children, tuple, out))
          return FALSE;
        break;

      default:
        tuple_error(ctx, "Unimplemented opcode %d!\n", curr->opcode);
        return FALSE;
        break;
    }

    if (str)
      g_string_append (out, str);

    curr = curr->next;
  }

  return TRUE;
}

void tuple_formatter_eval (TupleEvalContext * ctx, TupleEvalNode * expr,
 const Tuple * tuple, GString * out)
{
    g_string_truncate (out, 0);
    tuple_formatter_eval_do (ctx, expr, tuple, out);
}