match a sum through a pointer to it
A pointer was refused, where . reads through one; a match now reads through the whole chain of them to the sum.
This commit is contained in:
@@ -1018,7 +1018,7 @@
|
|||||||
;;; binders, or a variant left out with no `else' are all errors here:
|
;;; binders, or a variant left out with no `else' are all errors here:
|
||||||
;;; a missing case is the thing this form exists to catch.
|
;;; a missing case is the thing this form exists to catch.
|
||||||
(define (walk-match form env)
|
(define (walk-match form env)
|
||||||
(let* ((scrutinee (and (pair? (cdr form)) (second form)))
|
(let* ((scrutinee (and (pair? (cdr form)) (dereferenced (second form) env form)))
|
||||||
;; a comment between arms goes the way the writer already
|
;; a comment between arms goes the way the writer already
|
||||||
;; drops one between switch clauses
|
;; drops one between switch clauses
|
||||||
(arms (remove comment-form? (cddr form)))
|
(arms (remove comment-form? (cddr form)))
|
||||||
@@ -1042,6 +1042,13 @@
|
|||||||
,(match-branches name held variants arms))
|
,(match-branches name held variants arms))
|
||||||
env)))))
|
env)))))
|
||||||
|
|
||||||
|
;;; A pointer to a sum is matched as the sum, read through however many
|
||||||
|
;;; there are, as `.' reads through them: p : (* * shape) -> (* (* p))
|
||||||
|
(define (dereferenced expr env form)
|
||||||
|
(if (pointer-target (expression-type expr env))
|
||||||
|
(dereferenced (copy-form-source! form (list '* expr)) env form)
|
||||||
|
expr))
|
||||||
|
|
||||||
(define (match-binders arms)
|
(define (match-binders arms)
|
||||||
(append-map (lambda (arm)
|
(append-map (lambda (arm)
|
||||||
(if (eq? (car arm) 'else) '() (delete '_ (second (car arm)))))
|
(if (eq? (car arm) 'else) '() (delete '_ (second (car arm)))))
|
||||||
|
|||||||
@@ -504,6 +504,19 @@ ahead of its pub forms."
|
|||||||
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
|
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
|
||||||
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
||||||
"t.tag == shapeǂcircle"))
|
"t.tag == shapeǂcircle"))
|
||||||
|
;; a pointer to one was refused, where `.' reads through one
|
||||||
|
(test-assert "a pointer to a sum is matched as the sum"
|
||||||
|
(emits? (sum-src "(var p (* shape) (& s))"
|
||||||
|
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||||
|
"(*p).tag == shapeǂcircle"))
|
||||||
|
(test-assert "binding through it"
|
||||||
|
(emits? (sum-src "(var p (* shape) (& s))"
|
||||||
|
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||||
|
"float r = (*p).u.circle.r"))
|
||||||
|
(test-assert "and through a chain of them"
|
||||||
|
(emits? (sum-src "(var p (* shape) (& s)) (var pp (* * shape) (& p))"
|
||||||
|
"(match pp ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||||
|
"(*(*pp)).tag == shapeǂcircle"))
|
||||||
(test-assert "and a scrutinee that is not a sum"
|
(test-assert "and a scrutinee that is not a sum"
|
||||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||||
"match wants a sum")))
|
"match wants a sum")))
|
||||||
|
|||||||
Reference in New Issue
Block a user