-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
181 lines (174 loc) · 3.56 KB
/
core.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
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "delim.h"
typedef enum {
NOKEY = -1, NIL, T,
ATOM, EQ, CAR, CDR, CONS,
QUOTE, COND, LABEL, LAMBDA,
LIST, MACRO, BIND, NKEYS
} Keywrd;
typedef struct {
const char *sym;
Keywrd key;
} Atom;
typedef struct cell {
const Atom *atm;
struct cell *car;
struct cell *cdr;
} Cell;
const char *keysyms[] = {
/*none,*/ "nil", "t",
"atom", "eq", "car", "cdr", "cons",
"quote", "cond", "label", "lambda",
"list", "macro", "bind"
};
extern Cell *nil, *tru;
extern Cell *bindenv(Cell *sym, Cell *val);
Atom *new_atom(const char *s, Keywrd k)
{
Atom *ap;
ap = (Atom *) malloc((size_t) sizeof(Atom));
if (!ap) {
fprintf(stderr, "error: memory allocation failed\n");
} else {
ap->sym = s;
ap->key = k;
}
return ap;
}
Cell *new_cell(const Atom *ap)
{
Cell *cp = (Cell *) malloc((size_t) sizeof(Cell));
if (!cp) {
fprintf(stderr, "error: memory allocation failed\n");
} else {
cp->atm = ap;
cp->car = cp->cdr = NULL;
}
return cp;
}
bool is_atom(Cell *x)
{
if (!x) {
fputs("fatal: is_atom() received NULL pointer arg\n", stderr);
exit(1);
}
return x->atm != NULL;
}
bool is_eq(Cell *x, Cell *y)
{
if (!is_atom(x) || !is_atom(y)) {
puts("warning: is_eq() on non-atoms is undefined");
return false;
}
return x->atm == y->atm;
}
Cell *car(Cell *x)
{
if (is_atom(x)) {
puts("warning: car() on atom is undefined");
return NULL;
}
return x->car;
}
Cell *cdr(Cell *x)
{
if (is_atom(x)) {
puts("warning: cdr() on atom is undefined");
return NULL;
}
return x->cdr;
}
Cell *cons(Cell *x, Cell *y)
{
Cell *e = new_cell(NULL);
if (e) {
e->car = x;
e->cdr = y;
}
return e;
}
bool is_null(Cell *x)
{
return is_atom(x) && is_eq(x, nil);
}
#define is_true(x) !is_null(x)
#define prop(b) (b ? tru : nil)
#define caar(x) car(car(x))
#define cadr(x) car(cdr(x))
#define cadar(x) cadr(car(x))
#define caddr(x) cadr(cdr(x))
#define caddar(x) caddr(car(x))
#define list(x, y) cons(x, cons(y, nil))
Cell *append(Cell *x, Cell *y)
{
return is_null(x) ? y : cons(car(x), append(cdr(x), y));
}
Cell *pair(Cell *x, Cell *y)
{
if (is_null(x) && is_null(y))
return nil;
else if (!is_atom(x) && !is_atom(y))
return cons(list(car(x), car(y)), pair(cdr(x), cdr(y)));
return NULL;
}
Cell *assoc(Cell *x, Cell *y)
{
return is_eq(caar(y), x) ? cadar(y) : assoc(x, cdr(y));
}
Cell *eval(Cell *, Cell *);
Cell *evcon(Cell *c, Cell *a)
{
return is_true(eval(caar(c), a)) ? eval(cadar(c), a) : evcon(cdr(c), a);
}
Cell *evlis(Cell *m, Cell *a)
{
return is_null(m) ? nil : cons(eval(car(m), a), evlis(cdr(m), a));
}
Cell *eval(Cell *e, Cell *a)
{
Cell *cp;
if (is_atom(e))
return assoc(e, a);
if (is_atom(cp = car(e))) {
switch (cp->atm->key) {
case QUOTE:
return cadr(e);
case ATOM:
return prop(is_atom(eval(cadr(e), a)));
case EQ:
return prop(is_eq(eval(cadr(e), a), eval(caddr(e), a)));
case COND:
return evcon(cdr(e), a);
case CAR:
return car(eval(cadr(e), a));
case CDR:
return cdr(eval(cadr(e), a));
case CONS:
return cons(eval(cadr(e), a), eval(caddr(e), a));
case LIST:
return evlis(cdr(e), a);
case BIND:
return bindenv(cadr(e), eval(caddr(e), a));
default:
return eval(cons(assoc(car(e), a), cdr(e)), a);
}
}
if (is_atom(cp = caar(e))) {
switch (cp->atm->key) {
case LABEL:
return eval(cons(caddar(e), cdr(e)),
cons(list(cadar(e), car(e)), a));
case LAMBDA:
return eval(caddar(e),
append(pair(cadar(e), evlis(cdr(e), a)), a));
case MACRO:
return eval(eval(caddar(e),
append(pair(cadar(e), cdr(e)), a)), a);
default:
break;
}
}
return NULL;
}