forked from alex-eg/sex
implement sum and match
A sum is the tag enum, a payload struct per variant that carries fields, the record holding both, and a constructor each. match is a switch over the tag whose arms open a block and bind the payload as locals, so the shape of the generated C is the shape a C programmer would have written by hand. What it is for is the refusal: a match that leaves a variant out is an error naming the ones missing, and so are an arm the sum does not have, a wrong number of binders, and the same arm written twice. C cannot say any of that, which is why every switch over a tag in the wild ends in a defensive default nobody can tell from a real case. Only the sum is typedef'd. A payload struct would put shapeːcircle in the ordinary namespace, where the constructor of that name already lives; as a bare tag it collides with nothing. The sum's own typedef is emitted ahead of the payload structs, since a variant may hold a pointer back to the sum, and constructors are static even for a pub sum, which is pasted into the importer whole. The scrutinee is read once per binding, so one that is not a path of names is held in a local first -- otherwise (match (f) ...) would call f once for the tag and again for every field it binds.
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
|
||||
SEXC ?= ../../sexc
|
||||
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\ncolour 123
|
||||
|
||||
check:
|
||||
@$(SEXC) greet.sex -c -o greet.o
|
||||
|
||||
@@ -37,4 +37,9 @@
|
||||
;; the bare record name, then the method both ways round
|
||||
(var v vec2 #(vec2 : 3 4))
|
||||
(printf "vec2 %d %d\n" (. v manhattan) (vec2/manhattan (& v)))
|
||||
|
||||
(var c colour (colour/rgb 1 2 3))
|
||||
(match c
|
||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||
((none ()) (printf "colour none\n")))
|
||||
(return 0))
|
||||
|
||||
@@ -60,6 +60,11 @@
|
||||
(pub fn manhattan ((self (* const vec2))) int
|
||||
(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
|
||||
;;; static: two units defining colourːrgb would not link.
|
||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||
|
||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||
(fn unused-helper () void
|
||||
(printf "private\n"))
|
||||
|
||||
78
tests/sex-programs/sums.sex
Normal file
78
tests/sex-programs/sums.sex
Normal file
@@ -0,0 +1,78 @@
|
||||
(input)
|
||||
(output "3.14 12.00 0.00"
|
||||
"held once: a 1"
|
||||
"nested: -(2 + 3) = -5"
|
||||
"tagged only: 1")
|
||||
(return 0)
|
||||
|
||||
;;; A sum is a tag enum, a payload struct per variant that carries
|
||||
;;; fields, the record holding both, and a constructor each. 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 ")"))))
|
||||
|
||||
;;; 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)))
|
||||
|
||||
(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)))
|
||||
|
||||
(var l light (light/green))
|
||||
(match l
|
||||
((red ()) (printf "tagged only: 0\n"))
|
||||
((green ()) (printf "tagged only: 1\n")))
|
||||
(return 0))
|
||||
@@ -9,6 +9,11 @@
|
||||
type-pattern-matches?
|
||||
map-fields
|
||||
|
||||
add-sum!
|
||||
get-sum
|
||||
variant-fields
|
||||
normalize-fields
|
||||
|
||||
add-method!
|
||||
get-method
|
||||
qualify-op
|
||||
|
||||
Reference in New Issue
Block a user