ref: c2d0420274fb50473a9c9246b8fd5ed3d5462d41 scheme-vm/lib/scheme/base.scm -rw-r--r-- 18.6 KiB View raw
                                                                                
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
(define-library (scheme base)
  (export
   +
   -
   *
   /
   >
   >=
   <
   <=
   =
   abs
   and
   append
   apply
   begin
   boolean?
   boolean=?
   call-with-current-continuation
   call/cc
   car
   caar cadr cdar cddr
   caaar caadr cadar caddr cdaar cdadr cddar cdddr
   caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
   case
   cdr
   char->integer
   char-alphabetic? char-numeric? char-whitespace?
   char-ci<=? char-ci<? char-ci=? char-ci>=? char-ci>?
   char-downcase char-upcase
   char-lower-case? char-upper-case?
   char<=? char<? char=? char>=? char>? char?
   cond
   cons
   define
   define-syntax
   do
   empty?
   eq?
   equal?
   eqv?
   even?
   if
   integer?
   integer->char
   lambda
   last
   length
   let
   let*
   let-syntax
   letrec-syntax
   letrec
   letrec*
   list
   list?
   list->string
   make-list
   map
   max
   memq
   memv
   min
   modulo
   negative?
   newline
   not
   null?
   number?
   number->string
   odd?
   or
   pair?
   positive?
   quasiquote
   quote
   reverse
   set!
   set-car!
   set-cdr!
   string
   string=?
   string?
   string-length
   string-ref
   string->list
   string->number
   string->symbol
   string-append
   symbol?
   symbol=?
   symbol->string
   unless
   when
   write-string
   zero?)

  (begin
    (--define-native + base_+)
    (--define-native - base_-)
    (--define-native * base_*)
    (--define-native / base_/)
    (--define-native > base_>)
    (--define-native < base_<)
    (--define-native append base_append)
    (--define-native apply base_apply)
    (--define-native call-with-current-continuation base_call_cc)
    (--define-native call/cc base_call_cc)
    (--define-native car-macro base_car)
    (--define-native cdr-macro base_cdr)
    (--define-native char? base_char?)
    (--define-native char->integer base_char_to_integer)
    (--define-native cons base_cons)
    (--define-native define base_define)
    (--define-native define-syntax base_define_syntax)
    (--define-native empty-macro base_null?)
    (--define-native eq? base_eq?)
    (--define-native if base_if)
    (--define-native integer? base_integer?)
    (--define-native integer->char base_integer_to_char)
    (--define-native lambda base_lambda)
    (--define-native let-syntax base_let_syntax)
    (--define-native letrec-syntax base_letrec_syntax)
    (--define-native list base_list)
    (--define-native list->string base_list_to_string)
    (--define-native modulo base_modulo)
    (--define-native null? base_null?)
    (--define-native pair? base_pair?)
    (--define-native quasiquote base_quasiquote)
    (--define-native quote base_quote)
    (--define-native set! base_set!)
    (--define-native set-car! base_set_car!)
    (--define-native set-cdr! base_set_cdr!)
    (--define-native string base_string)
    (--define-native string? base_string?)
    (--define-native string-length base_string_length)
    (--define-native string-ref base_string_ref)
    (--define-native string->symbol base_string_to_symbol)
    (--define-native symbol? base_symbol?)
    (--define-native symbol->string base_symbol_to_string)

    (define-syntax begin
      (syntax-rules ()
        ((begin exp ...)
        ((lambda () exp ...)))))

    (define-syntax let
      (syntax-rules ()
        ((let ((name val) ...) body1 body2 ...)
        ((lambda (name ...) body1 body2 ...)
          val ...))
        ((let tag ((name val) ...) body1 body2 ...)
        ((letrec ((tag (lambda (name ...)
                          body1 body2 ...)))
            tag)
          val ...))))

    (define-syntax let*
      (syntax-rules ()
        ((let* () body1 body2 ...)
         (let () body1 body2 ...))
        ((let* ((name1 val1) (name2 val2) ...)
           body1 body2 ...)
        (let ((name1 val1))
          (let* ((name2 val2) ...)
            body1 body2 ...)))))

    (define-syntax letrec
      (syntax-rules ()
        ((letrec ((var1 init1) ...) body ...)
        (letrec "generate temp names"
          (var1 ...)
          ()
          ((var1 init1) ...)
          body ...))
        ((letrec "generate temp names"
          ()
          (temp1 ...)
          ((var1 init1) ...)
          body ...)
        (let ((var1 (if #f #f)) ...)
          (let ((temp1 init1) ...)
            (set! var1 temp1)
            ...
            body ...)))
        ((letrec "generate temp names"
          (x y ...)
          (temp ...)
          ((var1 init1) ...)
          body ...)
        (letrec "generate temp names"
          (y ...)
          (newtemp temp ...)
          ((var1 init1) ...)
          body ...))))

    (define-syntax letrec*
      (syntax-rules ()
        ((letrec* ((var1 init1) ...) body1 body2 ...)
        (let ((var1 (if #f #f)) ...)
          (set! var1 init1)
          ...
          (let () body1 body2 ...)))))

    (define-syntax do
      (syntax-rules ()
        ((do ((var init step ...) ...)
            (test expr ...)
          command ...)
        (letrec
            ((loop
              (lambda (var ...)
                (if test
                    (begin
                      (if #f #f)
                      expr ...)
                    (begin
                      command
                      ...
                      (loop (do "step" var step ...)
                            ...))))))
          (loop init ...)))
        ((do "step" x)
        x)
        ((do "step" x y)
        y)))

    (define-syntax and
      (syntax-rules ()
        ((and) #t)
        ((and test) test)
        ((and test1 test2 ...)
        (if test1 (and test2 ...) #f))))

    (define-syntax or
      (syntax-rules ()
        ((or) #f)
        ((or test) test)
        ((or test1 test2 ...)
          (let ((x test1))
            (if x x (or test2 ...))))))

    (define not
      (lambda (condition)
        (if condition
            #f
            #t)))

    (define-syntax =
      (syntax-rules ()
        ((=) #t)
        ((= n) #t)
        ((= n1 n2) (eq? n1 n2))
        ((= n1 n2 n3 ...)
         (and (eq? n1 n2)
              (= n2 n3 ...)))))

    ; FIXME: this seems like a dirty hack
    (define (car list)
      (car-macro list))

    ; FIXME: this seems like a dirty hack
    (define (cdr list)
      (cdr-macro list))

    ; FIXME: this seems like a dirty hack
    (define (empty? list)
      (empty-macro list))

    (define (>= a b)
      (or (= a b) (> a b)))

    (define (<= a b)
      (or (= a b) (< a b)))

    (define (abs n)
      (if (< n 0)
        (* n -1)
        n))

    (define (eqv? a b)
      (eq? a b))

    (define (char-downcase c)
      (if (char? c)
        (let ((i (char->integer c)))
          (if (and (>= i 65) (<= i 90))
            (integer->char (+ i 32))
            c))))

    (define (char-upcase c)
      (if (char? c)
        (let ((i (char->integer c)))
          (if (and (>= i 97) (<= i 122))
            (integer->char (- i 32))
            c))))

    (define (memq obj list)
      (if (empty? list)
          #f
          (if (eq? obj (car list))
              list
              (memq obj (cdr list)))))

    (define (memv obj list)
      (if (empty? list)
          #f
          (if (eqv? obj (car list))
              list
              (memv obj (cdr list)))))

    (define (caar l) (car (car l)))
    (define (cadr l) (car (cdr l)))
    (define (cdar l) (cdr (car l)))
    (define (cddr l) (cdr (cdr l)))

    (define (caaar l) (car (car (car l))))
    (define (caadr l) (car (car (cdr l))))
    (define (cadar l) (car (cdr (car l))))
    (define (caddr l) (car (cdr (cdr l))))
    (define (cdaar l) (cdr (car (car l))))
    (define (cdadr l) (cdr (car (cdr l))))
    (define (cddar l) (cdr (cdr (car l))))
    (define (cdddr l) (cdr (cdr (cdr l))))

    (define (caaaar l) (car (car (car (car l)))))
    (define (caaadr l) (car (car (car (cdr l)))))
    (define (caadar l) (car (car (cdr (car l)))))
    (define (caaddr l) (car (car (cdr (cdr l)))))
    (define (cadaar l) (car (cdr (car (car l)))))
    (define (cadadr l) (car (cdr (car (cdr l)))))
    (define (caddar l) (car (cdr (cdr (car l)))))
    (define (cadddr l) (car (cdr (cdr (cdr l)))))
    (define (cdaaar l) (cdr (car (car (car l)))))
    (define (cdaadr l) (cdr (car (car (cdr l)))))
    (define (cdadar l) (cdr (car (cdr (car l)))))
    (define (cdaddr l) (cdr (car (cdr (cdr l)))))
    (define (cddaar l) (cdr (cdr (car (car l)))))
    (define (cddadr l) (cdr (cdr (car (cdr l)))))
    (define (cdddar l) (cdr (cdr (cdr (car l)))))
    (define (cddddr l) (cdr (cdr (cdr (cdr l)))))

    (define-syntax case
      (syntax-rules (else =>)
        ((case (key ...)
           clauses ...)
         (let ((atom-key (key ...)))
           (case atom-key clauses ...)))
        ((case key
           (else => result))
         (result key))
        ((case key
           (else result1 result2 ...))
         (begin result1 result2 ...))
        ((case key
           ((atoms ...) result1 result2 ...))
         (if (memv key '(atoms ...))
             (begin result1 result2 ...)))
        ((case key
           ((atoms ...) => result))
         (if (memv key '(atoms ...))
             (result key)))
        ((case key
           ((atoms ...) => result)
           clause clauses ...)
         (if (memv key '(atoms ...))
             (result key)
             (case key clause clauses ...)))
        ((case key
           ((atoms ...) result1 result2 ...)
           clause clauses ...)
         (if (memv key '(atoms ...))
             (begin result1 result2 ...)
             (case key clause clauses ...)))))

    (define-syntax cond
      (syntax-rules (else =>)
        ((cond (else result1 result2 ...))
        (begin result1 result2 ...))
        ((cond (test => result))
        (let ((temp test))
          (if temp (result temp))))
        ((cond (test => result) clause1 clause2 ...)
        (let ((temp test))
          (if temp
              (result temp)
              (cond clause1 clause2 ...))))
        ((cond (test)) test)
        ((cond (test) clause1 clause2 ...)
        (let ((temp test))
          (if temp temp
              (cond clause1 clause2 ...))))
        ((cond (test result1 result2 ...))
        (if test (begin result1 result2 ...)))
        ((cond (test result1 result2 ...)
              clause1 clause2 ...)
        (if test
            (begin result1 result2 ...)
            (cond clause1 clause2 ...)))))

    (define (list? l)
      (if (empty? l)
        #t
        (if (pair? l)
          (let ((next (cdr l)))
            (or (empty? next) (list? next)))
          #f)))

    (define (length l)
      (letrec ((f (lambda (ll count)
                    (if (empty? ll)
                      count
                      (f (cdr ll) (+ 1 count))))))
        (f l 0)))

    (define (last l)
      (if (empty? l)
          (list)
          (if (= 1 (length l))
              (car l)
              (last (cdr l)))))

    (define (boolean? b)
      (or (eq? b #t) (eq? b #f)))

    (define (boolean=? a b)
      (or
        (and (eq? a #t) (eq? b #t))
        (and (eq? a #f) (eq? b #f))))

    (define (number? n)
      (or (integer? n)))

    (define (negative? n)
      (< n 0))

    (define (positive? n)
      (>= n 0))

    (define (number->string num)
      (letrec* ((i->c (lambda (i)
                       (integer->char (+ i 48))))
                (n->s (lambda (n l)
                        (if (< n 10)
                          (cons (i->c n) l)
                          (n->s (/ n 10) (cons (i->c (modulo n 10)) l)))))
                (digits (n->s (abs num) '())))
        (if (negative? num)
          (list->string (cons #\- digits))
          (list->string digits))))

    (define (string->list str)
      (letrec ((s->l (lambda (l i)
                       (if (< i 0)
                         l
                         (s->l (cons (string-ref str i) l) (- i 1))))))
        (s->l '() (- (string-length str) 1))))

    (define (string-append . strings1)
      (letrec ((s->l (lambda strings2
                       (if (= 0 (length strings2))
                         (list)
                         (append (string->list (car strings2))
                                 (apply s->l (cdr strings2)))))))
        (list->string (apply s->l strings1))))

    (--define-native write write) ; don't export this

    (define (newline)
      (write #\newline))

    (define (write-string . args)
      (if (not (empty? args))
          (begin
            (write (car args))
            (apply write-string (cdr args)))))

    (define equal? '()) ; temporary

    (define (list=? list1 list2) ; don't export this
      (letrec ((l=? (lambda (l1 l2)
                      (or (empty? l1)
                          (and (equal? (car l1) (car l2))
                               (l=? (cdr l1) (cdr l2)))))))
        (and (= (length list1) (length list2))
             (l=? list1 list2))))

    (define (string=? . strings)
      (if (< (length strings) 2)
        #t
        (let ((s1 (car strings))
              (s2 (cadr strings)))
          (if (and
                (string? s1)
                (string? s2)
                (list=? (string->list s1) (string->list s2)))
            (apply string=? (cdr strings))
            #f))))

    (define (pair=? a b) ; don't export this
      (if (equal? (car a) (car b))
        (equal? (cdr a) (cdr b))
        #f))

    (define (char=? a b)
      (if (and (char? a) (char? b))
        (eq? a b)
        #f))

    (define (char<? a b)
      (if (and (char? a) (char? b))
        (< (char->integer a) (char->integer b))
        #f))

    (define (char<=? a b)
      (or (char<? a b) (char=? a b)))

    (define (char>? a b)
      (if (and (char? a) (char? b))
        (> (char->integer a) (char->integer b))
        #f))

    (define (char>=? a b)
      (or (char>? a b) (char=? a b)))

    (define map '()) ; temporary

    (define (char-ci=? . chars)
      (apply char=? (map char-downcase chars)))

    (define (char-ci<? . chars)
      (apply char<? (map char-downcase chars)))

    (define (char-ci<=? . chars)
      (apply char<=? (map char-downcase chars)))

    (define (char-ci>? . chars)
      (apply char>? (map char-downcase chars)))

    (define (char-ci>=? . chars)
      (apply char>=? (map char-downcase chars)))

    (define (char-upper-case? c)
      (if (char? c)
        (let ((i (char->integer c)))
          (and (>= i 65) (<= i 90)))))

    (define (char-lower-case? c)
      (if (char? c)
        (let ((i (char->integer c)))
          (and (>= i 97) (<= i 122)))))

    (define (char-alphabetic? c)
      (if (char? c)
        (let ((i (char->integer c)))
          (or
            (and (>= i 65) (<= i 90))
            (and (>= i 97) (<= i 122))))))

    (define (char-numeric? c)
      (if (char? c)
        (let ((i (char->integer c)))
          (and (>= i 48) (<= i 57)))))

    (define (char-whitespace? c)
      (if (char? c)
        (let ((i (char->integer c)))
          (or
            (and (>= i 9) (<= i 13))
            (= i 32)
            (= i 133)))))

    (define (equal? a b)
      (cond
       ((and (boolean? a) (boolean? b)) (eq? a b))
       ((and (char? a)    (char? b))    (eq? a b))
       ((and (number? a)  (number? b))  (eq? a b))
       ((and (list? a)    (list? b))    (list=? a b))
       ((and (pair? a)    (pair? b))    (pair=? a b))
       ((and (string? a)  (string? b))  (string=? a b))
       ((and (symbol? a)  (symbol? b))  (eq? a b))
       (else #f)))

    (define (symbol=? . symbols)
      (if (< (length symbols) 2)
        #t
        (let ((s1 (car symbols))
              (s2 (cadr symbols)))
          (if (and
                (symbol? s1)
                (symbol? s2)
                (eq? s1 s2))
            (apply symbol=? (cdr symbols))
            #f))))

    (define-syntax when
      (syntax-rules ()
        ((when test result1 result2 ...)
         (if test
             (begin result1 result2 ...)))))

    (define-syntax unless
      (syntax-rules ()
        ((unless test result1 result2 ...)
         (if (not test)
             (begin result1 result2 ...)))))

    (define (reverse l)
      (letrec ((rv (lambda (l1 l2)
                 (if (empty? l1)
                     l2
                     (rv (cdr l1) (cons (car l1) l2))))))
        (rv l '())))

    (define (zero? n)
      (= n 0))

    (define (even? n)
      (= 0 (modulo n 2)))

    (define (odd? n)
      (= 1 (modulo n 2)))

    ; FIXME: not sure if this is a standard thing, but I need it below for now...
    (define (any? fn list)
      (cond
       ((empty? list) #f)
       ((fn (car list)) #t)
       (else (any? fn (cdr list)))))

    ; don't export this -- it is only used by the real map
    (define (map-over-single-list fn l)
      (letrec ((m (lambda (l l2)
                    (if (empty? l)
                      l2
                      (m (cdr l) (cons (fn (car l)) l2))))))
        (reverse (m l '()))))

    (define (map fn . lists)
      (if (= 1 (length lists))
        (map-over-single-list fn (car lists))
        (letrec ((m (lambda (l l2)
                      (if (any? empty? l)
                        l2
                        (let ((cars (map-over-single-list car l))
                              (cdrs (map-over-single-list cdr l)))
                          (m cdrs (cons (apply fn cars) l2)))))))
          (reverse (m lists '())))))

    ;(define map map-over-single-list)

    (define (string->number str)
      (letrec* ((digits (map (lambda (c)
                              (- (char->integer c) 48))
                            (string->list str)))
                (d->n (lambda (d n)
                        (if (empty? d)
                          n
                          (if (and (>= (car d) 0) (<= (car d) 9))
                            (d->n (cdr d) (+ (* n 10) (car d)))
                            (d->n (cdr d) n)))))
                (result (d->n digits 0)))
               (if (= (car digits) -3)
                 (* result -1)
                 result)))

    (define (max . nums)
      (letrec ((get-max (lambda (nums biggest)
                          (if (empty? nums)
                              biggest
                              (if (> (car nums) biggest)
                                  (get-max (cdr nums) (car nums))
                                  (get-max (cdr nums) biggest))))))
        (get-max (cdr nums) (car nums))))

    (define (min . nums)
      (letrec ((get-min (lambda (nums smallest)
                          (if (empty? nums)
                              smallest
                              (if (< (car nums) smallest)
                                  (get-min (cdr nums) (car nums))
                                  (get-min (cdr nums) smallest))))))
        (get-min (cdr nums) (car nums))))

    (define (make-list count fill)
      (letrec ((m-l (lambda (c l)
                      (if (= c 0)
                        l
                        (m-l (- c 1) (cons fill l))))))
        (m-l count '())))

  ))