Skip to main content



      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 07:10 Go to next message
Eclipse UserFriend
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 07:29 Go to previous messageGo to next message
Eclipse UserFriend
what about using rich strings as rich strings?

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

[Updated on: Tue, 22 November 2011 07:29] by Moderator

Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758242 is a reply to message #758236] Tue, 22 November 2011 07:28 Go to previous messageGo to next message
Eclipse UserFriend
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 05:23 Go to previous messageGo to next message
Eclipse UserFriend
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 05:39 Go to previous messageGo to next message
Eclipse UserFriend
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 05:42 Go to previous messageGo to next message
Eclipse UserFriend
«module.interface.intSignalDecls.map(e|e.outputFunctions(module.name)).join("") »

Re: [xtend2] Implicit Rich String Concatenation for Code Generation [message #758466 is a reply to message #758451] Wed, 23 November 2011 06:42 Go to previous message
Eclipse UserFriend
Thanks a lot, that's neat!
Previous Topic:improving generator development turnaround times?
Next Topic:Global references / scope in project
Goto Forum:
  


Current Time: Fri Jul 25 01:12:20 EDT 2025

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

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

Back to the top