code generation with Xtend

648 Views Asked by At

I am implementing my own DSL and using Xtend to generate C code. I have developed small prototype using XPand/Xtend but now I need to convert prototype into XTend.

In Xpand, I used to define "Generate" function to generate C file but I don't know how can I do similar thing using XTend. Any help in this regard would be highly appreciated.

«DEFINE Generate FOR dsl::defs::module»
«FILE "Com.c" -»
/* DATE «dateString()» */
«EXPAND COM_C::COM_Def_C »
«ENDFILE»
«ENDDEFINE»

Thanks and Regards, Hemal Bavishi

1

There are 1 best solutions below

0
On

You could do something like this:

def xtendFunc() '''
/* DATE «dateString()» */
'''

or

def xtendFunc() {
var = '''/* DATE «dateString()» */'''
return var
}

(Assuming the 'dateString()' function is in the same class) This in Xtend is called using template expressions(Enclosed within three quotes '''...'''). You can contain the result of the function in a val (final variable in Xtend) for the first case. Then use another function for the other template COM_C:COM_Def_C. Append the result into a variable and write into a file using simple java.