aboutsummaryrefslogtreecommitdiff
path: root/src/interp.c
blob: b350bdf951771101b3275e29b6b5aca661318810 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "interp.h"

#include <stdarg.h>

#include <algds/hash_table.h>

#include "sexp.h"

#define BUFSIZE 1024

#define REF(x) (Interp_ref(interp, (x)))

void PrimitiveEntry_show(PrimitiveEntry self, FILE *fp) { }
VECTOR_IMPL(PrimitiveEntry);

void Interp_init(Interp *self) {
    self->errmsg_buf = malloc(BUFSIZE);
    SExpVector_init(&self->objs);
    IntVector_init(&self->empty_space);
    PrimitiveEntryVector_init(&self->primitives);
    String2IntHashTable_init(&self->symbols);
    self->gc_paused = false;
    SExp sexp;
    sexp.type = kNilSExp;
    SExpVector_push_back(&self->objs, sexp);
    self->nil = (SExpRef){0};

    sexp.type = kEnvSExp;
    sexp.env.parent= self->nil;
    sexp.env.bindings = self->nil;
    SExpVector_push_back(&self->objs, sexp);
    self->top_level = (SExpRef){1};
    sexp.type = kEmptySExp;
    for (int i = 1; i < 1024; i++) {
        SExpVector_push_back(&self->objs, sexp);
        IntVector_push_back(&self->empty_space, i);
    }

    self->evaluating = self->nil;
    self->stack = lisp_cons(self, self->top_level, self->nil);

    Interp_add_primitive(self, "car", primitive_car);
    Interp_add_primitive(self, "cdr", primitive_cdr);
    Interp_add_primitive(self, "cons", primitive_cons);
    Interp_add_primitive(self, "add", primitive_add);
    Interp_add_primitive(self, "sub", primitive_sub);
}

void Interp_free(Interp *self) {
    for (size_t i = 0; i < SExpVector_len(&self->objs); i++) {
        SExp *obj = SExpVector_ref(&self->objs, i);
        if (obj->type == kSymbolSExp || obj->type == kStringSExp) {
            free((void*)obj->str);
        }
    }
    String2IntHashTable_free(&self->symbols);
    SExpVector_free(&self->objs);
    IntVector_free(&self->empty_space);
    PrimitiveEntryVector_free(&self->primitives);
    free(self->errmsg_buf);
}

SExp* Interp_ref(Interp *self, SExpRef ref) {
    if (ref.idx > SExpVector_len(&self->objs)) return NULL;
    SExp *res = SExpVector_ref(&self->objs, ref.idx);
    return res;
}

void Interp_add_primitive(Interp *self, const char *name, LispPrimitive fn) {
    PrimitiveEntryVector_push_back(&self->primitives, (PrimitiveEntry){
        .name = name,
        .fn = fn
    });
}

void Interp_gc(Interp *interp) {
    // TODO
}

SExpRef lisp_cons(Interp *interp, SExpRef car, SExpRef cdr) {
    SExpRef ret = new_sexp(interp);
    SExp *psexp = Interp_ref(interp, ret);
    psexp->type = kPairSExp;
    psexp->pair.car = car;
    psexp->pair.cdr = cdr;
    return ret;
}

SExpRef lisp_dup(Interp *interp, SExpRef val) {
    SExpRef ret = new_sexp(interp);
    *REF(ret) = *REF(val);
    return ret;
}

SExpRef lisp_cadr(Interp *interp, SExpRef val) {
    return lisp_car(interp, lisp_cdr(interp, val));
}
SExpRef lisp_cddr(Interp *interp, SExpRef val) {
    return lisp_cdr(interp, lisp_cdr(interp, val));
}
SExpRef lisp_caddr(Interp *interp, SExpRef val) {
    return lisp_car(interp, lisp_cddr(interp, val));
}
SExpRef lisp_cdddr(Interp *interp, SExpRef val) {
    return lisp_cdr(interp, lisp_cddr(interp, val));
}
SExpRef lisp_cadddr(Interp *interp, SExpRef val) {
    return lisp_car(interp, lisp_cdddr(interp, val));
}
SExpRef lisp_cddddr(Interp *interp, SExpRef val) {
    return lisp_cdr(interp, lisp_cdddr(interp, val));
}

SExpRef lisp_car(Interp *interp, SExpRef val) {
    if (REF(val)->type != kPairSExp) {
        return new_error(interp, "type error: car.\n");
    }
    return REF(val)->pair.car;
}

SExpRef lisp_cdr(Interp *interp, SExpRef val) {
    if (REF(val)->type != kPairSExp) {
        return new_error(interp, "type error: cdr.\n");
    }
    return REF(val)->pair.cdr;
}

bool lisp_check_list(Interp *interp, SExpRef val) {

}

SExpRef lisp_lookup(Interp *interp, const char *name) {
    // TODO
}

SExpRef lisp_check_argnum(Interp *interp, const char *name, int num, SExpRef args) {
    // TODO
    return interp->nil;
}

SExpRef primitive_car(Interp *interp, SExpRef args) {
    SExpRef check = lisp_check_argnum(interp, "car", 1, args);
    if (REF(check)->type == kErrSExp) return args;
    args = lisp_eval_args(interp, args);
    if (REF(args)->type == kErrSExp) return args;
    return lisp_car(interp, lisp_car(interp, args));
}

SExpRef primitive_cdr(Interp *interp, SExpRef args) {
    SExpRef check = lisp_check_argnum(interp, "cdr", 1, args);
    if (REF(check)->type == kErrSExp) return args;
    args = lisp_eval_args(interp, args);
    if (REF(args)->type == kErrSExp) return args;
    return lisp_cdr(interp, lisp_car(interp, args));
}

SExpRef lisp_eval(Interp *interp, SExpRef val) {
    SExpType type;
    type = REF(val)->type;
    if (type == kEnvSExp || type == kEnvSExp || type == kBindingSExp) {
        return new_error(interp, "type error: cannot eval.\n");
    }
    if (type == kIntegerSExp
            || type == kStringSExp
            || type == kBooleanSExp
            || type == kCharSExp
            || type == kErrSExp
            || type == kFuncSExp
            || type == kRealSExp) {
        return val;
    }
    if (type == kSymbolSExp) {
        return lisp_lookup(interp, REF(val)->str);
    }
    if (type == kPairSExp) {
        if (!lisp_check_list(interp, val)) {
            return new_error(interp, "eval: list not proper.\n");
        }
        SExpRef hd = lisp_car(interp, (lisp_car(interp, val)));
        if (REF(hd)->type != kSymbolSExp) {
            return new_error(interp, "eval: first elem must be a symbol.\n");
        }
        const char *symbol = REF(hd)->str;
        for (int i = 0; i < PrimitiveEntryVector_len(&interp->primitives); i++) {
            if (strcmp(symbol, PrimitiveEntryVector_ref(&interp->primitives, i)->name) == 0) {
                LispPrimitive primitive_fn =
                    PrimitiveEntryVector_ref(&interp->primitives, i)->fn;
                return (*primitive_fn)(interp, lisp_cdr(interp, val));
            }
            // TODO: macro / func
        }
    }
    return interp->nil;
}

SExpRef new_sexp(Interp *interp) {
    if (IntVector_len(&interp->empty_space) == 0) {
        if (interp->gc_paused) {
            SExp sexp;
            sexp.type = kEmptySExp;
            SExpVector_push_back(&interp->objs, sexp);
            return (SExpRef){ SExpVector_len(&interp->objs) - 1 };
        } else Interp_gc(interp);
    }
    int idx = *IntVector_ref(&interp->empty_space, IntVector_len(&interp->empty_space) - 1);
    IntVector_pop(&interp->empty_space);
    return (SExpRef){idx};
}

SExpRef new_boolean(Interp *interp, bool val) {
    SExpRef ret = new_sexp(interp);
    REF(ret)->type = kBooleanSExp;
    REF(ret)->boolean = val;
    return ret;
}

SExpRef new_error(Interp *interp, const char *format, ...) {
    va_list args;
    va_start(args, format);
    vsnprintf(interp->errmsg_buf, BUFSIZE, format, args);
    va_end(args);
    SExpRef ret = new_sexp(interp);
    REF(ret)->type = kErrSExp;
    REF(ret)->boolean = interp->errmsg_buf;
    return ret;
}

SExpRef new_char(Interp *interp, char val) {
    SExpRef ret = new_sexp(interp);
    SExp *psexp = Interp_ref(interp, ret);
    psexp->type = kCharSExp;
    psexp->character = val;
    return ret;
}

SExpRef new_integer(Interp *interp, int64_t val) {
    SExpRef ret = new_sexp(interp);
    SExp *psexp = Interp_ref(interp, ret);
    psexp->type = kIntegerSExp;
    psexp->integer = val;
    return ret;
}

SExpRef new_real(Interp *interp, double val) {
    SExpRef ret = new_sexp(interp);
    SExp *psexp = Interp_ref(interp, ret);
    psexp->type = kRealSExp;
    psexp->real = val;
    return ret;
}

SExpRef new_string(Interp *interp, const char *val) {
    char *dup = strdup(val);
    SExpRef ret = new_sexp(interp);
    SExp *psexp = Interp_ref(interp, ret);
    psexp->type = kStringSExp;
    psexp->str = dup;
    return ret;
}

SExpRef new_symbol(Interp *interp, const char *val) {
    String2IntHashTableIter iter = String2IntHashTable_find(&interp->symbols, val);
    if (iter == NULL) {
        char *dup = strdup(val);
        SExpRef ret = new_sexp(interp);
        SExp *psexp = Interp_ref(interp, ret);
        psexp->type = kSymbolSExp;
        psexp->str = dup;
        String2IntHashTable_insert(&interp->symbols, dup, ret.idx);
        return ret;
    } else {
        return (SExpRef){ iter->val };
    }
}

SExpRef new_list1(Interp *interp, SExpRef e1) {
    return lisp_cons(interp, e1, interp->nil);
}

SExpRef new_list2(Interp *interp, SExpRef e1, SExpRef e2) {
    return lisp_cons(interp, e1, new_list1(interp, e2));
}

SExpRef new_list3(Interp *interp, SExpRef e1, SExpRef e2, SExpRef e3) {
    return lisp_cons(interp, e1, new_list2(interp, e2, e3));
}

SExpRef new_list4(Interp *interp, SExpRef e1, SExpRef e2, SExpRef e3, SExpRef e4) {
    return lisp_cons(interp, e1, new_list3(interp, e2, e3, e4));
}

SExpRef new_list5(Interp *interp, SExpRef e1, SExpRef e2, SExpRef e3, SExpRef e4, SExpRef e5) {
    return lisp_cons(interp, e1, new_list4(interp, e2, e3, e4, e5));
}