GithubHelp home page GithubHelp logo

isabella232 / tools.analyzer.jvm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clojure/tools.analyzer.jvm

0.0 0.0 0.0 1.68 MB

additional jvm-specific passes for tools.analyzer

License: Eclipse Public License 1.0

Shell 0.20% Clojure 89.25% HTML 9.07% Smarty 1.48%

tools.analyzer.jvm's Introduction

tools.analyzer.jvm

An analyzer for Clojure code, written on top of tools.analyzer, providing additional jvm-specific passes.

Note for REPL usage

The AST tools.analyzer.jvm produces contains a lot of redundant information and while having this structure in memory will not require an excessive amount of memory thanks to structural sharing, attempting to print the AST of even a relatively small clojure expression can easily produce a several thousand lines output which might make your REPL irresponsive for several seconds or even crash it. For this reason, when exploring tools.analyzer.jvm ASTs on the REPL, I encourage you to:

  • set *print-length* and *print-level* to a small value, like 10
  • interactively explore the AST structure, inspecting the :children and :op fields of a node and the keys function rather than printing it to see its content

Example Usage

Calling analyze on the form is all it takes to get its AST (the output has been pretty printed for clarity):

user> (require '[clojure.tools.analyzer.jvm :as ana.jvm])
nil
user> (ana.jvm/analyze 1)
{:op        :const,
 :env       {:context :ctx/expr, :locals {}, :ns user},
 :form      1,
 :top-level true,
 :val       1,
 :type      :number,
 :literal?  true,
 :id        0,
 :tag       long,
 :o-tag     long}

To get a clojure form out of an AST, use the emit-form pass:

user> (require '[clojure.tools.analyzer.passes.jvm.emit-form :as e])
nil
user> (e/emit-form (ana.jvm/analyze '(let [a 1] a)))
(let* [a 1] a)

Note that the output will be fully macroexpanded. You can also get an hygienic form back, using the emit-hygienic-form pass:

user> (e/emit-hygienic-form (ana.jvm/analyze '(let [a 1 a a] a)))
(let* [a__#0 1 a__#1 a__#0] a__#1)

As you can see the local names are renamed to resolve shadowing.

The analyze function can take an environment arg (when not provided it uses the default empty-env) which allows for more advanced usages, like injecting locals from an outer scope:

user> (-> '(let [a a] a)
        (ana.jvm/analyze (assoc (ana.jvm/empty-env)
                           :locals '{a {:op    :binding
                                        :name  a
                                        :form  a
                                        :local :let}}))
        e/emit-hygienic-form)
(let* [a__#0 a] a__#0)

There's also an analyze+eval function that, as the name suggests, evaluates the form after its analysis and stores the resulting value in the :result field of the AST, this function should be used when analyzing multiple forms, as the analysis of a clojure form might require the evaluation of a previous one to make sense.

This would not work using analyze but works fine when using analyze+eval:

user> (ana.jvm/analyze+eval '(defmacro x []))
{:op        :do,
 :top-level true,
 :form      (do (clojure.core/defn x ([&form &env])) (. (var x) (setMacro)) (var x)),
 ... ,
 :result    #'user/x}
user> (ana.jvm/analyze+eval '(x))
{:op        :const,
 :env       {:context :ctx/expr, :locals {}, :ns user},
 :form      nil,
 :top-level true,
 :val       nil,
 :type      :nil,
 :literal?  true,
 :tag       java.lang.Object,
 :o-tag     java.lang.Object,
 :result    nil}

To analyze a whole namespace, use analyze-ns which behaves like analyze+eval and puts the ASTs for each analyzed form in a vector, in order.

user> (ana.jvm/analyze-ns 'clojure.string)
[{:op        :do,
  :result    nil,
  :top-level true,
  :form      (do (clojure.core/in-ns (quote clojure.string)) ..),
  ...}
..]

Note that the quickref refers to the last stable release of t.a.jvm and might not be valid for the current SNAPSHOT version or for previous ones. Note also that the documented node fields refer to the output of t.a.jvm/analyze running the default passes and using the default configuration.

SPONSORSHIP

YourKit

YourKit has given an open source license for their profiler, greatly simplifying the profiling of tools.analyzer.jvm performance.

YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products:

Releases and Dependency Information

Latest stable release: 1.2.2

Leiningen dependency information:

[org.clojure/tools.analyzer.jvm "1.2.2"]

Maven dependency information:

<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>tools.analyzer.jvm</artifactId>
  <version>1.2.2</version>
</dependency>

API Index

Developer Information

License

Copyright © 2013-2021 Nicola Mometto, Rich Hickey & contributors.

Distributed under the Eclipse Public License, the same as Clojure.

tools.analyzer.jvm's People

Contributors

bronsa avatar puredanger avatar jafingerhut avatar frenchy64 avatar arrdem avatar slipset avatar deraen avatar hiredman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.