feat: Implement AST macros and templates
This commit introduces support for AST macros and templates, enabling users to define and use custom, reusable components within the visual DSL. Key changes include: - A new `MacroRegistry` to manage macro definitions. - A `MacroExpander` to process macro calls and expand templates. - A `MacroEvaluator` trait for evaluating expressions during expansion. - The `Expansion` node in `BoundKind` to preserve the original macro call and its expanded form for debugging. - Updates to the `Binder`, `Dumper`, and `UpvalueAnalyzer` to handle the new macro constructs. - New examples demonstrating various macro functionalities like `unless`, splicing, and nested macros.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
;; Closure Scope Test
|
||||
;; Benchmark: 9.4us
|
||||
;; Benchmark: 600ns
|
||||
;; Output: 15
|
||||
(do
|
||||
(def make-adder (fn [x]
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
;; Complex Data Structure Test
|
||||
;; Benchmark: 64.4us
|
||||
;; Benchmark: 49.8us
|
||||
;; Output: {:input 10, :name "Fibonacci", :output 55}
|
||||
(do
|
||||
(def fib (fn [n]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
;; Benchmark: 25.9us
|
||||
;; Benchmark: 1.5us
|
||||
;; Output: 36
|
||||
(do
|
||||
;; Excessive capture test
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
;; Fibonacci Recursive
|
||||
;; Benchmark: 60.3us
|
||||
;; Benchmark: 49.7us
|
||||
;; Output: 55
|
||||
(do
|
||||
(def fib (fn [n]
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
;; Higher-Order Function Example
|
||||
;; Benchmark: 8.9us
|
||||
;; Benchmark: 600ns
|
||||
;; Output: 25
|
||||
(do
|
||||
(def apply (fn [f x] (f x)))
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
;; Benchmark: 400ns
|
||||
;; Financial DSL Macro example
|
||||
;; Demonstrates generating complex records from simple parameters.
|
||||
;; Output: {:price 100, :size 2, :total 200}
|
||||
|
||||
(do
|
||||
(macro trade [p s] `{:price ~p :size ~s :total (* ~p ~s)})
|
||||
(trade 100 2))
|
||||
@@ -0,0 +1,8 @@
|
||||
;; Benchmark: 300ns
|
||||
;; Nested Macro and Arithmetic example
|
||||
;; Output: 81
|
||||
|
||||
(do
|
||||
(macro square [x] `(* ~x ~x))
|
||||
(macro power4 [x] `(square (square ~x)))
|
||||
(power4 3))
|
||||
@@ -0,0 +1,8 @@
|
||||
;; Benchmark: 200ns
|
||||
;; Macro Splicing example
|
||||
;; Demonstrates unrolling a list into another list.
|
||||
;; Output: [0 1 2 3 4]
|
||||
|
||||
(do
|
||||
(macro wrap [items] `[0 ~@items 4])
|
||||
(wrap [1 2 3]))
|
||||
@@ -0,0 +1,8 @@
|
||||
;; Benchmark: 100ns
|
||||
;; Classic "unless" macro example
|
||||
;; Demonstrates simple template substitution.
|
||||
;; Output: 42
|
||||
|
||||
(do
|
||||
(macro unless [c b] `(if ~c void ~b))
|
||||
(unless false 42))
|
||||
Reference in New Issue
Block a user