1
0
forked from alex-eg/sex

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:
2026-10-03 22:29:55 +03:00
parent 622db8eadc
commit dea36e3c02
12 changed files with 246 additions and 10 deletions

View File

@@ -82,6 +82,9 @@
((or ('fn . _)
('pub 'fn . _)
('extern 'fn . _)) (process-fn sex-form acc))
(('ops . _) (process-ops sex-form acc))
(('pub 'ops . _)
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
((or ('record . _)
('pub 'record . _)) (process-record sex-form acc))
((or ('struct . _)
@@ -259,6 +262,39 @@
(with-docstring doc processed
(append (hash-table-ref env :lambda-aux-code) acc))))))
;;; (ops vec2
;;; (pub fn length ((self (* const vec2))) float ...))
;;;
;;; is (pub fn vec2/length ...) plus a registry entry, so that
;;; (. v length) can find it later. The block itself never reaches the
;;; writer; `pub' inside it means what it means outside.
(define (process-ops form acc)
(let ((type (and (pair? (cdr form)) (cadr form))))
(unless (symbol? type)
(sex-error form "ops needs a type name" form))
(fold (lambda (method acc) (process-op type method acc))
acc
(cddr form))))
(define (process-op type form acc)
(if (comment-form? form)
(cons form acc)
(let ((core (and (pair? form) (fn-core form))))
(unless (and (pair? core) (eq? (car core) 'fn)
(pair? (cdr core)) (symbol? (second core)))
(sex-error form "ops takes fn forms" form))
(let ((name (second core)))
(when (get-method type name)
(sex-error form "method is already defined on" type name))
(let ((qualified (qualify-op type name)))
(add-method! type name qualified)
(process-fn (rename-fn form qualified) acc))))))
(define (rename-fn form name)
(if (memq (first form) '(pub extern))
(copy-form-source! form (cons (first form) (rename-fn (cdr form) name)))
(copy-form-source! form `(fn ,name ,@(cddr form)))))
(define (make-fn-env fn-form)
(let ((env (make-hash-table))
(parameters (make-hash-table)))
@@ -430,6 +466,8 @@
(cddr walked))))
walked)))
((dot-access ->) (walk-member-access form env))
(else
(let ((closure (receiver-closure-type (car form) env)))
(if closure
@@ -556,6 +594,66 @@
(define (walk-parts form env)
(copy-form-source! form (walk-body form env)))
;;; (. v length) is member access while `length' is a field of v, and a
;;; call to vec2ːlength once it is a method:
;;;
;;; (. v length) -> (vec2/length (& v))
;;; (-> p length) -> (vec2/length p)
;;; (. v x) -> untouched, v.x
;;;
;;; A field wins, so adding a method cannot quietly shadow one.
(define (walk-member-access form env)
(let* ((arrow? (eq? (first form) '->))
(object (second form))
(name (and (>= (length form) 3) (third form)))
(type (let ((written (expression-type object env)))
(if arrow? (pointer-target written) written)))
(key (and (symbol? name) (ops-key type)))
(method (and key
(not (member-path-type type (list name)))
(get-method key name))))
(if method
(copy-form-source!
form
`(,method
,(method-receiver method arrow? (walk-statement object env) form)
,@(map (lambda (argument) (walk-statement argument env))
(cdddr form))))
(convert-arguments (walk-parts form env) env))))
;;; A method taking (* Self) wants an address and one taking Self wants
;;; a value; the receiver was written as one or the other, so one of
;;; the two needs `&' or `*' put in front of it
(define (method-receiver method arrow? object form)
(let ((pointer? (let ((self (method-self-type method)))
(and (pair? self) (eq? '* (car self))))))
(cond ((eq? pointer? arrow?) object)
((not pointer?) `(* ,object))
;; `&(f())' is not C, and the method cannot be given a
;; temporary from inside an expression
((not (lvalue? object))
(sex-error form "receiver is not an lvalue; bind it to a var first"
method))
(else `(& ,object)))))
(define (lvalue? expr)
(cond ((symbol? expr) #t)
((vector? expr) #t) ; a compound literal is one
((not (pair? expr)) #f)
(else (case (car expr)
((dot-access -> ¤) #t)
((*) (= 2 (length expr)))
(else #f)))))
(define (method-self-type method)
(let ((signature (get-name-type method)))
(and (pair? signature)
(eq? 'fn (car signature))
(pair? (cdr signature))
(pair? (second signature))
(let ((self (car (second signature))))
(if (pair? self) (car self) self)))))
(define (as-closure-type type)
(cond