-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
3296 lines (3002 loc) · 92.1 KB
/
eval.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 <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <wchar.h>
#include <locale.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include "ffi.h"
#include <assert.h>
extern int isatty(int);
#define TAG_INT 0
//#define LIB_GC 1
#define GC_APP_HEADER int type;
#define GC_SAVE 1
#if (LIB_GC)
# include "libgc.c"
#else
# include "gc.c"
#endif
#include "wcs.c"
#include "buffer.c"
# include <malloc.h>
# define swnprintf swprintf
union Object;
typedef union Object *oop;
typedef oop (*imp_t)(oop args, oop env);
typedef union {
int arg_int;
int32_t arg_int32;
int64_t arg_int64;
long arg_long;
float arg_float;
double arg_double;
void *arg_pointer;
char *arg_string;
wchar_t *arg_String;
} arg_t;
typedef void (*cast_t)(oop arg, void **argp, arg_t *buf);
typedef struct {
int arg_count;
int arg_rest;
ffi_type *arg_types[32];
cast_t arg_casts[32];
} proto_t;
#define nil ((oop)0)
enum { Undefined, Data, Long, Double, String, Symbol, Pair, _Array, Array, Expr, Form, Fixed, Subr, Variable, Env, Context, Int64 };
struct Data { int empty[0]; };
struct Long { long bits; };
struct Int64 { int64_t bits; };
struct Double { double bits; };
struct String { oop size; wchar_t *bits; }; /* bits is in managed memory */
struct Symbol { wchar_t *bits; };
struct Pair { oop head, tail, source; };
struct Array { oop size, _array; };
struct Expr { oop name, defn, ctx, profile; };
struct Form { oop function, symbol; };
struct Fixed { oop function; };
struct Subr { wchar_t *name; imp_t imp; proto_t *sig; int profile; };
struct Variable { oop name, value, env, index, type; };
struct Env { oop parent, level, offset, bindings, stable; };
struct Context { oop home, env, bindings, callee, pc; };
union Object {
struct Data Data;
struct Long Long;
struct Int64 Int64;
struct Double Double;
struct String String;
struct Symbol Symbol;
struct Pair Pair;
struct Array Array;
struct Expr Expr;
struct Form Form;
struct Fixed Fixed;
struct Subr Subr;
struct Variable Variable;
struct Env Env;
struct Context Context;
};
static void fatal(char *reason, ...);
#define setType(OBJ, TYPE) (ptr2hdr(OBJ)->type= (TYPE))
#if (TAG_INT)
static inline int getType(oop obj) { return obj ? (((long)obj & 1) ? Long : ptr2hdr(obj)->type) : Undefined; }
#else
static inline int getType(oop obj) { return obj ? ptr2hdr(obj)->type : Undefined; }
#endif
#define is(TYPE, OBJ) ((OBJ) && (TYPE == getType(OBJ)))
#if defined(NDEBUG)
# define checkType(OBJ, TYPE) OBJ
#else
# define checkType(OBJ, TYPE) _checkType(OBJ, TYPE, #TYPE, __FILE__, __LINE__)
static inline oop _checkType(oop obj, int type, char *name, char *file, int line)
{
if (obj && !((uint64_t)obj & 1) && !ptr2hdr(obj)->used) fatal("%s:%i: attempt to access dead object %s\n", file, line, name);
if (!is(type, obj)) fatal("%s:%i: typecheck failed for %s (%i != %i)\n", file, line, name, type, getType(obj));
return obj;
}
#endif
#define get(OBJ, TYPE, FIELD) (checkType(OBJ, TYPE)->TYPE.FIELD)
#define set(OBJ, TYPE, FIELD, VALUE) (checkType(OBJ, TYPE)->TYPE.FIELD= (VALUE))
#define getHead(OBJ) get(OBJ, Pair,head)
#define getTail(OBJ) get(OBJ, Pair,tail)
#define setHead(OBJ, VAL) set(OBJ, Pair,head, VAL)
#define setTail(OBJ, VAL) set(OBJ, Pair,tail, VAL)
static oop car(oop obj) { return is(Pair, obj) ? getHead(obj) : nil; }
static oop cdr(oop obj) { return is(Pair, obj) ? getTail(obj) : nil; }
static oop caar(oop obj) { return car(car(obj)); }
static oop cadr(oop obj) { return car(cdr(obj)); }
static oop cddr(oop obj) { return cdr(cdr(obj)); }
//static oop caaar(oop obj) { return car(car(car(obj))); }
//static oop cadar(oop obj) { return car(cdr(car(obj))); }
static oop caddr(oop obj) { return car(cdr(cdr(obj))); }
static oop cadddr(oop obj) { return car(cdr(cdr(cdr(obj)))); }
#define newBits(TYPE) _newBits(TYPE, sizeof(struct TYPE))
#define newOops(TYPE) _newOops(TYPE, sizeof(struct TYPE))
static oop _newBits(int type, size_t size) { oop obj= GC_malloc_atomic(size); setType(obj, type); return obj; }
static oop _newOops(int type, size_t size) { oop obj= GC_malloc(size); setType(obj, type); return obj; }
static char *argv0;
static oop symbols= nil, globals= nil, expanders= nil, encoders= nil, evaluators= nil, applicators= nil, backtrace= nil, arguments= nil, input= nil, output= nil;
static oop s_set= nil, s_define= nil, s_let= nil, s_lambda= nil, s_quote= nil, s_quasiquote= nil, s_unquote= nil, s_unquote_splicing= nil, s_t= nil, s_dot= nil, s_bracket= nil, s_brace= nil, s_main= nil;
static oop f_set= nil, f_quote= nil, f_lambda= nil, f_let= nil, f_define;
static int opt_b= 0, opt_g= 0, opt_O= 0, opt_p= 0, opt_v= 0;
static oop traceStack= nil;
static int traceDepth= 0;
static oop newData(size_t len) { return _newBits(Data, len); }
#if (TAG_INT)
static inline int isLong(oop x) { return (((long)x & 1) || Long == getType(x)); }
static inline oop newLong(long x) { if ((x ^ (x << 1)) < 0) { oop obj= newBits(Long); set(obj, Long,bits, x); return obj; } return ((oop)((x << 1) | 1)); }
static inline long getLong(oop x) { if ((long)x & 1) return (long)x >> 1; return get(x, Long,bits); }
#else
# define isLong(X) is(Long, (X))
static oop newLong(long bits) { oop obj= newBits(Long); set(obj, Long,bits, bits); return obj; }
# define getLong(X) get((X), Long,bits)
static oop newInt64(int64_t bits) { oop obj = newBits(Int64); set(obj, Int64, bits, bits); return obj; }
#define getInt64(X) get((X), Int64, bits)
#define isInt64(X) is(Int64, (X))
#endif
static void setDouble(oop obj, double bits) { memcpy(&obj->Double.bits, &bits, sizeof(bits)); }
static double getDouble(oop obj) { double bits; memcpy(&bits, &obj->Double.bits, sizeof(bits)); return bits; }
#define isDouble(X) is(Double, (X))
#define isPair(X) is(Pair, (X))
static inline int isNumeric(oop obj) { return isLong(obj) || isDouble(obj); }
static oop newDouble(double bits) { oop obj= newBits(Double); setDouble(obj, bits); return obj; }
static oop _newString(size_t len)
{
wchar_t *gstr= (wchar_t *)_newBits(-1, sizeof(wchar_t) * (len + 1)); GC_PROTECT(gstr); /* + 1 to ensure null terminator */
oop obj= newOops(String); GC_PROTECT(obj);
set(obj, String,size, newLong(len)); GC_UNPROTECT(obj);
set(obj, String,bits, gstr); GC_UNPROTECT(gstr);
return obj;
}
static oop newStringN(wchar_t *cstr, size_t len)
{
oop obj= _newString(len);
memcpy(get(obj, String,bits), cstr, sizeof(wchar_t) * len);
return obj;
}
static oop newString(wchar_t *cstr)
{
return newStringN(cstr, wcslen(cstr));
}
static int stringLength(oop string)
{
return getLong(get(string, String,size));
}
static oop newSymbol(wchar_t *cstr) {
oop obj= newBits(Symbol);
set(obj, Symbol,bits, wcsdup(cstr));
return obj;
}
static oop newPair(oop head, oop tail) { oop obj= newOops(Pair); set(obj, Pair,head, head); set(obj, Pair,tail, tail); return obj; }
static oop newPairFrom(oop head, oop tail, oop source)
{
oop obj= newOops(Pair);
set(obj, Pair,head, head);
set(obj, Pair,tail, tail);
set(obj, Pair,source, get(source, Pair,source));
return obj;
}
static oop newArray(int size)
{
int cap= size ? size : 1;
oop elts= _newOops(_Array, sizeof(oop) * cap); GC_PROTECT(elts);
oop obj= newOops( Array); GC_PROTECT(obj);
set(obj, Array,_array, elts);
set(obj, Array,size, newLong(size)); GC_UNPROTECT(obj); GC_UNPROTECT(elts);
return obj;
}
static int arrayLength(oop obj)
{
return is(Array, obj) ? getLong(get(obj, Array,size)) : 0;
}
static oop arrayAt(oop array, int index)
{
if (is(Array, array)) {
oop elts= get(array, Array,_array);
int size= arrayLength(array);
if ((unsigned)index < (unsigned)size)
return ((oop *)elts)[index];
}
return nil;
}
static oop arrayAtPut(oop array, int index, oop val)
{
if (is(Array, array)) {
int size= arrayLength(array);
oop elts= get(array, Array,_array);
if ((unsigned)index >= (unsigned)size) {
GC_PROTECT(array);
int cap= GC_size(elts) / sizeof(oop);
if (index >= cap) {
while (cap <= index) cap *= 2;
oop oops= _newOops(_Array, sizeof(oop) * cap);
memcpy((oop *)oops, (oop *)elts, sizeof(oop) * size);
elts= set(array, Array,_array, oops);
}
set(array, Array,size, newLong(index + 1));
GC_UNPROTECT(array);
}
return ((oop *)elts)[index]= val;
}
return nil;
}
static oop arrayAppend(oop array, oop val)
{
return arrayAtPut(array, arrayLength(array), val);
}
static oop arrayInsert(oop obj, size_t index, oop value)
{
size_t len= arrayLength(obj);
arrayAppend(obj, value);
if (index < len) {
oop elts= get(obj, Array,_array);
oop *oops= (oop *)elts + index;
memmove(oops + 1, oops, sizeof(oop) * (len - index));
}
arrayAtPut(obj, index, value);
return value;
}
static oop oopAt(oop obj, int index)
{
if (obj && !isLong(obj) && !GC_atomic(obj)) {
int size= GC_size(obj) / sizeof(oop);
if ((unsigned)index < (unsigned)size) return ((oop *)obj)[index];
}
return nil;
}
static oop oopAtPut(oop obj, int index, oop value)
{
if (obj && !isLong(obj) && !GC_atomic(obj)) {
int size= GC_size(obj) / sizeof(oop);
if ((unsigned)index < (unsigned)size) return ((oop *)obj)[index]= value;
}
return nil;
}
static oop newExpr(oop defn, oop ctx)
{
oop obj= newOops(Expr); GC_PROTECT(obj);
set(obj, Expr,defn, defn);
set(obj, Expr,ctx, ctx);
set(obj, Expr,profile, newLong(0)); GC_UNPROTECT(obj);
return obj;
}
static oop newForm(oop fn, oop sym) { oop obj= newOops(Form); set(obj, Form,function, fn); set(obj, Form,symbol, sym); return obj; }
static oop newFixed(oop function) { oop obj= newOops(Fixed); set(obj, Fixed,function, function); return obj; }
static oop newSubr(wchar_t *name, imp_t imp, proto_t *sig)
{
oop obj= newBits(Subr);
set(obj, Subr,name, name);
set(obj, Subr,imp, imp);
set(obj, Subr,sig, sig);
set(obj, Subr,profile, 0);
return obj;
}
static oop newVariable(oop name, oop value, oop env, int index)
{
oop obj= newOops(Variable); GC_PROTECT(obj);
set(obj, Variable,name, name);
set(obj, Variable,value, value);
set(obj, Variable,env, env);
set(obj, Variable,index, newLong(index));
set(obj, Variable,type, 0); GC_UNPROTECT(obj);
return obj;
}
static oop newEnv(oop parent, int level, int offset)
{
oop obj= newOops(Env); GC_PROTECT(obj);
set(obj, Env,parent, parent);
set(obj, Env,level, newLong((nil == parent) ? 0 : getLong(get(parent, Env,level)) + level));
set(obj, Env,offset, newLong(offset));
set(obj, Env,bindings, newArray(0)); GC_UNPROTECT(obj);
return obj;
}
static oop newBaseContext(oop home, oop caller, oop env)
{
oop obj= newOops(Context); GC_PROTECT(obj);
set(obj, Context,home, home);
set(obj, Context,env, env);
set(obj, Context,bindings, newArray(0)); GC_UNPROTECT(obj);
return obj;
}
static oop newContext(oop home, oop caller, oop env)
{
oop obj= nil;
//xxx fix escape analysis for nested lambdas with free variables
#if 0
if ((nil != caller) && (nil != (obj= get(caller, Context,callee)))) {
set(obj, Context,home, home);
set(obj, Context,env, env);
return obj;
}
#endif
obj= newBaseContext(home, caller, env);
if (nil != caller) set(caller, Context,callee, obj);
return obj;
}
static void dump(oop);
static void dumpln(oop);
static oop findLocalVariable(oop env, oop name)
{
oop bindings= get(env, Env,bindings);
int index= arrayLength(bindings);
while (--index >= 0) {
oop var= arrayAt(bindings, index);
if (get(var, Variable,name) == name)
return var;
}
return nil;
}
static oop findVariable(oop env, oop name)
{
while (nil != env) {
oop var= findLocalVariable(env, name);
if (nil != var) return var;
env= get(env, Env,parent);
}
return nil;
}
static oop lookup(oop env, oop name)
{
oop var= findVariable(env, name);
if (nil == var) fatal("undefined variable: %ls", get(name, Symbol,bits));
return get(var, Variable,value);
}
static oop define(oop env, oop name, oop value)
{
oop bindings= get(env, Env,bindings);
{
int index= arrayLength(bindings);
while (--index >= 0) {
oop var= arrayAt(bindings, index);
if (get(var, Variable,name) == name) {
set(var, Variable,value, value);
return var;
}
}
}
int off= getLong(get(env, Env,offset));
oop var= newVariable(name, value, env, off); GC_PROTECT(var);
arrayAppend(bindings, var); GC_UNPROTECT(var);
set(env, Env,offset, newLong(off + 1));
if (is(Expr, value) && (nil == get(value, Expr,name)))
set(value, Expr,name, name);
return var;
}
static int isGlobal(oop var)
{
oop env= get(var, Variable,env);
return (nil != env) && (0 == getLong(get(env, Env,level)));
}
static oop newBool(int b) { return b ? s_t : nil; }
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
static oop intern(wchar_t *string)
{
ssize_t lo= 0, hi= arrayLength(symbols) - 1, c= 0;
oop s= nil;
while (lo <= hi) {
size_t m= (lo + hi) / 2;
s= arrayAt(symbols, m);
c= wcscmp(string, get(s, Symbol,bits));
if (c < 0) hi= m - 1;
else if (c > 0) lo= m + 1;
else return s;
}
GC_PROTECT(s);
s= newSymbol(string);
arrayInsert(symbols, lo, s);
GC_UNPROTECT(s);
return s;
}
#include "chartab.h"
static int isPrint(int c) { return (0 <= c && c <= 127 && (CHAR_PRINT & chartab[c])) || (c >= 128); }
static int isAlpha(int c) { return (0 <= c && c <= 127 && (CHAR_ALPHA & chartab[c])) || (c >= 128); }
static int isDigit10(int c) { return (0 <= c && c <= 127 && (CHAR_DIGIT10 & chartab[c])); }
static int isDigit16(int c) { return (0 <= c && c <= 127 && (CHAR_DIGIT16 & chartab[c])); }
static int isLetter(int c) { return (0 <= c && c <= 127 && (CHAR_LETTER & chartab[c])) || (c >= 128); }
#define DONE ((oop)-4) /* cannot be a tagged immediate */
static oop currentPath= 0;
static oop currentLine= 0;
static oop currentSource= 0;
static void beginSource(wchar_t *path)
{
currentPath= newString(path);
currentLine= newLong(1);
currentSource= newPair(currentSource, nil);
set(currentSource, Pair,source, newPair(currentPath, currentLine));
}
static void advanceSource(void)
{
currentLine= newLong(getLong(currentLine) + 1);
set(currentSource, Pair,source, newPair(currentPath, currentLine));
}
static void endSource(void)
{
currentSource= get(currentSource, Pair,head);
oop src= get(currentSource, Pair,source);
currentPath= car(src);
currentLine= cdr(src);
}
static oop read(FILE *fp);
static oop readList(FILE *fp, int delim)
{
oop head= nil, tail= head, obj= nil;
GC_PROTECT(head);
GC_PROTECT(obj);
obj= read(fp);
if (obj == DONE) goto eof;
head= tail= newPairFrom(obj, nil, currentSource);
for (;;) {
obj= read(fp);
if (obj == DONE) goto eof;
if (obj == s_dot) {
obj= read(fp);
if (obj == DONE) fatal("missing item after .");
tail= set(tail, Pair,tail, obj);
obj= read(fp);
if (obj != DONE) fatal("extra item after .");
goto eof;
}
obj= newPairFrom(obj, nil, currentSource);
tail= set(tail, Pair,tail, obj);
}
eof:;
int c= getwc(fp);
if (c != delim) {
if (c < 0) fatal("EOF while reading list");
fatal("mismatched delimiter: expected '%c' found '%c'", delim, c);
}
GC_UNPROTECT(obj);
GC_UNPROTECT(head);
return head;
}
static int digitValue(wint_t c)
{
if (c >= '0' && c <= '9') return c - '0';
else if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
else if (c >= 'a' && c <= 'z') return c - 'a' + 10;
else return 0;
}
static int isHexadecimal(wint_t c)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) return 1;
else return 0;
}
static int isOctal(wint_t c)
{
return '0' <= c && c <= '7';
}
static int readChar(wint_t c, FILE *fp)
{
if ('\\' == c) {
c= getwc(fp);
switch (c) {
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case 'u': {
wint_t a= getwc(fp), b= getwc(fp), c= getwc(fp), d= getwc(fp);
return (digitValue(a) << 12) + (digitValue(b) << 8) + (digitValue(c) << 4) + digitValue(d);
}
case 'x': {
int x= 0;
if (isHexadecimal(c= getwc(fp))) {
x= digitValue(c);
if (isHexadecimal(c= getwc(fp))) {
x= x * 16 + digitValue(c);
c= getwc(fp);
}
}
ungetwc(c, fp);
return x;
}
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
int x= digitValue(c);
if (isOctal(c= getwc(fp))) {
x= x * 8 + digitValue(c);
if (isOctal(c= getwc(fp))) {
x= x * 8 + digitValue(c);
c= getwc(fp);
}
}
ungetwc(c, fp);
return x;
}
default:
if (isAlpha(c) || isDigit10(c)) fatal("illegal character escape: \\%c", c);
return c;
}
}
return c;
}
static oop read(FILE *fp)
{
for (;;) {
wint_t c= getwc(fp);
switch (c) {
case WEOF: {
return DONE;
}
case '\n': {
while ('\r' == (c= getwc(fp)));
if (c >= 0) ungetwc(c, fp);
advanceSource();
continue;
}
case '\r': {
while ('\n' == (c= getwc(fp)));
ungetwc(c, fp);
advanceSource();
continue;
}
case '\t': case ' ': {
continue;
}
case ';': {
for (;;) {
c= getwc(fp);
if (EOF == c) break;
if ('\n' == c || '\r' == c) {
ungetwc(c, fp);
break;
}
}
continue;
}
case '"': {
static struct buffer buf= BUFFER_INITIALISER;
buffer_reset(&buf);
for (;;) {
c= getwc(fp);
if ('"' == c) break;
c= readChar(c, fp);
if (EOF == c) fatal("EOF in string literal");
buffer_append(&buf, c);
}
oop obj= newString(buffer_contents(&buf));
//buffer_free(&buf);
return obj;
}
case '?': {
return newLong(readChar(getwc(fp), fp));
}
case '\'': {
oop obj= read(fp);
if (obj == DONE)
obj= s_quote;
else {
GC_PROTECT(obj);
obj= newPairFrom(obj, nil, currentSource);
obj= newPairFrom(s_quote, obj, currentSource);
GC_UNPROTECT(obj);
}
return obj;
}
case '`': {
oop obj= read(fp);
if (obj == DONE)
obj= s_quasiquote;
else {
GC_PROTECT(obj);
obj= newPairFrom(obj, nil, currentSource);
obj= newPairFrom(s_quasiquote, obj, currentSource);
GC_UNPROTECT(obj);
}
return obj;
}
case ',': {
oop sym= s_unquote;
c= getwc(fp);
if ('@' == c) sym= s_unquote_splicing;
else ungetwc(c, fp);
oop obj= read(fp);
if (obj == DONE)
obj= sym;
else {
GC_PROTECT(obj);
obj= newPairFrom(obj, nil, currentSource);
obj= newPairFrom(sym, obj, currentSource);
GC_UNPROTECT(obj);
}
return obj;
}
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
doDigits: {
static struct buffer buf= BUFFER_INITIALISER;
buffer_reset(&buf);
do {
buffer_append(&buf, c);
c= getwc(fp);
} while (isDigit10(c));
if (('.' == c) || ('e' == c)) {
if ('.' == c) {
do {
buffer_append(&buf, c);
c= getwc(fp);
} while (isDigit10(c));
}
if ('e' == c) {
buffer_append(&buf, c);
c= getwc(fp);
if ('-' == c) {
buffer_append(&buf, c);
c= getwc(fp);
}
while (isDigit10(c)) {
buffer_append(&buf, c);
c= getwc(fp);
}
}
ungetwc(c, fp);
oop obj= newDouble(wcstod(buffer_contents(&buf), 0));
return obj;
}
if (('x' == c) && (1 == buf.position))
do {
buffer_append(&buf, c);
c= getwc(fp);
} while (isDigit16(c));
ungetwc(c, fp);
oop obj= newLong(wcstoul(buffer_contents(&buf), 0, 0));
return obj;
}
case '(': return readList(fp, ')'); case ')': ungetwc(c, fp); return DONE;
case '[': {
oop obj= readList(fp, ']'); GC_PROTECT(obj);
obj= newPairFrom(s_bracket, obj, obj); GC_UNPROTECT(obj);
return obj;
}
case ']': ungetwc(c, fp); return DONE;
case '{': {
oop obj= readList(fp, '}'); GC_PROTECT(obj);
obj= newPairFrom(s_brace, obj, obj); GC_UNPROTECT(obj);
return obj;
}
case '}': ungetwc(c, fp); return DONE;
case '-': {
wint_t d= getwc(fp);
ungetwc(d, fp);
if (isDigit10(d)) goto doDigits;
/* fall through... */
}
default: {
if (isLetter(c)) {
static struct buffer buf= BUFFER_INITIALISER;
oop obj= nil; GC_PROTECT(obj);
//oop in= nil; GC_PROTECT(in);
buffer_reset(&buf);
while (isLetter(c) || isDigit10(c)) {
// if (('.' == c) && buf.position) {
// c= getwc(fp);
// if (!isLetter(c) && !isDigit10(c)) {
// ungetwc(c, fp);
// c= '.';
// }
// else {
// obj= intern(buffer_contents(&buf));
// in= newPair(obj, in);
// buffer_reset(&buf);
// }
// }
buffer_append(&buf, c);
c= getwc(fp);
}
ungetwc(c, fp);
obj= intern(buffer_contents(&buf));
// while (nil != in) {
// obj= newPair(obj, nil);
// obj= newPair(getHead(in), obj);
// obj= newPair(s_in, obj);
// in= getTail(in);
// }
//GC_UNPROTECT(in);
GC_UNPROTECT(obj);
return obj;
}
fatal(isPrint(c) ? "illegal character: 0x%02x '%c'" : "illegal character: 0x%02x", c, c);
}
}
}
}
static void doprint(FILE *stream, oop obj, int storing)
{
if (!obj) {
fprintf(stream, "()");
return;
}
if (obj == get(globals, Variable,value)) {
fprintf(stream, "<globals>");
return;
}
switch (getType(obj)) {
case Undefined: fprintf(stream, "UNDEFINED"); break;
case Data: {
//int i, j= GC_size(obj);
fprintf(stream, "<data[%i]", (int)GC_size(obj));
//for (i= 0; i < j; ++i) fprintf(stream, " %02x", ((unsigned char *)obj)[i]);
fprintf(stream, ">");
break;
}
case Long: fprintf(stream, "%ld", getLong(obj)); break;
case Double: fprintf(stream, "%lf", getDouble(obj)); break;
case String: {
if (!storing)
fprintf(stream, "%ls", get(obj, String,bits));
else {
wchar_t *p= get(obj, String,bits);
int c;
putc('"', stream);
while ((c= *p++)) {
if (c >= ' ')
switch (c) {
case '"': printf("\\\""); break;
case '\\': printf("\\\\"); break;
default: putwc(c, stream); break;
}
else fprintf(stream, "\\%03o", c);
}
putc('"', stream);
}
break;
}
case Symbol: fprintf(stream, "%ls", get(obj, Symbol,bits)); break;
case Pair: {
#if 1
if (nil != get(obj, Pair,source)) {
oop source= get(obj, Pair,source);
oop path= car(source);
oop line= cdr(source);
fprintf(stream, "<%ls:%ld>", get(path, String,bits), getLong(line));
}
#endif
fprintf(stream, "(");
for (;;) {
assert(is(Pair, obj));
#if defined(NDEBUG)
if (is(Env, getHead(obj))) {
obj= getTail(obj);
if (!is(Pair, obj)) break;
continue;
}
#endif
doprint(stream, getHead(obj), storing);
obj= getTail(obj);
if (!is(Pair, obj)) break;
fprintf(stream, " ");
}
if (nil != obj) {
fprintf(stream, " . ");
doprint(stream, obj, storing);
}
fprintf(stream, ")");
break;
}
case Array: {
int i, len= arrayLength(obj);
fprintf(stream, "Array<%d>(", arrayLength(obj));
for (i= 0; i < len; ++i) {
if (i) fprintf(stream, " ");
doprint(stream, arrayAt(obj, i), storing);
}
fprintf(stream, ")");
break;
}
case Expr: {
fprintf(stream, "Expr");
if (nil != (get(obj, Expr,name))) {
fprintf(stream, ".");
doprint(stream, get(obj, Expr,name), storing);
}
fprintf(stream, "=");
doprint(stream, cadr(get(obj, Expr,defn)), storing);
break;
}
case Form: {
fprintf(stream, "Form(");
doprint(stream, get(obj, Form,function), storing);
fprintf(stream, ", ");
doprint(stream, get(obj, Form,symbol), storing);
fprintf(stream, ")");
break;
}
case Fixed: {
if (isatty(1)) {
fprintf(stream, "[1m");
doprint(stream, get(obj, Fixed,function), storing);
fprintf(stream, "[m");
}
else {
fprintf(stream, "Fixed<");
doprint(stream, get(obj, Fixed,function), storing);
fprintf(stream, ">");
}
break;
}
case Subr: {
if (get(obj, Subr,name))
fprintf(stream, "%ls", get(obj, Subr,name));
else
fprintf(stream, "Subr<%p>", get(obj, Subr,imp));
break;
}
case Variable: {
if (!isGlobal(obj) && isatty(1)) fprintf(stream, "[4m");
doprint(stream, get(obj, Variable,name), 0);
if (!isGlobal(obj) && isatty(1)) fprintf(stream, "[m");
#if !defined(NDEBUG)
oop env= get(obj, Variable,env);
if (nil != env) fprintf(stream, ";%ld+%ld", getLong(get(env, Env,level)), getLong(get(obj, Variable,index)));
#endif
break;
}
case Env: {
oop level= get(obj, Env,level);
oop offset= get(obj, Env,offset);
fprintf(stream, "Env%s%s<%ld+%ld:", ((nil == get(obj, Env,parent)) ? "*" : ""), ((nil == get(obj, Env,stable)) ? "=" : ""),
is(Long, level) ? getLong(level) : -1, is(Long, offset) ? getLong(offset) : -1);
#if 0
oop bnd= get(obj, Env,bindings);
int idx= arrayLength(bnd);
while (--idx >= 0) {
doprint(stream, arrayAt(bnd, idx), storing);
if (idx) fprintf(stream, " ");
}
#endif
fprintf(stream, ">");
break;
}
case Context: {
fprintf(stream, "Context<");
doprint(stream, get(obj, Context,env), storing);
fprintf(stream, "=");
doprint(stream, get(obj, Context,bindings), storing);
fprintf(stream, ">");
break;
}
default: {
oop name= lookup(get(globals, Variable,value), intern(L"%type-names"));
if (is(Array, name)) {
name= arrayAt(name, getType(obj));
if (is(Symbol, name)) {
fprintf(stream, "[34m%ls[m", get(name, Symbol,bits));
break;
}
}
fprintf(stream, "<type=%i>", getType(obj));
break;
}
}
}
static void print(oop obj) { doprint(stdout, obj, 0); fflush(stdout); }
static void fdump(FILE *stream, oop obj) { doprint(stream, obj, 1); fflush(stream); }
static void dump(oop obj) { fdump(stdout, obj); }
static void fdumpln(FILE *stream, oop obj)
{
fdump(stream, obj);
fprintf(stream, "\n");
fflush(stream);
}
static void dumpln(oop obj) { fdumpln(stdout, obj); }
static oop apply(oop fun, oop args, oop ctx);
static oop concat(oop head, oop tail)
{
if (!is(Pair, head)) return tail;
tail= concat(getTail(head), tail); GC_PROTECT(tail);
head= newPairFrom(getHead(head), tail, head); GC_UNPROTECT(tail);
return head;
}
static void setSource(oop obj, oop src)
{
if (!is(Pair, obj)) return;
if (nil == get(obj, Pair,source)) set(obj, Pair,source, src);
setSource(getHead(obj), src);
setSource(getTail(obj), src);
}