Cirru/sepal.clj: tool for generating Clojure code from syntax tree

This is a library used in Cirru Editor for code generation. Just refactored and released 0.2.0 of:

I’m not sure if “syntax tree” describes it well. But if you provide data:

[ "defn" "f1" [ "x" ]
  [ "+" "x" "1" ] ]

then you can get code in Clojure, with some of the details tweaked for writing purpose in Cirru Editor:

(defn f1 [x] (+ x 1))

There are two APIs in the package cirru/sepal:

(cirru-sepal.core/write-code [["println" ["+" "2" "2"]]])

(cirru-sepal.analyze/write-file {:ns ["ns" "a.b"], :proc [], :defs {:main! ["defn" "main!" ["a" "b"]]}})

To generate a file, you need some fields:

{:ns ["ns" "a.b"],
 :proc [["println" "|x"]],
 :defs {:main! ["defn" "main!" ["a" "b"]]}}

There’s a simple sort algorithm for place the definitions by order, if they refer to each other. And :proc is prepared for code that does not fit into functions.

There are some strange tricks to simplify the tree, like:

  • "|x" actually generates "x", which is string, and "|x x" generates "x x" with spaces.
  • ["[]" "1" "2"] generates [1 2], all syntaxes are turned into prefix notations.

There are more of them in Examples. And I will add docs later.

The nice part is this structure fits into Cirru Editor very well, and vectors can be easily turned into DOM structure, then you render it with all kinds of CSS styles. I will explore more styles anyway.