type a macro call by what it expands to
A receiver, a scrutinee or the start of a path was typed before the walk had expanded it, so (.len (the a)) had no type to dispatch on and (. (the p) x) read a pointer as a value.
This commit is contained in:
23
semen.scm
23
semen.scm
@@ -561,12 +561,7 @@
|
|||||||
;; expanded here rather than before the walk, so the macro body can
|
;; expanded here rather than before the walk, so the macro body can
|
||||||
;; ask `(type-of x)' about a local the walk has already passed
|
;; ask `(type-of x)' about a local the walk has already passed
|
||||||
((macro? form)
|
((macro? form)
|
||||||
(let ((expansion (parameterize
|
(let ((expansion (expand-call form env)))
|
||||||
((current-type-of
|
|
||||||
(lambda (queried)
|
|
||||||
(unresolve-closure-types
|
|
||||||
(expression-type queried env)))))
|
|
||||||
(macroexpand form (list)))))
|
|
||||||
(if (and (pair? expansion) (null? (cdr expansion)))
|
(if (and (pair? expansion) (null? (cdr expansion)))
|
||||||
(walk-statement (car expansion) env)
|
(walk-statement (car expansion) env)
|
||||||
(cons walk-embed-result (walk-body expansion env)))))
|
(cons walk-embed-result (walk-body expansion env)))))
|
||||||
@@ -671,6 +666,14 @@
|
|||||||
(walk-parts form env))
|
(walk-parts form env))
|
||||||
env)))))))))
|
env)))))))))
|
||||||
|
|
||||||
|
;;; The forms a macro call stands for, with `(type-of x)' answered from
|
||||||
|
;;; ENV
|
||||||
|
(define (expand-call form env)
|
||||||
|
(parameterize ((current-type-of
|
||||||
|
(lambda (queried)
|
||||||
|
(unresolve-closure-types (expression-type queried env)))))
|
||||||
|
(macroexpand form (list))))
|
||||||
|
|
||||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||||
(define (resolve-wildcard form env)
|
(define (resolve-wildcard form env)
|
||||||
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
||||||
@@ -1029,6 +1032,14 @@
|
|||||||
(let ((written (car parts)))
|
(let ((written (car parts)))
|
||||||
(if (= 1 (length written)) (car written) written)))))
|
(if (= 1 (length written)) (car written) written)))))
|
||||||
((symbol? expr) (or (lookup-name env expr) (get-name-type expr)))
|
((symbol? expr) (or (lookup-name env expr) (get-name-type expr)))
|
||||||
|
;; the walk expands `(the a)' before it gets there, so its type is
|
||||||
|
;; that of what it expands to:
|
||||||
|
;;
|
||||||
|
;; (.len (the a)) (match (the s) ...) (. (the a) x)
|
||||||
|
((macro? expr)
|
||||||
|
(let ((expansion (expand-call expr env)))
|
||||||
|
(and (pair? expansion) (null? (cdr expansion))
|
||||||
|
(expression-type (car expansion) env))))
|
||||||
((not (and (list? expr) (pair? expr))) #f)
|
((not (and (list? expr) (pair? expr))) #f)
|
||||||
;; a call of no arguments is still a call
|
;; a call of no arguments is still a call
|
||||||
((< (length expr) 2)
|
((< (length expr) 2)
|
||||||
|
|||||||
@@ -125,6 +125,14 @@
|
|||||||
(test-assert "one that cannot change it takes it"
|
(test-assert "one that cannot change it takes it"
|
||||||
(emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))")
|
(emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))")
|
||||||
"vec2ːlength(c)"))
|
"vec2ːlength(c)"))
|
||||||
|
(test-assert "a macro receiver is typed by what it expands to"
|
||||||
|
(emits? (string-append "(defmacro (the a) a)"
|
||||||
|
(ops-src "(var n float (.length (the v)))"))
|
||||||
|
"vec2ːlength(&v)"))
|
||||||
|
(test-assert "and so is the start of a path"
|
||||||
|
(emits? (string-append "(defmacro (the a) a)"
|
||||||
|
(ops-src "(var q float (. (the p) x))"))
|
||||||
|
"p->x"))
|
||||||
(test-assert "an address cannot be taken of a call"
|
(test-assert "an address cannot be taken of a call"
|
||||||
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
||||||
;; the inner call has to be typed to find the outer one's receiver
|
;; the inner call has to be typed to find the outer one's receiver
|
||||||
@@ -285,6 +293,11 @@
|
|||||||
(test-assert "a path scrutinee is not copied"
|
(test-assert "a path scrutinee is not copied"
|
||||||
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||||
"ƛm0")))
|
"ƛm0")))
|
||||||
|
(test-assert "a macro scrutinee is typed by what it expands to"
|
||||||
|
(emits? (string-append "(defmacro (the a) a)"
|
||||||
|
(sum-src "(match (the s) ((circle (r)) (= out r))"
|
||||||
|
" (else (= out 0.0)))"))
|
||||||
|
"shape ƛm0 = s"))
|
||||||
(test-assert "else becomes default"
|
(test-assert "else becomes default"
|
||||||
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||||
"default:"))
|
"default:"))
|
||||||
|
|||||||
Reference in New Issue
Block a user