Variable assignment with macros
Hello!
I am trying to learn MPS basics.
Pretty much of a problem is inside the project attached.
For an example, I want to create a tiny DSL for manipulating 3-dimensional points. By now I am able to create points and make some handy literals for points representation. But when I want to assign a value from a literal to a point valiable, I get stuck with building generator for it. Can you please take a look at my project and recommend the proper way for building that type of a generator?
Best regards!
Attachment(s):
Points.zip
Please sign in to leave a comment.
Hi,
i made the following changes to your project (see result attached):
1) created property macros for your point-values in "reduce_PointLiteral"
2) replaced the <no_name>-errors in your templates with a placeholder "_noname_" (no matter what) to have mps not complain about errors when generating
Now you can generate the text by righ-click on "Points.sandbox.sandbox.Main" -> "Generate Text" (within Project-View) and you get the point-constructor-call in your output with the values of x,y,z.
Hope that helps!
Regards,
Mirko
Attachment(s):
Points.zip
Hi!
Thanks a lot, that's exactly what I needed! But could you explain me what you did?:)
There are links to some dummy variables of float point type in the generator as I understand. So, if we have a complex type instead of a float-point value in Point constructor, is the generator going to work the same method (using links to dummies of that type) or I get something wrong?
Hi,
in the generator you define the type to be double. If you would use f.i. string here, your template would break. What the property-macro does is: copy the primitive value to the cell the macro surrounds. MPS - magically - does the rest (check for validity) in that case.
You can easily check that behavior out by changing the type of x,y,z to string (in your concept) and providing some none-double-character-sequence in your literal (f.e. "foo"). Then enable "Save Transient Models" (via settings or the "Generate"-Menu) and confirm the popup with "Save Transient Models". What you get is the intermediate model as last entry ("transient models") of your project-tree. Here you will see that - even if the text is (or better: seems to be) generated properly - the model is broken.
Regards,
Mirko
Thanks for your explainations!
Now, I would like to clear up one more thing: if I have a Point type with 3 coordinates (custom type declared in a runtime solution) and another type, say Cube, which contains exactly eight Points as fields, how do I pass these Points (existing variables by their names) to Cube constructor in the generator? What macro or generator rule can help me with this?
Hi!
Following modifications were made :
So, now you can generate text for Points.sandbox to see an output.
I've used in-line templates with $COPY_SRC$ macros.
Attachment(s):
Points.zip
Thank you very much, that really helped me understand MPS a little more!