-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.c
executable file
·106 lines (100 loc) · 1.82 KB
/
evaluator.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
#include "cx.h"
void next_node(void)
{
if(tag_of(current) != TAG_CCELL || current->cdr == NULL)
{
current = pop(callstack);
if(tag_of(current)==TAG_CCELL)
input = current->car;
else
input = current;
}
else
{
current = current->cdr;
input = current->car;
}
}
void evaluate(void)
{
schedule_next(((cd*)input)->content);
while(true) eval_current();
}
void eval_current(void)
{
int tag;
next_node();
tag = tag_of(input);
switch(tag)
{
case TAG_CD:
eval_cd();
break;
case TAG_PRIMITIVE:
((primitive*)input)->block();
break;
case TAG_SYMBOL:
eval_symbol();
break;
case TAG_INT:
case TAG_ARRAY:
case TAG_STRING:
case TAG_NIL:
case TAG_CONTEXT:
execute_waiter();
break;
case TAG_I_S:
push(wait_stack,((inp_stub*)input)->func);
break;
default:
printf("evaluated object of unknown tag:%i ",tag);
break;
}
check_gc(0.5 KB);
}
void eval_cd(void)
{
int tag = ((cd*)input)->col_tag;
switch(tag)
{
case LIVEQ:
schedule_next(((cd*)input)->content);
break;
case LIST:
schedule_next(trigger);
schedule_next(((cd*)input)->content);
push(collections,NULL);
push(wait_stack,collector);
break;
case LITLIST:
case QUOTE:
execute_waiter();
break;
default:
debug("unknown col... ");
break;
}
}
void eval_symbol(void)
{
symbol* the_sym = (symbol*)input;
if(the_sym->meta_level == 0)
{
binding* the_binding = find_binding_delegating(the_sym->id,current_scope);
if(the_binding != NULL)
{
void* value = the_binding->value;
if(value != the_sym)
schedule_next(value);
else /* the symbol is a singleton */
execute_waiter();
}
else
printf("error:\nsymbol :%s is bound to nothing in this accumulative context", get_symbol_name(the_sym->id));
}
else
{
input = make_symbol(the_sym->id,the_sym->meta_level-1);
execute_waiter();
}
}