This page covers the architecture of the (JVM-based) Clojure implementation.
So, it may not accurately describe some aspects of other variants
(eg, ClojureCLR, ClojureScript).
Clojure is basically a Lisp
based on a reader
, an evaluator/compiler,
and a virtual machine
The evaluator/compiler accepts incoming code (encoded as Clojure data structures),
handles expansion of syntactic macros
the result into Java bytecode
The bytecode is loaded and run
by the Java Virtual Machine
The choice of the JVM is unusual for a Lisp,
but it allows Clojure to take advantage of the entire Java ecosystem:
community, libraries, tooling, etc.
This capability is further supported by Clojure's
interoperability support, use of Java data types, etc.
The diagram below is adapted from the "Syntactic Abstraction" slide
in Rich Hickey's video, Clojure for Java Programmers
It shows Clojure's overall architecture and typical data flows:
- The Reader accepts Clojure source code (characters) from the REPL (Read/Eval/Print Loop) and/or code file(s).
- The Reader's output (Clojure data structures, encoding an Abstract Syntax Tree) is sent to the evaluator/compiler for further processing. Other data structures (ie, ASTs) may come in from running program(s).
- If the evaluator/compiler detects invocation of a macro, it sends the current data structures off for macro expansion. The expanded versions are accepted in place of the originals. This process is repeated until all macros have been resolved.
- The resulting data structures, consisting of function calls and values, are compiled into collections of Java bytecode.
- The generated bytecode is sent to the JVM (Java Virtual Machine) for execution. Assorted optimizations are typically performed by the JVM at runtime.
- The running bytecode performs input and/or output, as appropriate.
This architecture allows some interesting possibilities.
For example, program code can be created and/or modified at runtime,
either via the REPL or by another part of the program.
This wiki page is maintained by Rich Morin
an independent consultant specializing in software design, development, and documentation.
Please feel free to email
comments, inquiries, suggestions, etc!