forked from alex-eg/sex
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:
@@ -684,7 +684,12 @@ compiles."
|
||||
(test-assert "a type we cannot work out is read as a value"
|
||||
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
||||
(test-assert "and said so"
|
||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this")))
|
||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
||||
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
||||
;; is an ordinary head -- a threading macro that nobody imported,
|
||||
;; and a call like any other unknown
|
||||
(test-assert "a bare -> is not the arrow"
|
||||
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
|
||||
|
||||
;; Toplevel forms used to be separated by a blank line unconditionally,
|
||||
;; which spread a block of adjacent typedefs over twice the lines its
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
|
||||
(ops vec2
|
||||
(pub fn manhattan ((self (* const vec2))) int
|
||||
(return (+ (-> self x) (-> self y)))))
|
||||
(return (+ (. self x) (. self y)))))
|
||||
|
||||
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
||||
;;; the declarations -- and of the constructors, which is why those are
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
(pub realize (printable vec2)
|
||||
(fn show ((self (* const vec2))) void
|
||||
(printf "vec2(%d %d)" (-> self x) (-> self y))))
|
||||
(printf "vec2(%d %d)" (. self x) (. self y))))
|
||||
|
||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||
(fn unused-helper () void
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
;; a compound literal is an lvalue, so its address can be taken --
|
||||
;; until the end of the enclosing block, and no longer
|
||||
(var pp (* struct point) (& #(struct point : 9 9)))
|
||||
(printf "through a pointer %d\n" (-> pp x))
|
||||
(printf "through a pointer %d\n" (. pp x))
|
||||
|
||||
;; an array literal decays the way an array does
|
||||
(var a (* int) #([int 3] : 10 20 30))
|
||||
|
||||
@@ -29,14 +29,14 @@
|
||||
;;; the self-application hidden inside
|
||||
(fn fix ((step (* (struct rec)))) (closure ((int)) int)
|
||||
(return (closure ((n int)) int (step)
|
||||
(return ((-> step f) step n)))))
|
||||
(return ((. step f) step n)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var fac-knot (struct rec))
|
||||
(= (. fac-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (<= n 1) (return 1))
|
||||
(return (* n ((-> self f) self (- n 1))))))
|
||||
(return (* n ((. self f) self (- n 1))))))
|
||||
|
||||
;; the knot applied to itself directly, without fix
|
||||
(printf "direct: %d\n" ((. fac-knot f) (& fac-knot) 5))
|
||||
@@ -45,8 +45,8 @@
|
||||
(= (. fib-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (< n 2) (return n))
|
||||
(return (+ ((-> self f) self (- n 1))
|
||||
((-> self f) self (- n 2))))))
|
||||
(return (+ ((. self f) self (- n 1))
|
||||
((. self f) self (- n 2))))))
|
||||
|
||||
;; one combinator, two different recursions
|
||||
(var fac (closure ((int)) int) (fix (& fac-knot)))
|
||||
|
||||
@@ -43,8 +43,8 @@
|
||||
(var m (struct point))
|
||||
;; No `_' here: `m' has no initializer to infer from. Inference fills
|
||||
;; in a type, it does not invent one.
|
||||
(= (. m x) (/ (+ (-> a x) (-> b x)) 2))
|
||||
(= (. m y) (/ (+ (-> a y) (-> b y)) 2))
|
||||
(= (. m x) (/ (+ (. a x) (. b x)) 2))
|
||||
(= (. m y) (/ (+ (. a y) (. b y)) 2))
|
||||
(return m))
|
||||
|
||||
;;; The closure's type is written once, in the signature; `_' reads it
|
||||
@@ -118,7 +118,7 @@
|
||||
(var p (* _) (& origin))
|
||||
|
||||
;; Member access reads the same type database the macros do.
|
||||
(var x _ (-> p x))
|
||||
(var x _ (. p x))
|
||||
(printf "%d\n" x)
|
||||
|
||||
;; A call into a function Sex has actually parsed: the return type is
|
||||
|
||||
@@ -9,25 +9,25 @@
|
||||
(fn-name (cat 'make-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||
(= (-> list next) NULL)
|
||||
(= (. list next) NULL)
|
||||
(return list))))
|
||||
|
||||
(pub defmacro (add-value-list-T type is-public?)
|
||||
(let ((list-type (list 'struct (cat 'list- type)))
|
||||
(fn-name (cat 'add-value-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next)))
|
||||
(= (-> list next) (,(cat 'make-list- type)))
|
||||
(= (-> list value) value))))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next)))
|
||||
(= (. list next) (,(cat 'make-list- type)))
|
||||
(= (. list value) value))))
|
||||
|
||||
(pub defmacro (length-list-T type is-public?)
|
||||
(let ((fn-name (cat 'length-list- type))
|
||||
(list-type (list 'struct (cat 'list- type))))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||
(var n size-t 0)
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next))
|
||||
(++ n))
|
||||
(return n))))
|
||||
|
||||
@@ -35,14 +35,14 @@
|
||||
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||
((list ,(list '* 'struct (cat 'list- type))))
|
||||
bool
|
||||
(return (== (-> list next) NULL))))
|
||||
(return (== (. list next) NULL))))
|
||||
|
||||
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||
(let ((list-var-2 (cat list-var '-2)))
|
||||
`(do
|
||||
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
||||
(while (!= (-> ,list-var-2 next) NULL)
|
||||
(var ,elt-var ,elt-type (. ,list-var-2 value))
|
||||
(while (!= (. ,list-var-2 next) NULL)
|
||||
,what-do
|
||||
(= ,list-var-2 (-> ,list-var-2 next))
|
||||
(= ,elt-var (-> ,list-var-2 value))))))
|
||||
(= ,list-var-2 (. ,list-var-2 next))
|
||||
(= ,elt-var (. ,list-var-2 value))))))
|
||||
|
||||
@@ -23,12 +23,12 @@
|
||||
(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))))))
|
||||
(return (sqrtf (+ (* (. self x) (. self x))
|
||||
(* (. self y) (. self y))))))
|
||||
|
||||
(fn scale ((self (* vec2)) (k float)) void
|
||||
(*= (-> self x) k)
|
||||
(*= (-> self y) k))
|
||||
(*= (. self x) k)
|
||||
(*= (. self y) k))
|
||||
|
||||
(fn sum-of ((self vec2)) float
|
||||
;; value self, so (.sum-of p) has to dereference
|
||||
@@ -38,7 +38,7 @@
|
||||
|
||||
(ops raw
|
||||
(fn total ((self (* const (struct raw)))) int
|
||||
(return (+ (-> self a) (-> self b)))))
|
||||
(return (+ (. self a) (. self b)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var v vec2 #(vec2 : 3.0 4.0))
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
(struct raw ((a int) (b int)))
|
||||
|
||||
(fn shift ((p (* point)) (dx int) (dy int)) void
|
||||
(+= (-> p x) dx)
|
||||
(+= (-> p y) dy))
|
||||
(+= (. p x) dx)
|
||||
(+= (. p y) dy))
|
||||
|
||||
(fn area ((p point)) int
|
||||
(return (* (. p x) (. p y))))
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
((* const char) "%s")
|
||||
(else (error "print-struct: unsupported field type"
|
||||
name type))))
|
||||
(-> v ,name))))))
|
||||
(. v ,name))))))
|
||||
(if (not printers)
|
||||
(error "print-struct: no such struct" type-name)
|
||||
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
||||
|
||||
@@ -30,15 +30,15 @@
|
||||
|
||||
(realize (printable money)
|
||||
(fn show ((self (* const money))) void
|
||||
(printf "$%d.%02d" (/ (-> self cents) 100) (% (-> self cents) 100))))
|
||||
(printf "$%d.%02d" (/ (. self cents) 100) (% (. self cents) 100))))
|
||||
|
||||
(realize (printable point)
|
||||
(fn show ((self (* const point))) void
|
||||
(printf "(%d, %d)" (-> self x) (-> self y))))
|
||||
(printf "(%d, %d)" (. self x) (. self y))))
|
||||
|
||||
(realize (ord money)
|
||||
(fn less ((a (* const money)) (b (* const money))) bool
|
||||
(return (< (-> a cents) (-> b cents)))))
|
||||
(return (< (. a cents) (. b cents)))))
|
||||
|
||||
;;; A bounded generic: the macro refuses before it expands
|
||||
(defmacro (max-of type)
|
||||
|
||||
50
tests/sex-programs/threading.sex
Normal file
50
tests/sex-programs/threading.sex
Normal file
@@ -0,0 +1,50 @@
|
||||
(input)
|
||||
(output "arithmetic: 26"
|
||||
"a bare step needs no parens: 20"
|
||||
"through a path: 1 4"
|
||||
"mixed with a method: 25"
|
||||
"one step is just the value: 7")
|
||||
(return 0)
|
||||
|
||||
;;; `->' used to be C's arrow. The arrow is what `.' writes now when
|
||||
;;; the thing it reads from is a pointer, which left the name free for
|
||||
;;; threading -- the first module of the standard library.
|
||||
;;;
|
||||
;;; What this pins is that a step may be a call, a bare name or a
|
||||
;;; method, that the value is threaded into the *first* argument, and
|
||||
;;; that the compiler's own generated arrows survived the rename --
|
||||
;;; `ops' below lifts a method, which is where the captures live.
|
||||
|
||||
(include stdio.h)
|
||||
(import threading-macros)
|
||||
|
||||
(struct point ((x int) (y int)))
|
||||
(struct line ((a (* struct point)) (b (struct point))))
|
||||
|
||||
(fn double-it ((n int)) int
|
||||
(return (* n 2)))
|
||||
|
||||
(fn add ((n int) (m int)) int
|
||||
(return (+ n m)))
|
||||
|
||||
(ops point
|
||||
(pub fn length-sq ((self (* const struct point))) int
|
||||
(return (+ (* (. self x) (. self x))
|
||||
(* (. self y) (. self y))))))
|
||||
|
||||
(pub fn main () int
|
||||
;; (add (double-it 5) 3) doubled: inside out, written outside in
|
||||
(printf "arithmetic: %d\n" (-> 5 double-it (add 3) double-it))
|
||||
(printf "a bare step needs no parens: %d\n" (-> 5 double-it double-it))
|
||||
|
||||
(var p (struct point) #(1 2))
|
||||
(var l (struct line) #(struct line : .a (& p) .b #(3 4)))
|
||||
(var lp (* struct line) (& l))
|
||||
;; a member path is an ordinary step, and reads through both pointers
|
||||
(printf "through a path: %d %d\n" (-> lp (. a x)) (. lp b y))
|
||||
|
||||
(var q (struct point) #(3 4))
|
||||
(printf "mixed with a method: %d\n" (-> q .length-sq))
|
||||
|
||||
(printf "one step is just the value: %d\n" (-> 7))
|
||||
(return 0))
|
||||
@@ -42,7 +42,7 @@
|
||||
(printf "members: %d %g\n" px py)
|
||||
|
||||
(var pp _ (& p))
|
||||
(printf "pointers: %d %g\n" (-> pp x) (-> pp y))
|
||||
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
||||
|
||||
(var table (¤ int 3))
|
||||
(= (¤ table 0) 10)
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
;; a wildcard inside a written type: only it is solved
|
||||
(var pp2 (* _) (& p))
|
||||
(printf "partial: %d %g\n" (-> pp2 x) (-> pp2 y))
|
||||
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||
|
||||
;; C's usual arithmetic conversions, far enough to answer `_'
|
||||
(var d double 1.5)
|
||||
|
||||
Reference in New Issue
Block a user