Add prelude and while macro

This commit introduces a new prelude file that defines the `while` macro
and registers it during environment bootstrapping. The `again` macro has
been updated to accept multiple arguments for its recursive call, and
several examples have been adjusted to reflect this change.
Additionally, the `MacroRegistry` in the compiler is now cloneable and
can be moved out of the `MacroExpander`.
This commit is contained in:
Michael Schimmel
2026-02-28 12:41:19 +01:00
parent 83324a1892
commit 7126668934
8 changed files with 56 additions and 26 deletions
+3 -3
View File
@@ -1,10 +1,10 @@
;; Benchmark: 2.5us
;; Benchmark-Repeat: 804
;; Benchmark: 2.5us
;; Benchmark-Repeat: 804
;; Output: 120
(do
(def factorial (fn [n acc]
(if (= n 0)
acc
(again [(- n 1) (* acc n)]))))
(again (- n 1) (* acc n)))))
(factorial 5 1))
+10 -19
View File
@@ -1,21 +1,12 @@
;; Benchmark: 1.7ms
;; Benchmark-Repeat: 3
;; Benchmark: TBD
;; Tests the effect of record inlining and field lookup optimization
;; Output: 10000
(do
(macro while [cond body]
`(do
(def _while_loop (fn []
(if ~cond
(do ~body (_while_loop))
...)))
(_while_loop)))
(def config {:start 0 :limit 10000 :step 2})
(def x (.start config))
(while (< x (.limit config))
(assign x (+ x (.step config))))
x
)
;; Tests the effect of record inlining and field lookup optimization
;; Output: 10000
(do
(def config {:start 0 :limit 10000 :step 2})
(def x (.start config))
(while (< x (.limit config))
(assign x (+ x (.step config))))
x
)