Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Calculating offset for SKIP instruction(Noob)
Calculating offset for SKIP instruction [message #1159540] Mon, 28 October 2013 16:01 Go to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
My DSL (adding functionality to a DSP assembly language) is going quite well. This is amazing to me as 4 weeks ago I had very little idea what a DSL even was. It turns out that Xtext and Xtend are exactly what I was looking for. I just wish I'd known about them sooner! Very Happy

Here's my current issue.

I need to be able to count the lines that occur between a line such as:

SKIP (flags, label)


and

label:


For example:

SKIP (RUN_FLAG, init)
SET_Register(LFO_SPEED, 34)
SET_Register(LFO_WIDTH, 4096)
ReadRegister (POT0)
init:
Next_Instruction(blah)


Needs to parse to:

SKIP (RUN_FLAG, 3)
SET_Register(LFO_SPEED, 34)
SET_Register(LFO_WIDTH, 4096)
ReadRegister (POT0)
Next_Instruction(blah)


My first thought was to start pushing instructions into a FIFO queue as soon as I hit the "Skip" instruction, then count how many are consumed until I find the matching label. After that I could complete the Skip instruction and process the instructions that went into the queue. But this is sounding a little complicated, assuming that there is an easier way.

I am pretty sure that Skip instructions only need to go forward, but I'll double check.

Any suggestions?

Thanks,

GW
Re: Calculating offset for SKIP instruction [message #1159662 is a reply to message #1159540] Mon, 28 October 2013 17:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6538
Registered: July 2009
Senior Member
Hi,

what about something like (calculating on the model)

Model:
	elements+=Element*;
	
Element:
	Label | RefToLabel
;	

Label:
	name=ID ":"
;

RefToLabel:
	"GOTO" "(" label=[Label]")"
;


package org.xtext.example.mydsl2;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.xtext.example.mydsl2.myDsl.RefToLabel;

public class Util {
	
	public int getDiff(RefToLabel rtl) {
		int labelIndex = indexOf(rtl);
		int refIndex = indexOf(rtl.getLabel());
		return labelIndex - refIndex;
	}
	
	private int indexOf(EObject o) {
		if (o.eContainingFeature()!=null && o.eContainingFeature().isMany()) {
			@SuppressWarnings("unchecked")
			List<EObject> siblings = (List<EObject>) o.eContainer().eGet(o.eContainingFeature());
			int indexOf = siblings.indexOf(o);
			return indexOf;
		}
		return -1;
	}

}


(not tested)
Re: Calculating offset for SKIP instruction [message #1160993 is a reply to message #1159662] Tue, 29 October 2013 14:00 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
Hi Christian, thanks a lot for the tip. It looks reasonable. I created a new Java class and entered the code above, substituting for my model where appropriate:

package com.holycityaudio.spincad.generator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import com.holycityaudio.spincad.spinCAD.Skip;

public class UtilSC {
	
	public int getDiff(Skip rtl) {
		int labelIndex = indexOf(rtl);
		int refIndex = indexOf(rtl.getLabel());
		return labelIndex - refIndex;
	}
	
	private int indexOf(EObject o) {
		if (o.eContainingFeature()!=null && o.eContainingFeature().isMany()) {
			@SuppressWarnings("unchecked")
			List<EObject> siblings = (List<EObject>) o.eContainer().eGet(o.eContainingFeature());
			int indexOf = siblings.indexOf(o);
			return indexOf;
		}
		return -1;
	}
}


I put this Java class in the same package as my Xtend code generator.

Now, I am trying to reference the getDiff() function within my code generator:

import com.holycityaudio.spincad.generator.UtilSC
[-- lines missing --]
	def genSkip(Skip inst) {
	'''
	sfxb.skip(«inst.getFlags()», «getDiff(inst)»));
	'''}


However I can't resolve getDiff from the Xtend code, in spite of the import (which warns that it's not being used). Doesn't make any difference if I use the fully qualified name.

Clearly I don't understand how to reference something in a Java class from Xtend. ???

Thanks,

GW
Re: Calculating offset for SKIP instruction [message #1161009 is a reply to message #1160993] Tue, 29 October 2013 14:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6538
Registered: July 2009
Senior Member
Did you add a

Import static extension blafasel.Util;

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Calculating offset for SKIP instruction [message #1161798 is a reply to message #1161009] Wed, 30 October 2013 01:52 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
OK, I tried a few things.

In Xtend:

import static extension com.holycityaudio.spincad.generator.UtilSC.*;

// there is just a warning here that the import is not used

[some lines missing]

My first try was:
	def genSkip(Skip inst) {
	'''
	sfxb.skip(«inst.getFlags()», «getDiff(inst)»));
	'''}


and then:
def genSkip(Skip inst) {
	'''
	sfxb.skip(«inst.getFlags()», «inst.getDiff(inst.getLabel())»));
	'''}

inst.getDiff is highlighted and error message: The method getDiff is undefined for the type SpinCADGenerator.

current file name is:

/src/com/holycityaudio/spincad/generator/SpinCADGenerator.xtend

Here's the extension method:
package com.holycityaudio.spincad.generator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import com.holycityaudio.spincad.spinCAD.Skip;

public class UtilSC {
	
	public int getDiff(Skip rtl) {
		int labelIndex = indexOf(rtl);
		int refIndex = indexOf(rtl.getLabel());
		return labelIndex - refIndex;
	}
	
	private int indexOf(EObject o) {
		if (o.eContainingFeature()!=null && o.eContainingFeature().isMany()) {
			@SuppressWarnings("unchecked")
			List<EObject> siblings = (List<EObject>) o.eContainer().eGet(o.eContainingFeature());
			int indexOf = siblings.indexOf(o);
			return indexOf;
		}
		return -1;
	}
}

[Updated on: Wed, 30 October 2013 02:29]

Report message to a moderator

Re: Calculating offset for SKIP instruction [message #1162009 is a reply to message #1161798] Wed, 30 October 2013 05:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6538
Registered: July 2009
Senior Member
You call getdiff for label and not for skip

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Calculating offset for SKIP instruction [message #1167133 is a reply to message #1162009] Sat, 02 November 2013 12:22 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
Thanks Christian,

My fundamental problem was that I didn't realize this:

Static class members are accessed via :: and not . like in Java.

http://jnario.org/org/jnario/jnario/documentation/20FactsAboutXtendSpec.html

But now that I realize that, I can resolve the Java function OK, even without the import statement (the Java source is in the same package as the generator xtend).

It's going to take a bit more work as there could be another skip label between a skip instruction and its actual cross reference (this would throw the instruction count off). But I can hopefully iterate through the model from one point to the other and throw out any extraneous labels.

Thanks again,

GW

[Updated on: Sat, 02 November 2013 13:29]

Report message to a moderator

Re: Calculating offset for SKIP instruction [message #1167169 is a reply to message #1167133] Sat, 02 November 2013 13:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6538
Registered: July 2009
Senior Member
Hi,

which version do you use?
Re: Calculating offset for SKIP instruction [message #1167200 is a reply to message #1167169] Sat, 02 November 2013 13:28 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
Christian Dietrich wrote on Sat, 02 November 2013 09:00
Hi,

which version do you use?


Eclipse is Kepler 4.3.0.

Xtend is Version: 2.4.2.v201306120542
Re: Calculating offset for SKIP instruction [message #1167221 is a reply to message #1167200] Sat, 02 November 2013 13:47 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6538
Registered: July 2009
Senior Member
Hi,

i thought that :: thing recently changed (dont know if it was 2.4.2 or 2.4.3
Previous Topic:[SOLVED My Fault] Xtext generates the jvm model even for models in maven dependencies
Next Topic:How do I set the package for my generated Java files?
Goto Forum:
  


Current Time: Thu Dec 18 08:31:13 GMT 2014

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

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