forked from alex-eg/sex
57 lines
1.7 KiB
Scheme
57 lines
1.7 KiB
Scheme
(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))
|