Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xbase Initializing Values in XVariableDeclaration in (Method-) Body
Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1773951] Sat, 07 October 2017 11:03 Go to next message
René Bärnreuther is currently offline René BärnreutherFriend
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! :-)
Re: Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1774101 is a reply to message #1773951] Tue, 10 October 2017 15:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
What is the criteria to invent initializations inside the body?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1774106 is a reply to message #1774101] Tue, 10 October 2017 16:11 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
I want every object, which is not a primitive java type, to be initialized as an empty object.
I actually found a solution which works. It's probably not the best solution but it works the way I want it to.
I extended the XbaseCompiler with a Custom Implementation and appended the call to the constructer per Hand.
override _toJavaStatement(XVariableDeclaration vr, ITreeAppendable a, boolean isReferenced) {
		a.newLine()	
		var type = appendVariableTypeAndName(vr, a);
		if (type.simpleName.equals('int') || type.simpleName.equals("double") || type.simpleName.equals("float") ||
			type.simpleName.equals("boolean") /*.. and others  probably */) {
			a.append(";")
		} else {
			a.append("= new " + type + "();")
		}
	}


I check it by name, which I will try to change in the feature but all it does is appending something onto the Java statement when converting. If its not a primitive type it will append a Constructur with "new <Typename> ();".

Maybe it helps, if somebody else has the same problem or has an better / more beautiful idea :).
Re: Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1774109 is a reply to message #1774106] Tue, 10 October 2017 16:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Will work only if there is a default constructor and type is no interface

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 10 October 2017 16:20]

Report message to a moderator

Re: Xbase Initializing Values in XVariableDeclaration in (Method-) Body [message #1774112 is a reply to message #1774109] Tue, 10 October 2017 16:38 Go to previous message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
These cases don't appear in my language but thanks for the remark!
Previous Topic:Xtext Code Navigation
Next Topic:Recognize trailing text as a comment
Goto Forum:
  


Current Time: Tue Mar 19 05:05:53 GMT 2024

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

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

Back to the top