chlorinejs / chlorine

Main library for Chlorine compiler (a subset of Clojure to javascript)
https://github.com/chlorinejs/lein-cl2c
45 stars 4 forks source link

Dynamic binding #86

Open myguidingstar-zz opened 10 years ago

myguidingstar-zz commented 10 years ago

Cl2> (def a 1) ==> nil Cl2> a ==> 1 Cl2> (defn foo [] this.a) ==> nil Cl2> (foo) ==> 1 Cl2> (.call foo {:a 2}) ==> 2

myguidingstar-zz commented 10 years ago
(defmacro binding [bindings & body]
  (let [bindings (partition 2 bindings)
        ;; todo: unique symbol
        ks (map #(-> % first name
                     ((fn [s] (keyword s)))) bindings)
        vs (map second bindings)
        binding-map (zipmap ks vs)]
    `(let [binding-map ~binding-map]
       (.call (fn [] ~@body)
              binding-map))))

(binding [*a* 2] (foo))