-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_tablegen.c
1500 lines (1094 loc) · 29.5 KB
/
tg_tablegen.c
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <regex.h>
#include "tg_parser.h"
#include "tg_value.h"
#include "tg_inout.h"
enum TG_SYMTYPE {
TG_SYMTYPE_FUNCTION,
TG_SYMTYPE_VARIABLE,
TG_SYMTYPE_INPUT
};
enum TG_STATE {
TG_STATE_SUCCESS = 0,
TG_STATE_RETURN = 1,
TG_STATE_BREAK = 2,
TG_STATE_CONTINUE = 3
};
struct tg_variable {
struct tg_val *val;
};
struct tg_function {
int startnode;
};
struct tg_symbol {
union {
struct tg_variable var;
struct tg_function func;
struct tg_input input;
};
enum TG_SYMTYPE type;
TG_HASHFIELDS(struct tg_symbol, TG_HASH_SYMBOL)
};
TG_HASHED(struct tg_symbol, TG_HASH_SYMBOL)
struct tg_allocator symalloc;
struct tg_darray symtable;
struct tg_darray tg_currow;
struct tg_val *tg_valprinterr(struct tg_val *v)
{
if (v == NULL)
TG_WARNING("%s", tg_error);
return v;
}
static void tg_initsymtable()
{
tg_darrinit(&symtable, sizeof(struct tg_hash));
tg_allocinit(&symalloc, sizeof(struct tg_symbol));
}
static void tg_newscope()
{
struct tg_hash st;
tg_inithash(TG_HASH_SYMBOL, &st);
tg_darrpush(&symtable, &st);
}
static void tg_popscope()
{
struct tg_hash st;
if (tg_darrpop(&symtable, &st) >= 0)
tg_destroyhash(TG_HASH_SYMBOL, &st);
}
static void tg_destroysymtable()
{
tg_darrclear(&symtable);
tg_allocdestroy(&symalloc, NULL);
}
static void tg_symboladd(const char *name, struct tg_symbol *s)
{
struct tg_hash *st;
st = tg_darrget(&symtable, symtable.cnt - 1);
tg_hashset(TG_HASH_SYMBOL, st, name, s);
}
static struct tg_symbol *tg_symbolget(const char *name)
{
struct tg_hash *st;
int i;
for (i = symtable.cnt - 1; i >= 0; --i) {
struct tg_symbol *sym;
st = tg_darrget(&symtable, i);
if ((sym = tg_hashget(TG_HASH_SYMBOL, st, name)) != NULL)
return sym;
}
return NULL;
}
static const struct tg_val *tg_symbolgetval(const char *name)
{
struct tg_symbol *sym;
if ((sym = tg_symbolget(name)) == NULL)
return NULL;
if (sym->type == TG_SYMTYPE_VARIABLE)
return sym->var.val;
if (sym->type == TG_SYMTYPE_INPUT)
return tg_readsource(&(sym->input), tg_createval(TG_VAL_ARRAY));
else if (sym->type == TG_SYMTYPE_FUNCTION)
return tg_emptyval();
TG_WARNING("Symbol %s has unknown type.", name);
return NULL;
}
static struct tg_val *tg_printsymbols(FILE *f)
{
const char *key;
const struct tg_symbol *s;
const struct tg_hash *st;
int j;
for (j = 0; j < symtable.cnt; ++j) {
int k;
st = tg_darrget(&symtable, j);
TG_HASHFOREACH(struct tg_symbol, TG_HASH_SYMBOL, *st, key,
s = tg_hashget(TG_HASH_SYMBOL, st, key);
for (k = 0; k < j; ++k)
TG_NULLQUIT(tg_fprintf(f, "\t"));
TG_NULLQUIT(tg_fprintf(f, "%s: ", key));
if (s->type == TG_SYMTYPE_FUNCTION) {
TG_NULLQUIT(tg_fprintf(f, "function\n"));
}
else if (s->type == TG_SYMTYPE_VARIABLE) {
TG_NULLQUIT(tg_fprintf(f, "variable{"));
TG_NULLQUIT(tg_printval(f,
tg_symbolgetval(key)));
TG_NULLQUIT(tg_fprintf(f, "}\n"));
}
if (s->type == TG_SYMTYPE_INPUT) {
TG_NULLQUIT(tg_fprintf(f,
"input{type:\"%s\"; path:\"%s\"}\n",
s->input.type, s->input.path));
}
);
}
return tg_emptyval();
}
static int tg_readsourceslist(const char *sources)
{
char *s, *ss;
const char *p;
s = tg_strdup(sources);
TG_ASSERT((ss = malloc(strlen(sources))) != NULL,
"Error while allocating memory.");
p = strtok(s, ";");
do {
const char *name, *type, *path;
struct tg_symbol *in;
char *pp;
strcpy(ss, p);
pp = ss;
name = pp;
TG_ASSERT((pp = strchr(pp, ':')) != NULL,
"Error while reading sources list.");
*pp++ = '\0';
type = pp;
TG_ASSERT((pp = strchr(pp + 1, ':')) != NULL,
"Error while reading sources list.");
*pp++ = '\0';
path = pp;
in = tg_alloc(&symalloc);
in->type = TG_SYMTYPE_INPUT;
tg_createinput(&(in->input), type, path);
tg_symboladd(name, in);
} while ((p = strtok(NULL, ";")) != NULL);
free(s);
free(ss);
return 0;
}
static struct tg_val *tg_runnode(int ni);
static struct tg_val *tg_getattrlvalue(int eni, struct tg_val *a)
{
const char *attrname;
attrname = tg_nodetoken(tg_nodechild(eni, 0))->val.str;
return tg_valgetattrre(a, attrname, tg_emptyval());
}
static struct tg_val *tg_setlvalue(int ni, const struct tg_val *v)
{
struct tg_darray idxr;
struct tg_symbol *s;
struct tg_val *a;
const char *name;
int ini;
int i;
tg_darrinit(&idxr, sizeof(struct tg_val *));
if (tg_nodetype(ni) == TG_N_ID)
ini = tg_nodechild(ni, 0);
else if (tg_nodetype(ni) == TG_N_ADDRESS)
ini = tg_nodechild(tg_nodechild(ni, 0), 0);
else
goto notlvalue;
name = tg_nodetoken(ini)->val.str;
if ((s = tg_symbolget(name)) != NULL
&& s->type != TG_SYMTYPE_VARIABLE) {
tg_darrclear(&idxr);
TG_WARNING("Identificator %s is already used.", name);
return NULL;
}
for (i = 1; i < tg_nodeccnt(ni); ++i) {
struct tg_val *idx;
int eni;
eni = tg_nodechild(ni, i);
if (tg_nodetype(eni) == TG_N_ATTR)
continue;
if (tg_nodeccnt(eni) > 1)
goto notlvalue;
eni = tg_nodechild(eni, 0);
if (tg_nodeccnt(eni) > 1)
goto notlvalue;
eni = tg_nodechild(eni, 0);
if (tg_nodetype(eni) == TG_N_RANGE
|| tg_nodetype(eni) == TG_N_FILTER)
goto notlvalue;
if((idx = tg_runnode(eni)) == NULL)
goto notlvalue;
if (!tg_isscalar(idx->type))
goto notlvalue;
tg_darrset(&idxr, i, &idx);
}
if (s == NULL) {
s = tg_alloc(&symalloc);
s->type = TG_SYMTYPE_VARIABLE;
s->var.val = tg_emptyval();
tg_symboladd(tg_nodetoken(ini)->val.str, s);
}
s = tg_symbolget(name);
a = s->var.val;
if (tg_nodetype(ni) != TG_N_ADDRESS)
goto skipaddress;
for (i = 1; i < tg_nodeccnt(ni); ++i) {
struct tg_val *idx;
if (tg_nodetype(tg_nodechild(ni, i)) == TG_N_ATTR) {
a = tg_getattrlvalue(tg_nodechild(ni, i), a);
continue;
}
idx = *((struct tg_val **) tg_darrget(&idxr, i));
if (tg_isscalar(a->type))
tg_moveval(a, tg_castval(a, TG_VAL_ARRAY));
if (idx->type == TG_VAL_INT)
a = tg_arrgetre(a, idx->intval, tg_emptyval());
else {
idx = tg_castval(idx, TG_VAL_STRING);
a = tg_arrgethre(a, idx->strval.str, tg_emptyval());
}
}
skipaddress:
tg_darrclear(&idxr);
tg_moveval(a, v);
return s->var.val;
notlvalue:
tg_darrclear(&idxr);
TG_WARNING("Trying to assign into rvalue.");
return NULL;
}
static struct tg_val *tg_funcdef(int ni)
{
struct tg_symbol *s;
s = tg_alloc(&symalloc);
s->type = TG_SYMTYPE_FUNCTION;
s->func.startnode = ni;
tg_symboladd(tg_nodetoken(tg_nodechild(ni, 0))->val.str, s);
return tg_intval(TG_STATE_SUCCESS);
}
static struct tg_val *tg_if(int ni)
{
const struct tg_val *r;
TG_NULLQUIT(r = tg_runnode(tg_nodechild(ni, 0)));
if (tg_istrueval(r))
return tg_runnode(tg_nodechild(ni, 1));
else if (tg_nodeccnt(ni) > 2)
return tg_runnode(tg_nodechild(ni, 2));
return tg_intval(TG_STATE_SUCCESS);
}
static struct tg_val *tg_forclassic(int ni)
{
struct tg_val *r;
int initnode;
int cmpnode;
int stepnode;
int blocknode;
initnode = tg_nodechild(ni, 0);
cmpnode = tg_nodechild(ni, 1);
stepnode = tg_nodechild(ni, 2);
blocknode = tg_nodechild(ni, 3);
tg_newscope();
TG_NULLQUIT(tg_runnode(initnode));
while (1) {
TG_NULLQUIT(r = tg_runnode(cmpnode));
if (!tg_istrueval(r))
break;
TG_NULLQUIT(r = tg_runnode(blocknode));
if (r->intval == TG_STATE_RETURN)
goto forend;
else if (r->intval == TG_STATE_BREAK)
break;
TG_NULLQUIT(tg_runnode(stepnode));
}
r = tg_intval(TG_STATE_SUCCESS);
forend:
tg_popscope();
return r;
}
static struct tg_val *tg_fortable(int ni)
{
struct tg_val *r, *t;
int idnode;
int tablenode;
int blocknode;
int i;
idnode = tg_nodechild(ni, 0);
tablenode = tg_nodechild(ni, 1);
blocknode = tg_nodechild(ni, 2);
tg_newscope();
TG_NULLQUIT(t = tg_runnode(tablenode));
for (i = 0; i < t->arrval.arr.cnt; ++i) {
struct tg_val *v;
TG_ASSERT((v = tg_arrgetr(t, i)) != NULL,
"Error while iterating through array,");
TG_NULLQUIT(tg_setlvalue(idnode, v));
TG_NULLQUIT(r = tg_runnode(blocknode));
if (r->intval == TG_STATE_RETURN)
goto forend;
else if (r->intval == TG_STATE_BREAK)
break;
}
r = tg_intval(TG_STATE_SUCCESS);
forend:
tg_popscope();
return r;
}
static struct tg_val *tg_for(int ni)
{
if (tg_nodeccnt(ni) == 4) {
return tg_forclassic(ni);
}
else
return tg_fortable(ni);
}
static struct tg_val *tg_block(int ni)
{
struct tg_val *r;
int i;
tg_newscope();
for (i = 0; i < tg_nodeccnt(ni); ++i) {
int cni;
cni = tg_nodechild(ni, i);
if (tg_nodetype(cni) == TG_N_RETURN) {
if (tg_nodeccnt(cni) > 0) {
TG_NULLQUIT(r = tg_runnode(
tg_nodechild(cni, 0)));
tg_setretval(r);
}
r = tg_intval(TG_STATE_RETURN);
goto blockend;
}
else if (tg_nodetype(cni) == TG_T_BREAK) {
r = tg_intval(TG_STATE_BREAK);
goto blockend;
}
else if (tg_nodetype(cni) == TG_T_CONTINUE) {
r = tg_intval(TG_STATE_CONTINUE);
goto blockend;
}
TG_NULLQUIT(r = tg_runnode(cni));
if (tg_isflownode(tg_nodetype(cni))
&& r->intval != TG_STATE_SUCCESS) {
goto blockend;
}
}
r = tg_intval(TG_STATE_SUCCESS);
blockend:
tg_popscope();
return r;
}
static struct tg_val *tg_checkargs(const char *name,
int has, int expect)
{
if (has != expect) {
TG_WARNING("%s: expected %d arguments, got %d.",
name, expect, has);
return NULL;
}
return tg_intval(1);
}
static struct tg_val *tg_identificator(int ni)
{
struct tg_val *r;
struct tg_symbol *s;
const char *name;
int ani, fani, fni;
int i;
if (tg_nodeccnt(ni) == 1) {
const struct tg_val *rc;
if ((rc = tg_symbolgetval(tg_nodetoken(
tg_nodechild(ni, 0))->val.str)) == NULL)
return tg_emptyval();
return tg_copyval(rc);
}
ani = tg_nodechild(ni, 1);
name = tg_nodetoken(tg_nodechild(ni, 0))->val.str;
if (strcmp(name, "type") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
if (v->type == TG_VAL_EMPTY)
return tg_stringval("empty");
else if (v->type == TG_VAL_INT)
return tg_stringval("int");
else if (v->type == TG_VAL_FLOAT)
return tg_stringval("float");
else if (v->type == TG_VAL_STRING)
return tg_stringval("string");
else if (v->type == TG_VAL_TABLE)
return tg_stringval("table");
else if (v->type == TG_VAL_ARRAY)
return tg_stringval("array");
else if (v->type == TG_VAL_DELETED)
return tg_stringval("deleted");
else
return tg_stringval("unknown");
}
if (strcmp(name, "int") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_castval(v, TG_VAL_INT);
}
else if (strcmp(name, "float") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_castval(v, TG_VAL_FLOAT);
}
else if (strcmp(name, "string") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_castval(v, TG_VAL_STRING);
}
else if (strcmp(name, "array") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_castval(v, TG_VAL_ARRAY);
}
else if (strcmp(name, "table") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_castval(v, TG_VAL_TABLE);
}
else if (strcmp(name, "print") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_fprintf(stdout,
"%s", tg_castval(v, TG_VAL_STRING)->strval.str);
}
else if (strcmp(name, "dumpval") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
return tg_printval(stdout, v);
}
else if (strcmp(name, "dumpsymbols") == 0) {
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 0));
return tg_printsymbols(stdout);
}
else if (strcmp(name, "substr") == 0) {
struct tg_val *s, *b, *l;
if (tg_nodeccnt(ani) < 2) {
TG_WARNING("%s: expected at least %d arguments, got %d.",
name, 2, tg_nodeccnt(ani));
return NULL;
}
TG_NULLQUIT(s = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(b = tg_runnode(tg_nodechild(ani, 1)));
if (tg_nodeccnt(ani) > 2)
TG_NULLQUIT(l = tg_runnode(tg_nodechild(ani, 2)));
else
l = tg_intval(-1);
return tg_substr(s, b, l);
}
else if (strcmp(name, "match") == 0) {
struct tg_val *s, *reg;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 2));
TG_NULLQUIT(s = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(reg = tg_runnode(tg_nodechild(ani, 1)));
return tg_match(s, reg);
}
else if (strcmp(name, "size") == 0) {
struct tg_val *a;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(a = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(a = tg_castval(a, TG_VAL_ARRAY));
return tg_intval(a->arrval.arr.cnt);
}
else if (strcmp(name, "length") == 0) {
struct tg_val *s;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(s = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(s = tg_castval(s, TG_VAL_STRING));
return tg_intval(s->strval.len);
}
else if (strcmp(name, "defval") == 0) {
struct tg_val *v;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 2));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 0)));
if (v->type == TG_VAL_EMPTY)
return tg_runnode(tg_nodechild(ani, 1));
return v;
}
else if (strcmp(name, "rudate") == 0) {
struct tg_val *d, *m, *y;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 3));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(m = tg_runnode(tg_nodechild(ani, 1)));
TG_NULLQUIT(y = tg_runnode(tg_nodechild(ani, 2)));
return tg_rudate(d, m, y);
}
else if (strcmp(name, "day") == 0) {
struct tg_val *d;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
return tg_day(d);
}
else if (strcmp(name, "month") == 0) {
struct tg_val *d;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
return tg_month(d);
}
else if (strcmp(name, "year") == 0) {
struct tg_val *d;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
return tg_year(d);
}
else if (strcmp(name, "weekday") == 0) {
struct tg_val *d;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 1));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
return tg_weekday(d);
}
else if (strcmp(name, "monthend") == 0) {
struct tg_val *d, *m;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 2));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(m = tg_runnode(tg_nodechild(ani, 1)));
return tg_monthend(d, m);
}
else if (strcmp(name, "datecmp") == 0) {
struct tg_val *d1, *d2;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 2));
TG_NULLQUIT(d1 = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(d2 = tg_runnode(tg_nodechild(ani, 1)));
return tg_datecmp(d1, d2);
}
else if (strcmp(name, "datediff") == 0) {
struct tg_val *d1, *d2, *p;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 3));
TG_NULLQUIT(d1 = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(d2 = tg_runnode(tg_nodechild(ani, 1)));
TG_NULLQUIT(p = tg_runnode(tg_nodechild(ani, 2)));
return tg_datediff(d1, d2, p);
}
else if (strcmp(name, "dateadd") == 0) {
struct tg_val *d, *v, *p;
TG_NULLQUIT(tg_checkargs(name, tg_nodeccnt(ani), 3));
TG_NULLQUIT(d = tg_runnode(tg_nodechild(ani, 0)));
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, 1)));
TG_NULLQUIT(p = tg_runnode(tg_nodechild(ani, 2)));
return tg_dateadd(d, v, p);
}
if ((s = tg_symbolget(tg_nodetoken(
tg_nodechild(ni, 0))->val.str)) == NULL) {
return tg_emptyval();
}
if (s->type == TG_SYMTYPE_INPUT) {
struct tg_val *args;
args = tg_createval(TG_VAL_ARRAY);
for (i = 0; i < tg_nodeccnt(ani); ++i) {
struct tg_val *v;
TG_NULLQUIT(v = tg_runnode(tg_nodechild(ani, i)));
tg_arrpush(args, v);
}
return tg_readsource(&(s->input), args);
}
if (s->type != TG_SYMTYPE_FUNCTION)
return tg_emptyval();
fni = s->func.startnode;
fani = tg_nodechild(fni, 1);
TG_NULLQUIT(tg_checkargs(name,
tg_nodeccnt(ani), tg_nodeccnt(fani)));
tg_startframe();
tg_newscope();
tg_setretval(tg_emptyval());
for (i = 0; i < tg_nodeccnt(fani); ++i) {
struct tg_symbol *ss;
const char *sn;
sn = tg_nodetoken(tg_nodechild(fani, i))->val.str;
ss = tg_alloc(&symalloc);
ss->type = TG_SYMTYPE_VARIABLE;
if ((ss->var.val = tg_runnode(tg_nodechild(ani, i)))
== NULL) {
goto error;
}
tg_symboladd(sn, ss);
}
if (tg_runnode(tg_nodechild(fni, 2)) == NULL)
goto error;
r = tg_getretval();
tg_popscope();
tg_endframe();
return r;
error:
tg_popscope();
tg_endframe();
return NULL;
}
static struct tg_val *tg_const(int ni)
{
struct tg_token *t;
t = tg_nodetoken(tg_nodechild(ni, 0));
if (t->type == TG_T_STRING)
return tg_stringval(t->val.str);
else if (t->type == TG_T_INT)
return tg_intval(atoi(t->val.str));
else if (t->type == TG_T_FLOAT)
return tg_floatval(atof(t->val.str));
else if (t->type == TG_T_EMPTY)
return tg_emptyval();
TG_ERROR("Unknown constant value type: %d", t->type);
}
static struct tg_val *tg_getindexfilter(int fni, const struct tg_val *a)
{
struct tg_val *v, *b;
struct tg_val *r;
int i;
fni = tg_nodechild(fni, 0);
r = tg_createval(TG_VAL_ARRAY);
TG_ARRFOREACH(a, i, v,
if (tg_isscalar(v->type))
v = tg_castval(v, TG_VAL_ARRAY);
tg_darrpush(&tg_currow, &v);
if ((b = tg_runnode(fni)) == NULL) {
tg_darrpop(&tg_currow, NULL);
return NULL;
}
if (tg_istrueval(b))
tg_arrpush(r, tg_intval(v->arrpos));
tg_darrpop(&tg_currow, NULL);
);
return r;
}
static struct tg_val *tg_getindexexpr(int fni, const struct tg_val *a)
{
struct tg_val *idx;
TG_NULLQUIT(idx = tg_runnode(fni));
if (!tg_isscalar(idx->type))
return tg_emptyval();
if (idx->type == TG_VAL_INT) {
if ((a = tg_arrgetr(a, idx->intval)) == NULL)
return tg_emptyval();
}
else {
idx = tg_castval(idx, TG_VAL_STRING);
if ((a = tg_arrgethr(a, idx->strval.str)) == NULL)
return tg_emptyval();
}
return tg_intval(a->arrpos);
}
static struct tg_val *tg_getindexrange(int fni, const struct tg_val *a)
{
struct tg_val *idxb, *idxe;
struct tg_val *r;
int i;
idxb = tg_getindexexpr(tg_nodechild(fni, 0), a);
idxe = tg_getindexexpr(tg_nodechild(fni, 1), a);
if (idxb->type == TG_VAL_EMPTY || idxe->type == TG_VAL_EMPTY )
return tg_emptyval();
r = tg_createval(TG_VAL_ARRAY);
for (i = idxb->intval; i <= idxe->intval; ++i)
tg_arrpush(r, tg_intval(i));
return r;
}
int tg_idxsort_f(const void *v1, const void *v2)
{
int i1, i2;
i1 = (*((struct tg_val **) v1))->intval;
i2 = (*((struct tg_val **) v2))->intval;
if (i1 == i2) return 0;
else if (i1 < i2) return -1;
else return 1;
}
static struct tg_val *tg_getindexval(int ini, const struct tg_val *a)
{
struct tg_val *ri, *r, *v;
int prevri;
int i;
if (tg_isscalar(a->type))
a = tg_castval(a, TG_VAL_ARRAY);
ri = tg_createval(TG_VAL_ARRAY);
for (i = 0; i < tg_nodeccnt(ini); ++i) {
struct tg_val *rr;
int fni;
int j;
fni = tg_nodechild(ini, i);
if (tg_nodetype(fni) == TG_T_PERCENT)
continue;
if (tg_nodetype(fni) == TG_N_RANGE)
TG_NULLQUIT(rr = tg_getindexrange(fni, a));
else if (tg_nodetype(fni) == TG_N_FILTER)
TG_NULLQUIT(rr = tg_getindexfilter(fni, a));
else {
TG_NULLQUIT(rr = tg_getindexexpr(fni, a));
TG_NULLQUIT(rr = tg_castval(rr, TG_VAL_ARRAY));
}
if (rr->type == TG_VAL_EMPTY)
continue;
TG_ARRFOREACH(rr, j, v, tg_arrpush(ri, v));
}
tg_darrsort(&(ri->arrval.arr), tg_idxsort_f);
r = tg_createval(TG_VAL_ARRAY);
prevri = -1;
TG_ARRFOREACH(ri, i, v,
if (v->intval == prevri)
continue;
prevri = v->intval;
v = tg_arrgetr(a, v->intval);
if (v->ishashed)
tg_arrseth(r, tg_hashkey(TG_HASH_ARRAY, *v), v);
else
tg_arrpush(r, v);
);