Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating operation code Xtend/Xtend
Generating operation code Xtend/Xtend [message #822419] Fri, 16 March 2012 15:44 Go to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
Hello, how can I generated and operation from this xtext models??

Expression :
TerminalExpression ({Operation.left=current}
op='+' right=Expression)?
;

TerminalExpression returns Expression:
'(' Expression ')' |
{IntLiteral} value=INT
;

I cant access to the parts...


I try this:

def declare(Expression elem){
''' «elem.left» '''

}

but there isnt a left atribute...

Any idea???


Thank you very much
Re: Generating operation code Xtend/Xtend [message #822439 is a reply to message #822419] Fri, 16 March 2012 16:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi the subclass Operation has the left not Expression itself regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating operation code Xtend/Xtend [message #822512 is a reply to message #822419] Fri, 16 March 2012 18:41 Go to previous messageGo to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
Ok, but how can i get the operation object to get the left, the right and the op ???

Re: Generating operation code Xtend/Xtend [message #822527 is a reply to message #822512] Fri, 16 March 2012 19:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i do not understand your question. the grammar you gave leads to the following metamodel (you can always have a look at the ecore/generated java classes)

interface Expression {

}

interface Operation extends Expression {
left: Expression
right: Expression
op: String
}

interface IntLiteral extends Expression {
value: int
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating operation code Xtend/Xtend [message #822582 is a reply to message #822527] Sat, 17 March 2012 00:47 Go to previous messageGo to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
sorry, what i want is creating a function in xtend like this:

def declareExpression(Expression elem){

''' «elem.left.declareTerminalExpression» «elem.op» «elem.right.declareExpression» '''

}

to generate something like this:

1+2+4

but a I dont know how to get the operation object of an expression to make this. Because I need to get the left, the right and de op, fields to be able to print it.

The aim is generating this operation with ints, strings, booleans...

Like this:

(5+4)/20
(true&&false)&&true
"hello"+ "world"

Thanks for the help.
Re: Generating operation code Xtend/Xtend [message #822830 is a reply to message #822582] Sat, 17 March 2012 10:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

what would you do in java? you would do instance of checks and typecasts
variable instanceof Operation

variable as Operation


or you leverage extends dispatch methods

def dispatch declareExpression(Operation elem){

''' «elem.left.declareExpression» «elem.op» «elem.right.declareExpression» '''

}

def dispatch declareExpression(TerminalExpression elem){

''' «elem.value» '''

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating operation code Xtend/Xtend [message #824252 is a reply to message #822830] Mon, 19 March 2012 12:45 Go to previous messageGo to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
Hi, I am not able to do the second way:

This is my xtext file:




Model:
greetings+=Expression*;

Greeting:
'Hello' name=ID '!';


Expression :
TerminalExpression ({Operation.left=current}
op='+' right=Expression)?
;

TerminalExpression returns Expression:
'(' Expression ')' |
{IntLiteral} value=INT
;





And this is my xtend file:

/*
* generated by Xtext
*/
package org.xtext.example.mydsl.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.xtext.example.mydsl.myDsl.*

class MyDslGenerator implements IGenerator {

override void doGenerate(Resource resource, IFileSystemAccess fsa) {
fsa.generateFile(resource.className+".php", toTropoPHP(resource.contents.head as Model, resource))

}
def className(Resource res) {
var name = res.URI.lastSegment
return name.substring(0, name.indexOf('.'))
}

def toTropoPHP(Model sm, Resource resource) {

'''«FOR c : sm.greetings»«c.declareExpression»
«ENDFOR»'''
}

def dispatch declareExpression(Operation elem){


''' «elem.left.declareExpression» «elem.op» «elem.right.declareExpression» '''

}

def dispatch declareExpression(TerminalExpression elem){

''' «elem.value» '''

}


}



}


What am I doing bad???


Thanks a lot.
Re: Generating operation code Xtend/Xtend [message #824253 is a reply to message #824252] Mon, 19 March 2012 12:46 Go to previous messageGo to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
Sorry, the orange words are the errors that I get Smile
Re: Generating operation code Xtend/Xtend [message #824266 is a reply to message #824252] Mon, 19 March 2012 13:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
My TerminalExpression is your IntLiteral

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating operation code Xtend/Xtend [message #824312 is a reply to message #824266] Mon, 19 March 2012 14:19 Go to previous messageGo to next message
Roberto Jimenez is currently offline Roberto JimenezFriend
Messages: 17
Registered: March 2012
Junior Member
Yes, your right Smile

Do you know how to print the brackets???

Ex:
(5+2)*4


Because I am not able to know if they appears or not
Re: Generating operation code Xtend/Xtend [message #824317 is a reply to message #824312] Mon, 19 March 2012 14:25 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You could try to find them in the node model but IMHO you do not need
them at all. If you want to be sure print them always


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Exporting Xtext Project to Plugin
Next Topic:Exclude Directories from the builder
Goto Forum:
  


Current Time: Fri Apr 26 15:53:05 GMT 2024

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

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

Back to the top