Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xtext arithmetics operation
xtext arithmetics operation [message #899784] Thu, 02 August 2012 06:52 Go to next message
Laigle jérôme is currently offline Laigle jérôme
Messages: 26
Registered: August 2012
Junior Member
Hi,

I'm working on a DSL I want to support addition soustraction division multiplication of variable or number for example


a+b*a
a+b/3
2+4

So, I've started with the following construct

OperateurArithmetiques:
Addition;

Addition returns OperateurArithmetiques:
Multiplication (({Plus.left=current} '+' | {Minus.left=current} '-') right=Multiplication)*;

Multiplication returns OperateurArithmetiques:
PrimaryExpression (({Multi.left=current} '*' | {Div.left=current} '/') right=PrimaryExpression)*;

PrimaryExpression returns OperateurArithmetiques:
'(' OperateurArithmetiques ')'
| {NumberLiteral}value=NUMBER
| name=[VariableDeclaration]

;


i realise test with xtend in parallel
and test doesn't work


my first test:
val compilationUnit =
'''
program MyProgramme
segment 0

(2+3)
eseg 0
end
'''.parse

val abalElement = compilationUnit.abalElements.head
val program = abalElement as Program
val segment= program.segment.get(0)

val expression= segment.body.get(0)
Assert::assertNotNull(expression)
Assert::assertTrue(expression instanceof OperateurArithmetiques)
val operateurArithmetiques=expression as OperateurArithmetiques
Assert::assertNotNull(operateurArithmetiques)
Assert::assertTrue(operateurArithmetiques instanceof Plus)
val plus=operateurArithmetiques as Plus
Assert::assertNotNull(plus)
Assert::assertEquals('2',plus.left)


plus.left is null i don't understand why

my second test :

val compilationUnit =
'''
program MyProgramme
segment 0

(a+b)
eseg 0
end
'''.parse


val abalElement = compilationUnit.abalElements.head
val program = abalElement as Program
val segment= program.segment.get(0)

val expression= segment.body.get(0)
Assert::assertNotNull(expression)
Assert::assertTrue(expression instanceof OperateurArithmetiques)
val operateurArithmetiques=expression as OperateurArithmetiques
Assert::assertNotNull(operateurArithmetiques)
Assert::assertTrue(operateurArithmetiques instanceof Plus)
val plus=operateurArithmetiques as Plus
Assert::assertNotNull(plus)
Assert::assertEquals('a',plus.left)
xtend error
junit.framework.AssertionFailedError: expected:<a> but was:<OperateurArithmetiquesImpl@2793d5d7>




Best regards,
Jérôme
Re: xtext arithmetics operation [message #899882 is a reply to message #899784] Thu, 02 August 2012 13:18 Go to previous message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6214
Registered: July 2009
Senior Member
hi,

where in your code do you declare the variables?
can you share a complete reproducable grammar and test case?

~Christian
Previous Topic:Split XText Grammar into multiple files
Next Topic:Dependency in grammars
Goto Forum:
  


Current Time: Sat Aug 23 07:27:48 EDT 2014

Powered by FUDForum. Page generated in 0.07463 seconds