Skip to content

Commit eb4e9f5

Browse files
committed
Identify and fix a bug in structure pattern matching.
1 parent da768ad commit eb4e9f5

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

langs/neerdowell/compile-expr.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
(list
347347
(seq (Mov rax (Offset rax (* 8 i)))
348348
i1
349-
(Mov rax (Offset rsp (* 8 (sub1 (length cm1)))))
349+
(Mov rax (Offset rsp (* 8 (- (length cm1) (length cm)))))
350350
is)
351351
(seq f1 fs)
352352
cmn)])])]))

langs/neerdowell/test/test-runner.rkt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@
382382
'()))
383383
666)
384384

385-
;; Outlaw examples
385+
;; Neerdowell examples
386386
(check-equal? (run '(struct foo ())
387387
'(foo? (foo)))
388388
#t)
@@ -434,7 +434,43 @@
434434
'(match (bar 5)
435435
[(foo x) #f]
436436
[(bar x) x]))
437-
5))
437+
5)
438+
(check-equal? (run '(struct nil ())
439+
'(struct pair (x y))
440+
'(define (len x)
441+
(match x
442+
[(nil) 0]
443+
[(pair _ x) (add1 (len x))]))
444+
'(len (pair 1 (pair 2 (pair 3 (nil))))))
445+
3)
446+
(check-equal? (run '(match (cons (cons 1 2) '())
447+
[(cons (cons x y) '()) y]))
448+
2)
449+
(check-equal? (run '(struct foo (p q))
450+
'(match (cons (foo 1 2) '())
451+
[(cons (foo x y) _) y]))
452+
2)
453+
(check-equal? (run '(struct foo (p q))
454+
'(match (cons (foo 1 2) '())
455+
[(cons (foo x 3) _) x]
456+
[_ 9]))
457+
9)
458+
(check-equal? (run '(struct foo (x q))
459+
'(define (get z)
460+
(match z
461+
['() #f]
462+
[(cons (foo x q) y) x]))
463+
'(get (cons (foo 7 2) '())))
464+
7)
465+
(check-equal? (run '(struct posn (x y))
466+
'(define (posn-xs ps)
467+
(match ps
468+
['() '()]
469+
[(cons (posn x y) ps)
470+
(cons x (posn-xs ps))]))
471+
'(posn-xs (cons (posn 3 4) (cons (posn 5 6) (cons (posn 7 8) '())))))
472+
'(3 5 7)))
473+
438474

439475
(define (test-runner-io run)
440476
;; Evildoer examples

langs/outlaw/compile-expr.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
(list
401401
(seq (Mov rax (Offset rax (*8 i)))
402402
i1
403-
(Mov rax (Offset rsp (*8 (sub1 (length cm1)))))
403+
(Mov rax (Offset rsp (*8 (- (length cm1) (length cm)))))
404404
is)
405405
(seq f1 fs)
406406
cmn)])])]))

langs/outlaw/test/test-runner.rkt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,47 @@
428428
(check-equal? (run '(struct foo (x))
429429
'(foo-x #t))
430430
'err)
431+
(check-equal? (run '(struct foo (x))
432+
'(struct bar (y))
433+
'(match (bar 5)
434+
[(foo x) #f]
435+
[(bar x) x]))
436+
5)
437+
(check-equal? (run '(struct nil ())
438+
'(struct pair (x y))
439+
'(define (len x)
440+
(match x
441+
[(nil) 0]
442+
[(pair _ x) (add1 (len x))]))
443+
'(len (pair 1 (pair 2 (pair 3 (nil))))))
444+
3)
445+
(check-equal? (run '(match (cons (cons 1 2) '())
446+
[(cons (cons x y) '()) y]))
447+
2)
448+
(check-equal? (run '(struct foo (p q))
449+
'(match (cons (foo 1 2) '())
450+
[(cons (foo x y) _) y]))
451+
2)
452+
(check-equal? (run '(struct foo (p q))
453+
'(match (cons (foo 1 2) '())
454+
[(cons (foo x 3) _) x]
455+
[_ 9]))
456+
9)
457+
(check-equal? (run '(struct foo (x q))
458+
'(define (get z)
459+
(match z
460+
['() #f]
461+
[(cons (foo x q) y) x]))
462+
'(get (cons (foo 7 2) '())))
463+
7)
464+
(check-equal? (run '(struct posn (x y))
465+
'(define (posn-xs ps)
466+
(match ps
467+
['() '()]
468+
[(cons (posn x y) ps)
469+
(cons x (posn-xs ps))]))
470+
'(posn-xs (cons (posn 3 4) (cons (posn 5 6) (cons (posn 7 8) '())))))
471+
'(3 5 7))
431472

432473
;; Outlaw examples
433474
(check-equal? (run '(+)) 0)

0 commit comments

Comments
 (0)