aboutsummaryrefslogtreecommitdiff
path: root/lib/forlan.c
blob: 174b60ba5864340849e81ed7668014e8f9e254d7 (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
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
/* This file is part of Eclat.
   Copyright (C) 2012 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat 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 General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <string.h>
#include <setjmp.h>
#include "grecs.h"
#include "forlan.h"

struct forlan_eval_env {
	union forlan_node *parse_tree;
	struct forlan_value *vartab;
	struct grecs_node *tree;
	struct grecs_node *last;
	struct grecs_node *top_node;
	struct forlan_value retval;
	union forlan_node *instr;
	jmp_buf stop_buf;
	jmp_buf loop_buf;
};

int forlan_dbg = -1;

void
forlan_init()
{
	forlan_dbg = debug_register("forlan");
}

union forlan_node *
forlan_node_create(enum forlan_type type)
{
	union forlan_node *p = grecs_zalloc(sizeof(*p));
	p->type = type;
	return p;
}

static void f_dump_node(FILE *fp, union forlan_node *p, int *num, int lev);

static void
free_value(void *p)
{
	if (p) {
		struct forlan_value *val = p;

		switch (val->type) {
		case forlan_value_void:
		case forlan_value_node:
		case forlan_value_boolean:
			break;
		case forlan_value_literal:
			free(val->v.string);
			break;
		default:
			abort();
		}
		val->type = forlan_value_void;
	}
}

static void
copy_value(struct forlan_value *dst, struct forlan_value *src)
{
	*dst = *src;
	if (dst->type == forlan_value_literal)
		dst->v.string = grecs_strdup(dst->v.string);
}

static char *
valtypestr(enum forlan_value_type t)
{
	switch (t) {
	case forlan_value_void:
		return "void";
	case forlan_value_node:
		return "node";
	case forlan_value_literal:
		return "string";
	case forlan_value_boolean:
		return "boolean";
	default:
		abort();
	}
}

static void
coerce_retval(forlan_eval_env_t env, int c)
{
	enum forlan_value_type t;
	struct grecs_node *node;
	
	switch (c) {
	case 's':
		t = forlan_value_literal;
		break;
	case 'n':
		t = forlan_value_node;
		break;
	case 'b':
		t = forlan_value_boolean;
		break;
	case ' ':
		t = forlan_value_void;
	}

	if (t == env->retval.type)
		return;

	if (t == forlan_value_void) {
		free_value(&env->retval);
		env->retval.type = forlan_value_void;
		return;
	}

	if (env->retval.type == forlan_value_void) {
		err("can't coerce %s to %s",
		    valtypestr(env->retval.type),
		    valtypestr(t));
		abort();
	}

	if (t == forlan_value_boolean) {
		int res = retval_boolean(env);
		free_value(&env->retval);
		env->retval.type = forlan_value_boolean;
		env->retval.v.num = res;
		return;
	}

	/* Convert between string and node */
	switch (env->retval.type) {
	case forlan_value_literal:
		node = grecs_find_node(env->tree, env->retval.v.string);
		free(env->retval.v.string);
		env->retval.v.node = node;
		break;

	case forlan_value_node:
		if (env->retval.v.node->type == grecs_node_stmt) {
			struct grecs_txtacc *acc = grecs_txtacc_create();
			grecs_txtacc_format_value(env->retval.v.node->v.value,
						  0, acc);
			env->retval.v.string = grecs_txtacc_finish(acc, 1);
			grecs_txtacc_free(acc);
		} else
			env->retval.v.string = grecs_strdup("");
		break;

	default:
		abort();
	}
	env->retval.type = t;
}
	
int
retval_boolean(forlan_eval_env_t env)
{
	int res;
	
	switch (env->retval.type) {
	case forlan_value_void:
		err("controlling expression returned void, aborting");
		abort();

	case forlan_value_node:
		res = env->retval.v.node != NULL;
		break;

	case forlan_value_literal:
		res = env->retval.v.string != NULL &&
			env->retval.v.string[0] != 0;
		break;

	case forlan_value_boolean:
		res = env->retval.v.num;
		break;
		
	default:
		abort();
	}
	return res;
}

void eval_func(forlan_eval_env_t env, union forlan_node *node);
void eval_last(forlan_eval_env_t env, union forlan_node *node);


static void
free_type_null(union forlan_node *p)
{
	warn("freeing undefined forlan_node");
}

void
dump_null(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "[undefined node]\n");
}

void
eval_null(forlan_eval_env_t env, union forlan_node *node)
{
	err("stumbled upon an undefined node, aborting");
	abort();
}

static void
free_type_comp(union forlan_node *p)
{
	int i;
	for (i = 0; i < p->comp.argc; i++) {
		free(p->comp.argv[i]);
		if (p->comp.labelv[i]) {
			free_value(p->comp.labelv[i]);
			free(p->comp.labelv[i]);
		}
	}
	free(p->comp.argv);
	free(p->comp.labelv);
}

void
dump_comp(FILE *fp, union forlan_node *p, int *num, int lev)
{
	int i;
	
	fprintf(fp, "COMP");
	if (p->comp.root) {
		fprintf(fp, "\n");
		fprintf(fp, "%04d: %*.*sROOT\n", ++*num, lev, lev, "");
		forlan_dump_node(fp, p->comp.root, num, lev + 1);
		fprintf(fp, "%04d: %*.*sCOMP: ", ++*num, lev, lev, "");
	}
	for (i = 0; i < p->comp.argc; i++) {
		fprintf(fp, ".%s", p->comp.argv[i]);
		if (p->comp.labelv[i]) {
			fprintf(fp, "[%s]", p->comp.labelv[i]->v.string);
		}
	}
	fputc('\n', fp);
}

grecs_match_buf_t 
eval_comp0(forlan_eval_env_t env, union forlan_node *p)
{
	struct grecs_node *root;
	grecs_match_buf_t mb = NULL;
	
	if (p->comp.root) {
		forlan_eval(env, p->comp.root);
		if (!retval_boolean(env))
			return;
		coerce_retval(env, 'n');
		root = env->retval.v.node;
	} else
		root = env->tree;
	if (p->comp.argc) {
		mb = grecs_match_buf_create(p->comp.argc, p->comp.argv,
					    p->comp.labelv);
		if (p->comp.wildcards) {
			root = grecs_match_buf_first(mb, root);
		} else {
			grecs_match_buf_set_root(mb, root);
			if (root->type != grecs_node_root)
				root = root->down;
			grecs_tree_recurse(root, grecs_node_exact_match, mb);
			root = grecs_match_buf_get_node(mb);
		}
	}
	env->retval.type = forlan_value_node;
	env->last = env->retval.v.node = root;
	return mb;
}

void
eval_comp(forlan_eval_env_t env, union forlan_node *p)
{
	free(eval_comp0(env, p));
}

static void
free_type_test(union forlan_node *p)
{
	free(p->test.comp);
	free(p->test.value);
}

void
dump_test(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "TEST: %s[%s]\n", p->test.comp, p->test.value);
}

void
eval_test(forlan_eval_env_t env, union forlan_node *node)
{
	err("stumbled upon a lone test node, aborting");
	abort();
}


static void
free_type_func(union forlan_node *p)
{
	grecs_list_free(p->func.args);
}

void
dump_func(FILE *fp, union forlan_node *p, int *num, int lev)
{
	struct grecs_list_entry *ep;
	fprintf(fp, "CALL: %s\n", p->func.fp->name);
	if (p->func.args)
		for (ep = p->func.args->head; ep; ep = ep->next)
			forlan_dump_node(fp, ep->data, num, lev + 1);
}		

void
eval_func(forlan_eval_env_t env, union forlan_node *node)
{
	struct grecs_list *vlist;
	struct grecs_list_entry *ep;
	char *type;
	
	free_value(&env->retval);

	vlist = grecs_list_create();
	vlist->free_entry = free_value;

	type = node->func.fp->argtypes;
	for (ep = node->func.args->head; ep; ep = ep->next) {
		struct forlan_value *val;
		
		forlan_eval(env, ep->data);
		if (*type) 
			coerce_retval(env, *type++);
		val = grecs_malloc(sizeof(*val));
		copy_value(val, &env->retval);
		grecs_list_append(vlist, val);
	}
	
	node->func.fp->func(env, vlist);
	grecs_list_free(vlist);
	if (env->retval.type == forlan_value_node)
		env->last = env->retval.v.node;
}


static void
free_type_cond(union forlan_node *p)
{
	forlan_node_free((union forlan_node *)p->cond.expr);
	forlan_node_free(p->cond.iftrue);
	forlan_node_free(p->cond.iffalse);
}

void
dump_cond(FILE *fp, union forlan_node *p, int *num, int lev)
{
	int n = *num;
	fprintf(fp, "COND\n");
	forlan_dump_node(fp, p->cond.expr, num, lev + 1);
	fprintf(fp, "%04d: %*.*sIFTRUE %04d\n", ++*num, lev, lev, "", n);
	forlan_dump_node(fp, p->cond.iftrue, num, lev + 1);
	fprintf(fp, "%04d: %*.*sIFFALSE %04d\n", ++*num, lev, lev, "", n);
	forlan_dump_node(fp, p->cond.iffalse, num, lev + 1);	
}

void
eval_cond(forlan_eval_env_t env, union forlan_node *node)
{
	forlan_eval(env, node->cond.expr);
	if (retval_boolean(env))
		forlan_eval(env, node->cond.iftrue);
	else if (node->cond.iffalse)
		forlan_eval(env, node->cond.iffalse);
}

static void
free_type_stmt(union forlan_node *p)
{
	forlan_node_free(p->stmt.stmt);
	forlan_node_free(p->stmt.next);
}

void
dump_stmt(FILE *fp, union forlan_node *p, int *num, int lev)
{
	f_dump_node(fp, p->stmt.stmt, num, lev);
	forlan_dump_node(fp, p->stmt.next, num, lev);
}

void
eval_stmt(forlan_eval_env_t env, union forlan_node *node)
{
	union forlan_node *np;

	while (node && node->type == forlan_type_stmt) {
		forlan_eval(env, node->stmt.stmt);
		node = node->stmt.next;
	}
	if (node)
		forlan_eval(env, node);
}

static void
free_type_lit(union forlan_node *p)
{
	free(p->lit.string);
}

void
dump_lit(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "LIT: \"%s\"\n", p->lit.string);
}

void
eval_lit(forlan_eval_env_t env, union forlan_node *node)
{
	free_value(&env->retval);
	env->retval.type = forlan_value_literal;
	env->retval.v.string = grecs_strdup(node->lit.string);
}


static void
free_type_expr(union forlan_node *p)
{
	forlan_node_free(p->expr.arg[0]);
	forlan_node_free(p->expr.arg[1]);
}

void
dump_expr(FILE *fp, union forlan_node *p, int *num, int lev)
{
	static char *opstr[] = { "NODE", "AND", "OR", "NOT" };

	fprintf(fp, "%s\n", opstr[p->expr.opcode]);
	forlan_dump_node(fp, p->expr.arg[0], num, lev + 1);
	if (p->expr.arg[1])
		forlan_dump_node(fp, p->expr.arg[1], num, lev + 1);
}

void
eval_expr(forlan_eval_env_t env, union forlan_node *node)
{
	int res;
	
	free_value(&env->retval);
	switch (node->expr.opcode) {
	case forlan_opcode_node:
		forlan_eval(env, node->expr.arg[0]); 
		break;

	case forlan_opcode_and:
		forlan_eval(env, node->expr.arg[0]); 
		if (retval_boolean(env))
			forlan_eval(env, node->expr.arg[1]);
		break;

	case forlan_opcode_or:
		forlan_eval(env, node->expr.arg[0]);
		if (!retval_boolean(env))
			forlan_eval(env, node->expr.arg[1]);
		break;

	case forlan_opcode_not:
		forlan_eval(env, node->expr.arg[0]); 
		res = retval_boolean(env);
		free_value(&env->retval);
		env->retval.type = forlan_value_boolean;
		env->retval.v.num = res;
		break;
			
	default:
		abort();
	}
}

static void
free_type_last(union forlan_node *p)
{
}

void
dump_last(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "LAST\n");
}

void
eval_last(forlan_eval_env_t env, union forlan_node *node)
{
	free_value(&env->retval);
	env->retval.type = forlan_value_node;
	env->retval.v.node = env->last;
}

static void
free_type_asgn(union forlan_node *p)
{
	forlan_node_free(p->asgn.node);
}

void
dump_asgn(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "ASGN %lu\n", (unsigned long) p->asgn.idx);
	forlan_dump_node(fp, p->asgn.node, num, lev + 1);
}

void
eval_asgn(forlan_eval_env_t env, union forlan_node *p)
{
	forlan_eval(env, p->asgn.node);
	env->vartab[p->asgn.idx] = env->retval;
}

static void
free_type_var(union forlan_node *p)
{
	/* nothing */
}

void
dump_var(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "VAR %lu\n", (unsigned long) p->asgn.idx);
	forlan_dump_node(fp, p->loop.node, num, lev + 1);
}

void
eval_var(forlan_eval_env_t env, union forlan_node *p)
{
	free_value(&env->retval);
	copy_value(&env->retval, &env->vartab[p->asgn.idx]);
}

static void
free_type_loop(union forlan_node *p)
{
	forlan_node_free(p->loop.node);
	forlan_node_free(p->loop.stmt);
}

void
dump_loop(FILE *fp, union forlan_node *p, int *num, int lev)
{
	int n = *num;
	fprintf(fp, "LOOP %lu\n", (unsigned long) p->loop.idx);
	fprintf(fp, "%04d: %*.*sEXPR\n", ++*num, lev, lev, "");
	forlan_dump_node(fp, p->loop.node, num, lev + 1);
	fprintf(fp, "%04d: %*.*sSTMT %lu\n", ++*num, lev, lev, "", n);
	forlan_dump_node(fp, p->loop.stmt, num, lev + 1);
	fprintf(fp, "%04d: %*.*sEND LOOP %lu\n", ++*num, lev, lev, "", n);
}

void
eval_loop(forlan_eval_env_t env, union forlan_node *p)
{
	jmp_buf save;
	grecs_match_buf_t mb;
	struct grecs_node *node;
	
	mb = eval_comp0(env, p->loop.node);
	memcpy(&save, &env->loop_buf, sizeof(save));
	if (setjmp(env->loop_buf) < 2) {
		while (retval_boolean(env)) {
			copy_value(&env->vartab[p->asgn.idx], &env->retval);
			forlan_eval(env, p->loop.stmt);
			node = grecs_match_next(mb);
			env->retval.type = forlan_value_node;
			env->retval.v.node = node;
		}			
	}
	memcpy(&env->loop_buf, &save, sizeof(save));
	free(mb);
}

void
dump_continue(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "CONTINUE\n");
}

void
eval_continue(forlan_eval_env_t env, union forlan_node *node)
{
	longjmp(env->loop_buf, 1);
}

void
dump_break(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "BREAK\n");
}

void
eval_break(forlan_eval_env_t env, union forlan_node *node)
{
	longjmp(env->loop_buf, 2);
}

void
dump_stop(FILE *fp, union forlan_node *p, int *num, int lev)
{
	fprintf(fp, "STOP\n");
}

void
eval_stop(forlan_eval_env_t env, union forlan_node *node)
{
	longjmp(env->stop_buf, 1);
}


struct forlan_node_method {
	void (*f_free)(union forlan_node *);
	void (*f_dump)(FILE *fp, union forlan_node *node, int *num, int lev);
	void (*f_eval)(forlan_eval_env_t, union forlan_node *node);
};

static struct forlan_node_method f_tab[] = {
	free_type_null,  dump_null, eval_null, /* Unknown/unset type */
	free_type_comp,  dump_comp, eval_comp, /* A path component */  
	free_type_test,	 dump_test, eval_test, /* Value test (f[X]) */ 
	free_type_func,	 dump_func, eval_func, /* Function call */     
	free_type_cond,	 dump_cond, eval_cond, /* Conditional */       
	free_type_stmt,	 dump_stmt, eval_stmt, /* Statement */         
	free_type_lit,	 dump_lit,  eval_lit,  /* Literal */           
	free_type_expr,	 dump_expr, eval_expr, /* Boolean expression */
	free_type_last,  dump_last, eval_last, /* "last" */
	free_type_asgn,  dump_asgn, eval_asgn, /* Variable assignment */
	free_type_var,   dump_var,  eval_var,  /* Variable reference */
	free_type_loop,  dump_loop, eval_loop, /* Loop */
	NULL,            dump_continue, eval_continue,  /* continue statement */
	NULL,            dump_break, eval_break,        /* break statement */
	NULL,            dump_stop,  eval_stop,         /* stop statement */
};

void
forlan_node_free(union forlan_node *p)
{
	if (!p)
		return;
	if (p->type > sizeof(f_tab) / sizeof(f_tab[0]))
		abort();
	if (f_tab[p->type].f_free)
		f_tab[p->type].f_free(p);
	free(p);
}

static void
stmt_list_free_entry(void *p)
{
	forlan_node_free(p);
}

struct grecs_list *
forlan_stmt_list()
{
	struct grecs_list *lp;

	lp = grecs_list_create();
	lp->free_entry = stmt_list_free_entry;
	return lp;
}

union forlan_node *
forlan_stmt_from_list(struct grecs_list *list)
{
	union forlan_node *prev = NULL, *ret = NULL;
	struct grecs_list_entry *ep;

	for (ep = list->head; ep; ep = ep->next) {
		union forlan_node *sp = forlan_node_create(forlan_type_stmt);
		sp->stmt.stmt = ep->data;
		if (prev) {
			prev->stmt.next = sp;
			sp->stmt.prev = prev;
		} else
			ret = sp;
		prev = sp;
	}
	list->free_entry = NULL;
	grecs_list_free(list);
	return ret;
}

static void
f_dump_node(FILE *fp, union forlan_node *p, int *num, int lev)
{
	if (p) {
		if (p->type > sizeof(f_tab) / sizeof(f_tab[0]))
			abort();
		if (f_tab[p->type].f_dump)
			f_tab[p->type].f_dump(fp, p, num, lev);
		else
			fprintf(fp, "type %d", p->type);
	} else
		fprintf(fp, "NULL");
}

void
forlan_dump_node(FILE *fp, union forlan_node *p, int *num, int lev)
{
	if (!p)
		return;
	++*num;
	fprintf(fp, "%04d: %*.*s", *num, lev, lev, "");
	f_dump_node(fp, p, num, lev);
}

void
forlan_dump_tree(FILE *fp, forlan_eval_env_t env)
{
	int n = 0;
	forlan_dump_node(fp, env->parse_tree, &n, 0);
}

void
func_dump(forlan_eval_env_t env, struct grecs_list *list)
{
	struct forlan_value *val = list->head->data;
	if (val->v.node)
		grecs_print_node(val->v.node, GRECS_NODE_FLAG_DEFAULT,
				 stdout);
	fputc('\n', stdout);
}

static void
generic_print(forlan_eval_env_t env, struct grecs_list *list, FILE *fp)
{
	struct grecs_list_entry *ep;
	struct forlan_value *val;

	for (ep = list->head; ep; ep = ep->next) {
		val = ep->data;

		switch (val->type) {
		case forlan_value_void:
			/* skip it */
			break;
		case forlan_value_node:
			if (val->v.node)
				grecs_print_node(val->v.node,
						 GRECS_NODE_FLAG_VALUE, fp);
			break;
		case forlan_value_literal:
			if (val->v.string)
				fwrite(val->v.string, strlen(val->v.string),
				       1, fp);
			break;
		case forlan_value_boolean:
			fprintf(fp, "%d", !!val->v.num);
		}
	}
}

void
func_print(forlan_eval_env_t env, struct grecs_list *list)
{
	generic_print(env, list, stdout);
}

void
func_error(forlan_eval_env_t env, struct grecs_list *list)
{
	generic_print(env, list, stderr);
}

void
func_parent(forlan_eval_env_t env, struct grecs_list *list)
{
	struct forlan_value *val = list->head->data;
	if (val->v.node) {
		env->retval.type = forlan_value_node;
		env->retval.v.node = val->v.node->up;
	}
}

static int
node_ident_cmp(struct grecs_node const *a, struct grecs_node const *b)
{
        return strcmp(a->ident, b->ident);
}

/* FIXME */
void
func_sort(forlan_eval_env_t env, struct grecs_list *list)
{
	struct forlan_value *val = list->head->data;
	if (val->v.node)
		grecs_tree_sort(val->v.node, node_ident_cmp);
}

static struct forlan_function functab[] = {
	{ "dump",   forlan_value_void, "n", 1, 1, func_dump },
	{ "print",  forlan_value_void, "", 1, -1, func_print },
	{ "error",  forlan_value_void, "", 1, -1, func_error },
	{ "parent", forlan_value_node, "n", 1, 1, func_parent },
	{ "sort",   forlan_value_void, "n", 1, 1, func_sort },
	{ NULL }
};

struct forlan_function *
forlan_find_function(const char *name)
{
	struct forlan_function *fp;
	for (fp = functab; fp->name; fp++)
		if (strcmp(name, fp->name) == 0)
			return fp;
	return NULL;
}	

void
forlan_eval(struct forlan_eval_env *env, union forlan_node *p)
{
	if (p->type > sizeof(f_tab) / sizeof(f_tab[0]))
		abort();
	if (f_tab[p->type].f_eval)
		f_tab[p->type].f_eval(env, p);
}

void
forlan_run(forlan_eval_env_t env, struct grecs_node *tree)
{
	env->last = NULL;
	env->top_node = NULL;
	env->retval.type = forlan_value_void;
	env->instr = NULL;
	env->tree = tree;
	if (setjmp(env->stop_buf) == 0)
		forlan_eval(env, env->parse_tree);
	free_value(&env->retval);
}

forlan_eval_env_t
forlan_create_environment(union forlan_node *parse_tree, size_t varcount)
{
	struct forlan_eval_env *env;
	env = grecs_zalloc(sizeof(*env));
	env->parse_tree = parse_tree;
	env->vartab = grecs_calloc(varcount, sizeof(env->vartab[0]));
	return env;
}

void
forlan_free_environment(forlan_eval_env_t env)
{
	forlan_node_free(env->parse_tree);
	free(env->vartab);
	free(env);
}

Return to:

Send suggestions and report system problems to the System administrator.