[beginner] library for java code generation

2012-03-23 Thread Alex Shabanov
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


Re: [beginner] library for java code generation

2012-03-23 Thread Tassilo Horn
Hi Alex,

I've done similar things in the past, also by throwing together plain
strings and using `format` as a kind of templating thingy.  I don't even
think it's a too bad approach.

With respect to the side-effects: you can omit all of them (glancing at
your code) by giving up the indentation.  There's no reason to do any
formatting in languages where indentation has no meaning.  Spit out one
large line or a bunch of unindented lines and use a tool like `astyle`
or the eclipse code formatter for making it pretty.

Bye,
Tassilo

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


Re: [beginner] library for java code generation

2012-03-23 Thread Herwig Hochleitner
Take a look at https://github.com/stathissideris/javamatic

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


Re: [beginner] library for java code generation

2012-03-23 Thread Ben Smith-Mannschott
On Fri, Mar 23, 2012 at 14:22, Alex Shabanov avshaba...@gmail.com wrote:
 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?

I've also looked for, but not found such a library.

I wrote a tool in Clojure which generates Java 5 enumerations from
data. The templating solution I settled on was to do this:

A template is a function which returns a template-result.
A template-result is a sequence of atoms and template-results.
An atom is a string, number, boolean, java.lang.Class, keyword, symbol or nil.

The result of running my code generator is thus a tree of atoms, which
I then flatten, remove the nils, and then convert the atoms:
- strings are just themselves
- keywords and symbols are their names
- java.lang.Class objects are the fully qualified name of the class
- booleans and numbers become java literals
Then concatenate the resulting strings.

It's odd, but it works, and has the advantage that templates
(functions) need not have side-effects and can be composed in
interesting ways.

e.g. (rough sketch):

(defn declare-field [name value]
  [public final  (unbox (class value))   name  =  value ;])

(defn declare-fields [name-val]
  (interpose \n
(map delare-field (keys name-val) (vals name-val

(template-result-string
  (declare-fields {:aNumber 1, :aBoolean false}))

Would produce something like:

  public final long aNumber = 1;
  public final boolean aBoolean = false;

This works OK for me, but it's really just a step up from raw printfs.
It doesn't understand anything about java syntax, so, handling
indentation -- for example -- is ad-hoc and brittle.

// ben

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