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

@@ -72,8 +72,8 @@ sex-macros.o: sex-macros.module.scm sex-macros.scm
reader.o: reader.module.scm reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils
@@ -109,6 +109,7 @@ SEX_TEST_PROGRAMS = c99 \
lambdas \
lists \
operators \
ops \
records \
serialize \
type-shapes \

View File

@@ -94,15 +94,23 @@ forms, and what remains."
(cons (walk-stmt (cadr cs))
(cons (walk-expr (car cs)) acc)))))))
;;; mouse-x -> mouse_x, vec2/length -> vec2ːlength.
;;;
;;; `ː' is U+02D0, a modifier letter, because C takes letters in an
;;; identifier and not symbols -- clang rejects `§' outright. Not `__',
;;; because the compiler composes these names and the user does not: a
;;; hand-written vec2__length would collide, and an identifier holding
;;; `__' is reserved in C++, which a pub header may be included from.
;;; Lifted closures carry a ƛ for the same reason.
(define (unkebabify sym)
(case sym
((-) sym)
((--) sym)
((->) sym)
((-=) sym)
((- -- -> -= / /=) sym) ; bare operators, not names
(else
(string->symbol
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")))))
(irregex-replace/all
"/"
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")
"ː")))))
(define (atom-to-fmt-c atom)
(case atom

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

View File

@@ -11,6 +11,7 @@
matchable
reader
srfi-1
types
utils)
(define +persistent-module-paths+ (list))
@@ -90,6 +91,22 @@
(('pub 'fn name args ret . _)
`(pub fn ,name ,args ,ret)))))
;;; An ops block is not pub itself -- its methods are. The block leaves
;;; as a block, its pub methods reduced to prototypes, so the importer
;;; runs its own `process-ops' over it: prototypes alone would say that
;;; vec2ːlength exists without saying that vec2 has a `length', and
;;; (. v length) needs the second thing.
(define (ops-public-interface form)
(let ((type (and (pair? (cdr form)) (cadr form)))
(methods (filter (lambda (m)
(and (pair? m) (eq? (car m) 'pub)
(pair? (cdr m)) (eq? (cadr m) 'fn)))
(cddr form))))
(and (symbol? type)
(pair? methods)
(copy-form-source! form
`(ops ,type ,@(map public-fn-interface methods))))))
;;; TODO: use semen facilities to analyze modules
(define (process-public-interface-form form acc)
(match form
@@ -102,6 +119,9 @@
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'struct 'typedef 'union) . _)
(cons (copy-form-source! form (cdr form)) acc))
(('ops . _)
(let ((exported (ops-public-interface form)))
(if exported (cons exported acc) acc)))
(('pub . _)
(sex-error form "pub must be followed by a definition" form))
(_ acc)))

View File

@@ -29,8 +29,8 @@ sex-macros.o: sex-macros.module.scm ../sex-macros.scm
reader.o: reader.module.scm ../reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm ../semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils

View File

@@ -20,7 +20,7 @@
SEXC ?= ../../sexc
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7
check:
@$(SEXC) greet.sex -c -o greet.o

View File

@@ -33,4 +33,8 @@
(var here (closure ((int)) int)
(closure ((x int)) int (base) (return (+ base x))))
(printf " %d\n" (apply-twice here 1))
;; the bare record name, then the method both ways round
(var v vec2 #(vec2 : 3 4))
(printf "vec2 %d %d\n" (. v manhattan) (vec2/manhattan (& v)))
(return 0))

View File

@@ -48,6 +48,18 @@
(pub fn apply-twice ((f (closure ((int)) int)) (x int)) int
(return (f (f x))))
;;; A record and an ops block crossing the boundary. The record has to
;;; arrive as the struct *and* its typedef, or the bare name does not
;;; work there; the ops block has to arrive as a block rather than as
;;; loose prototypes, or the importer learns that vec2ːmanhattan exists
;;; without learning that vec2 has a `manhattan' -- and (. v manhattan)
;;; needs the second thing.
(pub record vec2 ((x int) (y int)))
(ops vec2
(pub fn manhattan ((self (* const vec2))) int
(return (+ (-> self x) (-> self y)))))
;;; Not `pub': invisible to importers, and static in the generated C.
(fn unused-helper () void
(printf "private\n"))

View File

@@ -0,0 +1,52 @@
(input)
(output "5.0 5.0 10.0 14.0 14.0 6.0"
"struct receiver: 14"
"private: 8.0")
(return 0)
;;; An ops block is mangled fns plus a registry entry, so a method is
;;; reachable both ways: spelled out as vec2/length, or through the
;;; receiver as (. v length). A field still wins over a method of the
;;; same name, and `ops' works on a plain C struct too -- it keys on
;;; the tag, not on the typedef a record happens to add.
(include stdio.h)
(include math.h)
(record vec2 ((x float) (y float)))
(ops vec2
(pub fn length ((self (* const vec2))) float
"Pointer self, so (. v length) has to take the address."
(return (sqrtf (+ (* (-> self x) (-> self x))
(* (-> self y) (-> self y))))))
(fn scale ((self (* vec2)) (k float)) void
(*= (-> self x) k)
(*= (-> self y) k))
(fn sum-of ((self vec2)) float
;; value self, so (-> p sum-of) has to dereference
(return (+ (. self x) (. self y)))))
(struct raw ((a int) (b int)))
(ops raw
(fn total ((self (* const (struct raw)))) int
(return (+ (-> self a) (-> self b)))))
(pub fn main () int
(var v vec2 #(vec2 : 3.0 4.0))
(var p (* vec2) (& v))
(printf "%.1f %.1f " (. v length) (vec2/length (& v)))
(. v scale 2.0)
(printf "%.1f %.1f %.1f %.1f\n"
(-> p length) (. v sum-of) (-> p sum-of) (. v x))
(var r (struct raw) #(struct raw : 6 8))
(printf "struct receiver: %d\n" (. r total))
(var u vec2 #(vec2 : 8.0 0.0))
(printf "private: %.1f\n" (vec2/sum-of u))
(return 0))

View File

@@ -9,6 +9,11 @@
type-pattern-matches?
map-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of

View File

@@ -9,6 +9,11 @@
type-pattern-matches?
map-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of

View File

@@ -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))