4
votes

The initial task I want to solve is to build AST for an arbitrary Clojure code. So google suggests to use https://github.com/clojure/tools.analyzer.jvm for this purpose. I've tried this library on the simple Clojure form (print x) and this raised exception with message Could not resolve var: x. I used to think of building AST as a simple process without side-effects like validation, resolving variables and so on. It's also weird that there is no clean documentation on how one could omit this variable resolution step.

1
I played with an option :validate/unresolvable-symbol-handler - (aj/analyze '(print x) (assoc (aj/empty-env) :passes-opts {:validate/unresolvable-symbol-handler (fn [a b c] (print a)) })) but had no luck - hsestupin

1 Answers

1
votes

So I figured it out. The :validate/unresolvable-symbol-handler has to be passed with :passes-opts as a 3rd argument (not inside the environment as I tried)

(clojure.tools.analyzer.jvm/analyze '(print x) 
    (clojure.tools.analyzer.jvm/empty-env)
    {:passes-opts {:validate/unresolvable-symbol-handler
                                     (fn [a b c] nil)}})