forked from vtols/Scheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
136 lines (115 loc) · 3.49 KB
/
eval.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
/* eval.c - Evaluation */
#include <stdio.h>
#include <string.h>
/* parse_single() */
#include <parser.h>
/* object definition */
#include <object.h>
/* env_hashtable definition */
#include <hashtable.h>
#include <eval.h>
/* Parse and evaluate string */
object *eval_str(const char *s, env_hashtable *env)
{
object *obj = parse_single(s);
return eval(obj, env);
}
/* Evaluate object
* NULL return value means Nothing
*/
object *eval(object *obj, env_hashtable *env)
{
object *cur, *eobj,
*last_pair, *t,
*ecar, *ecdr;
if (!obj)
return NULL;
/* Detect syntatic construction */
if (TYPE(obj) == OBJ_PAIR &&
TYPE(CAR(obj)) == OBJ_SYMBOL) {
t = CAR(obj);
if (strcmp("lambda", STR(t)) == 0) {
t = CDDR(obj);
t = cons(symbol("begin"), t);
eobj = compound_procedure(CADR(obj), t, env);
return eobj;
} else if (strcmp("define", STR(t)) == 0) {
eobj = eval(CADDR(obj), env);
env_hashtable_insert(env, STR(CADR(obj)), eobj);
return NULL; /* Not error, just nothing */
} else if (strcmp("begin", STR(t)) == 0) {
obj = CDR(obj);
eobj = NULL; /* Not error, just nothing */
while (obj != null_object) {
eobj = eval(CAR(obj), env);
obj = CDR(obj);
}
return eobj;
} else if (strcmp("apply", STR(t)) == 0) {
eobj = eval(CADR(obj), env);
t = eval(CADDR(obj), env);
return apply(eobj, t);
} else if (strcmp("quote", STR(t)) == 0) {
return CADR(obj);
}
}
/* Object evaluation */
switch (TYPE(obj)) {
case OBJ_NUMBER:
case OBJ_BOOLEAN:
return obj;
case OBJ_SYMBOL:
return env_hashtable_find(env, STR(obj));
case OBJ_PAIR:
cur = obj;
eobj = null_object;
last_pair = NULL;
while (cur != null_object &&
TYPE(cur) == OBJ_PAIR) {
t = cons(eval(CAR(cur), env), null_object);
if (!last_pair)
eobj = t;
else
CDR(last_pair) = t;
last_pair = t;
cur = CDR(cur);
}
ecar = CAR(eobj);
ecdr = CDR(eobj);
return apply(ecar, ecdr);
default:
return NULL;
}
}
/* Apply procedure */
object *apply(object *proc, object *args)
{
primitive_proc p;
env_hashtable *e;
object *params, *param, *arg;
char *s_param;
if (TYPE(proc) == OBJ_PRIMITIVE_PROCEDURE) {
p = PROC(proc);
return p(args);
} else if (TYPE(proc) == OBJ_COMPOUND_PROCEDURE) {
e = env_hashtable_child(CPROC_ENV(proc));
params = CPROC_PARAMS(proc);
while (params != null_object) {
if (TYPE(params) != OBJ_PAIR) {
param = params;
arg = args;
} else {
param = CAR(params);
arg = CAR(args);
}
s_param = STR(param);
env_hashtable_insert(e, s_param, arg);
if (TYPE(params) != OBJ_PAIR)
break;
params = CDR(params);
args = CDR(args);
}
return eval(CPROC_BODY(proc), e);
}
return NULL;
}