-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-syntax.scm
54 lines (44 loc) · 1.42 KB
/
cj-syntax.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
;;; Copyright 2013 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
;; Random syntax extensions? (Utilities.)
(require cj-match
define-macro-star)
;; Like part of mcase, but simpler and not doing the overhead of
;; calling source-code: 'predicate case'
(define-macro* (pcase expr . cases)
;; hard coded optional 'else' syntax in last case (why not just bind
;; else to #t? dunno. Isn't that an optimization? Evil?)
(let ((rcases (reverse cases)))
(letv ((cases* maybe-else-body)
(mcase (car rcases)
(`(else . `body)
(values (reverse (cdr rcases)) body))
(else
(values cases #f))))
(with-gensym
V
`(let ((,V ,expr))
(cond ,@(map
;; heh now falling back to mcase interesting..
(mcase-lambda
(`(`pred . `body)
`((,pred ,V)
,@body)))
cases*)
;; don't permit nonmatches by default
(else
,@(or maybe-else-body
`((pcase-error ,V))))))))))
(define (pcase-error val)
(error "no match for:" val))
(TEST
> (pcase "foo" (string? 'yes))
yes
> (%try-error (pcase 'foo (string? 'yes)))
#(error "no match for:" foo)
> (pcase 'foo (string? 'yes) (else 'no))
no
)