Calculating offset for SKIP instruction [message #1159540] |
Mon, 28 October 2013 16:01 |
Gary Worsham Messages: 176 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!
Here's my current issue.
I need to be able to count the lines that occur between a line such as:
and
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 |
|
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)
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Calculating offset for SKIP instruction [message #1161798 is a reply to message #1161009] |
Wed, 30 October 2013 01:52 |
Gary Worsham Messages: 176 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
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03090 seconds