Hi all,

Is there any easy-to-use library for generating java code?
I ended up writing simple function that takes strings and characters
and prints them in formatted form (e.g. with tabs and newlines after
braces).
I really like compojure approach to build html page - is there any
similar libraries exist for arbitrary code generation?

P.S.: my approach to code generation is as follows:

;; ==================================================
;; fail function just throws exception

(def tab-count (ref 0))
(def was-newline (ref false))
(def tab-unit "    ")


(defn pgf-reset
  "Resets printing facility"
  {:static true}
  []
  (ref-set tab-count 0)
  (ref-set was-newline false))


(defn pgf
  "Printing facility for java programming language"
  {:static true}
  [& more]
  (letfn [(align []
            (let [aligned @was-newline]
              (if aligned (print (apply str (repeat @tab-count tab-
unit))))
              (ref-set was-newline false)
              aligned))
          (align-and-print [val]
            (align)
            (print val))
          (put-newline []
            (print \newline)
            (ref-set was-newline true))]
    (doseq [elem more]
      (cond
        (char? elem) (do
                       ;; update tab-count
                       (cond
                         (= elem \tab) (fail "Tab is auto-applied
hence unexpected in " more)
                         (= elem \newline) (put-newline)
                         (= elem \{) (do
                                       ;; print space before open
block if it isn't first in line
                                       (if-not (align) (print \space))
                                       (ref-set tab-count (inc @tab-
count))
                                       (print \{)
                                       (put-newline))
                         (= elem \}) (do
                                       (ref-set tab-count (dec @tab-
count))
                                       ;; validate tab-count
                                       (if (< @tab-count 0) (fail
"While printing " more ", tab-count=" @tab-count))
                                       ;; align, print '\{' and
newline
                                       (align-and-print \})
                                       (put-newline))
                         ;; just print element itself
                         :else (align-and-print elem)))
        (string? elem) (align-and-print elem)
        (number? elem) (align-and-print (str elem)) ;; TODO: java-
specific conversion should go here
        (symbol? elem) (align-and-print (str elem))
        :else (fail "Don't know how to print " elem)))))



#_(dosync
    (pgf-reset)
    (pgf "class Foo" \{
        "public static void main(String[] args)" \{
          "System.out.println(" \" "Hello, world!" \" ");" \newline
        \}
      \})
    nil)
;; ==================================================

I personally don't like it for (1) side effects and (2) using outer
variables, but the other approaches I came up with seem to me to be
more verbose/inelegant.
Constructive criticism is greatly appreciated.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to