implement ops
A method is reachable two ways, spelled out as vec2/length or through its receiver as (. v length), and both land on the same function. The registry behind the second one is why this is a form and not a defmacro: a macro expands and vanishes, while resolution, module export and the spec bounds still to come all want a declaration site they can read. The receiver is adjusted to whatever the method asks for -- & where a value meets a pointer self, * where a pointer meets a value one -- so the call reads the same whichever is in hand. A field wins over a method of the same name, so adding one cannot shadow a field in silence, and needing & on something that is not an lvalue is refused here rather than by cc: &(f()) is not C, and an expression has nowhere to put a temporary. Qualified names are spelled vec2ːlength. The compiler composes them and the user does not, so __ would collide with a hand-written vec2__length, and is reserved in C++ besides. The separator has to be a letter, C admitting letters in an identifier and not symbols, which is what ruled out § -- hence U+02D0, on the same grounds as the ƛ a lifted closure carries. A block crosses a module boundary as a block, its pub methods cut down to prototypes. Prototypes alone would say that vec2ːlength exists without saying that vec2 has a length, and the receiver needs the second thing; which is also why sex-modules now links against types.
This commit is contained in:
31
types.scm
31
types.scm
@@ -90,6 +90,37 @@
|
||||
|
||||
(define (type-of form) ((current-type-of) form))
|
||||
|
||||
;;; 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 method) qualified))
|
||||
|
||||
(define (get-method type method)
|
||||
(hash-table-ref/default +ops-db+ (cons 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)
|
||||
((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)))
|
||||
|
||||
(define (add-name-type! name type)
|
||||
(hash-table-set! +name-db+ name type))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user