Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64

Merged
pkulev merged 40 commits from static-dispatch into main 2026-10-19 17:07:45 +02:00
2 changed files with 30 additions and 6 deletions
Showing only changes of commit bb2f05fa36 - Show all commits

View File

@@ -561,12 +561,7 @@
;; expanded here rather than before the walk, so the macro body can
;; ask `(type-of x)' about a local the walk has already passed
((macro? form)
(let ((expansion (parameterize
((current-type-of
(lambda (queried)
(unresolve-closure-types
(expression-type queried env)))))
(macroexpand form (list)))))
(let ((expansion (expand-call form env)))
(if (and (pair? expansion) (null? (cdr expansion)))
(walk-statement (car expansion) env)
(cons walk-embed-result (walk-body expansion env)))))
@@ -671,6 +666,14 @@
(walk-parts form 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))'.
(define (resolve-wildcard form env)
(if (and (>= (length form) 3) (wildcard-type? (third form)))
@@ -1029,6 +1032,14 @@
(let ((written (car parts)))
(if (= 1 (length written)) (car written) written)))))
((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)
;; a call of no arguments is still a call
((< (length expr) 2)

View File

@@ -125,6 +125,14 @@
(test-assert "one that cannot change it takes it"
(emits? (ops-src "(var c (* const vec2) p) (var n float (.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"
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
;; 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"
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"ƛ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"
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"default:"))