Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtend2] Implicit Rich String Concatenation for Code Generation
[xtend2] Implicit Rich String Concatenation for Code Generation [message #758236] Tue, 22 November 2011 12:10 Go to next message
Christian Motika is currently offline Christian MotikaFriend
Messages: 11
Registered: July 2009
Junior Member
Is there a better way to use Xtend2 for code generation and have something like the implicit string concatenation to reduce the 'noise' of appending everything together?

More specifically, is there a more elegant way to code the following:

   def dispatch outputFunctions(Output it, String moduleName) {
   	  var gen = '''''';
   	  gen.newLine();
   	  for (signal : signals)  {
   	  	gen.append('''«moduleName»_O_«signal.name»(''');
   	  	if (signal.type == "int") {
   	  		gen.append('''int i''');
   	  	}
   	  	gen.append('''){  	
   	  		value = cJSON_CreateObject();
			cJSON_AddTrueToObject(value, "present");''')
   	  	if (signal.type == "int") {
			gen.newLine();
   	  		gen.append('''cJSON_AddNumberToObject(value, "value", i);''')
   	  	} 
   	  	gen.append('''cJSON_AddItemToObject(output, "«signal.name»", value);}''')
		gen.newLine();
   	  }
   	  gen;
   }


It would be really more readable if Strings can be concatenated implicitly like this :
   def dispatch outputFunctionsWithImplicitStringConcatenation(Output it, String moduleName) {
   	  for (signal : signals)  {
   	  	'''«moduleName»_O_«signal.name»(''';
   	  	if (signal.type == "int") {
   	  		'''int i''';
   	  	}
   	  	'''){  	
   	  		value = cJSON_CreateObject();
			cJSON_AddTrueToObject(value, "present");'''
   	  	if (signal.type == "int") {
   	  		'''cJSON_AddNumberToObject(value, "value", i);'''
   	  	} 
   	  	'''cJSON_AddItemToObject(output, "«signal.name»", value);}'''
   	  }
   }
Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758241 is a reply to message #758236] Tue, 22 November 2011 12:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13424
Registered: July 2009
Senior Member
what about using rich strings as rich strings?

'''
<<FOR signal : signals>>
<<module_name>>_0_<<signal.name>>
<<ENDFOR>>
'''


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 22 November 2011 12:29]

Report message to a moderator

Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758242 is a reply to message #758236] Tue, 22 November 2011 12:28 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 672
Registered: July 2009
Location: Trondheim, Norway
Senior Member
You can use <<FOR>> and <<IF>> (here I use << and >> instead of the
French quotes).

def dispatch outputFunctions(Output it, String moduleName) {
'''
<<FOR signal : signals>>
«moduleName»_O_«signal.name»(
<<IF signal.type == "int">>
int i
<<ENDIF>>
{
value = cJSON_CreateObject();
cJSON_AddTrueToObject(value, "present");
<<IF (signal.type == "int>>
cJSON_AddNumberToObject(value, "value", i);
<<ENDIF>>
cJSON_AddItemToObject(output, "«signal.name»", value);
}
<<ENDFOR>>
''';
}

I'm sure there are mistakes here, but I think I get the idea.

Hallvard
Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758441 is a reply to message #758236] Wed, 23 November 2011 10:23 Go to previous messageGo to next message
Christian Motika is currently offline Christian MotikaFriend
Messages: 11
Registered: July 2009
Junior Member
Christian and Hallvard,

thank you both very much for your suggestions. I was aware of the possibility to use Xpand-like syntax within rich strings but I thought the for and if statements of Xtend2 could possibly also be used for code generation.

The reason for this assumption is the fact that if you have several Xtend2 statements (each returning a string) within a rich string then the return value is the concatenation of all these strings. But if you have a Xtend2 for construct within this rich string, then this is no longer true. The only way to do this currently seems to be the following way:.

   // Generale method to create the c simulation interface
   def createCSimulationInterface (Module module) {
       '''
       «/* Generate the C header */»
       «esterelHeader()»

       «/* Generate output functions for each Esterel signal */» 
       «module.interface.intSignalDecls.map(e|e.outputFunctions(module.name)).toStringConcatenation()»

       «/* Generate input functions that are then called my the main function's
           tick function of the module */»
       «esterelSetInputsFunction(module)»

       «/* Generate the generic C main function */»
       «mainFunction(module)»

       '''
   } 


   def toStringConcatenation(List<StringConcatenation> list) {
   	   '''«FOR element : list»«element»«ENDFOR»'''
   }




Is there a more elegant way to handle the concatenation of the outputFunctions(module.name) function calls that also return a String? Especially the toStringConcatenation() function just concatenates Strings of a List together which seems to be a standard procedure that you don't want to declare each time you need it by yourself.

Of course you may suggest to then use the Xpand-FOR-construct directly but I tend to like the Xtend2 functional nature also for code generation.

Thanks for your advice in advance!
Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758450 is a reply to message #758441] Wed, 23 November 2011 10:39 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 672
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

I often use <<list.join(..., [e | '''...'''])>> instead of <<FOR e :
list SEPARATOR ...>>...<<ENDFOR>>. This lets me use a functional style
and you can have variable declarations and more normal xtend2 code in
the body of the function. Note that the separator may be "".

Hallvard

On 23.11.11 11.23, Christian Motika wrote:
> Christian and Hallvard,
>
> thank you both very much for your suggestions. I was aware of the
> possibility to use Xpand-like syntax within rich strings but I thought
> the for and if statements of Xtend2 could possibly also be used for code
> generation.
> The reason for this assumption is the fact that if you have several
> Xtend2 statements (each returning a string) within a rich string then
> the return value is the concatenation of all these strings. But if you
> have a Xtend2 for construct within this rich string, then this is no
> longer true. The only way to do this currently seems to be the following
> way:.
>
>
> // Generale method to create the c simulation interface
> def createCSimulationInterface (Module module) {
> '''
> «/* Generate the C header */»
> «esterelHeader()»
>
> «/* Generate output functions for each Esterel signal */»
> «module.interface.intSignalDecls.map(e|e.outputFunctions(module.name)).toStringConcatenation()»
>
>
> «/* Generate input functions that are then called my the main function's
> tick function of the module */»
> «esterelSetInputsFunction(module)»
>
> «/* Generate the generic C main function */»
> «mainFunction(module)»
>
> '''
> }
>
> def toStringConcatenation(List<StringConcatenation> list) {
> '''«FOR element : list»«element»«ENDFOR»'''
> }
>
>
>
>
> Is there a more elegant way to handle the concatenation of the
> outputFunctions(module.name) function calls that also return a String?
> Especially the toStringConcatenation() function just concatenates
> Strings of a List together which seems to be a standard procedure that
> you don't want to declare each time you need it by yourself.
>
> Of course you may suggest to then use the Xpand-FOR-construct directly
> but I tend to like the Xtend2 functional nature also for code generation.
>
> Thanks for your advice in advance!
>
Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758451 is a reply to message #758441] Wed, 23 November 2011 10:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13424
Registered: July 2009
Senior Member
«module.interface.intSignalDecls.map(e|e.outputFunctions(module.name)).join("") »



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758466 is a reply to message #758451] Wed, 23 November 2011 11:42 Go to previous message
Christian Motika is currently offline Christian MotikaFriend
Messages: 11
Registered: July 2009
Junior Member
Thanks a lot, that's neat!
Previous Topic:improving generator development turnaround times?
Next Topic:Global references / scope in project
Goto Forum:
  


Current Time: Mon Apr 06 19:17:12 GMT 2020

Powered by FUDForum. Page generated in 0.02318 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top