-
Notifications
You must be signed in to change notification settings - Fork 1
/
case.scm
659 lines (580 loc) · 23.9 KB
/
case.scm
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
;;; case extension including pattern matching
;;;
;;; The nomenclature of r7rs is (case key ((datum...) expression...)),
;;; but I prefer "selector" to "key", "target" to "datum", and "body" to "expression".
;;;
;;; any target not a list, vector, or pattern descriptor (see below) is matched with equivalent?
;;; pattern descriptors are of the form #< whatever >
;;;
;;; #<> any expr matches
;;; #<func> expr matches if (func expr)
;;; #<label:func> expr matches as above, expr is saved under "label"
;;; #<label:> any expr matches, and is saved under "label"
;;; #<label> expr must match value saved under "label"
;;; #<...> skip exprs covered by the ellipsis
;;; #<label:...> skip as above, saved skipped exprs under "label" as a quoted list
;;; a pattern can have any number of labelled ellipses overall, but just one unnamed ellipsis, and only one ellipsis per pair or vector
;;; #<label,func:...> labelled ellipsis which matches if (func expr) -- expr is the ellipsis list
;;; label is not optional in this case
;;; #<"regexp"> pattern is a regular expression to be matched against a string
;;; #<label:"regexp"> labelled string that matches regexp
;;;
;;; lists and vectors are matched item by item, other sequences are matched directly via equivalent?
;;; if a label occurs in the result body, the expression it labelled is substituted for it
;;; the labels and case*'s matching function can be used anywhere -- see below, "match?" etc
;;;
;;; (case* x ((3.14) 'pi)) returns 'pi if x is 3.14
;;; (case* x ((#<symbol?>))) returns #t if x is a symbol
;;; (case* x (((+ 1 #<symbol?>)))) matches if x is any list of the form '(+ 1 x) or any other symbol in place of "x"
;;; (case* x (((#<symbol?> #<e1:...> (+ #<e2:...>))) (append #<e1> #<e2>))), passed '(a b c d (+ 1 2)), returns '(b c d 1 2)
(provide 'case.scm)
(unless (provided? 'windows)
(require libc.scm))
(define case*
(let ((case*-labels (lambda (label)
(let ((labels ((funclet ((funclet 'case*) 'case*-helper)) 'labels)))
(labels (symbol->string label))))) ; if ellipsis, this has been quoted by case*
(case*-match? (lambda* (matchee pattern (e (curlet)))
(let ((matcher ((funclet ((funclet 'case*) 'case*-helper)) 'handle-sequence)))
(or (equivalent? matchee pattern)
(and (or (pair? matchee)
(vector? matchee))
(begin
(fill! ((funclet ((funclet 'case*) 'case*-helper)) 'labels) #f) ; clear labels
((matcher pattern e) matchee)))))))
(case*-helper
(with-let (unlet)
(define labels (make-hash-table))
(define (ellipsis? pat)
(and (undefined? pat)
(or (equal? pat #<...>)
(let ((str (object->string pat)))
(and (char-position #\: str)
(string=? "...>" (substring str (- (length str) 4))))))))
(define (ellipsis-pair-position pos pat)
(and (pair? pat)
(if (ellipsis? (car pat))
pos
(ellipsis-pair-position (+ pos 1) (cdr pat)))))
(define (ellipsis-vector-position pat vlen)
(let loop ((pos 0))
(and (< pos vlen)
(if (ellipsis? (pat pos))
pos
(loop (+ pos 1))))))
(define (splice-out-ellipsis sel pat pos e)
(let ((sel-len (length sel))
(new-pat-len (- (length pat) 1))
(ellipsis-label (and (not (eq? (pat pos) #<...>))
(let* ((str (object->string (pat pos)))
(colon (char-position #\: str)))
(and colon
(substring str 2 colon))))))
(let ((func (and (string? ellipsis-label)
(let ((comma (char-position #\, ellipsis-label)))
(and comma
(let ((str (substring ellipsis-label (+ comma 1))))
(set! ellipsis-label (substring ellipsis-label 0 comma))
(let ((func-val (symbol->value (string->symbol str) e)))
(if (undefined? func-val)
(error 'unbound-variable "function ~S is undefined\n" func))
(if (not (procedure? func-val))
(error 'wrong-type-arg "~S is not a function\n" func))
func-val)))))))
(if (pair? pat)
(cond ((= pos 0) ; ellipsis at start of pattern
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len new-pat-len))))))
(values (list-tail sel (- sel-len new-pat-len))
(cdr pat)
(or (not func)
(func (cadr (labels ellipsis-label)))))) ; value is (quote ...) and we want the original list here
((= pos new-pat-len) ; ellipsis at end of pattern
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len pos)) pos))))
(values (copy sel (make-list pos))
(copy pat (make-list pos))
(or (not func)
(func (cadr (labels ellipsis-label))))))
(else ; ellipsis somewhere in the middle
(let ((new-pat (make-list new-pat-len))
(new-sel (make-list new-pat-len)))
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len new-pat-len)) pos))))
(copy pat new-pat 0 pos)
(copy pat (list-tail new-pat pos) (+ pos 1))
(copy sel new-sel 0 pos)
(copy sel (list-tail new-sel pos) (- sel-len pos))
(values new-sel new-pat
(or (not func)
(func (cadr (labels ellipsis-label))))))))
(cond ((= pos 0)
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len new-pat-len))))))
(values (subvector sel (max 0 (- sel-len new-pat-len)) sel-len) ; was new-pat-len (max 0 (- sel-len new-pat-len))
(subvector pat 1 (+ new-pat-len 1)) ; new-pat-len 1
(or (not func)
(func (cadr (labels ellipsis-label))))))
((= pos new-pat-len)
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len new-pat-len)) pos))))
(values (subvector sel 0 new-pat-len)
(subvector pat 0 new-pat-len)
(or (not func)
(func (cadr (labels ellipsis-label))))))
(else
(let ((new-pat (make-vector new-pat-len))
(new-sel (make-vector new-pat-len)))
(if ellipsis-label
(set! (labels ellipsis-label)
(list 'quote (copy sel (make-list (- sel-len new-pat-len)) pos))))
(copy pat new-pat 0 pos)
(copy pat (subvector new-pat pos new-pat-len) (+ pos 1)) ; (- new-pat-len pos) pos) copy: (+ pos 1))
(copy sel new-sel 0 pos)
(copy sel (subvector new-sel pos new-pat-len) (- sel-len pos))
; (- new-pat-len pos) pos) copy: (- sel-len pos))
(values new-sel new-pat
(or (not func)
(cadr (func (labels ellipsis-label))))))))))))
(define handle-regex
(let ((rg ((*libc* 'regex.make))) ; is this safe?
(local-regcomp (*libc* 'regcomp))
(local-regerror (*libc* 'regerror))
(local-regexec (*libc* 'regexec))
(local-regfree (*libc* 'regfree)))
(lambda (reg)
(lambda (x)
(and (string? x)
(let ((res (local-regcomp rg (substring reg 1 (- (length reg) 1)) 0)))
(unless (zero? res)
(error 'regex-error "~S~%" (local-regerror res rg)))
(set! res (local-regexec rg x 0 0))
(local-regfree rg)
(zero? res)))))))
(define (undefined->function undef e) ; handle the pattern descriptor ("undef") of the form #< whatever >, "e" = caller's curlet
(let* ((str1 (object->string undef))
(str1-end (- (length str1) 1)))
(if (not (char=? (str1 str1-end) #\>))
(error 'wrong-type-arg "pattern descriptor does not end in '>': ~S\n" str1))
(let ((str (substring str1 2 str1-end)))
(if (= (length str) 0) ; #<> = accept anything
(lambda (x) #t)
(let ((colon (char-position #\: str)))
(cond (colon ; #<label:...> might be #<label:> or #<label:func>
(let ((label (substring str 0 colon)) ; str is label:...
(func (substring str (+ colon 1)))) ; func might be ""
(cond ((labels label) ; see if we already have saved something under this label
(lambda (sel) ; if so, return function that will return an error
(error 'syntax-error "label ~S is defined twice: old: ~S, new: ~S~%" label (labels label) sel)))
;; otherwise the returned function needs to store the current sel-item under label in labels
((zero? (length func))
(lambda (x)
(set! (labels label) x) ; #<label:>, set label, accept anything
#t))
((char=? (func 0) #\") ; labelled regex, #<label:"regexp">
(lambda (x)
(set! (labels label) x)
(handle-regex func)))
(else ; #<label:func>
(let ((func-val (symbol->value (string->symbol func) e)))
(if (undefined? func-val)
(error 'unbound-variable "function ~S is undefined\n" func)
(if (not (procedure? func-val))
(error 'wrong-type-arg "~S is not a function\n" func)
(lambda (x) ; set label and call func
(set! (labels label) x)
(func-val x)))))))))
;; if no colon either #<label> or #<func> or #<"regexp"> -- label means match its saved expr, func = call func
((char=? (str 0) #\")
(handle-regex str))
(else ; #<label> or #<func>
(let ((saved (labels str)))
(if saved ; #<label>
(lambda (x) (equivalent? x saved))
(symbol->value (string->symbol str) e)))))))))) ; #<func> using curlet=e passed in above
(define (handle-pattern sel-item pat-item e)
(and (undefined? pat-item) ; turn #<func> into func and call it on the current selector element
(not (eq? pat-item #<undefined>))
(let ((func (undefined->function pat-item e)))
(if (undefined? func)
(error 'unbound-variable "function ~S is undefined\n" pat-item))
(if (not (procedure? func))
(error 'wrong-type-arg "~S is not a function\n" func))
(func sel-item))))
(define (handle-sequence pat e)
(lambda (sel)
;(format *stderr* "~S ~S~%" sel pat)
(and (eq? (type-of sel) (type-of pat))
(let ((func-ok #t))
(when (or (pair? pat) ; look for ellipsis
(vector? pat))
(if (pair? (cyclic-sequences pat))
(error 'wrong-type-arg "case* pattern is cyclic: ~S~%" pat))
(let ((pos (if (pair? pat)
(ellipsis-pair-position 0 pat)
(ellipsis-vector-position pat (length pat)))))
(when (and pos
(>= (length sel) (- (length pat) 1))) ; else pat without ellipsis is too long for sel
(let ((new-vars (list (splice-out-ellipsis sel pat pos e))))
(set! sel (car new-vars))
(set! pat (cadr new-vars))
(set! func-ok (caddr new-vars))))))
(and (= (length sel) (length pat)) ; march through selector and current target matching elements
func-ok
(call-with-exit
(lambda (return)
(for-each
(lambda (sel-item pat-item)
(or (equivalent? sel-item pat-item) ; items match
(and (or (pair? pat-item) ; recursive check (* (+ #<symbol?> 1) 2), pat-item: (+ #symbol?> 1)
(vector? pat-item)) ; pat-item, not sel-item here so pat-item can cover anything (a list for example)
((handle-sequence pat-item e) sel-item))
(handle-pattern sel-item pat-item e)
(return #f))) ; else give up (selector does not match target)
sel pat)
;; dotted list, check final cdr
(unless (or (not (pair? sel))
(proper-list? sel))
(let ((sel-item (list-tail sel (abs (length sel))))
(pat-item (list-tail pat (abs (length pat)))))
(return (or (equivalent? sel-item pat-item)
(handle-pattern sel-item pat-item e)))))
#t)))))))
(define (find-labelled-pattern tree)
;; walk body looking for a labelled pattern
(or (undefined? tree)
(and (pair? tree)
(or (find-labelled-pattern (car tree))
(find-labelled-pattern (cdr tree))))
(and (vector? tree)
(let vector-walker ((pos 0))
(and (< pos (length tree))
(or (undefined? (tree pos))
(and (pair? (tree pos))
(find-labelled-pattern (tree pos)))
(and (vector? (tree pos))
(vector-walker (tree pos)))
(vector-walker (+ pos 1))))))))
(define (handle-body select body return e)
(if (null? body)
(return select))
(when (find-labelled-pattern body) ; if labelled, remake the body substituting the labelled-exprs for the labels
(set! body (let pair-builder ((tree body))
(cond ((undefined? tree)
(let ((label (let ((str (object->string tree)))
(substring str 2 (- (length str) 1)))))
(or (labels label) tree)))
((pair? tree)
(cons (pair-builder (car tree))
(pair-builder (cdr tree))))
((vector? tree)
(vector (map pair-builder tree)))
(else tree)))))
;; evaluate the result (case* expands into a call on case*-helper; we need to evaluate the result expressions ourselves)
(return (eval (if (null? (cdr body))
(car body)
(if (eq? (car body) '=>)
(list (cadr body) select)
(cons 'begin body)))
e)))
;; case*-helper
(lambda (select clauses e)
(call-with-exit
(lambda (return)
(for-each
(lambda (clause) ;((target...) body...)
(let ((targets (car clause))
(body (cdr clause)))
(fill! labels #f) ; clear previous labels
(if (memq targets '(else #t)) ; (else...) or (#t...)
(return (eval (cons 'begin body) e))
(for-each
(lambda (target)
(if (or (equivalent? target select)
(and (undefined? target) ; #<...>
(not (eq? target #<undefined>))
(let ((func (undefined->function target e)))
;(format *stderr* "func: ~S~%" func)
;; (if (undefined? func) (error 'unbound-variable "function ~A is undefined\n" str))
;; not the above check because we want to be able to pass patterns as selectors! (scase37 in s7test)
;; this seems like a mistake: #<symbol?> won't work?
(and (procedure? func)
(func select))))
(and (sequence? target)
((handle-sequence target e) select)))
(handle-body select body return e)))
targets))))
clauses)))))))
;; case*
(#_macro (selector . clauses)
`(((#_funclet 'case*) 'case*-helper) ,selector ',clauses (#_curlet)))))
;;; --------------------------------------------------------------------------------
#|
;;; there are more tests in s7test.scm
(define-macro (test expr res)
`(let ((value ,expr))
(unless (equivalent? value ,res)
(format *stderr* "~S, expected: ~S, got: ~S~%" ',expr ,res value))))
(define (scase x)
(case* x
((a b) 'a-or-b)
((1 2/3 3.0) => (lambda (a) (* a 2)))
((#_pi) 1 123)
(("string1" "string2"))
((#<symbol?>) 'symbol!)
(((+ x #<symbol?>)) 'got-list)
((#(1 x 3)) 'got-vector)
(((+ #<>)) 'empty)
(((* #<x:symbol?> #<x>)) 'got-label)
(((#<> #<x:> #<x>)) 'repeated)
(((#<symbol?> #<symbol?>)) 'two)
(((#<x:> #<x>)) 'pair)
((#(#<x:> #<x>)) 'vector)
((#(#<symbol?> #<...> #<number?>)) 'vectsn)
((#(#<...> #<number?>)) 'vectstart)
((#(#<string?> #<char-whitespace?> #<...>)) 'vectstr)
(else 'oops)))
(test (scase 3.0) 6.0)
(test (scase pi) 123)
(test (scase "string1") "string1")
(test (scase "string3") 'oops)
(test (scase 'a) 'a-or-b)
(test (scase 'abc) 'symbol!)
(test (scase #()) 'oops)
(test (scase '(+ x z)) 'got-list)
(test (scase #(1 x 3)) 'got-vector)
(test (scase '(+ x 3)) 'oops)
(test (scase '(+ x)) 'empty)
(test (scase '(* z z)) 'got-label)
(test (scase '(* z x)) 'oops)
(test (scase '(+ (abs x) (abs x))) 'repeated)
(test (scase '(+ (abs x) (abs y))) 'oops)
(test (scase '(a b)) 'two)
(test (scase '(1 1)) 'pair)
(test (scase '(1 1 2)) 'oops)
(test (scase #(1 1)) 'vector)
(test (scase #(a b c 3)) 'vectsn)
(test (scase #(1 b 2)) 'vectstart)
(test (scase #("asdf" #\space +nan.0 #<eof>)) 'vectstr)
(test (scase #(a 3)) 'vectsn)
(test (scase #(1)) 'vectstart)
(test (scase #("asdf" #\space)) 'vectstr)
(test (scase #("asdf")) 'oops)
(define (scase3 x)
(let ((local-func (lambda (target) (eqv? target 1))))
(case* x
((2 3 a) 'oops)
((#<local-func>) 'yup))))
(test (scase3 2) 'oops)
(test (scase3 32) #<unspecified>)
(test (scase3 1) 'yup)
(define (ecase x)
(case* x
(((#<symbol?> #<...> #<symbol?>)) 'both-symbol)
(((#<symbol?> #<...>)) 'car-symbol)
(((#<...> #<symbol?> #<symbol?>)) 'two-symbols)
(((#<...> #<symbol?>)) 'end-symbol)
(else #f)))
(test (ecase '(a b 1)) 'car-symbol)
(test (ecase '(1 2 c)) 'end-symbol)
(test (ecase '(a 1 2 3 c)) 'both-symbol)
(test (ecase '(1 2 3 b c)) 'two-symbols)
(define (palindrome? x) ; x can be a list or a vector
(case* x
((() (#<>) #() #(#<>))
#t)
(((#<x:> #<middle:...> #<x>) #(#<x:> #<middle:...> #<x>))
(palindrome? #<middle>))
(else #f)))
(test (palindrome? '(a b a)) #t)
(test (palindrome? '(a b c a)) #f)
(test (palindrome? '(a b c b a)) #t)
(test (palindrome? #(a b a)) #t)
(test (palindrome? #(a b c a)) #f)
(test (palindrome? #(a b c b a)) #t)
(define (scase15 x)
(case* x
(((+ #<x:> #<x>)) (* 2 #<x>))
(((#<x:> #<y:>)) (list #<y> #<x>))
(else 'oops)))
(test (scase15 '(1 2)) '(2 1))
(test (scase15 '(+ 1 1)) 2)
(test (scase15 '(+ (* 2 3) (* 2 3))) 12)
(define (scase16 x)
(case* x
(((+ (* #<symbol?> 2) 3)) 0)
(else 1)))
(test (scase16 '(+ (* y 2) 3)) 0)
(test (scase16 '(+ (* y 1) 3)) 1)
(define (scase17 x)
(let ((a1 3))
(case* x
(((+ #<add1:symbol?> (* #<mul1:number?> 2))) (+ #<mul1> (* #<add1> 2)))
(else 'oops))))
(test (scase17 '(+ a1 (* 5 2))) 11)
(define (case-reverse x) ; maybe the least efficient reverse ever
(case* x
(((#<>) ()) x)
(((#<first:> #<rest:...>))
(append (case-reverse #<rest>)
(list (quote #<first>))))))
(test (case-reverse '(a b c)) '(c b a))
(test (case-reverse '(a b)) '(b a))
(define (scase19 x)
(case* x
(((#<integer?> . #<symbol?>)) 'ok)
(else #f)))
(test (scase19 (cons 1 'a)) 'ok)
(test (scase19 (list 1 'a)) #f)
(define (scase20 x)
(case* x
((#(+ (* #<symbol?> 2) 3)) 0)
(else 1)))
(test (scase20 #(+ (* y 2) 3)) 0)
(test (scase20 #(+ (* y 1) 3)) 1)
(define (scase21 x)
(let ((pair2? (lambda (p)
(= (length p) 2))))
(case* x
(((+ #<pair2?> 3)) #t)
(else #f))))
(test (scase21 '(+ (abs x) 3)) #t)
(test (scase21 '(+ (* 2 x) 3)) #f)
(define (scase22 x)
(letrec ((symbols?
(lambda (x)
(or (null? x)
(and (pair? x)
(symbol? (car x))
(symbols? (cdr x)))))))
(case* x
((#<symbols?>) #t)
(else #f))))
(display (scase22 '(+ a b c))) (newline)
(display (scase22 '(+ a b 3))) (newline)
(define (scase23 x)
(let ((numeric-op? (lambda (x)
(let ((func (symbol->value x)))
(and (signature func)
(memq (car (signature func)) '(number? complex? real? float? rational? integer? byte?)))))))
(case* x
(((#<numeric-op?> #<number?>)
(#<numeric-op?> #<number?> #<number?>)) #t)
(else #f))))
(display (scase23 '(+ 1 2))) (newline)
(display (scase23 '(floor 32.1))) (newline)
(display (scase23 '(abs))) (newline)
(define (scase24 x)
(case* x
(((+ #<rest:...>))
(+ (apply values #<rest>)))
(else 'oops)))
(display (scase24 '(+ 1 2 3))) (newline)
(display (let ((a 1) (b 2) (c 3)) (scase24 `(+ ,a ,b ,c)))) (newline)
(define (scase25 x)
(case* x
(((#<symbol?> #<ellip1:...> (+ #<ellip2:...>))) (append #<ellip1> #<ellip2>))
(else #f)))
(display (scase25 '(a b c d (+ 1 2)))) (newline)
(define (scase26 x)
(case* x
(((if (not #<test:>) (begin #<body:...>))) (cons 'unless (cons '#<test> #<body>)))
(((if (not #<test:>) #<body:>)) (cons 'unless (list '#<test> '#<body>)))
(((if #<test:> (begin #<body:...>))) (cons 'when (cons '#<test> #<body>)))
(((if #<test:> #<body:>)) (cons 'when (list '#<test> '#<body>)))))
(display (scase26 '(if (not (> i 3)) (display i)))) (newline) ; '(unless (> i 3) (display i))
(display (scase26 '(if (not (> i 3)) (begin (display i) (newline))))) (newline) ; '(unless (> i 3) (display i) (newline))
(display (scase26 '(if (> i 3) (display i)))) (newline) ; '(when (> i 3) (display i))
(display (scase26 '(if (> i 3) (begin (display i) (newline))))) (newline) ; '(when (> i 3) (display i) (newline))
(define (scase27 x)
(let ((efunc? (lambda (x)
(and (pair? x)
(number? (car x))))))
(case* x
(((#<label,efunc?:...>)) #t)
(else #f))))
(display (scase27 '(1 2 3))) (newline)
(display (scase27 '(a 2 3))) (newline)
(display (scase27 '(3))) (newline)
(display (scase27 ())) (newline)
(define (scase29 x)
(let ((match? ((funclet 'case*) 'case*-match?)))
(let ((multiplier? (lambda (x)
(or (match? x '(* 1 #<integer?>))
(match? x '(* 2 #<integer?>))))))
(case* x
(((+ #<integer?> #<multiplier?> #<integer?>)) #t)
(else #f)))))
(display (scase29 '(+ 1 (* 1 2) 3))) (newline)
(display (scase29 '(+ 1 (* 3 2) 3))) (newline)
(define (scase30 x)
(let ((match? ((funclet 'case*) 'case*-match?)))
(match? x '(+ #<symbol?> 1))))
(display (scase30 '(+ a 1))) (newline)
(display (scase30 '(+ 1 1))) (newline)
(define* (scase31 x (e (curlet)))
(let ((match? ((funclet 'case*) 'case*-match?))
(labels ((funclet 'case*) 'case*-labels)))
(and (match? x '(#<symbol?> #<ellip1:...> (+ #<ellip2:...>)))
(append (cadr (labels 'ellip1)) (cadr (labels 'ellip2))))))
(display (scase31 '(a b c d (+ 1 2)))) (newline)
(define (scase32 x)
(let ((match? ((funclet 'case*) 'case*-match?))
(labels ((funclet 'case*) 'case*-labels)))
(if (match? x '(if #<test:> (begin #<body:...>)))
(cons 'when (cons (labels 'test) (cadr (labels 'body)))))))
(display (scase32 '(if (> i 3) (begin (display i) (newline))))) (newline)
(display (scase32 '(if 32/15 (begin (display i) (newline))))) (newline)
(define (scase33 x)
(case* x
((#<"a.b">) #t)
(else #f)))
(display (scase33 "a1b")) (newline)
(display (scase33 "abc")) (newline)
(display (scase33 "a123b")) (newline)
(display (scase33 'a1b)) (newline)
(define (scase34 x)
(case* x
((#<reg:"a.b">) #<reg>)
(else #f)))
(display (scase34 "a1b")) (newline)
(define (scase35 x)
(let ((quotes? (lambda (x)
(char-position #\" x))))
(case* x
((#<"^dog">) 'dog0)
((#<"gray\|grey">) 'graey) ; basic regex so it needs \, apparently doesn't work in OSX?
((#<"h\(a\|e\)y">) 'haey)
((#<"p[ae]y">) 'paey)
((#<"b[aeiou]bble">) 'bxbble)
((#<"z\{3,6\}">) 'zzz)
((#<"\d">) 'digit)
((#<"<>">) 'brackets)
((#<quotes?>) 'quotes)
((#<"[^i*&2@]">) 'not-i)
(else #f))))
(display (scase35 "dog")) (newline)
(display (scase35 "i7+")) (newline)
(display (scase35 "gray")) (newline)
(display (scase35 "hay")) (newline)
(display (scase35 "pay")) (newline)
(display (scase35 "bubble")) (newline)
(display (scase35 "ab0d")) (newline)
(display (scase35 "+-<>-+")) (newline)
(display (scase35 "zzzz")) (newline)
(display (scase35 (string #\a #\"))) (newline)
;;; for other types:
(define (hlt x)
(case* (with-input-from-string (object->string x) read)
(((hash-table 'a #<integer?>)) 'hash-table)
(((inlet 'a #<integer?>)) 'inlet)
(else #f)))
(display (hlt (inlet 'a 1))) (newline)
(display (hlt (hash-table 'a 1))) (newline)
(append (list 'float-vector) (vector->list #r(1 2 3))): (float-vector 1.0 2.0 3.0)
|#