file methods, realizations and sums under what a typedef names

A typedef had the fields of its record but none of its methods, and a
typedef of a sum was not a sum to match. A typedef that ends in a
scalar still keeps its own, so (typedef meters float) does not lend
its methods to every float.
This commit is contained in:
2026-10-14 16:54:16 +03:00
parent bb2f05fa36
commit 224ef8fb6b
2 changed files with 64 additions and 7 deletions

View File

@@ -133,6 +133,33 @@
(emits? (string-append "(defmacro (the a) a)" (emits? (string-append "(defmacro (the a) a)"
(ops-src "(var q float (. (the p) x))")) (ops-src "(var q float (. (the p) x))"))
"p->x")) "p->x"))
(test-assert "a typedef of the type has its methods"
(emits? (string-append
"(record w ((x int)))"
"(typedef alias w)"
"(ops w (fn get ((self (* const w))) int (return (. self x))))"
"(fn f () int (var a alias #(alias : 1)) (return (.get a)))")
"wːget(&a)"))
(test-assert "and a method written on the typedef is the type's"
(emits? (string-append
"(record w ((x int)))"
"(typedef alias w)"
"(ops alias (fn get ((self (* const alias))) int (return 1)))"
"(fn f () int (var a w #(w : 1)) (return (.get a)))")
"aliasːget(&a)"))
(test-assert "a typedef of a scalar keeps its own"
(reports? (string-append
"(typedef meters float)"
"(ops meters (fn half ((self meters)) meters (return self)))"
"(fn f () void (var x float 1.0) (.half x))")
"no such method on"))
(test-assert "which a typedef of it shares"
(emits? (string-append
"(typedef meters float)"
"(typedef km meters)"
"(ops meters (fn half ((self meters)) meters (return self)))"
"(fn f () void (var x km 1.0) (var y km (.half x)))")
"metersːhalf(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
@@ -208,6 +235,9 @@
(test-assert "a spec is declared once" (test-assert "a spec is declared once"
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))" (reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
"spec is already declared")) "spec is already declared"))
(test-assert "a typedef realizes what its type does"
(reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))")
"spec is already realized for"))
(test-assert "a type realizes a spec once" (test-assert "a type realizes a spec once"
(reports? (string-append (spec-src "") "(realize (p v)" (reports? (string-append (spec-src "") "(realize (p v)"
" (fn s ((a (* const v))) void (return)))") " (fn s ((a (* const v))) void (return)))")
@@ -298,6 +328,13 @@
(sum-src "(match (the s) ((circle (r)) (= out r))" (sum-src "(match (the s) ((circle (r)) (= out r))"
" (else (= out 0.0)))")) " (else (= out 0.0)))"))
"shape ƛm0 = s")) "shape ƛm0 = s"))
(test-assert "a typedef of a sum is matched as the sum"
(emits? (string-append
"(sum shape ((circle ((r int))) (empty ())))"
"(typedef alias shape)"
"(fn f () int (var s alias (shape/circle 1))"
" (match s ((circle (r)) (return r)) ((empty ()) (return 0))))")
"case shapeǂcircle"))
(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:"))

View File

@@ -100,10 +100,10 @@
(define +sum-db+ (make-hash-table)) (define +sum-db+ (make-hash-table))
(define (add-sum! name variants) (define (add-sum! name variants)
(hash-table-set! +sum-db+ name variants)) (hash-table-set! +sum-db+ (type-key name) variants))
(define (get-sum name) (define (get-sum name)
(hash-table-ref/default +sum-db+ name #f)) (hash-table-ref/default +sum-db+ (type-key name) #f))
(define (variant-fields variants name) (define (variant-fields variants name)
(let ((entry (assq name variants))) (let ((entry (assq name variants)))
@@ -130,11 +130,11 @@
(define (spec-methods spec) (cdr spec)) (define (spec-methods spec) (cdr spec))
(define (add-realization! spec type) (define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec type) #t)) (hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle) ;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
(define (realizes? spec type) (define (realizes? spec type)
(hash-table-exists? +realize-db+ (cons spec type))) (hash-table-exists? +realize-db+ (cons spec (type-key type))))
;;; Both of these refuse a second declaration of the same name, which ;;; Both of these refuse a second declaration of the same name, which
;;; is per translation unit rather than per process -- so a process ;;; is per translation unit rather than per process -- so a process
@@ -156,10 +156,10 @@
(define +ops-db+ (make-hash-table)) (define +ops-db+ (make-hash-table))
(define (add-method! type method qualified) (define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons type method) qualified)) (hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(define (get-method type method) (define (get-method type method)
(hash-table-ref/default +ops-db+ (cons type method) #f)) (hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength ;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name) (define (qualify-op type name)
@@ -169,13 +169,33 @@
;;; The name an `ops' block was written against: `(struct point)' and a ;;; The name an `ops' block was written against: `(struct point)' and a
;;; `const' wrapper both name `point', and anything else names nothing. ;;; `const' wrapper both name `point', and anything else names nothing.
(define (ops-key type) (define (ops-key type)
(cond ((symbol? type) type) (cond ((symbol? type) (type-key type))
((not (and (pair? type) (symbol? (car type)))) #f) ((not (and (pair? type) (symbol? (car type)))) #f)
((memq (car type) '(struct union enum)) ((memq (car type) '(struct union enum))
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type))) (and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
((memq (car type) '(const volatile)) (ops-key (cadr type))) ((memq (car type) '(const volatile)) (ops-key (cadr type)))
(else #f))) (else #f)))
;;; The name methods, realizations and sums are filed under. A typedef
;;; is followed to the aggregate it names, and one that ends in anything
;;; else stops at the last typedef, so a scalar does not share them:
;;;
;;; (record v ...) (typedef alias v) alias -> v
;;; (typedef meters float) meters -> meters
;;; (typedef km meters) km -> meters
(define (type-key name)
(let follow ((name name) (seen (list)))
(let ((target (and (not (memq name seen)) (typedef-target name))))
(cond ((not target) name)
((symbol? target)
(if (typedef-target target) (follow target (cons name seen)) name))
((target-tag target))
(else name)))))
(define (typedef-target name)
(let ((info (and (symbol? name) (get-type-info name))))
(and info (eq? (car info) 'typedef) (caddr info))))
(define (add-name-type! name type) (define (add-name-type! name type)
(hash-table-set! +name-db+ name type)) (hash-table-set! +name-db+ name type))