implement thread-first ->

`.' 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.
This commit is contained in:
2026-10-05 02:08:15 +03:00
parent fb4b8aa1f9
commit dd3a4bdac5
21 changed files with 219 additions and 66 deletions

View File

@@ -777,7 +777,7 @@
(cond ((symbol? expr) #t)
((not (pair? expr)) #f)
(else (case (car expr)
((dot-access ->) (simple-path? (second expr)))
((dot-access %->) (simple-path? (second expr)))
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
(else #f)))))
@@ -907,7 +907,7 @@
(unless (or type told?)
(sex-warning form "cannot type this; `.' reads it as a value"
base))
(step (list (if target '-> 'dot-access) expr (car fields))
(step (list (if target '%-> 'dot-access) expr (car fields))
(field-type (or target type) (car fields))
(cdr fields)
(or told? (not type))))))))
@@ -917,7 +917,7 @@
((vector? expr) #t) ; a compound literal is one
((not (pair? expr)) #f)
(else (case (car expr)
((dot-access -> ¤) #t)
((dot-access %-> ¤) #t)
((*) (= 2 (length expr)))
(else #f)))))
@@ -991,8 +991,8 @@
(arithmetic-type expr env)))
((dot-access) (member-path-type (expression-type (second expr) env)
(cddr expr)))
((->) (member-path-type (pointer-target (expression-type (second expr) env))
(cddr expr)))
((%->) (member-path-type (pointer-target (expression-type (second expr) env))
(cddr expr)))
((cast) (and (= 3 (length expr)) (third expr)))
((sizeof) 'size-t)
;; `(closure ((x int)) int () ...)' is a `(closure ((int)) int)',
@@ -1351,7 +1351,7 @@
(iota (length (second type))))))
(unless (hash-table-exists? +closure-conversions+ convert)
(hash-table-set! +closure-conversions+ convert #t)
(let ((call `((-> ƛcaptures f) ,@(map first params))))
(let ((call `((%-> ƛcaptures f) ,@(map first params))))
(for-each
(lambda (emitted)
(set! *pending-closure-structs*
@@ -1369,7 +1369,7 @@
(= (dot-access ƛc code) ,thunk)
(var ƛcaptures (* (struct ,record))
(cast (& (dot-access ƛc env)) (* (struct ,record))))
(= (-> ƛcaptures f) f)
(= (%-> ƛcaptures f) f)
(return ƛc))))))
convert))
@@ -1487,7 +1487,7 @@
`((var ƛcaptures (* (struct ,record)) ƛe)
,@(map (lambda (cap)
`(var ,(first cap) ,(second cap)
(-> ƛcaptures ,(first cap))))
(%-> ƛcaptures ,(first cap))))
caps)))
,@body)
@@ -1505,7 +1505,7 @@
`((var ƛcaptures (* (struct ,record))
(cast (& (dot-access ƛc env)) (* (struct ,record))))
,@(map (lambda (cap)
`(= (-> ƛcaptures ,(first cap)) ,(first cap)))
`(= (%-> ƛcaptures ,(first cap)) ,(first cap)))
caps)))
(return ƛc))))))))
(else (sex-error form "malformed closure" form))))