Files
sex/tests/sex-programs/sums.sex
alex-eg 6d84553652 lower match to an if chain, so break in an arm leaves the loop
The switch it lowered to took a break in an arm for its own, and the
loop around the match went on. With no else the last arm is the chain's
else, which keeps C from warning that a function may return nothing.
2026-10-27 01:31:37 +03:00

120 lines
3.7 KiB
Scheme

(input)
(output "3.14 12.00 0.00"
"held once: a 1"
"nested: -(2 + 3) = -5"
"size: 4"
"tagged only: 1"
"broke out after 2"
"rows: 18 5")
(return 0)
;;; A sum is a tag enum, a payload struct per variant that carries
;;; fields, and the record holding both; a constructor is the value
;;; itself, written in place. match is a switch over the tag whose arms
;;; bind the payload as locals, and the compiler refuses one that leaves
;;; a variant out -- which is the thing the form exists for.
(include stdio.h)
(sum shape
((circle ((r float)))
(rect ((w float) (h float)))
(empty ())))
(fn area ((s shape)) float
(var out float 0.0)
(match s
((circle (r)) (= out (* 3.14159 r r)))
((rect (w h)) (= out (* w h)))
((empty ()) (= out 0.0)))
(return out))
;;; Recursive through pointers, because a value cannot hold itself
(sum expr
((lit ((v float)))
(neg ((e (* const expr))))
(add ((l (* const expr)) (r (* const expr))))))
(fn eval ((self (* const expr))) float
(match (* self)
((lit (v)) (return v))
((neg (e)) (return (- (eval e))))
((add (l r)) (return (+ (eval l) (eval r))))))
(fn show ((self (* const expr))) void
(match (* self)
((lit (v)) (printf "%g" v))
((neg (e)) (printf "-") (show e))
((add (l r)) (printf "(") (show l) (printf " + ") (show r) (printf ")"))))
;;; A binder may take the name the scrutinee is spelled with, and a
;;; later binder still reads the scrutinee, not the earlier binder
(fn size ((e (* const expr))) int
(match (* e)
((lit (v)) (return 1))
((neg (e)) (return (+ 1 (size e))))
((add (e r)) (return (+ 1 (size e) (size r))))))
;;; No fields anywhere: the record is the tag alone, with no union
(sum light ((red ()) (green ())))
(fn mk-shape () shape (return (shape/circle 1.0)))
(sum held ((a ((x int) (y int))) (b ())))
(fn mk-held () held (return (held/a 1 2)))
;;; An array field is handed elements, never an array: C copies neither
;;; into a parameter nor into another array, so both the constructor
;;; and the binder spell it out
(define N 4)
(sum row ((cells ((xs [int N]))) (none ())))
(pub fn main () int
(printf "%.2f %.2f %.2f\n"
(area (mk-shape))
(area (shape/rect 3.0 4.0))
(area (shape/empty)))
;; the scrutinee is a call, so it is read into a local once
(match (mk-held)
((a (x _)) (printf "held once: a %d\n" x)) ; `_' drops y
(else (printf "held once: other\n")))
(var two expr (expr/lit 2.0))
(var three expr (expr/lit 3.0))
(var both expr (expr/add (& two) (& three)))
(var e expr (expr/neg (& both)))
(printf "nested: ")
(show (& e))
(printf " = %g\n" (eval (& e)))
(printf "size: %d\n" (size (& e)))
(var l light (light/green))
(match l
((red ()) (printf "tagged only: 0\n"))
((green ()) (printf "tagged only: 1\n")))
;; there is no switch in the source, so `break' in an arm leaves the
;; loop around the match; one in a loop of the arm's own leaves that
(var lights [light 3] #((light/green) (light/red) (light/green)))
(var seen int 0)
(for (var i int 0) (< i 3) (++ i)
(++ seen)
(match [lights i]
((red ()) (while true (break)) (break))
((green ()) (continue))))
(printf "broke out after %d\n" seen)
(var a [int N] #(5 6 7 8))
(var rows [row 2] #((row/cells a) (row/cells #(1 2 3 4))))
(= [a 0] 50) ; the row holds a copy
(var sum int 0)
(for (var i int 0) (< i 2) (++ i)
(match [rows i]
((cells (xs))
(= [xs 0] 100) ; and so does the binder
(+= sum (+ [xs 1] [xs 2])))
((none ()) (+= sum 0))))
(printf "rows: %d %d\n" sum (. [rows 0] u cells [xs 0]))
(return 0))