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:
34
types.scm
34
types.scm
@@ -100,10 +100,10 @@
|
||||
(define +sum-db+ (make-hash-table))
|
||||
|
||||
(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)
|
||||
(hash-table-ref/default +sum-db+ name #f))
|
||||
(hash-table-ref/default +sum-db+ (type-key name) #f))
|
||||
|
||||
(define (variant-fields variants name)
|
||||
(let ((entry (assq name variants)))
|
||||
@@ -130,11 +130,11 @@
|
||||
(define (spec-methods spec) (cdr spec))
|
||||
|
||||
(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)
|
||||
(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
|
||||
;;; is per translation unit rather than per process -- so a process
|
||||
@@ -156,10 +156,10 @@
|
||||
(define +ops-db+ (make-hash-table))
|
||||
|
||||
(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)
|
||||
(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
|
||||
(define (qualify-op type name)
|
||||
@@ -169,13 +169,33 @@
|
||||
;;; 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)
|
||||
(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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user