-
Notifications
You must be signed in to change notification settings - Fork 2
/
lisp.c
178 lines (151 loc) · 2.61 KB
/
lisp.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
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
#define NULL ((void*)0)
extern int getchar();
typedef void * val;
struct cons {
val car, cdr;
};
struct cons conses[512];
panic(s)
char *s;
{
putstr("FATAL: ");
putstr(s);
halt();
}
// null-terminated strings allocated end-to-end
char symbs[512];
int symbend = 0;
val symb_print;
int
strlen(c)
char *c;
{
int len = 0;
while(*(c++) != '\0') len++;
return len;
}
val
intern(str)
char * str;
{
int i = 0;
int len = 0;
char *str1 = str;
while (i < symbend) {
if (*str1 == symbs[i]) {
if (symbs[i] == '\0') break;
str1++;
i++;
len++;
} else {
str1 = str;
len = 0;
while (str[i] != NULL) {
i++;
}
}
}
if (i == symbend) {
while (*str != '\0') {
if (symbend == sizeof(symbs))
panic("Ran out of symbol space");
symbs[symbend++] = *(str++);
}
} else {
return &symbs[i-len];
}
}
// Symbol mappings
struct tblent {
val key, val;
};
struct tblent binds[256];
int nbinds = 0;
bind(name, vl)
val name;
val vl;
{
if (nbinds == sizeof(binds)/sizeof(binds[0])) {
panic("Too many bindings");
}
binds[nbinds].key = name;
binds[nbinds].val = vl;
nbinds++;
}
typedef void (*builtin)(nargs, args);
builtin builtins[32];
int nbuiltins = 0;
struct cons *
ascons(v)
val v;
{
if (v > (char*) &conses &&
v < (char*) &conses + sizeof(conses)) {
return (struct cons *)v;
}
panic("ascons: bad cell");
}
val eval();
builtin_apply(fn, args)
builtin fn;
struct cons *args;
{
val *bargs = calloc(8*sizeof(val));
int nargs;
while (args != NULL) {
bargs[nargs++] = eval(args->car);
args = ascons(args->cdr);
}
fn(bargs, nargs);
}
val
eval(term)
val term;
{
struct cons *term1;
// integers are all less than 512, obviously
if (term < 512) {
return term;
} else if (term > (char*) symbs &&
term < (char*) symbs + sizeof(symbs)) {
int i;
for (i = 0; i < nbinds; ++i) {
if (term == binds[i].key) {
return binds[i].val;
}
}
panic("Unbound variable");
} else if (term > (char*) conses &&
term < (char*) conses + sizeof(conses)) {
val fn;
term1 = (struct cons *) term;
fn = eval(term1->car);
if (fn > &builtins &&
fn < &builtins + sizeof(builtins)) {
builtin_apply(fn, ascons(term1->cdr));
}
}
}
lisp_print(nargs, args)
val *args;
{
if (nargs != 1) {
panic("Bad args to print");
}
putstr(args[0]);
}
val
make_builtin(name, blt)
char *name;
builtin blt;
{
if (nbuiltins == sizeof(builtins)/sizeof(builtins[0])) {
panic("Too many builtins");
}
builtins[nbuiltins] = blt;
bind(intern(name), &builtins[nbuiltins++]);
}
lisp_init()
{
make_builtin("print", lisp_print);
}