Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1773951] |
Sat, 07 October 2017 11:03 |
René Bärnreuther Messages: 31 Registered: October 2017 |
Member |
|
|
Hello,
I'm currently starting out with developing a small programming language with Xbase. I want to implement a C Struct like structure using a Java Class with the appropriate fields in them. The generation works fine, but my problem is the following:
When I write something like the following:
type Struct : structure {
attribute test: int;
}
var Struct struct
{
var Struct t
}
I get the following Java Class:
public class Test {
private Struct struct = new Struct();
public void main() {
Struct t = null;
}
}
The field "struct" is working fine because I added the initializer in the Inferrer. What I want to happen is that the Struct "t" in the main method gets initialized as well as a new Struct like in the field.
Is there any way to achieve this?
Here are my grammar and my inferrer:
Program returns XBlockExpression:
{Program}
type=Typing
expressions += XVariableDeclaration
body=XBlockExpression
;
Typing returns XExpression:
/*Enumeration | */ Composition // Enumeration not relevant here
;
Composition returns XExpression:
{Composition} "type" name=ID ":" "structure" "{" (attributes+=Attribute*";") "}"
;
Attribute returns XExpression:
{Attribute}
"attribute" name=ID ":" type=JvmTypeReference ";"?
;
The inferrer looks like this:
def dispatch void infer(Program element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass("Test")) [
if(!isPreIndexingPhase){
for(expression : element.expressions){
if(expression instanceof XVariableDeclaration){
members+=expression.toField(expression.name, expression.type)[
initializer = '''new «expression.type.simpleName»()'''
]
}
}
}
members += element.toMethod("main", typeRef(Void.TYPE))[
body = element.body
]
]
if (element.type instanceof Composition) {
acceptor.accept(element.type.toClass("Struct"))[]
}
}
I hardcoded some bits here like the name of the Struct, but this shouldnt matter for the problem I think. What needs to change is something in the main Method where "body = element.body" but I don't know how to achieve this.
My guess would be to change something in the compiler, but to be honest, I have no idea how or what to change there, so any help would be greatly appreciated.
If there are any further questions feel free to ask and I will try to explain it in more detail.
Thanks in advance! :-)
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01856 seconds