Inheritance of abstract concepts and IF intention

  • 6
  • 23

Hello fellows.

I have a problem when i try to define how to generate code from my DSL, and can't solve it by myself as I don't have any experience in using MPS.


I have defined several concepts to model a Logo Program.
One of those is an abstract concept that I call Instruction.
A program contains several instructions and each instruction extend the Instruction concept.

In my Java Program, I want to go through all the instructions, so I defined a $LOOP$ intention with node.instructions in mapped nodes. In this LOOP intention, what has to be displayed depends on the type of the instruction So i used an $IF$ intention with node.isInstanceOf(MY CONCEPT); in the condition.

And here come the problem.

I want in that if to access attribute proper to the concept that extend INSTRUCTION, but as i'm iterating over instruction in the loop the node is of type INSTRUCTION instead of the more specific type.
I tried to cast the node in my intentions, but concepts are not Java types.

Does someone have a quick fix for my problem ?

Cheers


PS: this is a concrete example of my problem.

$LOOP$ $IF$forward(0);
/* Here I want to set a macro property on 0 in order to replace it by a value of a property of the FORWRD concept, which is not available as we are iterating over INSTRUCTION concepts
(FORWARD extends INSTRUCTION) */




If I understood right, you have Instruction subconcepts to be a children of a Root node. In this case you might have more freedom for language evolution if you map Reduction rules for every subconcept of Instruction, a template for each subconcept of Instruction (maybe declared directly in reduction rule), and simply do $LOOP$(root.instructions)$COPY_SRC$(node) in your Root template.

Also, it might be helpful for you to see some examples that bundled with MPS and see documentation (which I find very comprehensive for beginners), especially Generator section - http://confluence.jetbrains.com/display/MPSD30/Generator

There are a couple of ways (that I know of, probably more) to do this. One is to use $COPY_SRCL on your Instruction list and then add reduction rules with a template fragment for each subconcept. This is how I usually end up solving problems like this.

Another way is to use a template switch, but that can get messy quickly if you have a lot of subconcepts.

If you throw your code up on Github or somewhere else, I can take a look at it and try to help you out.

Thanks,

Mike

Thanks for the fast reply !

I'll try your recommendations and keep you informed if I manage to do what I want or if I ridiculously fail =)
In the meanwhile I join my project to this message. It's pretty small and straightforward, as I just want to have a first look at MPS. (The SVN I use can't be accessed from outside of my institution)



Logo.tar (520KB)

There's still something I don't get ... It's how to go from a node of an Abstract type to this same node but "casted" to the implementing type.

In other words, If forward extends the abstract concept Instruction, How to go from node<Instruction> to node<Forward>.

If you ask about cast, you can achieve this by using a colon operator ( : )

node<Instruction> node = getNode();
if (node.isInstance(Forward)){
    node<Forward>fwd = node : Forward;
    // ...
}

Deat Askar, thanks a lot !

That's exactly what I was looking for ! And it seems so simple that I feel a bit ....

Anyway, thanks a lot fellows !