1
0
forked from alex-eg/sex

order types.scm by what each part reads

The lookups came after the tables built on them, type-key after the
sums filed under it, and reset-declarations! before +ops-db+.
This commit is contained in:
2026-10-19 15:35:26 +03:00
parent b5030e3824
commit 5f130f4f32

233
types.scm
View File

@@ -90,122 +90,6 @@
(define (type-of form) ((current-type-of) form))
;;; A sum's variants, in declaration order:
;;;
;;; (sum shape ((circle ((r float))) (empty ())))
;;; -> shape -> ((circle (r float)) (empty))
;;;
;;; Fields are normalized, so a caller counts them and reads their
;;; types without re-parsing the declaration.
(define +sum-db+ (make-hash-table))
(define (add-sum! name variants)
(hash-table-set! +sum-db+ (type-key name) variants))
(define (get-sum name)
(hash-table-ref/default +sum-db+ (type-key name) #f))
;;; shape/circle -> (shape . circle), for each variant of each sum
(define +constructor-db+ (make-hash-table))
(define (add-constructor! constructor sum variant)
(hash-table-set! +constructor-db+ constructor (cons sum variant)))
(define (get-constructor name)
(hash-table-ref/default +constructor-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
(and entry (cdr entry))))
;;; A spec is a parameter and a list of methods, each of which either
;;; declares a signature or also carries a default body:
;;;
;;; (spec (printable T) (fn show ((self (* const T))) void))
;;; -> printable -> (T (fn show ((self (* const T))) void))
;;;
;;; And a realization is only ever asked whether it exists -- the
;;; methods it provides land in +ops-db+ like any others.
(define +spec-db+ (make-hash-table))
(define +realize-db+ (make-hash-table))
(define (add-spec! name parameter methods)
(hash-table-set! +spec-db+ name (cons parameter methods)))
(define (get-spec name)
(hash-table-ref/default +spec-db+ name #f))
(define (spec-parameter spec) (car spec))
(define (spec-methods spec) (cdr spec))
(define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
(define (realizes? spec type)
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
;;; Both of these refuse a second declaration of the same name, which
;;; is per translation unit rather than per process -- so a process
;;; that compiles two units has to start the second one clean.
(define (reset-declarations!)
(set! +sum-db+ (make-hash-table))
(set! +constructor-db+ (make-hash-table))
(set! +ops-db+ (make-hash-table))
(set! +spec-db+ (make-hash-table))
(set! +realize-db+ (make-hash-table)))
;;; Methods of an `ops' block: (type . method) -> the qualified name
;;; the call is rewritten to.
;;;
;;; (ops vec2 (fn length ((self (* const vec2))) float ...))
;;; -> (vec2 . length) -> vec2/length
;;;
;;; The signature is not kept here; `vec2/length' is an ordinary
;;; function by the time anything asks, so +name-db+ already has it.
(define +ops-db+ (make-hash-table))
(define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(define (get-method type method)
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name)
(string->symbol
(string-append (symbol->string type) "/" (symbol->string name))))
;;; The name an `ops' block was written against: `(struct point)' and a
;;; `const' wrapper both name `point', and anything else names nothing.
(define (ops-key type)
(cond ((symbol? type) (type-key type))
((not (and (pair? type) (symbol? (car type)))) #f)
((memq (car type) '(struct union enum))
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
((memq (car type) '(const volatile)) (ops-key (cadr type)))
(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)
(hash-table-set! +name-db+ name type))
@@ -287,6 +171,123 @@
(and tag (or (get-tag-info tag) (get-type-info tag))))
info))))
;;; 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))))
;;; A sum's variants, in declaration order:
;;;
;;; (sum shape ((circle ((r float))) (empty ())))
;;; -> shape -> ((circle (r float)) (empty))
;;;
;;; Fields are normalized, so a caller counts them and reads their
;;; types without re-parsing the declaration.
(define +sum-db+ (make-hash-table))
(define (add-sum! name variants)
(hash-table-set! +sum-db+ (type-key name) variants))
(define (get-sum name)
(hash-table-ref/default +sum-db+ (type-key name) #f))
;;; shape/circle -> (shape . circle), for each variant of each sum
(define +constructor-db+ (make-hash-table))
(define (add-constructor! constructor sum variant)
(hash-table-set! +constructor-db+ constructor (cons sum variant)))
(define (get-constructor name)
(hash-table-ref/default +constructor-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
(and entry (cdr entry))))
;;; Methods of an `ops' block: (type . method) -> the qualified name
;;; the call is rewritten to.
;;;
;;; (ops vec2 (fn length ((self (* const vec2))) float ...))
;;; -> (vec2 . length) -> vec2/length
;;;
;;; The signature is not kept here; `vec2/length' is an ordinary
;;; function by the time anything asks, so +name-db+ already has it.
(define +ops-db+ (make-hash-table))
(define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(define (get-method type method)
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name)
(string->symbol
(string-append (symbol->string type) "/" (symbol->string name))))
;;; The name an `ops' block was written against: `(struct point)' and a
;;; `const' wrapper both name `point', and anything else names nothing.
(define (ops-key type)
(cond ((symbol? type) (type-key type))
((not (and (pair? type) (symbol? (car type)))) #f)
((memq (car type) '(struct union enum))
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
((memq (car type) '(const volatile)) (ops-key (cadr type)))
(else #f)))
;;; A spec is a parameter and a list of methods, each of which either
;;; declares a signature or also carries a default body:
;;;
;;; (spec (printable T) (fn show ((self (* const T))) void))
;;; -> printable -> (T (fn show ((self (* const T))) void))
;;;
;;; And a realization is only ever asked whether it exists -- the
;;; methods it provides land in +ops-db+ like any others.
(define +spec-db+ (make-hash-table))
(define +realize-db+ (make-hash-table))
(define (add-spec! name parameter methods)
(hash-table-set! +spec-db+ name (cons parameter methods)))
(define (get-spec name)
(hash-table-ref/default +spec-db+ name #f))
(define (spec-parameter spec) (car spec))
(define (spec-methods spec) (cdr spec))
(define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
(define (realizes? spec type)
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
;;; Sums, constructors, methods and specs refuse a second declaration
;;; of the same name, which is per translation unit rather than per
;;; process -- so a process that compiles two units has to start the
;;; second one clean.
(define (reset-declarations!)
(set! +sum-db+ (make-hash-table))
(set! +constructor-db+ (make-hash-table))
(set! +ops-db+ (make-hash-table))
(set! +spec-db+ (make-hash-table))
(set! +realize-db+ (make-hash-table)))
;;; Type matcher macro
;;; (type-match type
;;; (int ...)