-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.c
141 lines (137 loc) · 3.45 KB
/
evaluate.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
#include "evaluate.h"
struct Value *match(struct Value *pattern, struct Value *term,
struct Value *env)
{
match_top:
if (!pattern || !term)
return 0;
switch (pattern->type)
{
case SYMBOL:
return extended(pattern, term, env);
case PAIR:
env = match(((struct Pair*)pattern->value)->car,
((struct Pair*)term->value)->car,
env);
pattern = ((struct Pair*)pattern->value)->cdr;
term = ((struct Pair*)term->value)->cdr;
goto match_top;
default:
if (*((int*)equal(pattern, term)->value))
return env;
fprintf(stderr, "no match\n");
return 0;
}
}
void evlis(struct Value *l, struct Value *env, struct Value **out)
{
struct Value *i, *o, *e, *t;
o = e = 0;
for (i = l; i->type == PAIR; i = ((struct Pair*)i->value)->cdr)
{
evaluate(((struct Pair*)i->value)->car, env, &t);
if (o)
{
set_cdr((struct Pair*)e->value, pair(t, 0));
e = ((struct Pair*)e->value)->cdr;
}
else
o = e = pair(t, 0);
}
evaluate(i, env, &t);
if (o)
set_cdr((struct Pair*)e->value, t);
else
o = t;
*out = o;
}
void evaluate(struct Value *exp, struct Value *env, struct Value **out)
{
eval_top:
switch (exp->type)
{
case PAIR:
{
struct Value *operator, *argument;
operator = ((struct Pair*)exp->value)->car;
evaluate(operator, env, &operator);
if (!operator)
{
fprintf(stderr, "could not produce operator\n");
return;
}
set_car(((struct Pair*)exp->value), operator);
switch (operator->type)
{
case CLOSURE:
{
struct Closure *c;
c = (struct Closure*)operator->value;
argument = ((struct Pair*)exp->value)->cdr;
evlis(argument, env, &argument);
if (!(env = match(c->args, argument, c->env)))
{
fprintf(stderr, "cannot combine\n");
return;
}
exp = c->body;
goto eval_top;
}
case OPERATOR:
{
struct Operator *o;
o = (struct Operator*) operator->value;
argument = ((struct Pair*)exp->value)->cdr;
if (!(env = match(o->args, argument, o->env)))
{
fprintf(stderr, "cannot combine\n");
return;
}
exp = o->body;
goto eval_top;
}
case INTRINSIC:
{
((struct Intrinsic*)operator->value)->implementation
(((struct Pair*)exp->value)->cdr, env, out);
return;
}
default:
fprintf(stderr, "non operative\n");
print_value(operator);
return;
}
}
case SYMBOL:
{
struct Value *t;
if (!(t = resolve(exp, env)))
{
fprintf(stderr, "could not resolve\n");
return;
}
*out = t;
return;
}
default:
*out = exp;
return;
}
}
void builtin_eval(struct Value *argument, struct Value *env,
struct Value **out)
{
struct Value *exp, *e;
exp = ((struct Pair*)argument->value)->car;
e = ((struct Pair*)argument->value)->cdr;
if (e->type == PAIR)
{
e = ((struct Pair*)e->value)->car;
evaluate(e, env, out);
}
else
{
e = env;
evaluate(exp, e, out);
}
}