File: srtest.scm

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (148 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download | duplicates (3)
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
; Scheme 9 from Empty Space
; SYNTAX-RULES Test Suite
; By Nils M Holm, 2007-2010

(load-from-library "syntax-rules.scm")

(define Errors 0)

(define (fail expr result expected)
  (display "test failed: ")
  (write expr)
  (newline)
  (display "got result:  ")
  (write result)
  (newline)
  (display "expected:    ")
  (write expected)
  (newline)
  (set! Errors (+ 1 Errors)))

(define (test3 expr result expected)
;  (write expr) (display " => ") (write result) (newline)
  (if (not (equal? result expected))
      (fail expr result expected)))

(define-syntax (test form result)
  `(test3 ',form ,form ,result))

(define-syntax keyword
  (syntax-rules ()
    ((_) '())))
(test (keyword) '())

(define-syntax iff
  (syntax-rules (then else)
    ((_ p then t)
      (and p t))
    ((_ p then t else f)
      (cond (p t) (else f)))))
(test (iff #t then 'foo) 'foo)
(test (iff #f then 'foo) #f)
(test (iff #t then 'foo else 'bar) 'foo)
(test (iff #f then 'foo else 'bar) 'bar)
(test (iff #f then (error "error")) #f)
(test (iff #t then 'foo else (error "error")) 'foo)
(test (iff #f then (error "error") else 'bar) 'bar)

(define-syntax lett
  (syntax-rules ()
    ((_ ((v a) ...) x ...)
     ((lambda (v ...) x ...) a ...))))
(test (lett () 1) 1)
(test (lett ((a 1)) a) 1)
(test (lett ((a 1) (b 2)) (+ a b)) 3)
(test (lett ((a 1) (b 2)) 1 2 (+ a b)) 3)
(test (lett () 1 2 3) 3)

(define-syntax foo-syntax
  (syntax-rules ()
    ((_ x) x)
    ((_ x y ...) (cons x (foo-syntax y ...)))))
(test (foo-syntax 1 2 3 4 5) '(1 2 3 4 . 5))

(define-syntax bar-syntax
  (syntax-rules ()
    ((_) '())
    ((_ x ...) (list '(x x) ...))))
(test (bar-syntax) '())
(test (bar-syntax x) '((x x)))
(test (bar-syntax x y) '((x x) (y y)))
(test (bar-syntax x y z) '((x x) (y y) (z z)))

(define-syntax rev-syntax
  (syntax-rules ()
    ((_ () r) r)
    ((_ (a . d) r) (rev-syntax d (a . r)))
    ((_ x) (rev-syntax x ()))))
(test (rev-syntax ()) '())
(test (rev-syntax (2 1 cons)) '(1 . 2))
(test (rev-syntax ('bar 'foo #t if)) 'foo)

(define-syntax ell
  (syntax-rules ()
    ((_ ((a b) ...) c ...)
       (list '((b a) ...) c ...))))
(test (ell ()) '(()))
(test (ell () 0) '(() 0))
(test (ell ((1 2)) 3) '(((2 1)) 3))
(test (ell ((1 2) (3 4) (5 6)) 7) '(((2 1) (4 3) (6 5)) 7))
(test (ell ((1 2)) 3 4 5) '(((2 1)) 3 4 5))

(define-syntax false
  (syntax-rules ()
    ((_ x y ...)
       (if x (list y ...) (if #f #f)))))

(test (false #t 1 2 3) '(1 2 3))
(test (false #f 1 2 3) (if #f #f))

(define-syntax fluid-let
  (syntax-rules ()
    ((_ () expr . exprs)
      (begin expr . exprs))
    ((_ ((v1 a1) (v2 a2) ...) expr . exprs)
      (let ((outer-v v1))
        (set! v1 a1)
        (fluid-let ((v2 a2) ...)
          (let ((r (begin expr . exprs)))
            (set! v1 outer-v)
            r))))))

(test (let ((x  0)
            (y  1)
            (z  2)
            (fx #f)
            (fy #f))
        (fluid-let ((x -2)
                    (y -1))
          (set! fx x)
          (set! fy y))
        (list fx fy x y z))
      '(-2 -1 0 1 2))

(define-syntax foo-syntax
  (syntax-rules ()
    ((_ x ...)
      (list #t x ... #f))))

(test (foo-syntax) '(#t #f))
(test (foo-syntax 1) '(#t 1 #f))
(test (foo-syntax 1 2 3) '(#t 1 2 3 #f))

(define-syntax local-foo
  (syntax-rules ()
    ((_ x)
      (let ((local 0))
        x))))

(test (let ((local 1)) (local-foo local)) 1)

(cond ((zero? Errors)
        (display "Everything fine!"))
      (else
        (display Errors)
        (if (> Errors 1)
            (display " errors.")
            (display " error."))))
(display #\newline)