`.' now writes whichever of C's member operators the type calls for, so nothing needs the arrow spelled out and the name is free for the macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in the order it happens rather than inside out. It has no business in the prelude, which every unit pays for whether or not it threads anything, so it opens the standard library instead and is imported like anything else. The compiler finds lib/ by asking the binary where it is -- beside sexc in a work tree, under ../share/sex once installed -- and looks there last, so a module of one's own by that name still wins. The C arrow becomes `%->', and that rename is what makes the rest possible. The closure lifter writes arrows of its own for the capture record, those forms go back through walk-statement, and a macro named `->' threads them: (-> ƛcaptures n) turns into a call to n, with nothing saying so until the C compiler. fmt-c rendered a bare `->' as the arrow, which left the one spelling that cannot be right -- threading with no import -- looking like the operator it stopped being. It goes through c-apply now, the way any other head nothing knows does.
58 lines
1.8 KiB
Scheme
58 lines
1.8 KiB
Scheme
(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 as (.length v).
|
|
;;; The method sits in the head, not in a field position, so (. v x) is
|
|
;;; always the field x and the two cannot be confused -- here vec2 has
|
|
;;; both a field x and methods, and nothing arbitrates between them.
|
|
;;;
|
|
;;; Whether the receiver is addressed, dereferenced or passed along is
|
|
;;; decided by its type against the method's own, so (.length v) and
|
|
;;; (.length p) both work. `ops' takes 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 (.length v) 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 (.sum-of p) 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 " (.length v) (vec2/length (& v)))
|
|
(.scale v 2.0)
|
|
(printf "%.1f %.1f %.1f %.1f\n"
|
|
(.length p) (.sum-of v) (.sum-of p) (. v x))
|
|
|
|
(var r (struct raw) #(struct raw : 6 8))
|
|
(printf "struct receiver: %d\n" (.total r))
|
|
|
|
(var u vec2 #(vec2 : 8.0 0.0))
|
|
(printf "private: %.1f\n" (vec2/sum-of u))
|
|
(return 0))
|