-
Notifications
You must be signed in to change notification settings - Fork 5
/
0.sf
2144 lines (1714 loc) · 65.1 KB
/
0.sf
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
; #F, part 0: SFC runtime library
; SFC's predefined forms:
;
; begin define define-syntax if lambda quote
; set! syntax-lambda syntax-rules
;------------------------------------------------------------------------------
; basic syntax constructs, extended lambda
(define-syntax syntax-rule
(syntax-rules ()
[(_ pat tmpl) (syntax-rules () [(__ . pat) tmpl])]))
(define-syntax let-syntax
(syntax-rules ()
[(_ ([kw init] ...))
(begin)]
[(_ ([kw init] ...) . body)
((syntax-lambda (kw ...) . body)
init ...)]))
(define-syntax letrec-syntax
(let-syntax ([let-syntax let-syntax] [define-syntax define-syntax])
(syntax-rules ()
[(_ ([kw init] ...) . body)
(let-syntax ()
(define-syntax kw init) ... (let-syntax () . body))])))
(define-syntax lambda
(let-syntax ([old-lambda lambda])
(syntax-rules ()
[(_ args . body)
(old-lambda args (let-syntax () . body))])))
; definition forms
(define-syntax define
(let-syntax ([old-define define])
(letrec-syntax
([new-define
(syntax-rules ()
[(_ exp) (old-define exp)]
[(_ (var-or-prototype . args) . body)
(new-define var-or-prototype (lambda args . body))]
[(_ . other) (old-define . other)])])
new-define)))
(define-syntax define-inline
(syntax-rules ()
[(_ (op . ll) . body)
(define-syntax op (lambda ll . body))]
[(_ op val)
(define-syntax op val)]))
(define-syntax define-integrable
(syntax-rules ()
[(_ (op . ll) . body)
(define-syntax op
(%quote (letrec ([op (lambda ll . body)]) op)))]))
(define-syntax define-rule
(syntax-rules ()
[(_ (op . pat) . body)
(define-syntax op (syntax-rule pat . body))]))
; primitive definition helpers
(define-syntax %prim*/rev
(letrec-syntax
([loop
(syntax-rules ()
[(_ prim () args)
(%prim* prim . args)]
[(_ prim (arg . more) args)
(loop prim more (arg . args))])])
(syntax-rules ()
[(_ prim arg ...)
(loop prim (arg ...) ())])))
; binding forms
(define-syntax let
(syntax-rules ()
[(_ ([var init] ...) . body)
((lambda (var ...) . body) init ...)]
[(_ name ([var init] ...) . body)
((letrec ([name (lambda (var ...) . body)])
name)
init ...)]))
(define-syntax let*
(syntax-rules ()
[(_ () . body) (let () . body)]
[(_ ([var init] . bindings) . body)
(let ([var init]) (let* bindings . body))]))
(define-syntax letrec
(syntax-rules ()
[(_ ([var init] ...) . body)
(let () (define var init) ... (let () . body))]))
(define-syntax letrec*
(syntax-rules ()
[(_ ([var expr] ...) . body)
(let ([var #f] ...)
(set! var expr)
...
(let () . body))]))
(define-syntax rec
(syntax-rules ()
[(_ (name . args) . body)
(letrec ([name (lambda args . body)]) name)]
[(_ name expr)
(letrec ([name expr]) name)]))
; projecting function-like syntax forms
(define-syntax function
(letrec-syntax
([loop
(syntax-rules (?)
[(_ (id ...) (arg ...) () e)
(lambda (id ...) (e arg ...))]
[(_ (id ...) (arg ...) (? . more) e)
(loop (id ... i) (arg ... i) more e)]
[(_ ids (arg ...) (kw . more) e)
(loop ids (arg ... kw) more e)])])
(syntax-rules ()
[(_ (?-or-kw ...) e)
(loop () () (?-or-kw ...) e)])))
; type assertions
(define-syntax the
(syntax-rules ()
[(_ ta expr) (let ([id expr]) (ta id) id)]))
; control
(define-syntax do
(let-syntax ([do-step (syntax-rules () [(_ x) x] [(_ x y) y])])
(syntax-rules ()
[(_ ([var init step ...] ...)
[test expr ...]
command ...)
(let loop ([var init] ...)
(if test
(begin (if #f #f) expr ...)
(let ()
command ...
(loop (do-step var step ...) ...))))])))
(define-syntax cond
(syntax-rules (else =>)
[(_) (if #f #f)] ; undefined
[(_ [else . exps]) (let () . exps)]
[(_ [x] . rest) (or x (cond . rest))]
[(_ [x => proc] . rest)
(let ([tmp x]) (cond [tmp (proc tmp)] . rest))]
[(_ [x . exps] . rest)
(if x (let () . exps) (cond . rest))]))
(define-syntax and
(syntax-rules ()
[(_) #t]
[(_ test) (let () test)]
[(_ test . tests) (if test (and . tests) #f)]))
(define-syntax or
(syntax-rules ()
[(_) #f]
[(_ test) (let () test)]
[(_ test . tests) (let ([x test]) (if x x (or . tests)))]))
(define-syntax when
(syntax-rules ()
[(_ test . body) (if test (let-syntax () . body))]))
(define-syntax unless
(syntax-rules ()
[(_ test . body) (if test (if #f #f) (let-syntax () . body))]))
; varargs & continuations
(define-syntax letcc
(let-syntax ([old-letcc letcc])
(syntax-rule (var . body)
(old-letcc var (let-syntax () . body)))))
(define-syntax call-with-current-continuation
(lambda (f) (letcc k (f k))))
(define-syntax call/cc call-with-current-continuation)
(define-syntax throw
(syntax-rule (k expr ...)
(withcc (%prim "ktrap()") (k expr ...))))
(define-syntax values
(syntax-rule (expr ...)
(letcc k (throw k expr ...))))
(define-syntax receive
(syntax-rule ((var ...) expr . body)
(letcc k
(withcc
(lambda (var ...)
(withcc k (let-syntax () . body)))
expr))))
(define-syntax let-values
(syntax-rules ()
[(_ () . body)
(let () . body)]
[(_ ([formals expr] . more) . body)
(let ([thunk (lambda () expr)])
(let-values more
(receive formals (thunk) . body)))]))
(define-syntax let*-values
(syntax-rules ()
[(_ () . body)
(let () . body)]
[(_ ([formals expr] . more) . body)
(receive formals expr
(let*-values more . body))]))
;------------------------------------------------------------------------------
; scheme data types
(%definition "/* basic object representation */")
; immediate objects have 7-bit tag followed by at least 24 bits of data
; subtype bits follow lsb which is 1 in non-pointer objects
(%definition "#define isimm(o, t) (((o) & 0xff) == (((t) << 1) | 1))")
(%localdef "int getimmu(obj o, int t) {
assert(isimm(o, t));
return (int)((o >> 8) & 0xffffff);
}")
(%localdef "int getimms(obj o, int t) {
assert(isimm(o, t));
return (int)((((o >> 8) & 0xffffff) ^ 0x800000) - 0x800000);
}")
(%definition "#ifdef NDEBUG
#define getimmu(o, t) (int)(((o) >> 8) & 0xffffff)
#define getimms(o, t) (int)(((((o) >> 8) & 0xffffff) ^ 0x800000) - 0x800000)
#else
extern int getimmu(obj o, int t);
extern int getimms(obj o, int t);
#endif")
(%definition "#define mkimm(o, t) (obj)((((o) & 0xffffff) << 8) | ((t) << 1) | 1)")
; native blocks are 1-element blocks containing a native
; (non-cx) pointer as 0th element and cxtype ptr in block header
(%localdef "#ifndef NDEBUG
int isnative(obj o, cxtype_t *tp) {
return isobjptr(o) && objptr_from_obj(o)[-1] == (obj)tp;
}
void *getnative(obj o, cxtype_t *tp) {
assert(isnative(o, tp));
return (void*)(*objptr_from_obj(o));
}
#endif")
(%definition "#ifdef NDEBUG
static int isnative(obj o, cxtype_t *tp)
{ return isobjptr(o) && objptr_from_obj(o)[-1] == (obj)tp; }
#define getnative(o, t) ((void*)(*objptr_from_obj(o)))
#else
extern int isnative(obj o, cxtype_t *tp);
extern void *getnative(obj o, cxtype_t *tp);
#endif")
; tagged blocks are heap blocks with runtime int tag as 0th element
; (disjoint from closures which have a pointer as 0th element)
(%localdef "int istagged(obj o, int t) {
if (!isobjptr(o)) return 0;
else { obj h = objptr_from_obj(o)[-1];
return notaptr(h) && size_from_obj(h) >= 1
&& hblkref(o, 0) == obj_from_size(t); }
}")
(%localdef "obj cktagged(obj o, int t) {
assert(istagged(o, t));
return o;
}")
(%localdef "int taggedlen(obj o, int t) {
assert(istagged(o, t));
return hblklen(o) - 1;
}")
(%localdef "obj* taggedref(obj o, int t, int i) {
int len; assert(istagged(o, t));
len = hblklen(o);
assert(i >= 0 && i < len-1);
return &hblkref(o, i+1);
}")
(%definition "extern int istagged(obj o, int t);")
(%definition "#ifdef NDEBUG
#define cktagged(o, t) (o)
#define taggedlen(o, t) (hblklen(o)-1)
#define taggedref(o, t, i) (&hblkref(o, (i)+1))
#else
extern obj cktagged(obj o, int t);
extern int taggedlen(obj o, int t);
extern obj* taggedref(obj o, int t, int i);
#endif")
; void
; this is the value to be used where it doesn't really matter what value
; is used. Standard header supports void value, which is some immediate
; which looks funny in the debugger; it might correspond to a useful value,
; but we don't really care.
(define-inline (void) (%prim "void(0)"))
; booleans
; #f is (obj)0, #t is immediate 0 with tag 0 (singular true object)
; this layout is compatible with C conventions (0 = false, 1 = true)
; note that any obj but #f is counted as true in conditionals and that
; bool_from_obj and bool_from_bool are already defined in std prelude
(%definition "/* booleans */")
(%definition "#define TRUE_ITAG 0")
(%definition "typedef int bool_t;")
(%definition "#define is_bool_obj(o) (!((o) & ~(obj)1))")
(%definition "#define is_bool_bool(b) ((void)(b), 1)")
(%definition "#define void_from_bool(b) (void)(b)")
(%definition "#define obj_from_bool(b) ((b) ? mkimm(0, TRUE_ITAG) : 0)")
(define-syntax %const
(let-syntax ([old-%const %const])
(syntax-rules (boolean)
[(_ boolean b) (%prim ("bool(" b ")"))]
[(_ arg ...) (old-%const arg ...)])))
(define-inline (boolean? x)
(%prim "bool(is_bool_$arg)" x))
(define-inline (not x)
(%prim "bool(!bool_from_$arg)" x))
; fixnums
(%definition "/* fixnums */")
(%definition "#define FIXNUM_ITAG 1")
(%definition "typedef int fixnum_t;")
(%definition "#define is_fixnum_obj(o) (isimm(o, FIXNUM_ITAG))")
(%definition "#define is_fixnum_fixnum(i) ((void)(i), 1)")
(%definition "#define fixnum_from_obj(o) (getimms(o, FIXNUM_ITAG))")
(%definition "#define fixnum_from_fixnum(i) (i)")
(%definition "#define void_from_fixnum(i) (void)(i)")
(%definition "#define obj_from_fixnum(i) mkimm(i, FIXNUM_ITAG)")
(%definition "#define FIXNUM_MIN -8388608")
(%definition "#define FIXNUM_MAX 8388607")
(define-syntax %const
(let-syntax ([old-%const %const])
(syntax-rules (integer +)
[(_ integer 8 + digs 8) (%prim ("fixnum(" "0" digs ")"))]
[(_ integer 16 + digs 8) (%prim ("fixnum(" "0" digs ")"))]
[(_ integer 24 + digs 8) (%prim ("fixnum(" "0" digs ")"))]
[(_ integer 8 sign digs 10) (%prim ("fixnum(" #&(id->string sign) digs ")"))]
[(_ integer 16 sign digs 10) (%prim ("fixnum(" #&(id->string sign) digs ")"))]
[(_ integer 24 sign digs 10) (%prim ("fixnum(" #&(id->string sign) digs ")"))]
[(_ integer 8 + digs 16) (%prim ("fixnum(" "0x" digs ")"))]
[(_ integer 16 + digs 16) (%prim ("fixnum(" "0x" digs ")"))]
[(_ integer 24 + digs 16) (%prim ("fixnum(" "0x" digs ")"))]
[(_ arg ...) (old-%const arg ...)])))
(define-inline (fixnum? x)
(%prim "bool(is_fixnum_$arg)" x))
(define-syntax fixnum::
(syntax-rules ()
[(_ x) (define (%prim! "void(assert(is_fixnum_$arg))" x))]
[(_ x ...) (begin (fixnum:: x) ...)]))
(define-inline (fxzero? x)
(%prim "bool(fixnum_from_$arg == 0)" x))
(define-inline (fxnegative? x)
(%prim "bool(fixnum_from_$arg < 0)" x))
(define-inline (fxpositive? x)
(%prim "bool(fixnum_from_$arg > 0)" x))
(define-inline (fxeven? x)
(%prim "bool(fixnum_from_$arg % 2 == 0)" x))
(define-inline (fxodd? x)
(%prim "bool(fixnum_from_$arg % 2 != 0)" x))
(define-syntax fx+
(syntax-rules ()
[(_) (%prim "fixnum(0)")]
[(_ x) x]
[(_ x y) (%prim "fixnum(fixnum_from_$arg + fixnum_from_$arg)" x y)]
[(_ x y z ...) (fx+ x (fx+ y z ...))]))
(define-syntax fx-
(syntax-rules ()
[(_ x) (%prim "fixnum(-fixnum_from_$arg)" x)]
[(_ x y) (%prim "fixnum(fixnum_from_$arg - fixnum_from_$arg)" x y)]
[(_ x y z ...) (fx- x (fx+ y z ...))]))
(define-syntax fx*
(syntax-rules ()
[(_) (%prim "fixnum(1)")]
[(_ x) x]
[(_ x y) (%prim "fixnum(fixnum_from_$arg * fixnum_from_$arg)" x y)]
[(_ x y z ...) (fx* x (fx* y z ...))]))
(define-syntax fx/ ; aka quotient?
(syntax-rules ()
[(_ x y) (%prim "fixnum(fixnum_from_$arg / fixnum_from_$arg)" x y)]))
(define-syntax fx% ; aka remainder?
(syntax-rules ()
[(_ x y) (%prim "fixnum(fixnum_from_$arg % fixnum_from_$arg)" x y)]))
(define-syntax fxlogand
(syntax-rules ()
[(_) (%prim "fixnum(-1)")]
[(_ x) x]
[(_ x y) (%prim "fixnum(fixnum_from_$arg & fixnum_from_$arg)" x y)]
[(_ x y z ...) (fxlogand x (fxlogand y z ...))]))
(define-syntax fxlogor
(syntax-rules ()
[(_) (%prim "fixnum(0)")]
[(_ x) x]
[(_ x y) (%prim "fixnum(fixnum_from_$arg | fixnum_from_$arg)" x y)]
[(_ x y z ...) (fxlogor x (fxlogor y z ...))]))
(define-syntax fxlogxor
(syntax-rules ()
[(_) (%prim "fixnum(0)")]
[(_ x) x]
[(_ x y) (%prim "fixnum(fixnum_from_$arg ^ fixnum_from_$arg)" x y)]
[(_ x y z ...) (fxlogxor x (fxlogxor y z ...))]))
(define-syntax fxlognot
(syntax-rules ()
[(_ x) (%prim "fixnum(~fixnum_from_$arg)" y)]))
(define-syntax fxsll ; shift left, logical
(syntax-rules ()
[(_ x y) (%prim "fixnum(fixnum_from_$arg << fixnum_from_$arg)" x y)]))
(define-syntax fxsrl ; shift right, logical
(syntax-rules ()
[(_ x y) (%prim "fixnum(fixnum_from_$arg >> fixnum_from_$arg)" x y)]))
(define-syntax fx=?
(syntax-rules ()
[(_ x y) (%prim "bool(fixnum_from_$arg == fixnum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fx=? x t) (fx=? t z ...)))]))
(define-syntax fx<?
(syntax-rules ()
[(_ x y) (%prim "bool(fixnum_from_$arg < fixnum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fx<? x t) (fx<? t z ...)))]))
(define-syntax fx>?
(syntax-rules ()
[(_ x y) (%prim "bool(fixnum_from_$arg > fixnum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fx>? x t) (fx>? t z ...)))]))
(define-syntax fx<=?
(syntax-rules ()
[(_ x y) (%prim "bool(fixnum_from_$arg <= fixnum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fx<=? x t) (fx<=? t z ...)))]))
(define-syntax fx>=?
(syntax-rules ()
[(_ x y) (%prim "bool(fixnum_from_$arg >= fixnum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fx>=? x t) (fx>=? t z ...)))]))
(define-syntax fxmin
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (fx<? a b) a b))]
[(_ x y z ...) (fxmin (fxmin a b) z ...)]))
(define-syntax fxmax
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (fx>? a b) a b))]
[(_ x y z ...) (fxmax (fxmax a b) z ...)]))
(define-inline (fxabs x)
(%prim "fixnum(labs(fixnum_from_$arg))" x))
(define (fxexpt x y)
(let ex ([y y])
(cond [(fxzero? y) 1]
[(fxzero? x) 0]
[(fx<? y 0) 0] ; this is a fixnum-only operation!
[(fx=? y 1) x]
[(fxodd? y) (fx* x (ex (fx- y 1)))]
[else (let ([z (ex (fx/ y 2))]) (fx* z z))])))
; flonums
(%include <math.h>)
(%include <errno.h>)
(%definition "/* flonums */")
(%localdef "static cxtype_t cxt_flonum = { \"flonum\", free };")
(%localdef "cxtype_t *FLONUM_NTAG = &cxt_flonum;")
(%definition "extern cxtype_t *FLONUM_NTAG;")
(%definition "typedef double flonum_t;")
(%definition "#define is_flonum_obj(o) (isnative(o, FLONUM_NTAG))")
(%definition "#define is_flonum_flonum(f) ((void)(f), 1)")
(%definition "#define flonum_from_obj(o) (*(flonum_t*)getnative(o, FLONUM_NTAG))")
(%definition "#define flonum_from_flonum(l, f) (f)")
(%definition "#define void_from_flonum(l, f) (void)(f)")
(%definition "#define obj_from_flonum(l, f) hpushptr(dupflonum(f), FLONUM_NTAG, l)")
(%definition "extern flonum_t *dupflonum(flonum_t f);")
(%localdef "flonum_t *dupflonum(flonum_t f) {
flonum_t *pf = cxm_cknull(malloc(sizeof(flonum_t)), \"malloc(flonum)\");
*pf = f; return pf;
}")
(define-syntax %const
(let-syntax ([old-%const %const])
(syntax-rules (decimal e)
[(_ decimal e str)
(%prim* ("flonum($live, " str ")"))]
[(_ decimal e ms indigs frdigs es exdigs)
(%prim* ("flonum($live, " #&(id->string ms)
indigs "." frdigs "e" #&(id->string es) exdigs ")"))]
[(_ arg ...) (old-%const arg ...)])))
(define-inline (flonum? x)
(%prim "bool(is_flonum_$arg)" x))
(define-syntax flonum::
(syntax-rules ()
[(_ x) (define (%prim! "void(assert(is_flonum_$arg))" x))]
[(_ x ...) (begin (flonum:: x) ...)]))
(define-inline (flzero? x)
(%prim "bool(flonum_from_$arg == 0.0)" x))
(define-inline (flnegative? x)
(%prim "bool(flonum_from_$arg < 0.0)" x))
(define-inline (flpositive? x)
(%prim "bool(flonum_from_$arg > 0.0)" x))
(define-syntax fl+
(syntax-rules ()
[(_) (%prim* "flonum($live, 0.0)")]
[(_ x) x]
[(_ x y) (%prim* "flonum($live, flonum_from_$arg + flonum_from_$arg)" x y)]
[(_ x y z ...) (fl+ x (fl+ y z ...))]))
(define-syntax fl-
(syntax-rules ()
[(_ x) (%prim* "flonum($live, -flonum_from_$arg)" x)]
[(_ x y) (%prim* "flonum($live, flonum_from_$arg - flonum_from_$arg)" x y)]
[(_ x y z ...) (fl- x (fl+ y z ...))]))
(define-syntax fl*
(syntax-rules ()
[(_) (%prim* "flonum($live, 1.0)")]
[(_ x) x]
[(_ x y) (%prim* "flonum($live, flonum_from_$arg * flonum_from_$arg)" x y)]
[(_ x y z ...) (fl* x (fl* y z ...))]))
(define-syntax fl/
(syntax-rules ()
[(_ x) (%prim* "flonum($live, 1.0 / flonum_from_$arg)" x)]
[(_ x y) (%prim* "flonum($live, flonum_from_$arg / flonum_from_$arg)" x y)]
[(_ x y z ...) (fl/ x (fl* y z ...))]))
(define-syntax fl=?
(syntax-rules ()
[(_ x y) (%prim "bool(flonum_from_$arg == flonum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fl=? x t) (fl=? t z ...)))]))
(define-syntax fl<?
(syntax-rules ()
[(_ x y) (%prim "bool(flonum_from_$arg < flonum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fl<? x t) (fl<? t z ...)))]))
(define-syntax fl>?
(syntax-rules ()
[(_ x y) (%prim "bool(flonum_from_$arg > flonum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fl>? x t) (fl>? t z ...)))]))
(define-syntax fl<=?
(syntax-rules ()
[(_ x y) (%prim "bool(flonum_from_$arg <= flonum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fl<=? x t) (fl<=? t z ...)))]))
(define-syntax fl>=?
(syntax-rules ()
[(_ x y) (%prim "bool(flonum_from_$arg >= flonum_from_$arg)" x y)]
[(_ x y z ...) (let ([t y]) (and (fl>=? x t) (fl>=? t z ...)))]))
(define-syntax flmin
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (fl<? a b) a b))]
[(_ x y z ...) (flmin (flmin a b) z ...)]))
(define-syntax flmax
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (fl>? a b) a b))]
[(_ x y z ...) (flmax (flmax a b) z ...)]))
(define-inline (fixnum->flonum n)
(%prim* "flonum($live, (flonum_t)fixnum_from_$arg)" n))
(define-inline (flonum->fixnum x)
(%prim "fixnum((fixnum_t)flonum_from_$arg)" x))
(define-inline (flabs x)
(%prim* "flonum($live, fabs(flonum_from_$arg))" x))
(define-inline (flfloor x)
(%prim* "flonum($live, floor(flonum_from_$arg))" x))
(define-inline (flceiling x)
(%prim* "flonum($live, ceil(flonum_from_$arg))" x))
(define-inline (flsqrt x)
(%prim* "flonum($live, sqrt(flonum_from_$arg))" x))
(define-inline (flexp x)
(%prim* "flonum($live, exp(flonum_from_$arg))" x))
(define-inline (fllog x)
(%prim* "flonum($live, log(flonum_from_$arg))" x))
(define-inline (flsin x)
(%prim* "flonum($live, sin(flonum_from_$arg))" x))
(define-inline (flcos x)
(%prim* "flonum($live, cos(flonum_from_$arg))" x))
(define-inline (fltan x)
(%prim* "flonum($live, tan(flonum_from_$arg))" x))
(define-inline (flasin x)
(%prim* "flonum($live, asin(flonum_from_$arg))" x))
(define-inline (flacos x)
(%prim* "flonum($live, acos(flonum_from_$arg))" x))
(define-syntax flatan
(syntax-rules ()
[(_ x) (%prim* "flonum($live, atan(flonum_from_$arg))" x)]
[(_ y x) (%prim* "flonum($live, atan2(flonum_from_$arg, flonum_from_$arg))" y x)]))
(define-inline (flexpt x y)
(%prim* "flonum($live, pow(flonum_from_$arg, flonum_from_$arg))" x y))
(define-inline (flinteger? x)
(%prim "{ /* flinteger? */
flonum_t f = flonum_from_$arg;
$return bool(f == floor(f)); }" x))
; standard math operators are mapped to fixnum ones
(define-inline (number? x)
(or (fixnum? x) (flonum? x)))
(define-inline (real? x)
(or (fixnum? x) (flonum? x)))
(define-inline (integer? x)
(fixnum? x))
(define-syntax exact? fixnum?)
(define-syntax inexact? flonum?)
(define-inline (zero? x)
(fxzero? x))
(define-syntax +
(syntax-rules ()
[(_) (%prim "fixnum(0)")]
[(_ x) x]
[(_ x y) (fx+ x y)]
[(_ x y z ...) (+ x (+ y z ...))]))
(define-syntax -
(syntax-rules ()
[(_ x) (fx- x)]
[(_ x y) (fx- x y)]
[(_ x y z ...) (- x (+ y z ...))]))
(define-syntax *
(syntax-rules ()
[(_) (%prim "fixnum(1)")]
[(_ x) x]
[(_ x y) (fx* x y)]
[(_ x y z ...) (* x (* y z ...))]))
(define-syntax /
(syntax-rules ()
[(_ x) (/ 1 x)]
[(_ x y) (fx/ x y)]
[(_ x y z ...) (/ x (* y z ...))]))
(define-syntax =
(syntax-rules ()
[(_ x y) (fx=? x y)]
[(_ x y z ...) (let ([t y]) (and (= x t) (= t z ...)))]))
(define-syntax <
(syntax-rules ()
[(_ x y) (fx<? x y)]
[(_ x y z ...) (let ([t y]) (and (< x t) (< t z ...)))]))
(define-syntax >
(syntax-rules ()
[(_ x y) (fx>? x y)]
[(_ x y z ...) (let ([t y]) (and (> x t) (> t z ...)))]))
(define-syntax <=
(syntax-rules ()
[(_ x y) (fx<=? x y)]
[(_ x y z ...) (let ([t y]) (and (<= x t) (<= t z ...)))]))
(define-syntax >=
(syntax-rules ()
[(_ x y) (fx>=? x y)]
[(_ x y z ...) (let ([t y]) (and (>= x t) (>= t z ...)))]))
(define-syntax min
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (< a b) a b))]
[(_ x y z ...) (min (min a b) z ...)]))
(define-syntax max
(syntax-rules ()
[(_ x y) (let ([a x] [b y]) (if (> a b) a b))]
[(_ x y z ...) (max (max a b) z ...)]))
(define-inline (negative? x) (< x 0))
(define-inline (positive? x) (> x 0))
(define-inline (even? x)
(fxeven? x))
(define-inline (odd? x)
(fxodd? x))
(define-inline (abs x)
(fxabs x))
(define-inline (quotient x y)
(fx/ x y)) ;fixme: fx/ may behave as div or floor quotient
(define-inline (remainder x y)
(fx% x y)) ;fixme: fx% may behave as mod or floor remainder
(define-inline (modulo x y)
(fxmodulo x y))
(define-inline (expt x y)
(fxexpt x y))
; characters
(%include <ctype.h>)
(%definition "/* characters */")
(%definition "#define CHAR_ITAG 2")
(%definition "typedef int char_t;")
(%definition "#define is_char_obj(o) (isimm(o, CHAR_ITAG))")
(%definition "#define is_char_char(i) ((i), 1)")
(%definition "#define char_from_obj(o) (getimms(o, CHAR_ITAG))")
(%definition "#define char_from_char(i) (i)")
(%definition "#define void_from_char(i) (void)(i)")
(%definition "#define obj_from_char(i) mkimm(i, CHAR_ITAG)")
(define-syntax %const
(let-syntax ([old-%const %const])
(syntax-rules (char)
[(_ char 8 c) (%prim ("char(" c ")"))]
[(_ char cs) (%prim ("char('" cs "')"))]
[(_ arg ...) (old-%const arg ...)])))
(define-inline (char? x)
(%prim "bool(is_char_$arg)" x))
(define-inline (char=? x y)
(%prim "bool(char_from_$arg == char_from_$arg)" x y))
(define-inline (char<? x y)
(%prim "bool(char_from_$arg < char_from_$arg)" x y))
(define-inline (char>? x y)
(%prim "bool(char_from_$arg > char_from_$arg)" x y))
(define-inline (char<=? x y)
(%prim "bool(char_from_$arg <= char_from_$arg)" x y))
(define-inline (char>=? x y)
(%prim "bool(char_from_$arg >= char_from_$arg)" x y))
(define-inline (char-ci=? x y)
(%prim "bool(tolower(char_from_$arg) == tolower(char_from_$arg))" x y))
(define-inline (char-ci<? x y)
(%prim "bool(tolower(char_from_$arg) < tolower(char_from_$arg))" x y))
(define-inline (char-ci>? x y)
(%prim "bool(tolower(char_from_$arg) > tolower(char_from_$arg))" x y))
(define-inline (char-ci<=? x y)
(%prim "bool(tolower(char_from_$arg) <= tolower(char_from_$arg))" x y))
(define-inline (char-ci>=? x y)
(%prim "bool(tolower(char_from_$arg) >= tolower(char_from_$arg))" x y))
(define-inline (char-alphabetic? x)
(%prim "bool(isalpha(char_from_$arg))" x))
(define-inline (char-numeric? x)
(%prim "bool(isdigit(char_from_$arg))" x))
(define-inline (char-whitespace? x)
(%prim "bool(isspace(char_from_$arg))" x))
(define-inline (char-upper-case? x)
(%prim "bool(isupper(char_from_$arg))" x))
(define-inline (char-lower-case? x)
(%prim "bool(islower(char_from_$arg))" x))
(define-inline (char->integer x)
(%prim "fixnum((fixnum_t)char_from_$arg)" x))
(define-inline (integer->char x)
(%prim "char((char_t)fixnum_from_$arg)" x))
(define-inline (char-upcase x)
(%prim "char(toupper(char_from_$arg))" x))
(define-inline (char-downcase x)
(%prim "char(tolower(char_from_$arg))" x))
; strings
(%include <string.h>)
(%definition "/* strings */")
(%localdef "static cxtype_t cxt_string = { \"string\", free };")
(%localdef "cxtype_t *STRING_NTAG = &cxt_string;")
(%definition "extern cxtype_t *STRING_NTAG;")
(%definition "#define isstring(o) (isnative(o, STRING_NTAG))")
(%definition "#define stringdata(o) ((int*)getnative(o, STRING_NTAG))")
(%definition "#define stringlen(o) (*stringdata(o))")
(%definition "#define stringchars(o) ((char*)(stringdata(o)+1))")
(%definition "#define hpushstr(l, s) hpushptr(s, STRING_NTAG, l)")
(%localdef "char* stringref(obj o, int i) {
int *d = stringdata(o);
assert(i >= 0 && i < *d);
return ((char*)(d+1))+i;
}")
(%definition "#ifdef NDEBUG
#define stringref(o, i) (stringchars(o)+(i))
#else
extern char* stringref(obj o, int i);
#endif")
(%definition "extern int *newstring(char *s);")
(%localdef "int *newstring(char *s) {
int l, *d; assert(s); l = (int)strlen(s);
d = cxm_cknull(malloc(sizeof(int)+l+1), \"malloc(string)\");
*d = l; strcpy((char*)(d+1), s); return d;
}")
(%definition "extern int *allocstring(int n, int c);")
(%localdef "int *allocstring(int n, int c) {
int *d; char *s; assert(n+1 > 0);
d = cxm_cknull(malloc(sizeof(int)+n+1), \"malloc(string)\");
*d = n; s = (char*)(d+1); memset(s, c, n); s[n] = 0;
return d;
}")
(%definition "extern int *substring(int *d, int from, int to);")
(%localdef "int *substring(int *d0, int from, int to) {
int n = to-from, *d1; char *s0, *s1; assert(d0);
assert(0 <= from && from <= to && to <= *d0);
d1 = cxm_cknull(malloc(sizeof(int)+n+1), \"malloc(string)\");
*d1 = n; s0 = (char*)(d0+1); s1 = (char*)(d1+1);
memcpy(s1, s0+from, n); s1[n] = 0;
return d1;
}")
(%definition "extern int *stringcat(int *d0, int *d1);")
(%localdef "int *stringcat(int *d0, int *d1) {
int l0 = *d0, l1 = *d1, n = l0+l1; char *s0, *s1, *s;
int *d = cxm_cknull(malloc(sizeof(int)+n+1), \"malloc(string)\");
*d = n; s = (char*)(d+1); s0 = (char*)(d0+1); s1 = (char*)(d1+1);
memcpy(s, s0, l0); memcpy(s+l0, s1, l1); s[n] = 0;
return d;
}")
(%definition "extern int *dupstring(int *d);")
(%localdef "int *dupstring(int *d0) {
int n = *d0, *d1 = cxm_cknull(malloc(sizeof(int)+n+1), \"malloc(string)\");
memcpy(d1, d0, sizeof(int)+n+1);
return d1;
}")
(%definition "extern void stringfill(int *d, int c);")
(%localdef "void stringfill(int *d, int c) {
int l = *d, i; char *s = (char*)(d+1);
for (i = 0; i < l; ++i) s[i] = c;
}")
(%definition "extern int strcmp_ci(char *s1, char*s2);")
(%localdef "int strcmp_ci(char *s1, char *s2) {
int c1, c2, d;
do { c1 = *s1++; c2 = *s2++; d = (unsigned)tolower(c1) - (unsigned)tolower(c2); }
while (!d && c1 && c2);
return d;
}")
(define-syntax %const
(let-syntax ([old-%const %const])
(syntax-rules (string)
[(_ string s)
(%prim* ("obj(hpushstr($live, newstring(\"" s "\")))"))]
[(_ string 8 c ...)
(%prim* ("{ static char s[] = { " (c ", ") ... "0 };\n"
" $return obj(hpushstr($live, newstring(s))); }"))]
[(_ arg ...) (old-%const arg ...)])))
(define-inline (string? x)
(%prim "bool(isstring(obj_from_$arg))" x))
(define-syntax make-string
(syntax-rules ()
[(_ k) (%prim* "obj(hpushstr($live, allocstring(fixnum_from_$arg, '?')))" k)]
[(_ k c) (%prim* "obj(hpushstr($live, allocstring(fixnum_from_$arg, char_from_$arg)))" k c)]))
(define-rule (string c ...)
(%prim* "{ /* string */
obj o = hpushstr($live, allocstring($argc, ' '));
unsigned char *s = (unsigned char *)stringchars(o);
${*s++ = char_from_$arg;
$}$return obj(o); }" c ...))
(define-inline (string-length s)
(%prim "fixnum(stringlen(obj_from_$arg))" s))