Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64

Merged
pkulev merged 40 commits from static-dispatch into main 2026-10-19 17:07:45 +02:00
2 changed files with 47 additions and 1 deletions
Showing only changes of commit 737864dadc - Show all commits

View File

@@ -117,7 +117,8 @@ SEX_TEST_PROGRAMS = c99 \
type-shapes \ type-shapes \
unicode \ unicode \
unnamed-params \ unnamed-params \
wildcards wildcards \
worked-example
# Multi-module linking is checked end to end; see tests/modules/Makefile. # Multi-module linking is checked end to end; see tests/modules/Makefile.
check-modules: sexc check-modules: sexc

View File

@@ -0,0 +1,45 @@
(input)
(output "-(2 + 3)"
"-5")
(return 0)
;;; The worked example from Encapsulation.org, kept here so it stays

Who? From where? Why worked-example.sex?

Who? From where? Why worked-example.sex?

Something that moved from an uncommited doc. Will be rephrased.

Something that moved from an uncommited doc. Will be rephrased.
;;; true: a recursive sum, an inherent method on it, and a spec
;;; realized for it, which is every layer that is built at once.
(include stdio.h)
(sum expr
((lit ((v float)))
(neg ((e (* const expr))))
(add ((l (* const expr)) (r (* const expr))))))
(ops expr
(pub 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)))))))
(spec (printable T)
(fn show ((self (* const T))) void)
(fn show-line ((self (* const T))) void ; default, calls the required one
(.show self)
(printf "\n")))
(realize (printable expr)
(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 ")")))))
(pub fn main () int
(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)))
(.show-line e) ; -(2 + 3)
(printf "%g\n" (.eval e)) ; -5
(return 0))