-
Notifications
You must be signed in to change notification settings - Fork 23
/
arc.c
2794 lines (2575 loc) · 63.6 KB
/
arc.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "arc.h"
#include <ctype.h>
char *error_string[] = { "", "Syntax error", "Symbol not bound", "Wrong number of arguments", "Wrong type", "File error", "" };
size_t stack_capacity = 0;
size_t stack_size = 0;
atom *stack = NULL;
struct pair *pair_head = NULL;
struct str *str_head = NULL;
struct table *table_head = NULL;
size_t alloc_count = 0;
size_t alloc_count_old = 0;
char **symbol_table = NULL;
size_t symbol_size = 0;
size_t symbol_capacity = 0;
const atom nil = { T_NIL };
atom env; /* the global environment */
/* symbols for faster execution */
atom sym_t, sym_quote, sym_quasiquote, sym_unquote, sym_unquote_splicing, sym_assign, sym_fn, sym_if, sym_mac, sym_apply, sym_cons, sym_sym, sym_string, sym_num, sym__, sym_o, sym_table, sym_int, sym_char, sym_do;
atom err_expr;
atom thrown;
/* Be sure to free after use */
void vector_new(struct vector *a) {
a->capacity = sizeof(a->static_data) / sizeof(a->static_data[0]);
a->size = 0;
a->data = a->static_data;
}
void vector_add(struct vector *a, atom item) {
if (a->size + 1 > a->capacity) {
a->capacity *= 2;
if (a->data == a->static_data) {
a->data = malloc(a->capacity * sizeof(atom));
memcpy(a->data, a->static_data, a->size * sizeof(atom));
}
else {
a->data = realloc(a->data, a->capacity * sizeof(atom));
}
}
a->data[a->size] = item;
a->size++;
}
void vector_clear(struct vector *a) {
a->size = 0;
}
void vector_free(struct vector *a) {
if (a->data != a->static_data) free(a->data);
}
atom vector_to_atom(struct vector *a, int start) {
atom r = nil;
int i;
for (i = a->size - 1; i >= start; i--) {
r = cons(a->data[i], r);
}
return r;
}
/* Be sure to free after use */
void atom_to_vector(atom a, struct vector *v) {
vector_new(v);
for (; !no(a); a = cdr(a)) {
vector_add(v, car(a));
}
}
void stack_add(atom a) {
switch (a.type) {
case T_CONS:
case T_CLOSURE:
case T_MACRO:
case T_STRING:
case T_TABLE:
break;
default:
return;
}
stack_size++;
if (stack_size > stack_capacity) {
stack_capacity = stack_size * 2;
stack = realloc(stack, stack_capacity * sizeof(atom));
}
stack[stack_size - 1] = a;
}
void stack_restore(int saved_size) {
stack_size = saved_size;
/* if there is waste of memory, realloc */
if (stack_size < stack_capacity / 4) {
stack_capacity = stack_size * 2;
stack = realloc(stack, stack_capacity * sizeof(atom));
}
}
void stack_restore_add(int saved_size, atom a) {
stack_size = saved_size;
/* if there is waste of memory, realloc */
if (stack_size < stack_capacity / 4) {
stack_capacity = stack_size * 2;
stack = realloc(stack, stack_capacity * sizeof(atom));
}
stack_add(a);
}
void consider_gc() {
if (alloc_count > 2 * alloc_count_old)
gc();
}
atom cons(atom car_val, atom cdr_val)
{
struct pair *a;
atom p;
alloc_count++;
a = malloc(sizeof(struct pair));
a->mark = 0;
a->next = pair_head;
pair_head = a;
p.type = T_CONS;
p.value.pair = a;
car(p) = car_val;
cdr(p) = cdr_val;
stack_add(p);
return p;
}
void gc_mark(atom root)
{
struct pair *a;
struct str *as;
struct table *at;
start:
switch (root.type) {
case T_CONS:
case T_CLOSURE:
case T_MACRO:
a = root.value.pair;
if (a->mark) return;
a->mark = 1;
gc_mark(car(root));
/* reduce recursion */
root = cdr(root);
goto start;
break;
case T_STRING:
as = root.value.str;
if (as->mark) return;
as->mark = 1;
break;
case T_TABLE: {
at = root.value.table;
if (at->mark) return;
at->mark = 1;
size_t i;
for (i = 0; i < at->capacity; i++) {
struct table_entry *e = at->data[i];
while (e) {
gc_mark(e->k);
gc_mark(e->v);
e = e->next;
}
}
break; }
default:
return;
}
}
void gc()
{
struct pair *a, **p;
struct str *as, **ps;
struct table *at, **pt;
/* mark atoms in the stack */
size_t i;
for (i = 0; i < stack_size; i++) {
gc_mark(stack[i]);
}
alloc_count_old = 0;
/* Free unmarked "cons" allocations */
p = &pair_head;
while (*p != NULL) {
a = *p;
if (!a->mark) {
*p = a->next;
free(a);
}
else {
p = &a->next;
a->mark = 0; /* clear mark */
alloc_count_old++;
}
}
/* Free unmarked "string" allocations */
ps = &str_head;
while (*ps != NULL) {
as = *ps;
if (!as->mark) {
*ps = as->next;
free(as->value);
free(as);
}
else {
ps = &as->next;
as->mark = 0; /* clear mark */
alloc_count_old++;
}
}
/* Free unmarked "table" allocations */
pt = &table_head;
while (*pt != NULL) {
at = *pt;
if (!at->mark) {
*pt = at->next;
size_t i;
for (i = 0; i < at->capacity; i++) {
struct table_entry *e = at->data[i];
while (e) {
struct table_entry *next = e->next;
free(e);
e = next;
}
}
free(at->data);
free(at);
}
else {
pt = &at->next;
at->mark = 0; /* clear mark */
alloc_count_old++;
}
}
alloc_count = alloc_count_old;
}
atom make_number(double x)
{
atom a;
a.type = T_NUM;
a.value.number = x;
return a;
}
atom make_sym(const char *s)
{
atom a;
int i;
for (i = symbol_size - 1; i >= 0; i--) { /* compare recent symbol first */
char *s2 = symbol_table[i];
if (strcmp(s2, s) == 0) {
a.type = T_SYM;
a.value.symbol = s2;
return a;
}
}
a.type = T_SYM;
a.value.symbol = (char*)strdup(s);
if (symbol_size >= symbol_capacity) {
symbol_capacity *= 2;
symbol_table = realloc(symbol_table, symbol_capacity * sizeof(char *));
}
symbol_table[symbol_size] = a.value.symbol;
symbol_size++;
return a;
}
atom make_builtin(builtin fn)
{
atom a;
a.type = T_BUILTIN;
a.value.builtin = fn;
return a;
}
error make_closure(atom env, atom args, atom body, atom *result)
{
atom p;
if (!listp(body))
return ERROR_SYNTAX;
/* Check argument names are all symbols or conses */
p = args;
while (!no(p)) {
if (p.type == T_SYM)
break;
else if (p.type != T_CONS || (car(p).type != T_SYM && car(p).type != T_CONS))
return ERROR_TYPE;
p = cdr(p);
}
if (no(body)) { /* no body */
p = nil;
}
else if (no(cdr(body))) { /* 1 form only: do form not required */
p = car(body);
}
else {
p = cons(sym_do, body);
}
*result = cons(env, cons(args, p));
result->type = T_CLOSURE;
return ERROR_OK;
}
atom make_string(char *x)
{
atom a;
struct str *s;
alloc_count++;
s = a.value.str = malloc(sizeof(struct str));
s->value = x;
s->mark = 0;
s->next = str_head;
str_head = s;
a.type = T_STRING;
stack_add(a);
return a;
}
atom make_input(FILE *fp) {
atom a;
a.type = T_INPUT;
a.value.fp = fp;
return a;
}
atom make_input_pipe(FILE *fp) {
atom a;
a.type = T_INPUT_PIPE;
a.value.fp = fp;
return a;
}
atom make_output(FILE *fp) {
atom a;
a.type = T_OUTPUT;
a.value.fp = fp;
return a;
}
atom make_char(char c) {
atom a;
a.type = T_CHAR;
a.value.ch = c;
return a;
}
void print_expr(atom a)
{
char *s = to_string(a, 1);
printf("%s", s);
free(s);
}
void pr(atom a)
{
char *s = to_string(a, 0);
printf("%s", s);
free(s);
}
error lex(const char *str, const char **start, const char **end)
{
const char *ws = " \t\r\n";
const char *delim = "()[] \t\r\n;";
const char *prefix = "()[]'`";
start:
str += strspn(str, ws);
if (str[0] == '\0') {
*start = *end = NULL;
return ERROR_FILE;
}
*start = str;
if (strchr(prefix, str[0]) != NULL)
*end = str + 1;
else if (str[0] == ',')
*end = str + (str[1] == '@' ? 2 : 1);
else if (str[0] == '"') {
str++;
while (1) {
if (*str == 0) return ERROR_FILE; /* string not terminated */
if (*str == '\\') str++;
else if (*str == '"') {
break;
}
str++;
}
*end = str + 1;
}
else if (str[0] == ';') { /* end-of-line comment */
str += strcspn(str, "\n");
goto start;
}
else
*end = str + strcspn(str, delim);
return ERROR_OK;
}
error parse_simple(const char *start, const char *end, atom *result)
{
char *p;
/* Is it a number? */
double val = strtod(start, &p);
if (p == end) {
result->type = T_NUM;
result->value.number = val;
return ERROR_OK;
}
else if (start[0] == '"') { /* "string" */
result->type = T_STRING;
size_t length = end - start - 2;
char *buf = (char*)malloc(length + 1);
const char *ps = start + 1;
char *pt = buf;
while (ps < end - 1) {
if (*ps == '\\') {
char c_next = *(ps + 1);
switch (c_next) {
case 'r':
*pt = '\r';
break;
case 'n':
*pt = '\n';
break;
case 't':
*pt = '\t';
break;
default:
*pt = c_next;
}
ps++;
}
else {
*pt = *ps;
}
ps++;
pt++;
}
*pt = 0;
buf = realloc(buf, pt - buf + 1);
*result = make_string(buf);
return ERROR_OK;
}
else if (start[0] == '#') { /* #\char */
char *buf = malloc(end - start + 1);
memcpy(buf, start, end - start);
buf[end - start] = 0;
size_t length = end - start;
if (length == 3 && buf[1] == '\\') { /* plain character e.g. #\a */
*result = make_char(buf[2]);
free(buf);
return ERROR_OK;
}
else {
char c;
if (strcmp(buf, "#\\nul") == 0)
c = '\0';
else if (strcmp(buf, "#\\return") == 0)
c = '\r';
else if (strcmp(buf, "#\\newline") == 0)
c = '\n';
else if (strcmp(buf, "#\\tab") == 0)
c = '\t';
else if (strcmp(buf, "#\\space") == 0)
c = ' ';
else {
free(buf);
return ERROR_SYNTAX;
}
free(buf);
*result = make_char(c);
return ERROR_OK;
}
}
/* NIL or symbol */
char *buf = malloc(end - start + 1);
memcpy(buf, start, end - start);
buf[end - start] = 0;
if (strcmp(buf, "nil") == 0)
*result = nil;
else if (strcmp(buf, ".") == 0)
*result = make_sym(buf);
else {
atom a1, a2;
long length = end - start, i;
for (i = length - 1; i >= 0; i--) { /* left-associative */
if (buf[i] == '.') { /* a.b => (a b) */
if (i == 0 || i == length - 1) {
free(buf);
return ERROR_SYNTAX;
}
error err;
err = parse_simple(buf, buf + i, &a1);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
err = parse_simple(buf + i + 1, buf + length, &a2);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
free(buf);
*result = cons(a1, cons(a2, nil));
return ERROR_OK;
}
else if (buf[i] == '!') { /* a!b => (a 'b) */
if (i == 0 || i == length - 1) {
free(buf);
return ERROR_SYNTAX;
}
error err;
err = parse_simple(buf, buf + i, &a1);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
err = parse_simple(buf + i + 1, buf + length, &a2);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
free(buf);
*result = cons(a1, cons(cons(sym_quote, cons(a2, nil)), nil));
return ERROR_OK;
}
else if (buf[i] == ':') { /* a:b => (compose a b) */
if (i == 0 || i == length - 1) {
free(buf);
return ERROR_SYNTAX;
}
error err;
err = parse_simple(buf, buf + i, &a1);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
err = parse_simple(buf + i + 1, buf + length, &a2);
if (err) {
free(buf);
return ERROR_SYNTAX;
}
free(buf);
*result = cons(make_sym("compose"), cons(a1, cons(a2, nil)));
return ERROR_OK;
}
}
if (length >= 2 && buf[0] == '~') { /* ~a => (complement a) */
atom a1;
error err = parse_simple(buf + 1, buf + length, &a1);
free(buf);
if (err) {
return ERROR_SYNTAX;
}
*result = cons(make_sym("complement"), cons(a1, nil));
return ERROR_OK;
}
*result = make_sym(buf);
}
free(buf);
return ERROR_OK;
}
error read_list(const char *start, const char **end, atom *result)
{
atom p;
*end = start;
p = *result = nil;
for (;;) {
const char *token;
atom item;
error err;
err = lex(*end, &token, end);
if (err)
return err;
if (token[0] == ')') {
return ERROR_OK;
}
if (!no(p) && token[0] == '.' && *end - token == 1) {
/* Improper list */
if (no(p)) return ERROR_SYNTAX;
err = read_expr(*end, end, &item);
if (err) return err;
cdr(p) = item;
/* Read the closing ')' */
err = lex(*end, &token, end);
if (!err && token[0] != ')') {
err = ERROR_SYNTAX;
}
return err;
}
err = read_expr(token, end, &item);
if (err)
return err;
if (no(p)) {
/* First item */
*result = cons(item, nil);
p = *result;
}
else {
cdr(p) = cons(item, nil);
p = cdr(p);
}
}
}
/* [...] => (fn (_) (...)) */
error read_bracket(const char *start, const char **end, atom *result)
{
atom p;
*end = start;
p = *result = nil;
/* First item */
*result = cons(sym_fn, nil);
p = *result;
cdr(p) = cons(cons(sym__, nil), nil);
p = cdr(p);
atom body = nil;
for (;;) {
const char *token;
atom item;
error err;
err = lex(*end, &token, end);
if (err) return err;
if (token[0] == ']') {
return ERROR_OK;
}
err = read_expr(token, end, &item);
if (err) return err;
if (no(body)) {
body = cons(item, nil);
cdr(p) = cons(body, nil);
p = body;
}
else {
cdr(p) = cons(item, nil);
p = cdr(p);
}
}
}
error read_expr(const char *input, const char **end, atom *result)
{
const char *token;
error err;
err = lex(input, &token, end);
if (err)
return err;
if (token[0] == '(') {
return read_list(*end, end, result);
}
else if (token[0] == ')')
return ERROR_SYNTAX;
else if (token[0] == '[') {
return read_bracket(*end, end, result);
}
else if (token[0] == ']')
return ERROR_SYNTAX;
else if (token[0] == '\'') {
*result = cons(sym_quote, cons(nil, nil));
return read_expr(*end, end, &car(cdr(*result)));
}
else if (token[0] == '`') {
*result = cons(sym_quasiquote, cons(nil, nil));
return read_expr(*end, end, &car(cdr(*result)));
}
else if (token[0] == ',') {
*result = cons(
token[1] == '@' ? sym_unquote_splicing : sym_unquote,
cons(nil, nil));
return read_expr(*end, end, &car(cdr(*result)));
}
else
return parse_simple(token, *end, result);
}
#ifndef READLINE
char *readline(char *prompt) {
return readline_fp(prompt, stdin);
}
#endif /* READLINE */
char *readline_fp(char *prompt, FILE *fp) {
size_t size = 80;
/* The size is extended by the input with the value of the provisional */
char *str;
int ch;
size_t len = 0;
printf("%s", prompt);
str = malloc(sizeof(char)* size); /* size is start size */
if (!str) return NULL;
while (EOF != (ch = fgetc(fp)) && ch != '\n') {
str[len++] = ch;
if (len == size) {
void *p = realloc(str, sizeof(char)*(size *= 2));
if (!p) {
free(str);
return NULL;
}
str = p;
}
}
if (ch == EOF && len == 0) {
free(str);
return NULL;
}
str[len++] = '\0';
return realloc(str, sizeof(char)*len);
}
atom env_create(atom parent)
{
return cons(parent, make_table(4));
}
atom env_create_cap(atom parent, size_t capacity)
{
return cons(parent, make_table(capacity));
}
error env_get(atom env, char *symbol, atom *result)
{
while (1) {
struct table *ptbl = cdr(env).value.table;
struct table_entry *a = table_get_sym(ptbl, symbol);
if (a) {
*result = a->v;
return ERROR_OK;
}
if (no(car(env))) {
/* printf("%s: ", symbol); */
return ERROR_UNBOUND;
}
env = car(env);
}
}
error env_assign(atom env, char *symbol, atom value) {
struct table *ptbl = cdr(env).value.table;
table_set_sym(ptbl, symbol, value);
return ERROR_OK;
}
error env_assign_eq(atom env, char *symbol, atom value) {
while (1) {
atom parent = car(env);
struct table *ptbl = cdr(env).value.table;
struct table_entry *a = table_get_sym(ptbl, symbol);
if (a) {
a->v = value;
return ERROR_OK;
}
if (no(parent)) {
return env_assign(env, symbol, value);
}
env = parent;
}
}
int listp(atom expr)
{
atom *p = &expr;
while (!no(*p)) {
if (p->type != T_CONS)
return 0;
p = &cdr(*p);
}
return 1;
}
size_t len(atom xs) {
atom *p = &xs;
size_t ret = 0;
while (!no(*p)) {
if (p->type != T_CONS)
return ret + 1;
p = &cdr(*p);
ret++;
}
return ret;
}
atom copy_list(atom list)
{
atom a, p;
if (no(list))
return nil;
a = cons(car(list), nil);
p = a;
list = cdr(list);
while (!no(list)) {
cdr(p) = cons(car(list), nil);
p = cdr(p);
list = cdr(list);
if (list.type != T_CONS) { /* improper list */
p = list;
break;
}
}
return a;
}
error destructuring_bind(atom arg_name, atom val, int val_unspecified, atom env) {
switch (arg_name.type) {
case T_SYM:
return env_assign(env, arg_name.value.symbol, val);
case T_CONS:
if (is(car(arg_name), sym_o)) { /* (o ARG [DEFAULT]) */
if (val_unspecified) { /* missing argument */
if (!no(cdr(cdr(arg_name)))) {
error err = eval_expr(car(cdr(cdr(arg_name))), env, &val);
if (err) return err;
}
}
return env_assign(env, car(cdr(arg_name)).value.symbol, val);
}
else {
if (val.type != T_CONS) {
return ERROR_ARGS;
}
error err = destructuring_bind(car(arg_name), car(val), 0, env);
if (err) return err;
return destructuring_bind(cdr(arg_name), cdr(val), no(cdr(val)), env);
}
case T_NIL:
if (no(val))
return ERROR_OK;
else
return ERROR_ARGS;
default:
return ERROR_ARGS;
}
}
error env_bind(atom env, atom arg_names, struct vector *vargs) {
/* Bind the arguments */
size_t i = 0;
while (!no(arg_names)) {
if (arg_names.type == T_SYM) {
env_assign(env, arg_names.value.symbol, vector_to_atom(vargs, i));
i = vargs->size;
break;
}
atom arg_name = car(arg_names);
atom val;
int val_unspecified = 0;
if (i < vargs->size) {
val = vargs->data[i];
}
else {
val = nil;
val_unspecified = 1;
}
error err = destructuring_bind(arg_name, val, val_unspecified, env);
if (err) {
return err;
}
arg_names = cdr(arg_names);
i++;
}
if (i < vargs->size) {
return ERROR_ARGS;
}
return ERROR_OK;
}
error apply(atom fn, struct vector *vargs, atom *result)
{
if (fn.type == T_BUILTIN)
return (*fn.value.builtin)(vargs, result);
else if (fn.type == T_CLOSURE) {
atom arg_names = car(cdr(fn));
atom env = env_create(car(fn));
atom body = cdr(cdr(fn));
error err = env_bind(env, arg_names, vargs);
if (err) {
return err;
}
/* Evaluate the body */
err = eval_expr(body, env, result);
if (err) {
return err;
}
return ERROR_OK;
}
else if (fn.type == T_CONTINUATION) {
if (vargs->size != 1) return ERROR_ARGS;
thrown = vargs->data[0];
longjmp(*fn.value.jb, 1);
}
else if (fn.type == T_STRING) { /* implicit indexing for string */
if (vargs->size != 1) return ERROR_ARGS;
size_t index = (size_t)(vargs->data[0]).value.number;
*result = make_char(fn.value.str->value[index]);
return ERROR_OK;
}
else if (fn.type == T_CONS && listp(fn)) { /* implicit indexing for list */
if (vargs->size != 1) return ERROR_ARGS;
size_t index = (size_t)(vargs->data[0]).value.number;
atom a = fn;
size_t i;
for (i = 0; i < index; i++) {
a = cdr(a);
if (no(a)) {
*result = nil;
return ERROR_OK;
}
}
*result = car(a);
return ERROR_OK;
}
else if (fn.type == T_TABLE) { /* implicit indexing for table */
long len1 = vargs->size;
if (len1 != 1 && len1 != 2) return ERROR_ARGS;
struct table_entry *pair = table_get(fn.value.table, vargs->data[0]);
if (pair) {
*result = pair->v;
}
else {
if (len1 == 2) /* default value is specified */
*result = vargs->data[1];
else
*result = nil;
}
return ERROR_OK;
}
else {
return ERROR_TYPE;
}
}
error builtin_car(struct vector *vargs, atom *result)
{
if (vargs->size != 1)
return ERROR_ARGS;
atom a = vargs->data[0];
if (no(a))
*result = nil;
else if (a.type != T_CONS)
return ERROR_TYPE;
else
*result = car(a);
return ERROR_OK;