Tutorial Question

Hello.  First, I'd like to say I'm very excited about the promise of MPS.  I'm starting with the tutorial @ http://www.jetbrains.com/mps/docs/tutorial.html

I've made it through the tutorial, everything works, but I have a two areas that I don't exactly understand what I've done.  I'd appreciate any clarification you could provide.

First.  In the Generator section where we are asked to label field declarations.  I'm unclear as to why this is necessary.  Just to see the difference it had on the generated source code, I did NOT create any labels, or reference them in my input/output fields.  The generated code was THE SAME as when the labels were there.  Is this just to illustrate a concept that will be important with more complicated generators?

Second.  In the section where the $COPY_SRC$ macro is used.  Is there any further detail as to what is exactly happening in the reduction rule.  I'm unclear as to the statement int i; i = i + 1;  What does i = i + 1 have to do with anything?  Is this just some sort of dummy statement to make sure that i is used?  Could you clarify how the reduction rule is applied and how the template fragment is used?

Third.  After completing the tutorial, what is the next logical step to enhance my learning?  Move on to the user guide?

Thanks in advance for any input.

Thanks.

-jon

1 comment
Comment actions Permalink

Hi!

Is this just to illustrate a concept that will be important with more complicated generators?

Yes. :-)

What does i = i + 1 have to do with anything?  Is this just some sort of dummy statement to make sure that i is used?  Could you clarify how the reduction rule is applied and how the template fragment is used?

By wrapping null with $COPY_SRC$ macro we are asking generator to replace null with the actual content of this macro (node.expression) and use other existing code generators to generate code for expression (generators from BaseLanguage). As we extend Expression concept with our custom concept (InputFieldReference) we need to specify code generator for this concept in our language.

Each input field value was assigned to a local variable with unique name (see $LOOP$[$MAP_SRC$LocalVar[int $[i] = 0];] construction in update() method of CalculatorImpl template), so now we'd like to substitute InputFieldReference with reference to this local variable - instance of LocalVariableReference concept from BaseLanguage. In other words, expression like: 2 + 3 + 9 + width should be transformed to 2 + 3 + 9 + i_a where i_a is a reference to local variable generated for input field width.

Let's discuss possible ways to create proper LocalVariableReference in our generator. Actually, you can use simple <in-line template> keeping LocalVariableReference concept instance in it and set corresponding $REFERENCE_MACRO$ to point to a proper local variable, but in this case additional error will be produced by BaseLanguage since LocalVariableReference pointing to un-existing local variable was detected. You can ignore this error and generate code, but in the tutorial we decided to keep templates error-less, so we need to create some "context" nodes around this LocalVariableReference to set up proper environment (in terms of BaseLanguage - our target language) for it and then mark particular LocalVariableReference instance as a template fragment (node which will be "returned" from the template). This is the only reason for using <in-line template with context> with a BlockStatement in it. As you remember, we need to create following code inside this BlockStatement:

int i;

i = 1 + i;

This is a valid java expression, so we are using it as an "environment" for this in-line template and mark LocalVariableReference _i_ as a template fragment. Now template code is valid and this is the only difference with first way to create LocalVariableReference.

Third.  After completing the tutorial, what is the next logical step to enhance my learning?  Move on to the user guide?

Yes, I think you can move to the user guide or start using MPS for your project(s). Another option is to participate in MPS Training.

0

Please sign in to leave a comment.