Skip to content

Commit 904811d

Browse files
committed
Add symbol patterns.
1 parent ee838f1 commit 904811d

10 files changed

Lines changed: 35 additions & 2 deletions

File tree

langs/mountebank/compile-expr.rkt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@
232232
(list (seq (Push rax))
233233
(seq)
234234
(cons x cm))]
235+
[(PSymb s)
236+
(let ((fail (gensym)))
237+
(list (seq (Lea r9 (Plus (symbol->data-label s) type-symb))
238+
(Cmp rax r9)
239+
(Jne fail))
240+
(seq (Label fail)
241+
(Add rsp (* 8 (length cm)))
242+
(Jmp next))
243+
cm))]
235244
[(PLit l)
236245
(let ((fail (gensym)))
237246
(list (seq (Cmp rax (imm->bits l))

langs/mountebank/interp-defun.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
(match p
105105
[(PWild) r]
106106
[(PVar x) (ext r x v)]
107+
[(PSymb s) (and (eq? s v) r)]
107108
[(PLit l) (and (eqv? l v) r)]
108109
[(PBox p)
109110
(match v

langs/mountebank/interp.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
(match p
104104
[(PWild) r]
105105
[(PVar x) (ext r x v)]
106+
[(PSymb s) (and (eq? s v) r)]
106107
[(PLit l) (and (eqv? l v) r)]
107108
[(PBox p)
108109
(match v

langs/mountebank/parse.rkt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@
6767
[(? char?) (PLit p)]
6868
['_ (PWild)]
6969
[(? symbol?) (PVar p)]
70+
[(list 'quote s) (PSymb s)]
7071
[(list 'quote (list))
7172
(PLit '())]
7273
[(list 'box p)
7374
(PBox (parse-pat p))]
7475
[(list 'cons p1 p2)
7576
(PCons (parse-pat p1) (parse-pat p2))]
7677
[(list 'and p1 p2)
77-
(PAnd (parse-pat p1) (parse-pat p2))]))
78+
(PAnd (parse-pat p1) (parse-pat p2))]
79+
[(cons 'list '())
80+
(PLit '())]
81+
[(cons 'list (cons p1 ps))
82+
(PCons (parse-pat p1)
83+
(parse-pat (cons 'list ps)))]))
7884

7985
;; Datum -> Datum
8086
(define (parse-datum d)

langs/mountebank/test/test-runner.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@
310310
(check-equal? (run ''foo) 'foo)
311311
(check-equal? (run '(eq? (match #t [_ "foo"]) "bar")) #f)
312312
(check-equal? (run '(eq? (match #t [_ 'foo]) 'bar)) #f)
313+
(check-equal? (run '(match 'foo ['bar #t] [_ #f])) #f)
314+
(check-equal? (run '(match 'foo ['foo #t] [_ #f])) #t)
313315

314316
;; Mountebank examples
315317
(check-equal? (run '#())

langs/mug/compile-expr.rkt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@
248248
(list (seq (Push rax))
249249
(seq)
250250
(cons x cm))]
251+
[(PSymb s)
252+
(let ((fail (gensym)))
253+
(list (seq (Lea r9 (Plus (symbol->data-label s) type-symb))
254+
(Cmp rax r9)
255+
(Jne fail))
256+
(seq (Label fail)
257+
(Add rsp (* 8 (length cm)))
258+
(Jmp next))
259+
cm))]
251260
[(PLit l)
252261
(let ((fail (gensym)))
253262
(list (seq (Cmp rax (imm->bits l))

langs/mug/interp-defun.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
(match p
113113
[(PWild) r]
114114
[(PVar x) (ext r x v)]
115+
[(PSymb s) (and (eq? s v) r)]
115116
[(PLit l) (and (eqv? l v) r)]
116117
[(PBox p)
117118
(match v

langs/mug/interp.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
(match p
112112
[(PWild) r]
113113
[(PVar x) (ext r x v)]
114+
[(PSymb s) (and (eq? s v) r)]
114115
[(PLit l) (and (eqv? l v) r)]
115116
[(PBox p)
116117
(match v

langs/mug/parse.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
[(? char?) (PLit p)]
7272
['_ (PWild)]
7373
[(? symbol?) (PVar p)]
74+
[(list 'quote s) (PSymb s)]
7475
[(list 'quote (list))
7576
(PLit '())]
7677
[(list 'box p)

langs/mug/test/test-runner.rkt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@
309309
(check-equal? (run '(eq? (symbol->string 'foo) "foo")) #f)
310310
(check-equal? (run ''foo) 'foo)
311311
(check-equal? (run '(eq? (match #t [_ "foo"]) "bar")) #f)
312-
(check-equal? (run '(eq? (match #t [_ 'foo]) 'bar)) #f))
312+
(check-equal? (run '(eq? (match #t [_ 'foo]) 'bar)) #f)
313+
(check-equal? (run '(match 'foo ['bar #t] [_ #f])) #f)
314+
(check-equal? (run '(match 'foo ['foo #t] [_ #f])) #t))
313315

314316
(define (test-runner-io run)
315317
;; Evildoer examples

0 commit comments

Comments
 (0)