File: node.c

package info (click to toggle)
midish 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,344 kB
  • sloc: ansic: 22,502; sh: 268; makefile: 119
file content (715 lines) | stat: -rw-r--r-- 15,612 bytes parent folder | download
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
/*
 * Copyright (c) 2003-2010 Alexandre Ratchov <alex@caoua.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * this module implements the tree containing interpreter code. Each
 * node of the tree represents one instruction.
 */

#include "utils.h"
#include "str.h"
#include "data.h"
#include "node.h"
#include "exec.h"
#include "cons.h"
#include "user.h"
#include "textio.h"

struct node *
node_new(struct node_vmt *vmt, struct data *data)
{
	struct node *o;

	o = xmalloc(sizeof(struct node), "node");
	o->vmt = vmt;
	o->data = data;
	o->list = o->next = NULL;
	return o;
}

void
node_delete(struct node *o)
{
	struct node *i, *inext;
	if (o == NULL) {
		return;
	}
	for (i = o->list; i != NULL; i = inext) {
		inext = i->next;
		node_delete(i);
	}
	if (o->data) {
		data_delete(o->data);
	}
	xfree(o);
}

void
node_log(struct node *o, unsigned depth)
{
#define NODE_MAXDEPTH 30
	static char str[2 * NODE_MAXDEPTH + 1] = "";
	struct node *i;

	log_puts(str);
	log_puts(o != NULL && o->next != NULL ? "+-" : "\\-");
	if (o == NULL) {
		log_puts("<EMPTY>\n");
		return;
	}
	log_puts(o->vmt->name);
	if (o->data) {
		log_puts("(");
		data_log(o->data);
		log_puts(")");
	}
	log_puts(depth >= NODE_MAXDEPTH && o->list ? "[...]\n" : "\n");
	if (depth < NODE_MAXDEPTH) {
		str[2 * depth] = o->next ? '|' : ' ';
		str[2 * depth + 1] = ' ';
		str[2 * depth + 2] = '\0';
		for (i = o->list; i != NULL; i = i->next) {
			node_log(i, depth + 1);
		}
		str[2 * depth] = '\0';
	}
#undef NODE_MAXDEPTH
}


void
node_insert(struct node **n, struct node *e)
{
	e->next = *n;
	*n = e;
}

void
node_replace(struct node **n, struct node *e)
{
	if (e->list != NULL) {
		log_puts("node_replace: e->list != NULL\n");
		panic();
	}
	e->list = *n;
	e->next = (*n)->next;
	(*n)->next = NULL;
	*n = e;
}


/*
 * run a node.
 * the following rule must be respected
 * 1) node_exec must be called always with *r == NULL
 * 2) in statements (slist,...) *r != NULL if and only if RETURN
 * 3) in expressions (add, ...) *r == NULL if and only if ERROR
 */
unsigned
node_exec(struct node *o, struct exec *x, struct data **r)
{
	unsigned result;
	if (x->depth == EXEC_MAXDEPTH) {
		cons_err("too many nested operations");
		return RESULT_ERR;
	}
	*r = NULL;
	x->depth++;
	result = o->vmt->exec(o, x, r);
	x->depth--;
	if (result == RESULT_ERR && *r) {
		data_delete(*r);
		*r = NULL;
	}
	return result;
}

/*
 * execute an unary operator ( '-', '!', '~')
 */
unsigned
node_exec_unary(struct node *o, struct exec *x, struct data **r,
	unsigned (*func)(struct data *)) {
	if (node_exec(o->list, x, r) == RESULT_ERR) {
		return RESULT_ERR;
	}
	if (!func(*r)) {
		return RESULT_ERR;
	}
	return RESULT_OK;
}

/*
 * execute a binary operator
 */
unsigned
node_exec_binary(struct node *o, struct exec *x, struct data **r,
	unsigned (*func)(struct data *, struct data *)) {
	struct data *lhs;
	if (node_exec(o->list, x, r) == RESULT_ERR) {
		return RESULT_ERR;
	}
	lhs = *r;
	*r = NULL;
	if (node_exec(o->list->next, x, r) == RESULT_ERR) {
		data_delete(lhs);
		return RESULT_ERR;
	}
	if (!func(lhs, *r)) {
		data_delete(lhs);
		return RESULT_ERR;
	}
	data_delete(*r);
	*r = lhs;
	return RESULT_OK;
}

/*
 * execute a procedure definition: just check arguments
 * and move the tree into a proc structure
 */
unsigned
node_exec_proc(struct node *o, struct exec *x, struct data **r)
{
	struct proc *p;
	struct data *a;
	struct name *args;

	args = NULL;
	for (a = o->data->val.list->next; a != NULL; a = a->next) {
		if (name_lookup(&args, a->val.ref)) {
			cons_err("duplicate arguments in proc definition");
			name_empty(&args);
			return RESULT_ERR;
		}
		name_add(&args, name_new(a->val.ref));
	}
	p = exec_proclookup(x, o->data->val.list->val.ref);
	if (p != NULL) {
		name_empty(&p->args);
		node_delete(p->code);
	} else {
		p = proc_new(o->data->val.list->val.ref);
		name_insert((struct name **)&x->procs, (struct name *)p);
	}
	p->args = args;
	p->code = o->list;
	o->list = NULL;
	return RESULT_OK;
}

/*
 * execute a list of statements
 */
unsigned
node_exec_slist(struct node *o, struct exec *x, struct data **r)
{
	struct node *i;
	unsigned result;

	for (i = o->list; i != NULL; i = i->next) {
		result = node_exec(i, x, r);
		if (result != RESULT_OK) {
			/* stop on ERR, BREAK, CONTINUE, RETURN, EXIT */
			return result;
		}
	}
	return RESULT_OK;
}

/*
 * execute a builtin function
 * if the function didn't set 'r', then set it to 'nil'
 */
unsigned
node_exec_builtin(struct node *o, struct exec *x, struct data **r)
{

	if (!((unsigned (*)(struct exec *, struct data **))
	    o->data->val.user)(x, r)) {
		return RESULT_ERR;
	}
	if (!*r) {
		*r = data_newnil();
	}
	return RESULT_OK;
}

/*
 * return a constant
 */
unsigned
node_exec_cst(struct node *o, struct exec *x, struct data **r)
{
	*r = data_newnil();
	data_assign(*r, o->data);
	return RESULT_OK;
}

/*
 * return the value of the variable (in the node)
 */
unsigned
node_exec_var(struct node *o, struct exec *x, struct data **r)
{
	struct var *v;

	v = exec_varlookup(x, o->data->val.ref);
	if (v == NULL) {
		cons_errss(x->procname, o->data->val.ref, "no such variable");
		return RESULT_ERR;
	}
	*r = data_newnil();
	data_assign(*r, v->data);
	return RESULT_OK;
}

/*
 * ignore the result of the expression
 * (used to ignore return values of calls)
 */
unsigned
node_exec_ignore(struct node *o, struct exec *x, struct data **r)
{
	unsigned result;

	result = node_exec(o->list, x, r);
	if (result == RESULT_ERR || result == RESULT_EXIT) {
		return result;
	}
	if (*r != NULL && (*r)->type != DATA_NIL) {
		data_print(*r);
		textout_putstr(tout, "\n");
	}
	data_delete(*r);
	*r = NULL;
	return RESULT_OK;
}

/*
 * call a procedure
 */
unsigned
node_exec_call(struct node *o, struct exec *x, struct data **r)
{
	struct proc *p;
	struct name **oldlocals, *newlocals;
	struct name *argn;
	struct node *argv;
	struct var *valist;
	char *procname_save;
	unsigned result;

	newlocals = NULL;
	result = RESULT_ERR;

	p = exec_proclookup(x, o->data->val.ref);
	if (p == NULL) {
		cons_errs(o->data->val.ref, "no such proc");
		goto finish;
	}
	valist = NULL;
	argv = o->list;
	for (argn = p->args; argn != NULL; argn = argn->next) {
		if (str_eq(argn->str, "...")) {
			valist = var_new(&newlocals, "...", data_newlist(NULL));
			break;
		}
		if (argv == NULL) {
			cons_errs(o->data->val.ref, "to few arguments");
			goto finish;
		}
		if (node_exec(argv, x, r) == RESULT_ERR) {
			goto finish;
		}
		var_new(&newlocals, argn->str, *r);
		argv = argv->next;
		*r = NULL;
	}
	if (valist == NULL && argv != NULL) {
		cons_errs(o->data->val.ref, "to many arguments");
		goto finish;
	}
	while (argv != NULL) {
		if (node_exec(argv, x, r) == RESULT_ERR)
			goto finish;
		data_listadd(valist->data, *r);
		argv = argv->next;
		*r = NULL;
	}
	oldlocals = x->locals;
	x->locals = &newlocals;
	procname_save = x->procname;
	x->procname = p->name.str;
	result = node_exec(p->code, x, r);
	if (result != RESULT_ERR) {
		if (*r == NULL) {	/* we always return something */
			*r = data_newnil();
		}
		if (result != RESULT_EXIT) {
			result = RESULT_OK;
		}
	}
	x->locals = oldlocals;
	x->procname = procname_save;
finish:
	var_empty(&newlocals);
	return result;
}

unsigned
node_exec_if(struct node *o, struct exec *x, struct data **r)
{
	unsigned cond, result;
	if (node_exec(o->list, x, r) == RESULT_ERR) {
		return RESULT_ERR;
	}
	cond = data_eval(*r);
	data_delete(*r);
	*r = NULL;
	if (cond) {
		result = node_exec(o->list->next, x, r);
		if (result != RESULT_OK) {
			return result;
		}
	} else if (o->list->next->next) {
		result = node_exec(o->list->next->next, x, r);
		if (result != RESULT_OK) {
			return result;
		}
	}
	return RESULT_OK;
}

unsigned
node_exec_for(struct node *o, struct exec *x, struct data **r)
{
	unsigned result;
	struct data *list, *i;
	struct var *v;

	if (node_exec(o->list, x, &list) == RESULT_ERR) {
		return RESULT_ERR;
	}
	if (list->type != DATA_LIST && list->type != DATA_RANGE) {
		cons_errs(x->procname,
		    "argument to 'for' must be a list or range");
		return RESULT_ERR;
	}
	v = exec_varlookup(x, o->data->val.ref);
	if (v == NULL) {
		v = var_new(x->locals, o->data->val.ref, data_newnil());
	}
	result = RESULT_OK;
	if (list->type == DATA_LIST) {
		for (i = list->val.list; i != NULL; i = i->next) {
			data_assign(v->data, i);
			result = node_exec(o->list->next, x, r);
			if (result == RESULT_CONTINUE) {
				continue;
			} else if (result == RESULT_BREAK) {
				break;
			} else if (result != RESULT_OK) {
				break;
			}
		}
	} else {
		if (list->val.range.min <= list->val.range.max) {
			i = data_newlong(list->val.range.min);
			while (1) {
				data_assign(v->data, i);
				result = node_exec(o->list->next, x, r);
				if (result == RESULT_CONTINUE) {
					continue;
				} else if (result == RESULT_BREAK) {
					break;
				} else if (result != RESULT_OK) {
					break;
				}
				if (i->val.num == list->val.range.max)
					break;
				i->val.num++;
			}
			data_delete(i);
		}
	}
	data_delete(list);
	return result;
}

unsigned
node_exec_return(struct node *o, struct exec *x, struct data **r)
{
	if (node_exec(o->list, x, r) == RESULT_ERR) {
		return RESULT_ERR;
	}
	return RESULT_RETURN;
}

unsigned
node_exec_exit(struct node *o, struct exec *x, struct data **r)
{
	return RESULT_EXIT;
}

unsigned
node_exec_assign(struct node *o, struct exec *x, struct data **r)
{
	struct var *v;
	struct data *expr;

	if (node_exec(o->list, x, &expr) == RESULT_ERR) {
		return RESULT_ERR;
	}
	v = exec_varlookup(x, o->data->val.ref);
	if (v == NULL) {
		v = var_new(x->locals, o->data->val.ref, expr);
	} else {
		data_delete(v->data);
		v->data = expr;
	}
	return RESULT_OK;
}

/*
 * do nothing
 */
unsigned
node_exec_nop(struct node *o, struct exec *x, struct data **r)
{
	return RESULT_OK;
}

/*
 * built a list from the expression list
 */
unsigned
node_exec_list(struct node *o, struct exec *x, struct data **r)
{
	struct node *arg;
	struct data *d;

	*r = data_newlist(NULL);

	for (arg = o->list; arg != NULL; arg = arg->next) {
		if (node_exec(arg, x, &d) == RESULT_ERR) {
			data_delete(*r);
			*r = NULL;
			return RESULT_ERR;
		}
		data_listadd(*r, d);
	}
	return RESULT_OK;
}

/*
 * built a range from two integers
 */
unsigned
node_exec_range(struct node *o, struct exec *x, struct data **r)
{
	struct data *min, *max;

	if (!node_exec(o->list, x, &min))
		return RESULT_ERR;
	if (!node_exec(o->list->next, x, &max))
		return RESULT_ERR;
	if (min->type != DATA_LONG || max->type != DATA_LONG) {
		cons_err("cannot create a range with non integers");
		return RESULT_ERR;
	}
	if (min->val.num > max->val.num) {
		cons_err("max > min, cant create a valid range");
		return RESULT_ERR;
	}
	*r = data_newrange(min->val.num, max->val.num);
	return RESULT_OK;
}

unsigned
node_exec_eq(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_eq);
}

unsigned
node_exec_neq(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_neq);
}

unsigned
node_exec_le(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_le);
}

unsigned
node_exec_lt(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_lt);
}


unsigned
node_exec_ge(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_ge);
}

unsigned
node_exec_gt(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_gt);
}

unsigned
node_exec_and(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_and);
}

unsigned
node_exec_or(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_or);
}

unsigned
node_exec_not(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_unary(o, x, r, data_not);
}

unsigned
node_exec_add(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_add);
}

unsigned
node_exec_sub(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_sub);
}

unsigned
node_exec_mul(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_mul);
}

unsigned
node_exec_div(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_div);
}

unsigned
node_exec_mod(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_mod);
}

unsigned
node_exec_neg(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_unary(o, x, r, data_neg);
}

unsigned
node_exec_lshift(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_lshift);
}

unsigned
node_exec_rshift(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_rshift);
}

unsigned
node_exec_bitand(struct node *o, struct exec *x, struct data **r)
{

	return node_exec_binary(o, x, r, data_bitand);
}

unsigned
node_exec_bitor(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_bitor);
}

unsigned
node_exec_bitxor(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_binary(o, x, r, data_bitxor);
}

unsigned
node_exec_bitnot(struct node *o, struct exec *x, struct data **r)
{
	return node_exec_unary(o, x, r, data_bitnot);
}

struct node_vmt
node_vmt_proc = { "proc", node_exec_proc },
node_vmt_slist = { "slist", node_exec_slist },
node_vmt_cst = { "cst", node_exec_cst },
node_vmt_var = { "var", node_exec_var },
node_vmt_call = { "call", node_exec_call },
node_vmt_ignore = { "ignore", node_exec_ignore },
node_vmt_builtin = { "builtin", node_exec_builtin },
node_vmt_if = { "if", node_exec_if },
node_vmt_for = { "for", node_exec_for },
node_vmt_return = { "return", node_exec_return },
node_vmt_exit = { "exit", node_exec_exit },
node_vmt_assign = { "assign", node_exec_assign },
node_vmt_nop = { "nop", node_exec_nop },
node_vmt_list = { "list", node_exec_list},
node_vmt_range = { "range", node_exec_range},
node_vmt_eq = { "eq", node_exec_eq },
node_vmt_neq = { "neq", node_exec_neq },
node_vmt_le = { "le", node_exec_le },
node_vmt_lt = { "lt", node_exec_lt },
node_vmt_ge = { "ge", node_exec_ge },
node_vmt_gt = { "gt", node_exec_gt },
node_vmt_and = { "and", node_exec_and },
node_vmt_or = { "or", node_exec_or },
node_vmt_not = { "not", node_exec_not },
node_vmt_add = { "add", node_exec_add },
node_vmt_sub = { "sub", node_exec_sub },
node_vmt_mul = { "mul", node_exec_mul },
node_vmt_div = { "div", node_exec_div },
node_vmt_mod = { "mod", node_exec_mod },
node_vmt_neg = { "neg", node_exec_neg },
node_vmt_lshift = { "lshift", node_exec_lshift },
node_vmt_rshift = { "rshift", node_exec_rshift },
node_vmt_bitand = { "bitand", node_exec_bitand },
node_vmt_bitor = { "bitor", node_exec_bitor },
node_vmt_bitxor = { "bitxor", node_exec_bitxor },
node_vmt_bitnot = { "bitnot", node_exec_bitnot };