Skip to content

Commit

Permalink
fully expand macros, resolve in tree iteration, not in protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Feb 19, 2024
1 parent 0588206 commit 2475826
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/dev/onionpancakes/chassis/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,27 @@
(defmacro compile*
[node]
(binding [*env* &env]
(-> (c/tree-serializer branch? children node)
(compact)
(vec)
(vary-meta assoc ::c/content true))))
(let [rch (fn [node]
(mapv resolved (children node)))
ret (->> (resolved node)
(c/tree-serializer branch? rch)
(compact)
(vec))]
(vary-meta ret assoc ::c/content true))))

(defmacro compile
[node]
(binding [*env* &env]
(let [ret (-> (c/tree-serializer branch? children node)
(compact)
(vec)
(vary-meta assoc ::c/content true))]
(let [rch (fn [node]
(mapv resolved (children node)))
ret (->> (resolved node)
(c/tree-serializer branch? rch)
(compact)
(vec))]
(case (count ret)
0 nil
1 (nth ret 0)
ret))))
(vary-meta ret assoc ::c/content true)))))

;; CompilableForm

Expand Down Expand Up @@ -135,7 +140,7 @@
(evaluated? [_] false)
;; But not macro barriers.
(resolved [this]
(macroexpand-1 this))
(macroexpand this))
clojure.lang.Symbol
(attrs? [this]
(or (attrs-symbol? this)
Expand Down Expand Up @@ -350,12 +355,11 @@
clojure.lang.IPersistentVector
(branch? [_] true)
(children [this]
(let [resv (resolved this)]
(if (c/element-vector? resv)
(if (c/alias-element? resv)
(compilable-alias-element-children resv)
(compilable-element-children resv))
resv)))
(if (c/element-vector? this)
(if (c/alias-element? this)
(compilable-alias-element-children this)
(compilable-element-children this))
this))
Object
(branch? [_] false)
(children [_] nil)
Expand Down
8 changes: 8 additions & 0 deletions test/dev/onionpancakes/chassis/tests/test_compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
[arg]
[:p arg])

(defmacro example-elem-macro-nested
[arg]
`(example-elem-macro ~arg))

(defmethod c/resolve-alias ::Foo
[_ _ attrs content]
[:p.alias attrs content])
Expand Down Expand Up @@ -55,6 +59,7 @@
[:div example-attrs]
[:div (example-elem-fn "foo")]
[:div (example-elem-macro "foo")]
[:div (example-elem-macro-nested "foo")]
[:div (for [i (range 4)]
[:p i])]
(map inc (range 5))
Expand All @@ -80,7 +85,10 @@
[:div {:foo "bar"} "123"]
[:div {:foo example-constant}]
[:div [:p "foo"] [:p "bar"]]
(example-elem-macro "123")
(example-elem-macro-nested "123")
[:div [:p "foo"] (example-elem-macro "123") [:p "bar"]]
[:div [:p "foo"] (example-elem-macro-nested "123") [:p "bar"]]
[c/doctype-html5 [:div "foo" c/nbsp "bar"]]))

;; Attributes reflection tests
Expand Down

0 comments on commit 2475826

Please sign in to comment.