GithubHelp home page GithubHelp logo

clr.tools.analyzer's Introduction

Interface to ClojureCLR's Analyzer

ClojureCLR's analysis compilation phase holds rich information about Clojure forms, like type/reflection information.

analyze provides an interface to this phase, callable a la carte. The output is similar to ClojureScript's analyzer.

Supports ClojureCLR 1.4.1 or later.

Contributing

Pull requests accepted from registered Clojure contributers

http://clojure.org/contributing

Usage

Generating AST from syntax

clojure.clr.tools.analyzer=> (ast [1])
{:op :constant, :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}}, :val [1]}

clojure.clr.tools.analyzer=> (-> (ast (if true 1 2)) clojure.pprint/pprint)
{:op :if,
 :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
 :test
 {:op :boolean,
  :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
  :val true},
 :then
 {:op :number,
  :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
  :val 1},
 :else
 {:op :number,
  :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
  :val 2}}
nil

clojure.clr.tools.analyzer=> (-> (ast (fn [x] (+ x 1))) clojure.pprint/pprint)
{:op :meta,
 :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
 :meta
 {:op :map,
  :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
  :keyvals
  ({:op :keyword,
    :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
    :val :source-span}
   {:op :map,
    :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
    :keyvals
    ({:op :keyword,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val :start-line}
     {:op :number,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val 12}
     {:op :keyword,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val :start-column}
     {:op :number,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val 10}
     {:op :keyword,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val :end-line}
     {:op :number,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val 12}
     {:op :keyword,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val :end-column}
     {:op :number,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :val 25})})},
 :expr
 {:name clojure.clr.tools.analyzer$fn__3171,
  :op :fn-expr,
  :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
  :methods
  ({:op :fn-method,
    :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
    :body
    {:op :do,
     :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
     :exprs
     ({:op :static-method,
       :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
       :class clojure.lang.Numbers,
       :method
       {:name add,
        :return-type System.Object,
        :declaring-class clojure.lang.Numbers,
        :parameter-types [System.Object System.Int64],
        :flags #{:hide-by-sig :reuse-slot :static :public}},
       :args
       ({:op :host-arg,
         :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
         :expr
         {:op :local-binding-expr,
          :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
          :local-binding
          {:op :local-binding,
           :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
           :sym x,
           :tag nil,
           :init nil},
          :tag nil}}
        {:op :host-arg,
         :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
         :expr
         {:op :number,
          :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
          :val 1}}),
       :tag nil})},
    :required-params
    ({:op :local-binding,
      :env {:locals {}, :ns {:name clojure.clr.tools.analyzer}},
      :sym x,
      :tag nil,
      :init nil}),
    :rest-param nil}),
  :variadic-method nil,
  :tag nil}}
nil

Syntax from AST

clojure.jvm.tools.analyzer=> (require '[clojure.jvm.tools.analyzer.emit-form :as e])
nil
clojure.jvm.tools.analyzer=> (-> (ast 1) e/emit-form)
1
clojure.jvm.tools.analyzer=> (-> (ast [(+ 1 2)]) e/emit-form)
[(clojure.lang.Numbers/add 1 2)]

Known Issues

Evaluating forms

Currently the analyzer evaluates each form after it is analyzed.

Incorrect handling of Var mappings within the same form

analyze is a thin wrapper over clojure.lang.Compiler, so to get our hands on analysis results some compromises are made.

The following form normally evaluates to the Var clojure.set/intersection, but analyses to clojure.core/require.

;normal evaluation
(eval
 '(do 
    (require '[clojure.set])
    (refer 'clojure.set 
           :only '[intersection] 
           :rename '{intersection require})
    require))
;=> #'clojure.set/intersection

;analysis result
(-> (ast 
      (do (require '[clojure.set])
        (refer 'clojure.set 
               :only '[intersection] 
               :rename '{intersection require})
        require))
  :exprs last :var)
;=> #'clojure.core/require

Todo

  • analyze a leiningen project.clj file
  • analyze clojure.core
  • use :locals if necessary

Examples

See clojure.jvm.tools.analyzer.examples.* namespaces.

Contributors

  • Jonas Enlund (jonase)
  • Nicola Mometto (Bronsa)
  • Chris Gray (chrismgray)

clr.tools.analyzer's People

Contributors

frenchy64 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.