Generate Java functions with n parameters Follow
Hi everyone,
I mentioned in a previous thread that I am working on a toy-language as a learning-by-doing approach to MPS.
I would like to implement a generator that creates a Java function with n parameters.
My DSL allows the user to write something like
CLASS Name
FUNC Name1
PARAM P1:SomeTypeA
PARAM P2:SomeTypeB
FUNC Name2
<...>
The generated Java output should be something like
I already created a $LOOP$ to generate the function stubs. But how do I create the lists of parameters? Can I achieve this with the $LOOP$ macro as well? If yes, how do I get the comma in between parameters?
One additional question: The name of a function is derived by saying "Func" + node.name in the respective property macro. Let's assume I have to refer to those names in other places. Can I reference a name somehow? Please note that a $LOOP$ generates the functions and their names but I would like to refer to only one of those functions (for instance to simulate a call).
I mentioned in a previous thread that I am working on a toy-language as a learning-by-doing approach to MPS.
I would like to implement a generator that creates a Java function with n parameters.
My DSL allows the user to write something like
CLASS Name
FUNC Name1
PARAM P1:SomeTypeA
PARAM P2:SomeTypeB
FUNC Name2
<...>
The generated Java output should be something like
public static void FuncName1(SomeTypeA P1, SomeTypeB P2) {} public static void FuncName2() {}
I already created a $LOOP$ to generate the function stubs. But how do I create the lists of parameters? Can I achieve this with the $LOOP$ macro as well? If yes, how do I get the comma in between parameters?
One additional question: The name of a function is derived by saying "Func" + node.name in the respective property macro. Let's assume I have to refer to those names in other places. Can I reference a name somehow? Please note that a $LOOP$ generates the functions and their names but I would like to refer to only one of those functions (for instance to simulate a call).
Please sign in to leave a comment.
Have you considered trying the generator tutorial to get a good grasp on these techniques? https://confluence.jetbrains.com/display/MPSD31/Generator+Demos
thanks, that brought me a step further.
I now have the following code fragment:
I want to generate this public, static init function which has multiple PARAMs (hence the $LOOP$). I use the $[p] property macro to generate unique names for those parameters. So that works.
But how can I now refer to those parameters one by one in the function's body?
Vaclav
I just tried your proposal and it worked.
I now have a similar case for which I wonder what the most efficient way of implementation in MPS is.
Suppose I have generated classes ACls and BCls, which have been created from some root template that handles instances of concept Cls. Cls implements INamedConcept in order to generate the class-names, i.e. class ACls has been generated for an instance of concept Cls with the name property being set to "A". Additionally the template generates setter functions for child-concepts of Cls, where the setter-names are derived from the respective child's concept-name.
A potential output:
A second root template is used to generate another class which has a static function. That function will take an instance of
ACls and BCls and calls their setter functions. The function is similar to the following
Note, that the generated names ACls and BCls appear as types. Similarly the generated setter functions for ACls and BCls should be used.
Now, I would like to know if there is an easy way to achieve this. This is very similar to my previous issue so I assume I have to use labels as well. But do I really need to create a separate label for the macro that generates the setter and the macro that generates the class names?
Or can I use only one label somehow?
I hope my explanations were clear enough.
Vaclav
genContext.get output myLabel for (node : Cls);
This gives me a parameter like: ACls a
How can I now call one of the generated setter functions, as in: a.setAlpha(...)?
I tried: a.->$[set]();
but how can I get the function name via genContext?
genContext.get output myLabel for (node : Cls).methods().where {//Here you need to identify the required generated method//};
I assume there's some logic in naming the methods that can be used to retrieve the correct method.
everything came together nicely. I am able to generate the code I want. Thanks for your valuable support. It helped me a lot.
But I still do have a question:
How can I use the generated Java files in a solution/model?
I have a Sandbox solution which contains a model that generates code based on my DSL. I now want to use those generated Java files in the same sandbox solution but in a different model where I want to extend the generated classes.
My structure should be similar to:
[S] sandbox
[M] gen
(N) Using my DSL -> generate Java classes
[M] impl
(C) SomeClass extending generated Java class
Then in your impl model set a Dependency on the java_stub model and you should be ready.
You may check out the details on setting model roots at https://confluence.jetbrains.com/display/MPSD32/Getting+the+dependencies+right#Gettingthedependenciesright-Solution
Vaclav