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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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"))
|
||||
|
||||
52
tests/sex-programs/ops.sex
Normal file
52
tests/sex-programs/ops.sex
Normal 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))
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user