aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 8a07041db1d8ab0989e22ce1b2b3d337c0911cf5 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lexer.h"
#include "value.h"
#include "env.h"

#ifdef ENABLE_MEMDEBUG
#define MEMDEBUG_OUT_OF_BOUNDS
#define MEMDEBUG_IMPLEMENTATION
#define MEMDEBUG_MAIN_VOID
#define MEMDEBUG_OUTPUT_DIR "files"
#endif

#include "common.h"
#include "builtin.h"

#define LEN(arr) (sizeof(arr)/sizeof(*(arr)))

// TODO:
// - Think about memory leakage and on non fatal errors
//   like failed list creation and faild s-exp parsing
// - Check for functions not wrapped in ERR_* macro
// - Add more error messages

#ifndef DEBUG
#define TOKEN_NEXT()                            \
    ERR_NZ(lexer_token_next(lexer, &token), r,  \
           die("Can't get next token"))
#else
#define TOKEN_NEXT() do {                           \
        ERR_NZ(lexer_token_next(lexer, &token), r,  \
               die("Can't get next token"));        \
        print_token(&token);                        \
    } while(0)
#endif

#define TOKEN_ASSERT(ttype, fail)               \
    if(token.type != ttype) {                   \
        err("Expected token '%s', not '%s'",    \
            token_type_string[ttype],           \
            token_type_string[token.type]);     \
        fail;                                   \
    }

#define TOKEN_SKIP(ttype, fail) do {            \
        TOKEN_ASSERT(ttype, fail)               \
        else {                                  \
            TOKEN_NEXT();                       \
        }                                       \
    } while(0)

#define TOKEN_MATCH(ttype, fail) do {           \
        TOKEN_NEXT();                           \
        TOKEN_ASSERT(ttype, fail);              \
    } while(0)

#define HAS_ENOUGH_ARGS(proc, type, argc, fail)                     \
    if(argc != proc->value.type.argc) {                             \
        err("Wrong number of arguemnts, expected %zu, but got %zu", \
            proc->value.type.argc, argc);                           \
        fail;                                                       \
    }

#define NOT_IMPLEMENTED() die("Not Implemented. ABORTING")

static void print_token(struct token *token)
{
    char buf[256] = {0};
    ERR_Z(token_value_string(token, LEN(buf), buf), return);
    info("%-12s %s", token_type_string[token->type], buf);
}

static void print_value(value_t value)
{
    char buf[256] = {0};
    value_string(value, LEN(buf), buf);
    info("%-12s %s", value ?
         value_type_string[value->type] : "VALUE", buf);
}

static lexer_t lexer = LEXER_EMPTY;
static env_t user_env = ENV_EMPTY;

env_t env = ENV_EMPTY;
struct token token;

value_t apply(value_t proc, size_t argc, value_t *argv);

value_t evaluate_expr(void);
value_t evaluate_sexp(void);
value_t evaluate_id(void);
value_t evaluate_lambda(void);
value_t evaluate_define(void);

value_t quote_expr(void);
value_t quote_sexp(void);

size_t toklist_expr(struct token **toklist);

static void destroy_env(char *key, value_t value)
{
    (void)key;
    value_destroy(value);
}

static void destroy_user_env(char *key, value_t value)
{
    free(key);
    value_destroy(value);
}

int main(void)
{
    user_env = env_create(ENV_EMPTY, destroy_user_env);
    env = env_create(user_env, destroy_env);

    for(size_t i = 0; i < BUILTIN_PROCEDURES; i++) {
        value_t proc_value = value_create(
            VALUE_PROC_BUILTIN,
            (void *)&builtin_proc_descriptions[i]);
        hashtable_insert(env->table,
                         (void *)builtin_proc_name_list[i],
                         (void *)proc_value, NULL, NULL);
    }
    
    char *filename = "files/test.l";
        
    FILE *fp = fopen(filename, "r");
    if(!fp) {
        die("fopen: %s", strerror(errno));
    }
    
    lexer = lexer_create(fp);

    while(lexer_token_next(lexer, &token) == 0) {
        value_t val = evaluate_expr();

        #ifdef DEBUG
        print_value(val);
        #else
        char buf[256] = {0};
        value_string(val, LEN(buf), buf);
        printf("%s:%zu:   %s\n", filename, lexer->line, buf);
        #endif    
        
        value_destroy(val);
    }

    lexer_destroy(lexer);
    fclose(fp);
  
    env_destroy(env);
    // env_destroy(user_env);
    return 0;
}

value_t apply(value_t proc, size_t argc, value_t *argv)
{
    // TODO: make a global nil and copy it
    if(proc == VALUE_EMPTY) return value_create(VALUE_NIL, NULL);
    
    switch(proc->type) {
    case VALUE_PROC:
        HAS_ENOUGH_ARGS(proc, proc, argc, return VALUE_EMPTY);
        // create new env
        // evaluate body
        // destroy new env
        // switch back to the previous env

        NOT_IMPLEMENTED();
        return VALUE_EMPTY;
    case VALUE_PROC_BUILTIN:
        HAS_ENOUGH_ARGS(proc, proc_builtin, argc, return VALUE_EMPTY);
        return proc->value.proc_builtin.proc(argv);
    default:
        err("Value is not a procedure");
        return VALUE_EMPTY;
    }
}

#define IS_NOT_FIRST(arg, fail)                                \
    if(arg != 0) {                                             \
        err("special forms can only be the first argument");   \
        fail;                                                  \
    }
#define SPECIAL_FORM(ret, argc, fn, fail) do {  \
        IS_NOT_FIRST(argc, fail);               \
        ERR_Z(ret = fn, fail);                  \
    } while(0)

value_t evaluate_expr(void)
{
    value_t ret = VALUE_EMPTY;
        
    switch(token.type) {
    case TOKEN_LP:
        ERR_Z(ret = evaluate_sexp(), goto exit);
        break;
    case TOKEN_ID:
        ERR_Z(ret = evaluate_id(), goto exit);
        break;
    case TOKEN_STR:
    case TOKEN_INT:
        ERR_Z(ret = value_from_token(&token), goto exit);
        break;
    case TOKEN_QUOTE:
        TOKEN_NEXT();
        ERR_Z(ret = quote_expr(), goto exit);
        break;
    default:
        err("Did not exptect token '%s'", token_type_string[token.type]);
        break;
    }

exit:
    return ret;
}

value_t evaluate_sexp(void)
{
    value_t ret = VALUE_EMPTY;
    value_t body[256] = {VALUE_EMPTY};
    size_t argc = 0;

    TOKEN_SKIP(TOKEN_LP, goto exit);
    
    for(argc = 0; token.type != TOKEN_RP; argc++)
    {
        if(argc >= LEN(body)) {
            err("Too many arguments");
            goto exit;
        }
        
        switch(token.type) {
        case TOKEN_LAMBDA:
            SPECIAL_FORM(ret, argc, evaluate_lambda(), goto exit);
            goto exit;
        case TOKEN_DEFINE:
            SPECIAL_FORM(ret, argc, evaluate_define(), goto exit);
            goto exit;
        case TOKEN_QUOTE_FORM:
            TOKEN_NEXT();
            SPECIAL_FORM(ret, argc, quote_expr(), goto exit);
            TOKEN_MATCH(TOKEN_RP,
                        value_destroy(ret);
                        ret = VALUE_EMPTY;
                        goto exit);
            goto exit;
        default:
            ERR_Z(body[argc] = evaluate_expr(), goto exit);
            break;
        }

        TOKEN_NEXT();
    }
    
    ret = apply(body[0], argc-1, &body[1]);

    #ifdef DEBUG
    info("------------------");
    info("Applying procedure");
    print_value(body[0]);
    info("With Arguemnts");
    if(argc > 0)
        for(size_t i = 0; i < argc-1; i++) print_value(body[i+1]);
    info("Returns");
    print_value(ret);
    info("-----------------");
    #endif

exit:
    for(size_t i = 0; i < argc; i++)
        value_destroy(body[i]);
    return ret;
}

static value_t evaluate_id_env(env_t env)
{
    if(env == ENV_EMPTY) return VALUE_EMPTY;
    
    value_t ret = VALUE_EMPTY;
    ERR_NZ(hashtable_query(env->table, (void *)token.value.id, (void **)&ret), _r, return evaluate_id_env(env->parent));
    
    return value_copy(ret);
}

value_t evaluate_id(void)
{
    return evaluate_id_env(env);
}

value_t evaluate_lambda(void)
{
    value_t ret = VALUE_EMPTY;
    
    value_t args[32] = {VALUE_EMPTY};
    size_t argc = 0;
    value_t *arg_keys = NULL;
    
    struct token *body = NULL;
    size_t body_len = 0;
    
    TOKEN_SKIP(TOKEN_LAMBDA, goto fail);
    TOKEN_SKIP(TOKEN_LP, goto fail);

    while(token.type != TOKEN_RP)
    {
        if(argc >= LEN(args)) {
            err("Too many arguments");
            goto fail;
        }
        
        if(token.type != TOKEN_ID) {
            err("Token '%s' not expected in lambda args", token_type_string[token.type]);
            goto fail;
        }

        ERR_Z(args[argc++] = value_from_token(&token), goto fail);

        TOKEN_NEXT();
    }
    
    ERR_Z(body_len = toklist_expr(&body), goto fail);

    TOKEN_MATCH(TOKEN_RP, goto fail);
    
    arg_keys = calloc(argc, sizeof(*arg_keys));
    memcpy(arg_keys, args, argc * sizeof(*arg_keys));

    struct proc proc = {env_copy(env), arg_keys, argc, body, body_len};
    ERR_Z(ret = value_create(VALUE_PROC, &proc), goto fail);
    
    return ret;
    
fail:
    err("Procedure creation failed");
    for(size_t i = 0; i < argc; i++) value_destroy(args[i]);
    if(body) {
        for(size_t i = 0; i < body_len; i++) token_dealloc(&body[i]);
        free(body);
    }
    if(arg_keys) free(arg_keys);
    return VALUE_EMPTY;
}

#define STR_ALLOC_COPY(dest, str) do {    \
        size_t len = strlen(str) + 1;     \
        dest = malloc(len);               \
        memcpy((dest), (str), len);       \
    } while(0)

value_t evaluate_define(void)
{
    // TODO: don't alloc when the key is the same
    char *key = NULL;    

    // only in the outside environement
    if(env->parent != user_env) {
        err("define can only be called in the outermost environement");
        goto fail;
    }
    
    TOKEN_SKIP(TOKEN_DEFINE, goto fail);

    switch(token.type)
    {
    case TOKEN_ID:
        STR_ALLOC_COPY(key, token.value.id);
        break;
    default:
        err("Did not exptect token '%s'", token_type_string[token.type]);
        goto fail;
    }

    TOKEN_NEXT();
    
    value_t val = VALUE_EMPTY;
    ERR_Z(val = evaluate_expr(), goto fail);
    
    TOKEN_MATCH(TOKEN_RP,
                value_destroy(val);
                goto fail);

    value_t prevval = VALUE_EMPTY;
    char *prevkey = NULL;
    
    ERR_NZ(
        hashtable_insert(user_env->table, (void *)key, (void *)val,
                         (void**)&prevkey, (void **)&prevval),
        r, {
            err("Couldn't insert symbol into the hashtable due to %s", strerror(r));
            value_destroy(val); // the copy
            goto fail;
        });

    if(prevkey) free(prevkey);
    value_destroy(prevval);
    return VALUE_EMPTY;
    
fail:
    if(key) free(key);
    return VALUE_EMPTY;
}

value_t quote_expr(void)
{
    value_t ret = VALUE_EMPTY;

    switch(token.type) {
    case TOKEN_ID:
    case TOKEN_STR:
    case TOKEN_INT:
        ERR_Z(ret = value_from_token(&token), goto exit);
        break;
    case TOKEN_QUOTE:
    case TOKEN_LAMBDA:
    case TOKEN_DEFINE:
    case TOKEN_QUOTE_FORM: ;
        char name[64] = {0};
        ERR_Z(token_value_string(&token, LEN(name), name), goto exit);
        struct token temp = {0}; temp.type = TOKEN_ID; temp.value.id = name;
        ERR_Z(ret = value_from_token(&temp), goto exit);
        break;
    case TOKEN_LP:
        ERR_Z(ret = quote_sexp(), goto exit);
        break;
    case TOKEN_UNQUOTE:
        TOKEN_SKIP(TOKEN_UNQUOTE, goto exit);
        ERR_Z(ret = evaluate_expr(), goto exit);
        break;
    default:
        err("Did not exptect token '%s'", token_type_string[token.type]);
        break;
    }
exit:
    return ret;
}

value_t quote_sexp(void)
{
    value_t ret   = VALUE_EMPTY;
    value_t left  = VALUE_EMPTY;
    value_t right = VALUE_EMPTY;
    value_t nil   = VALUE_EMPTY;

    // TODO: make global nil and copy it
    ERR_Z(nil = value_create(VALUE_NIL, NULL), goto exit);
    
    TOKEN_SKIP(TOKEN_LP, goto exit);

    // Parse NIL
    if(token.type == TOKEN_RP) {
        ret = value_copy(nil);
        goto exit;
    }
        
    ERR_Z(left = quote_expr(), goto exit);

    TOKEN_NEXT();
    if(token.type == TOKEN_DOT)
    {
        // Parse cons
        TOKEN_NEXT();
        ERR_Z(right = quote_expr(), return VALUE_EMPTY);
        TOKEN_MATCH(TOKEN_RP, return VALUE_EMPTY);
        
        struct cons cons = {value_copy(left), value_copy(right)};
        ret = value_create(VALUE_CONS, &cons);
        goto exit;
    }
    
    // Parse list
    right = value_copy(nil);
    value_t *rightmost = &right; // the final nil
    while(token.type != TOKEN_RP)
    {
        value_t new = VALUE_EMPTY;
        ERR_Z(new = quote_expr(),
              goto exit);
        
        value_t new_cons = VALUE_EMPTY;
        struct cons cons = {new, *rightmost};
        ERR_Z(new_cons = value_create(VALUE_CONS, &cons),
              value_destroy(new);
              goto exit);

        *rightmost = new_cons;
        rightmost = &new_cons->value.cons.right;
        
        TOKEN_NEXT();
    }

    struct cons cons = {value_copy(left), value_copy(right)};
    ret = value_create(VALUE_CONS, &cons);
exit:
    value_destroy(left);
    value_destroy(right);
    value_destroy(nil);
    return ret;
}

size_t toklist_expr(struct token **toklist)
{
    struct token tokens[256];
    size_t tokens_len = 0;
    
    size_t depth = 0;
    do {
        TOKEN_NEXT();
        
        if(tokens_len >= LEN(tokens)) {
            err("Too many tokens in expr");
            goto fail;
        }
            
        if(token.type == TOKEN_LP) depth++;
        else if(token.type == TOKEN_RP) depth--;

        token_clone(&tokens[tokens_len++], &token);
        
    } while(depth > 0);

    *toklist = calloc(tokens_len, sizeof(*tokens));
    memcpy(*toklist, tokens, tokens_len * sizeof(*tokens));

    return tokens_len;
    
fail:
    for(size_t i = 0; i < tokens_len; i++) token_dealloc(&tokens[i]);
    return 0;
}