Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with code generator
Problems with code generator [message #1720929] Thu, 21 January 2016 21:12 Go to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Hi at everyone,
i have some problems with the transform my grammar in a correct java code.
To be precise i would like to get enum type information for a method definition, in particularly, this is a part of my grammar:

Elemento:
	VarD | FuncD
;	
FuncD:
	("void"|type=Type) name=ID "(" (args+=Argument ("," args+=Argument)* )?  ")" body=Body	
;
Type:
	prova=BaseType
;
enum BaseType:
	INT_TYPE = "int" | DOUBLE_TYPE = "double" | BOOL_TYPE = "bool";


And this is my xtend file for code generation:

def CharSequence compile(Model model)
	{		
		'''
		   public class test{	
	   		«FOR e:model.elementi.filter(typeof(FuncD))»
	   		public «e.type.compileType» «e.name» ()
	   		{		   				

	   		}
	   		«ENDFOR»		   
		    }
  		'''
	}
	
	def compileType(Type type)
	{
 		''' 			
 			«IF type == null »void«ELSE» 	
	 			 	HOW CAN COMPARE WITH ENUM BASETYPE HERE???!
 			«ENDIF» 			
 		'''
 	}


With my "type" how can access to BaseType for compare with ENUM value?
Thanks in advance,
Daniele
Re: Problems with code generator [message #1720931 is a reply to message #1720929] Thu, 21 January 2016 21:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

i am not sure if i get your question

type.prova gives you the enum value. you can use it to compare with BaseType.<YourLiteral> (e.g. using ==)

id move that code to a method

def toCompiledType(BaseType t) {
//use switch statement here
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problems with code generator [message #1721117 is a reply to message #1720929] Sun, 24 January 2016 17:40 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
I'm sorry, i didn't regenerate "Xtext Artifact" so during the code writing, type.prova didn't exist.
I lost to much time for this stupid problem.
Thanks and sorry for the trouble
Re: Problems with code generator [message #1721121 is a reply to message #1720929] Sun, 24 January 2016 20:55 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Hello, i have a question about this part of grammar:

Quote:

VarD:
var=Argument ('=' init=Expression)? ";"
;

Expression:
OrExpression
;

OrExpression returns Expression:
AndExpression ( { OrExpression.left=current } "||" right=OrExpression )?
;

AndExpression returns Expression:
Relation ( { AndExpression.left=current } "&&" right=AndExpression )?
;

Relation returns Expression:
SumExpression ( {Relation.left=current} op=Relop right=SumExpression )?
;

SumExpression returns Expression:
MulExpression ( {SumExpression.left=current} op=('+'|'-'|'%') right=SumExpression)?
;

MulExpression returns Expression:
BaseExpression ( {MulExpression.left=current} op=('*'|'/') right=MulExpression)?
;

BaseExpression returns Expression:
RealConstant
| IntConstant
| BoolConstant
| "(" Expression ")"
| Variable
| FunctionCall
| Input
| CastExpression
;


In particularly i don't know how to identify the correct type of Expression in the comileExpression method.

Quote:

def compileExpression(Expression exp)
{
'''
«FOR e: exp.eAllContents.toIterable.filter(typeof(SumExpression))»
«IF e.left == Variable»
«e.left.compileExpression»
«ENDIF»
«IF e.left == IntConstant»
«e.left.compileExpression»
«ENDIF»
«e.op»
«IF e.right == SumExpression»
«e.right.compileExpression»
«ENDIF»
«IF e.right == IntConstant»
«e.right.compileExpression»
«ENDIF»
«ENDFOR»
«FOR e: exp.eAllContents.toIterable.filter(typeof(IntConstant))»
«e.value»
«ENDFOR»
«FOR e: exp.eAllContents.toIterable.filter(typeof(Variable))»
«e.id.name»
«ENDFOR»
'''
}


For example, if e.left is a Variable how can i detect it?, I've tried with "«IF e.left == Variable»" but without success.
Thanks in advance
Re: Problems with code generator [message #1721123 is a reply to message #1721117] Sun, 24 January 2016 23:33 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 81
Registered: June 2015
Member
Why don't you use this kind of expressions which use Xbase and JVM Model?
grammar org.xtext.example.forum1.Ex1 with org.eclipse.xtext.xbase.Xbase

...

Elemento:
	VarD | FuncD;

FuncD:
	ftype=JvmTypeReference name=ID "(" (params+=FullJvmFormalParameter ("," params+=FullJvmFormalParameter)*)? ")"
	body=XBlockExpression;

VarD:
	name=ValidID ':' type=JvmTypeReference;


and the dsl is something like this:
 a: int
 result: int
 void sum (int a,int b){
	val myStringValue = 'A final string value'
	var myIntValue_i = 0
	var myIntValue_j = 5
 }

and infer method can be used as following:
	def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
   		acceptor.accept(model.toClass("my.company.forum1.Ex1")) [
   			for (elem : model.elementos) {
   				switch elem {
   					VarD:{
   						val field = elem.toField(elem.name, elem.type)
   						members += field
						members += elem.toGetter(elem.name, elem.type)
						members += elem.toSetter(elem.name, elem.type)
   					}
   					FuncD : {
						members += elem.toMethod(elem.name, elem.ftype ?: inferredType) [
							for (p : elem.params) {
								parameters += p.toParameter(p.name, p.parameterType)
							}
							body = elem.body
						]
					}
   				}
   			}
   		]
	}

Re: Problems with code generator [message #1721222 is a reply to message #1720929] Mon, 25 January 2016 20:43 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Very interesting, i didn't know anything about this features.
But i can't use this, because i need to use this my grammar for my purpose.
So it's possibile to compare my Expression (left and right part) object with a specialized type?
For example,
printl(variable + 2)

i found the object "PrintCommand" with (Expression)

"variable + 2" can be describe by a sumexpression defined in left part by VariableType, op by "+" and right part by IntConstant.
So for every expression i need to identify the subtype of left and right, how can i do that?

Thanks for every help
Re: Problems with code generator [message #1721227 is a reply to message #1720929] Mon, 25 January 2016 21:48 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Ok i resolved my problems in this way:

Quote:

«IF e.left.class == IntConstantImpl»
«(e.left as IntConstantImpl).value»


Just a question please. At the moment with this code i obtain that result:
Quote:

from: println(value+2+2);
to: system.out.println(value
+
2
+
2
);

How can i manage the space and the carriage return?

Thanks in advance
Re: Problems with code generator [message #1721268 is a reply to message #1721227] Tue, 26 January 2016 08:54 Go to previous message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
what about using print() instead of println() which appends a newline.
use println() just at the end to start a new line OR just use "\n"
Previous Topic:Code Generation in RunTime
Next Topic:Calling parser/generator programmatically
Goto Forum:
  


Current Time: Fri Apr 19 22:56:33 GMT 2024

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

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

Back to the top