aboutsummaryrefslogtreecommitdiff
path: root/eval.c
blob: d71ebc663a93a805f898c27cd35d892a4fb8422e (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "eval.h"
#include "list.h"
#include "atom.h"
#include "parse.h"
#include "env.h"

#include <stdio.h>
#include <string.h>

static int atom_cmp(struct list *a, struct list *b)
{
    if (ATOM_TYPE(a) != ATOM_TYPE(b))
        return 0;

    if (IS_TRUE(a) && !IS_TRUE(b))
        return 0;

    if (IS_FALSE(a) && !IS_FALSE(b))
        return 0;

    if (IS_TRUE(b) && !IS_TRUE(a))
        return 0;

    if (IS_FALSE(b) && !IS_FALSE(a))
        return 0;

    if (IS_NIL(a) && !IS_NIL(b))
        return 0;

    if (IS_NIL(b) && !IS_NIL(a))
        return 0;

    int result = 1;

    switch (ATOM_TYPE(a))
    {
        case ATOM_INT:
            if (LIST_GET_ATOM(a)->l != LIST_GET_ATOM(b)->l)
                result = 0;
            break;

        case ATOM_STR:
        case ATOM_SYMBOL:
            if (strcmp(LIST_GET_ATOM(a)->str.str, LIST_GET_ATOM(b)->str.str) != 0)
                result = 0;
            break;

        case ATOM_LIST:
        {
            struct list *ai = LIST_GET_ATOM(a)->list;
            struct list *bi = LIST_GET_ATOM(b)->list;

            while (ai && bi)
            {
                if (!atom_cmp(ai, bi))
                {
                    result = 0;
                    break;
                }

                ai = ai->next;
                bi = bi->next;
            }

            if (ai != NULL || bi != NULL)
                result = 0;

            break;
        }
    }

    return result;
}

struct list *eval(struct list *list)
{
    struct list env;
    memset(&env, 0, sizeof(env));
    return eval_env(list, &env);
}

struct list *eval_str(const char *str)
{
    struct list env;
    memset(&env, 0, sizeof(env));
    return eval_str_env(str, &env);
}

struct list *eval_env(struct list *expr, struct list *env)
{
    if (IS_SYM(expr))
    {
        struct atom *atom = env_lookup(env, LIST_GET_ATOM(expr)->str.str);

        if (atom)
        {
            return list_append(NULL, atom);
        }
        else
        {
            printf("error: undefined variable: %s\n",
                LIST_GET_ATOM(expr)->str.str);
            return list_append(NULL, &nil_atom);
        }
    }

    if (!IS_LIST(expr))
        return expr;

    struct list *l = LIST_GET_ATOM(expr)->list;

    if (IS_SYM(l))
    {
        const char *sym = LIST_GET_ATOM(l)->str.str;

        if (strcmp(sym, "quote") == 0)
        {
            return l->next;
        }
        else if (strcmp(sym, "atom") == 0)
        {
            if (IS_LIST(l->next))
            {
                return list_append(NULL, &false_atom);
            }
            else
            {
                return list_append(NULL, &true_atom);
            }
        }
        else if (strcmp(sym, "eq") == 0)
        {
            struct list *a = CDR(l);
            struct list *b = CDR(a);

            if (!a || !b)
            {
                printf("error: eq takes 2 arguments\n");
                return list_append(NULL, &nil_atom);
            }

            a = eval_env(a, env);
            b = eval_env(b, env);

            if (atom_cmp(a, b))
                return list_append(NULL, &true_atom);

            return list_append(NULL, &false_atom);
        }
        else if (strncmp(sym, "+", 1) == 0 ||
                 strncmp(sym, "-", 1) == 0 ||
                 strncmp(sym, "/", 1) == 0 ||
                 strncmp(sym, "*", 1) == 0)
        {
            struct list *oper = CAR(l);
            struct list *a = CDR(oper);
            struct list *b = CDDR(oper);

            if (!a || !b)
                return list_append(NULL, &nil_atom);

            a = eval_env(a, env);
            b = eval_env(b, env);

            if (!(ATOM_TYPE(a) == ATOM_TYPE(b) && ATOM_TYPE(a) == ATOM_INT))
                return list_append(NULL, &nil_atom);

            long numa = LIST_GET_ATOM(a)->l;
            long numb = LIST_GET_ATOM(b)->l;
            long numr;

            switch (*sym)
            {
                case '+':
                    numr = numa + numb;
                    break;
                case '-':
                    numr = numa - numb;
                    break;
                case '/':
                    numr = numa / numb;
                    break;
                case '*':
                    numr = numa * numb;
                    break;
            }

            struct list *result = list_append(NULL, atom_new_int(numr));

            return result;
        }
        else if (strncmp(sym, ">", 1) == 0)
        {
            struct list *oper = CAR(l);
            struct list *a = CDR(oper);
            struct list *b = CDDR(oper);

            if (!a || !b)
                return list_append(NULL, &nil_atom);

            a = eval_env(a, env);
            b = eval_env(b, env);

            if (!(ATOM_TYPE(a) == ATOM_TYPE(b) && ATOM_TYPE(a) == ATOM_INT))
                return list_append(NULL, &nil_atom);

            long numa = LIST_GET_ATOM(a)->l;
            long numb = LIST_GET_ATOM(b)->l;

            if (numa > numb)
                return list_append(NULL, &true_atom);
            else
                return list_append(NULL, &false_atom);
        }
        else if (strcmp(sym, "if") == 0)
        {
            struct list *predicate = CDR(l);
            struct list *true_case = CDR(predicate);
            struct list *false_case = CDR(true_case);

            if (!predicate || !true_case || !false_case)
                return list_append(NULL, &nil_atom);

            predicate = eval_env(predicate, env);

            if (IS_TRUE(predicate))
                return eval_env(true_case, env);
            else
                return eval_env(false_case, env);
        }
        else if (strcmp(sym, "mod") == 0)
        {
            struct list *a = CDR(l);
            struct list *b = CDR(a);

            if (!a || !b)
            {
                printf("error: mod takes two arguments\n");
                return list_append(NULL, &nil_atom);
            }

            a = eval_env(a, env);
            b = eval_env(b, env);

            if (!IS_INT(a) || !IS_INT(b))
            {
                printf("error: mod arguments must be integers\n");
                return list_append(NULL, &nil_atom);
            }

            long result = LIST_GET_ATOM(a)->l % LIST_GET_ATOM(b)->l;

            return list_append(NULL, atom_new_int(result));
        }
        else if (strcmp(sym, "define") == 0)
        {
            struct list *expr_name = CDR(l);
            struct list *expr_value = CDR(expr_name);

            if (!expr_name || !expr_value)
            {
                printf("error: define takes two arguments\n");
                return list_append(NULL, &nil_atom);
            }

            if (!IS_SYM(expr_name))
            {
                printf("error: define: first arg must be symbol\n");
                return list_append(NULL, &nil_atom);
            }

            expr_value = eval_env(expr_value, env);

            env_set(env, LIST_GET_ATOM(expr_name)->str.str,
                LIST_GET_ATOM(expr_value));

            return list_append(NULL, expr_value);
        }
    }
    else if (IS_LIST(l))
    {
        return eval(l);
    }

    return list_append(NULL, &nil_atom);
}

struct list *eval_str_env(const char *expr, struct list *env)
{
    struct list *result;
    int pos = 0;

    result = eval_env(parse(expr, &pos), env);

    return result;
}

#ifdef BUILD_TEST

#include "test_util.h"

TEST(nested_expression)
{
    struct list *result = eval_str("(eq #f (> (- (+ 1 3) (* 2 (mod 7 4))) 4))");
    ASSERT_TRUE(result != NULL);
    ASSERT_TRUE(IS_TRUE(result));
}

TEST(basic_if)
{
    struct list *result = eval_str("(if #t 42 1000)");
    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_INT, ATOM_TYPE(result));
    ASSERT_EQ(42, LIST_GET_ATOM(result)->l);
}

TEST(if_with_sub_expressions)
{
    struct list *result = eval_str("(if (> 1 2) (- 1000 1) (+ 40 (- 3 1)))");
    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_INT, ATOM_TYPE(result));
    ASSERT_EQ(42, LIST_GET_ATOM(result)->l);
}

/* EVALUTE WITH ENVIRONMENT TESTS */

TEST(evaluate_symbol)
{
    struct list *env = env_new();
    env_set(env, "foo", atom_new_int(42));

    struct list *result = eval_str_env("foo", env);

    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_INT, ATOM_TYPE(result));
    ASSERT_EQ(42, LIST_GET_ATOM(result)->l);
}

TEST(evaluate_missing_symbol)
{
    struct list *env = env_new();

    struct list *result = eval_str_env("foo", env);

    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_NIL, ATOM_TYPE(result));
}

TEST(define)
{
    struct list *env = env_new();

    struct list *result = eval_str_env("(define x 100)", env);

    ASSERT_TRUE(result != NULL);

    struct atom *atom = env_lookup(env, "x");

    ASSERT_TRUE(atom != NULL);
    ASSERT_EQ(ATOM_INT, atom->type);
    ASSERT_EQ(100, atom->l);
}

TEST(define_missing_value)
{
    struct list *env = env_new();

    struct list *result = eval_str_env("(define x)", env);
    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_NIL, ATOM_TYPE(result));

    struct atom *atom = env_lookup(env, "x");
    ASSERT_TRUE(atom == NULL);
}

TEST(define_nonsymbol_as_name)
{
    struct list *env = env_new();

    struct list *result = eval_str_env("(define 1 100)", env);
    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_NIL, ATOM_TYPE(result));
}

TEST(define_with_val_as_expr)
{
    struct list *env = env_new();

    struct list *result = eval_str_env("(define x (* 3 3))", env);

    ASSERT_TRUE(result != NULL);

    struct atom *atom = env_lookup(env, "x");

    ASSERT_TRUE(atom != NULL);
    ASSERT_EQ(ATOM_INT, atom->type);
    ASSERT_EQ(9, atom->l);
}


#endif /* BUILD_TEST */