From 9ce8524f7156b1f39918e5cad379f563dad012b0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 10 Aug 2022 12:01:12 +0200 Subject: [PATCH] jsx demo (#50) --- src/cherry/compiler.cljc | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cherry/compiler.cljc b/src/cherry/compiler.cljc index 8e188d01..20242835 100644 --- a/src/cherry/compiler.cljc +++ b/src/cherry/compiler.cljc @@ -759,12 +759,33 @@ break;}" body) (defn transpile-form [f] (emit f {:context :statement})) +(defn html [v] + (cond (vector? v) + (let [tag (first v) + attrs (second v) + attrs (when (map? attrs) attrs) + elts (if attrs (nnext v) (next v)) + tag-name (symbol tag)] + (list 'let ['x (list* 'js* (format "<~{}~{}>%s\n" + (str/join " " (repeat (count elts) "~{}")) + ) tag-name (html attrs) + (concat (map html elts) [tag-name]))] + 'x)) + (map? v) + (emit v (expr-env {})) + (nil? v) (list 'js* "") + :else (list 'js* (format "{ %s }" (emit v (expr-env {})))))) + +(defn jsx [form] + (html form)) + (def cherry-parse-opts (e/normalize-opts {:all true :end-location false :location? seq? - :readers {'js #(vary-meta % assoc ::js true)} + :readers {'js #(vary-meta % assoc ::js true) + 'jsx jsx} :read-cond :allow :features #{:cljc}}))