-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp-zero-single.c
1412 lines (1107 loc) · 33.3 KB
/
lisp-zero-single.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
/* A Lisp version 0 interpreter
Copyright (c) 2010 James Craig Burley <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License , or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Cloned and wildly modified from
http://www.sonoma.edu/users/l/luvisi/sl3.c by Andru Luvisi.
Zero version implements "first" version of Lisp via Lisp code on
top of builtin functions quote, atom, eq, cons, car, cdr, and cond,
along with builtin recursive evaluations of the arguments to those
functions where appropriate.
This version incorporate the map.c source file, to comprise a
single source file, easing use of c2go to convert it to Go and
build it from there.
*/
#define __USE_XOPEN2K8 1
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
/**
* Copyright (c) 2014 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/
#define MAP_VERSION "0.1.0"
typedef struct map_node_s map_node_t;
struct map_node_s {
unsigned hash;
void *value;
map_node_t *next;
/* char key[]; */
/* char value[]; */
};
struct map_base_s {
map_node_t **buckets;
unsigned nbuckets, nnodes;
};
struct map_iter_s {
unsigned bucketidx;
map_node_t *node;
};
#define map_t(N,T) \
struct N ## _MAP { struct map_base_s base; T *ref; T tmp; }
#define map_init(m)\
memset(m, 0, sizeof(*(m)))
#define map_deinit(m)\
map_deinit_(&(m)->base)
#define map_get(m, key)\
( (m)->ref = map_get_(&(m)->base, key) )
#define map_set(m, key, value)\
( (m)->tmp = (value),\
map_set_(&(m)->base, key, &(m)->tmp, sizeof((m)->tmp)) )
#define map_remove(m, key)\
map_remove_(&(m)->base, key)
#define map_iter(m)\
map_iter_()
#define map_next(m, iter)\
map_next_(&(m)->base, iter)
void map_deinit_(struct map_base_s *m);
void *map_get_(struct map_base_s *m, const char *key);
int map_set_(struct map_base_s *m, const char *key, void *value, int vsize);
void map_remove_(struct map_base_s *m, const char *key);
struct map_iter_s map_iter_(void);
const char *map_next_(struct map_base_s *m, struct map_iter_s *iter);
typedef map_t(voidp,void*) map_void_t;
typedef map_t(charp,char*) map_str_t;
typedef map_t(int,int) map_int_t;
typedef map_t(char,char) map_char_t;
typedef map_t(float,float) map_float_t;
typedef map_t(double,double) map_double_t;
static unsigned
map_hash(const char *str)
{
unsigned hash = 5381;
while (*str) {
hash = ((hash << 5) + hash) ^ *str++;
}
return hash;
}
static map_node_t *
map_newnode(const char *key, void *value, int vsize)
{
map_node_t *node;
int ksize = strlen(key) + 1;
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
node = malloc(sizeof(*node) + voffset + vsize);
if (!node) return NULL;
memcpy(node + 1, key, ksize);
node->hash = map_hash(key);
node->value = ((char*) (node + 1)) + voffset;
memcpy(node->value, value, vsize);
return node;
}
static int
map_bucketidx(struct map_base_s *m, unsigned hash)
{
/* If the implementation is changed to allow a non-power-of-2 bucket count,
* the line below should be changed to use mod instead of AND */
return hash & (m->nbuckets - 1);
}
static void
map_addnode(struct map_base_s *m, map_node_t *node)
{
int n = map_bucketidx(m, node->hash);
node->next = m->buckets[n];
m->buckets[n] = node;
}
static int
map_resize(struct map_base_s *m, int nbuckets)
{
map_node_t *nodes, *node, *next;
map_node_t **buckets;
int i;
/* Chain all nodes together */
nodes = NULL;
i = m->nbuckets;
while (i--) {
node = (m->buckets)[i];
while (node) {
next = node->next;
node->next = nodes;
nodes = node;
node = next;
}
}
/* Reset buckets */
buckets = malloc(sizeof(*m->buckets) * nbuckets);
if (buckets != NULL) {
free(m->buckets);
m->buckets = buckets;
m->nbuckets = nbuckets;
}
if (m->buckets) {
memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets);
/* Re-add nodes to buckets */
node = nodes;
while (node) {
next = node->next;
map_addnode(m, node);
node = next;
}
}
/* Return error code if malloc() failed */
return (buckets == NULL) ? -1 : 0;
}
static map_node_t **map_getref(struct map_base_s *m, const char *key) {
unsigned hash = map_hash(key);
map_node_t **next;
if (m->nbuckets > 0) {
next = &m->buckets[map_bucketidx(m, hash)];
while (*next) {
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) {
return next;
}
next = &(*next)->next;
}
}
return NULL;
}
void map_deinit_(struct map_base_s *m) {
map_node_t *next, *node;
int i;
i = m->nbuckets;
while (i--) {
node = m->buckets[i];
while (node) {
next = node->next;
free(node);
node = next;
}
}
free(m->buckets);
}
void *
map_get_(struct map_base_s *m, const char *key)
{
map_node_t **next = map_getref(m, key);
return next ? (*next)->value : NULL;
}
int
map_set_(struct map_base_s *m, const char *key, void *value, int vsize)
{
int n, err;
map_node_t **next, *node;
/* Find & replace existing node */
next = map_getref(m, key);
if (next) {
memcpy((*next)->value, value, vsize);
return 0;
}
/* Add new node */
node = map_newnode(key, value, vsize);
if (node == NULL) goto fail;
if (m->nnodes >= m->nbuckets) {
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
err = map_resize(m, n);
if (err) goto fail;
}
map_addnode(m, node);
m->nnodes++;
return 0;
fail:
if (node) free(node);
return -1;
}
void
map_remove_(struct map_base_s *m, const char *key)
{
map_node_t *node;
map_node_t **next = map_getref(m, key);
if (next) {
node = *next;
*next = (*next)->next;
free(node);
m->nnodes--;
}
}
struct map_iter_s
map_iter_(void)
{
struct map_iter_s iter;
iter.bucketidx = UINT_MAX;
iter.node = NULL;
return iter;
}
const char *
map_next_(struct map_base_s *m, struct map_iter_s *iter)
{
if (!iter->node || !(iter->node = iter->node->next)) {
do {
if (++iter->bucketidx >= m->nbuckets) {
return NULL;
}
iter->node = m->buckets[iter->bucketidx];
} while (iter->node == NULL);
}
return (char*) (iter->node + 1);
}
static bool quiet = false;
#ifdef __GNUC__
#define __UNUSED__ __attribute__((__unused__))
#endif
#define TRACING 1
#if TRACING
#define TRACE(where...) where,
#define TRACEPARAMS(params...) params,
static bool tracing = false;
#else
#define TRACE(where...)
#define TRACEPARAMS(params...)
#endif
#define PRINT_ERROR_AND_EXIT(MSG, CODE) \
do \
{ \
char *m = (MSG); \
if (m) fprintf(stderr, "%s\n", m); \
if (!quiet) \
fprintf(stderr, "allocations: %" PRId64 "; total: %" PRId64 "\n", \
allocations, allocations_total); \
exit((CODE)); \
} while (0)
#define FPRINT_ERROR_AND_EXIT(CODE, F...) \
do \
{ \
fprintf(stderr, ## F); \
if (!quiet) \
fprintf(stderr, "allocations: %lld; total: %lld\n", \
allocations, allocations_total); \
exit((CODE)); \
} while (0)
/* Heap. */
static uint64_t allocations = 0;
static uint64_t allocations_total = 0;
char *
string_duplicate(char const *str)
{
size_t l = strlen(str) + 1;
char *dup = malloc(l);
if (!dup)
PRINT_ERROR_AND_EXIT("insufficient memory for duplicating a string", 998);
memcpy(dup, str, l);
++allocations;
allocations_total += strlen(str);
return dup;
}
/* Forward references. */
typedef struct Object_s *(*compiled_fn)(TRACEPARAMS(char const *) struct Object_s *, struct Object_s *);
struct Symbol_s {
char const *name;
};
union Cdr_u {
struct Object_s *obj;
struct Symbol_s *sym;
compiled_fn fn;
};
/* Objects (lists, atoms, etc.). */
typedef struct Object_s {
struct Object_s *car; /* Head item in this list, unless a BUILTIN_CAR node. */
union Cdr_u cdr; /* Tail list, unless car is a BUILTIN_CAR node. */
} Object;
#define BUILTIN_CAR(obj) \
static Object obj; \
static struct Object_s *p_##obj = &obj
/* If CAR of an Object is not one of these, it is a List. */
BUILTIN_CAR(atomic); /* Object.cdr is a struct Symbol_s *. */
BUILTIN_CAR(compiled); /* Object.cdr is a compiled_fn. */
#undef BUILTIN_CAR
struct Object_s *f_quote(TRACEPARAMS(char const *what) struct Object_s *args, struct Object_s *env);
#define OBJECT(list) static struct Object_s *p_##list = NULL
OBJECT(environment); /* Current evaluation environment (list of bindings). */
OBJECT(nil); /* Input/output as (). */
OBJECT(quote); /* Also input as '. */
OBJECT(atom);
OBJECT(eq);
OBJECT(cons);
OBJECT(car);
OBJECT(cdr);
OBJECT(cond);
OBJECT(eval);
OBJECT(apply);
OBJECT(defglobal);
OBJECT(dot_symbol_dump);
#undef OBJECT
bool
nilp(struct Object_s *list)
{
return list == p_nil;
}
/* Whether object is an atom in the traditional Lisp sense */
bool
atomp(struct Object_s *list)
{
return nilp(list) || list->car == p_atomic;
}
/* Whether object is an atom in this implementation */
bool
atomicp(struct Object_s *list)
{
return !nilp(list) && list->car == p_atomic;
}
bool
compiledp(struct Object_s *list)
{
return !atomp(list) && list->car == p_compiled;
}
bool
listp(struct Object_s *list)
{
return !atomp(list) && !compiledp(list);
}
bool
finalp(struct Object_s *list)
{
return listp(list) && nilp(list->cdr.obj);
}
/* Forward-declare this to support assert_or_dump(): */
static void object_write(FILE *output, struct Object_s *obj);
static char const *filename;
static unsigned lineno = 1;
static int max_object_write = -1;
static void
assert_or_dump_(unsigned srcline, bool ok, struct Object_s *obj, char const *what)
{
if (ok || max_object_write != -1)
return;
fprintf(stderr, "ERROR at %d: %s, but got:\n", lineno, what);
max_object_write = 10;
object_write(stderr, obj);
fprintf(stderr, "\n" __FILE__ ":%d: aborting\n", srcline);
assert(what == NULL);
}
#define assert_or_dump(ok, obj, what) assert_or_dump_(__LINE__, (ok), (obj), (what))
struct Object_s *
list_car(struct Object_s *list)
{
assert_or_dump(listp(list), list, "expected list");
return list->car;
}
struct Object_s *
list_cdr(struct Object_s *list)
{
assert_or_dump(listp(list), list, "expected list");
return list->cdr.obj;
}
struct Symbol_s *
object_symbol(struct Object_s *atom)
{
assert_or_dump(atomicp(atom), atom, "expected implementation atom");
return atom->cdr.sym;
}
compiled_fn
object_compiled(struct Object_s *compiled)
{
assert_or_dump(compiledp(compiled), compiled, "expected compiled function");
return compiled->cdr.fn;
}
static struct Object_s *
object_new(struct Object_s *car, struct Object_s *cdr)
{
struct Object_s *obj;
obj = (struct Object_s *) malloc(sizeof(Object));
if (!obj)
PRINT_ERROR_AND_EXIT("no more memory", 998);
++allocations;
allocations_total += sizeof(Object);
obj->car = car;
obj->cdr.obj = cdr;
return obj;
}
static struct Object_s *
object_new_compiled(compiled_fn fn)
{
struct Object_s *obj;
obj = (struct Object_s *) malloc(sizeof(Object));
if (!obj)
PRINT_ERROR_AND_EXIT("no more memory", 998);
++allocations;
allocations_total += sizeof(Object);
obj->car = p_compiled;
obj->cdr.fn = fn;
return obj;
}
#define object_new_atomic(SYM) object_new(p_atomic, (struct Object_s *) (SYM))
#define object_new_t() object_new_atomic(p_sym_t)
#define object_new_quote() object_new_atomic(p_sym_quote)
/* Symbols. */
#define symbol_name(SYMBOL) ((SYMBOL)->name)
static struct Symbol_s *
symbol_new(char const *name)
{
struct Symbol_s *sym;
sym = (struct Symbol_s *) malloc(sizeof(struct Symbol_s));
if (!sym)
PRINT_ERROR_AND_EXIT("no more memory", 998);
++allocations;
allocations_total += sizeof(struct Symbol_s);
sym->name = name;
return sym;
}
/* Map of symbols (keys) to values. */
static map_t(Symbol,struct Symbol_s *) map_sym;
static struct Symbol_s *
symbol_lookup(char const *name)
{
void **p_sym = (void **) map_get(&map_sym, name);
return p_sym ? *p_sym : NULL;
}
bool symbol_strdup = true;
static struct Symbol_s *
symbol_sym(char const *name)
{
struct Symbol_s *sym = symbol_lookup(name);
if (sym)
return sym;
sym = symbol_new(symbol_strdup ? string_duplicate(name) : name);
map_set(&map_sym, name, (struct Symbol_s *) sym);
return sym;
}
static void
symbol_dump(void)
{
struct map_iter_s iter = map_iter(&map_sym);
const char *key;
while ((key = map_next(&map_sym, &iter))) {
printf("%s -> %p\n", key, symbol_lookup(key));
}
}
static struct Symbol_s *p_sym_t = NULL;
static struct Symbol_s *p_sym_quote = NULL;
/* Environment (bindings). */
static struct Object_s *
binding_new(struct Object_s *sym, struct Object_s *val)
{
assert_or_dump(atomicp(sym), sym, "expected implementation atom");
return object_new(sym, val);
}
#define environment_new(SYM, VAL, ENV) (object_new(binding_new((SYM), (VAL)), (ENV)))
/* Bindings; each binding is either an atom (meaning its symbol is
explicitly unbound) or a key/value cons (the symbol is in the car,
its binding is in the cdr). */
struct Object_s *
binding_lookup(TRACEPARAMS(char const *what __UNUSED__) struct Symbol_s *key, struct Object_s *bindings)
{
if (nilp(bindings))
return p_nil;
#if TRACING
if (tracing) {
fprintf(stderr, "%s:%d: Searching for `%s' in:\n",
filename, lineno, key->name);
max_object_write = 10;
object_write(stderr, bindings);
max_object_write = -1;
fputs("\n\n", stderr);
}
#endif
/* Originally this used a recursive algorithm, but tail-recursion
optimization wasn't being done by gcc -g -O0, and it was annoying
to find oneself inside such deep stack traces during debugging.
*/
for (; !nilp(bindings); bindings = list_cdr(bindings))
{
assert_or_dump(listp(bindings), bindings, "expected list");
struct Object_s *binding = list_car(bindings);
if (atomicp(binding) && object_symbol(binding) == key)
return p_nil;
{
struct Object_s *symbol = list_car(binding);
if (atomicp(symbol) && object_symbol(symbol) == key)
return binding;
}
}
return p_nil;
}
/* Input */
static char *token_lookahead;
static int lookahead_valid = 0;
struct buffer_s {
size_t size;
size_t used;
char *contents;
};
static struct buffer_s *
buffer_new(size_t initial_size)
{
struct buffer_s *buf = (struct buffer_s *) malloc(sizeof(*buf));
char *contents = (char *) malloc(initial_size);
if (!buf || !contents)
PRINT_ERROR_AND_EXIT("cannot allocate buffer for lexemes", 998);
buf->size = initial_size;
buf->used = 0;
buf->contents = contents;
return buf;
}
static void
buffer_append(struct buffer_s *buf, char ch)
{
if (buf->used >= buf->size)
PRINT_ERROR_AND_EXIT("lexeme too long for this build", 997); /* TODO: realloc() */
buf->contents[buf->used++] = ch;
}
static char *
buffer_tostring(struct buffer_s *buf)
{
buffer_append(buf, '\0');
return buf->contents;
}
#define buffer_clear(buf) ((buf)->used = 0)
static void
token_putback(char *token)
{
assert (lookahead_valid == 0);
token_lookahead = token;
lookahead_valid = 1;
}
static int my_getc_next;
static FILE *my_getc_file;
static int
my_getc(FILE *input)
{
if (my_getc_file == input) {
my_getc_file = NULL;
return my_getc_next;
}
assert(!my_getc_file || ("No support for un-getting on two streams at the same time" == NULL));
return getc(input);
}
static void
my_ungetc(int ch, FILE *input)
{
assert(!my_getc_file || ("No support for un-getting on two streams at the same time" == NULL));
my_getc_file = input;
my_getc_next = ch;
}
static char *
token_get(FILE *input, struct buffer_s *buf) {
int ch;
buffer_clear(buf);
if (lookahead_valid)
{
lookahead_valid = 0;
return token_lookahead;
}
do
{
if ((ch = my_getc(input)) == EOF)
PRINT_ERROR_AND_EXIT(NULL, 0);
if (ch == ';')
while ((ch = my_getc(input)) != EOF && ch != '\n')
;
if (ch == '\n')
++lineno;
} while(isspace(ch));
buffer_append(buf, ch);
if(strchr("()\'", ch))
return buffer_tostring(buf);
for (;;) {
if ((ch = my_getc(input)) == EOF)
PRINT_ERROR_AND_EXIT(NULL, 0);
if (strchr("()\'", ch) || isspace(ch))
{
my_ungetc(ch, input);
return buffer_tostring(buf);
}
buffer_append(buf, ch);
}
}
static unsigned latest_lineno;
struct Object_s *list_read(FILE *input, struct buffer_s *buf);
struct Object_s *
object_read(FILE *input, struct buffer_s *buf)
{
char *token;
token = token_get(input, buf);
if (!strcmp(token, "("))
return list_read(input, buf);
if (!strcmp(token, "\'"))
{
struct Object_s *tmp = object_read(input, buf);
return object_new(object_new_atomic(p_sym_quote),
object_new(tmp, p_nil));
}
if (!strcmp(token, ")"))
PRINT_ERROR_AND_EXIT("unbalanced close paren", 1);
#if TRACING
if (tracing && lineno != latest_lineno) {
latest_lineno = lineno;
fprintf(stderr, "%s:%d: Seen `%s'.\n", filename, lineno, token);
}
#endif
return object_new_atomic(symbol_sym(token));
}
struct Object_s *
list_read(FILE *input, struct buffer_s *buf)
{
struct Object_s *first = p_nil;
struct Object_s **next = &first;
struct Object_s *cur = NULL;
do {
char *token = token_get(input, buf);
if (!strcmp(token, ")")) {
cur = p_nil;
break;
}
if (!strcmp(token, ".")) {
cur = object_read(input, buf);
if (strcmp(token_get(input, buf), ")"))
PRINT_ERROR_AND_EXIT("missing close parenthese for simple list", 3);
break;
}
token_putback(token);
/* Make sure we first read the object before going on to read the rest of the list. */
cur = object_new(object_read(input, buf), NULL);
*next = cur;
next = &cur->cdr.obj;
} while(true);
return first;
}
/* Output. */
/* true if object is (quote arg) */
/* TODO: Decide whether this look up quote in the current env to do the check */
static bool
quotep(struct Object_s *obj)
{
if (!listp(obj) || !finalp(list_cdr(obj)))
return false;
{
struct Object_s *car = list_car(obj);
return compiledp(car) && object_compiled(car) == f_quote;
}
}
static void
object_write(FILE *output, struct Object_s *obj)
{
if (nilp(obj))
{
fprintf(output, "()");
return;
}
if (atomicp(obj))
{
fprintf(output, "%s", symbol_name(object_symbol(obj)));
return;
}
if (compiledp(obj))
{
fprintf(output, "*COMPILED*"); /* TODO: Print name of function. */
return;
}
if (quotep(obj))
{
fprintf(output, "'");
object_write(output, list_car(list_cdr(obj)));
return;
}
if (max_object_write == 0)
{
fprintf(output, "(...)");
return;
}
if (max_object_write > 0)
--max_object_write;
fprintf(output, "(");
for (;;)
{
object_write(output, list_car(obj));
if (finalp(obj))
{
fprintf(output, ")");
return;
}
obj = list_cdr(obj);
if (!listp(obj))
{
fprintf(output, " . ");
object_write(output, obj);
fprintf(output, ")");
return;
}
fprintf(output, " ");
}
}
/* Evaluation */
struct Object_s *
binding_for(TRACEPARAMS(char const *what) struct Symbol_s *sym, struct Object_s *env)
{
struct Object_s *tmp;
tmp = binding_lookup(TRACE(what) sym, env);
if (nilp(tmp))
{
/* TODO: Throw an exception etc. */
char const *name = symbol_name(sym);
char buf[20 + strlen(name)];
sprintf(buf, "unbound symbol `%s'", name);
assert_or_dump(!nilp(tmp), env, buf);
}
return list_cdr(tmp);
}
struct Object_s *apply(TRACEPARAMS(char const *what) struct Object_s *func, struct Object_s *me, struct Object_s *forms, struct Object_s *env);
/* Does not support traditional lambdas or labels; just the built-ins and
our "unique" apply. */
struct Object_s *
eval(TRACEPARAMS(char const *what) struct Object_s *exp, struct Object_s *env)
{
if (nilp(exp) || compiledp(exp))
return exp;
if (atomicp(exp))
return binding_for(TRACE(what) object_symbol(exp), env);
assert_or_dump(listp(exp), exp, "expected list");
{
struct Object_s *func = eval(TRACE(what) list_car(exp), env);
struct Object_s *forms = list_cdr(exp);
#if TRACING
if (atomp(list_car(exp)))
what = symbol_name(object_symbol(list_car(exp)));
#endif
if (compiledp(func))
{
compiled_fn fn;
fn = object_compiled(func);
return (*fn)(TRACE(what) forms, env);
}
return apply(TRACE(what) func, func, forms, env);
}
}
/* Need to solve these problems:
(defun x (a) (progn (setq a 6) (a)))
(setq a 5)
(x a)