Administrator
|
To generate code, I used to use templates like the following (contrived example): ```smalltalk
``` This is *okay* for simple cases, but can get unwieldy, and doesn't benefit from e.g. (early) compiler warnings. It is also lacking when one wants to offer the behavior as a method and also as a script with no dependencies (maybe for CI). To overcome some of these limitations, I started saving the code template as an actual method and transforming the AST, e.g.: ```smalltalk
``` When I started doing this, I noticed two things: 1. There were idioms that were repeated over and over. 2. It still isn't super easy to turn a method into a script with no dependencies (e.g. self sends) So, I did a spike to wrap a few common idioms for this use case. It works like this. Say you have a method like this: ```
``` And you want to also provide it as source code for a script with no dependencies. You can do: ```
``` which would generate (for example, for a particular instance): ```
``` So my questions are: - Does something like this already exist? - Are there better ways to solve this problem? - Does this solution look promising/generally-helpful?
Cheers,
Sean |
Hi Sean, I'm aware of Julien Delplanque which has done something a bit
like this, or my own project. Quickly, my way of doing things is to use blocks instead of
strings. ```smalltalk generateValidatorVisitOf: aClass method := visitClass
``` This is a temp
variable, used as a selector, which will be replace when evaluating the
#asMethodWithBody:withArguments: message send. ```smalltalk visitExpression: anExpression ``` Have a nice day ! Pierre. On 16/11/2020 23:55,
[hidden email] wrote:
|
Administrator
|
hogoww wrote
> I'm aware of Julien Delplanque which has done something a bit like this, > or my own project. Thanks! Both interesting and good to know about. Your project took me a while to understand by reading and rereading the examples in the readme, but I really like the block idea. I think Julien's could work for my use case because there is a feature to generate his AST model from an existing method. One day, I'd love to sit down and properly compare all three approaches. I'll add it to the never-ending TODOs! ----- Cheers, Sean -- Sent from: http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html
Cheers,
Sean |
In reply to this post by hogoww
Hi Pierre, El mar., 17 nov. 2020 a las 5:04, hogoww (<[hidden email]>) escribió:
This is something which I've been expecting and trying to find some time to do myself. Just to be clear, you mean something like: [ :arg1 | arg1 + 0 ] , [ :arg2 | arg2 + 7 ] which would give: [ :arg1 :arg2 | arg1 + 0. arg2 + 7 ] Did I understand well? Thanks for this report! Cheers, Hernán
|
Indeed ! I'm going to show you a simplified example here, hopefully it'll
be understandable. body := [ :anInstance | ]
asPlainCodeBC. We first declare a block that we can
concatenate stuff too, then we add others blocks to it. The second part is
exactly the same as the first one, but for instance variables
that do not have to be iterate over. And an example of
generated code: visitStructureDefinition:
aStructureDefinition This
is an indexable object, a collection. Note that I initially
went with #+, but #, makes as much or more sense ! :)
Pierre On 18/11/2020 08:42, Hernán Morales
Durand wrote:
|
Free forum by Nabble | Edit this page |