Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created June 6, 2018 23:45
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 Sophia-Gold/d48be3ec125a453de7cec2033a465ef8 to your computer and use it in GitHub Desktop.
Save Sophia-Gold/d48be3ec125a453de7cec2033a465ef8 to your computer and use it in GitHub Desktop.
(ns walk-ast
(:require [com.rpl.specter :refer :all]
[clojure.tools.analyzer.jvm :refer [analyze]]
[fipp.edn :refer [pprint] :rename {pprint fipp}]))
(def map-key-walker
(recursive-path
[akey] p
(cond-path map? [ALL (if-path [FIRST #(= % akey)]
LAST
[LAST p])]
vector? [ALL (if-path map?
[ALL (if-path [FIRST #(= % akey)]
LAST
[LAST p])])])))
(defn select-locals [ast] (select (map-key-walker :locals) ast))
(def MAP-NODES
(recursive-path
[] p
(cond-path map? (continue-then-stay MAP-VALS p)
coll? [ALL p])))
(defn keep-locals [ast] (transform MAP-NODES #(select-keys % [:locals]) ast))
(def data (analyze '(fn [x y] (fn [a b] (+ x a)))))
(def unstructured (select-locals data))
(def structured (keep-locals data)) ;; => {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment