Skip to content

Instantly share code, notes, and snippets.

@sgrove
Created August 25, 2013 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrove/6335798 to your computer and use it in GitHub Desktop.
Save sgrove/6335798 to your computer and use it in GitHub Desktop.
Real Keywords 00002
; Changes to analyzer.clj
(def keyword_counter (atom 0))
(defn genid
"Returns a new symbol with a unique name. If a prefix string is
supplied, the name is prefix# where # is some unique number. If
prefix is not supplied, the prefix is 'K__'."
([] (genid "K__"))
([prefix-string]
(when (nil? keyword_counter)
(set! keyword_counter (atom 0)))
(symbol (str prefix-string (swap! keyword_counter inc)))))
(defn reset-constant-table! []
(reset! *constant-table* {}))
(defn register-constant! [k]
(println "Registering constant!")
(swap! *constant-table*
(fn [table]
(if (get table k)
table
(assoc table k (genid))))))
; Still doing it per-analyzer-method right now
(defn analyze-keyword
[env sym]
(when *real-keywords?*
(register-constant! sym))
{:op :constant :env env
:form sym})
; Taken from cljs.compiler/compile-file*
(let [src (java.io.File. "simple2.cljs")]
(binding [ana/*cljs-ns* 'cljs.user
ana/*cljs-file* (.getPath ^java.io.File src)
*data-readers* tags/*cljs-data-readers*
*emitted-provides* (atom #{})
*cljs-gen-line* (atom 0)
*cljs-gen-col* (atom 0)]
(loop [forms (ana/forms-seq src)
ns-name nil
deps nil]
(if (seq forms)
(let [env (ana/empty-env)
ast (ana/analyze env (first forms))]
(do (emit ast)
(if (= (:op ast) :ns)
(recur (rest forms) (:name ast) (merge (:uses ast) (:requires ast)))
(recur (rest forms) ns-name deps))))
ana/*constant-table*))))
=> #<Atom@6383d70b: {:fail K__8, :else K__7, :mind K__6, :damnit K__5, :perfidy K__4, :c K__3, :b K__2, :a K__1}>
(ns simple2.core)
(let [m {:a 10 :b 100}]
(:a m)
(:c m)
(:a m)
(+ (:a m) (:a m) (:perfidy m))
(cond (pos? (or (:c m) (:a 10))) (println :mind)
:else :fail)
(println 'a)
(println 'apple)
(println :damnit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment