[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-dev
Subject:    [groovy-dev] Macro extension methods
From:       Sergei Egorov <bsideup () gmail ! com>
Date:       2014-07-31 11:10:53
Message-ID: CAAJdKXnoRMEX+TuazPZOTWFYJSJ=hDY6N5XCnecVOmPcEu_xKA () mail ! gmail ! com
[Download RAW message or body]

Hello everyone!

I would like to introduce my new idea about Macro system for Groovy. This
is a brand new way to work with AST in Groovy, much bigger than just my old
"macro" method (but old macro method CAN be implemented with it)

First of all, I should not that this is still WIP and not ready to rigorous
review, but I think you will have what to say about it already.


Let's start from example:
https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy#L30
 https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/groovy- \
macro/src/main/java/org/codehaus/groovy/macro/runtime/MacroGroovyMethods.java#L68-84


Here you can see users side of this improvement. In simple words:
1) create extension methods class
2) create extension method with @Macro annotation on it
3) use AST nodes as arguments, not types itself
4) ???
5) Profit!

Now, every call to @Macro extension method will be executed at compile time
and will be transformed to returned expression.

Another good example - true inlines.

something like:

@Macro
public static Expression inlineSomeCode(Object self, Expression argument) {
   return new TernaryExpression(argument, argument, new
ConstantExpression(null));
}


every:
inlineSomeCode("hello")

will be translated in-place to:
"hello" ? "hello" : null

Without any additional Global AST transformations (only core macro
extension subsystem's one)



Implementation is pretty easy (except that fact that it's Grobal AST
transformation with GDM lookup):
https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/groovy-m \
acro/src/main/java/org/codehaus/groovy/macro/transform/MacroTransformation.java#L49-92




So please, I need your feedback. I'm pretty close to complete this feature
and send a new pull request instead of old one (about macro groovy)



Thanks!

-- 
Best regards,
Sergei Egorov


[Attachment #3 (text/html)]

<div dir="ltr"><div>Hello everyone!</div><div><br></div><div>I would like to \
introduce my new idea about Macro system for Groovy. This is a brand new way to work \
with AST in Groovy, much bigger than just my old &quot;macro&quot; method (but old \
macro method CAN be implemented with it)</div> <div><br></div><div>First of all, I \
should not that this is still WIP and not ready to  <span \
style="font-family:&#39;Helvetica Neue&#39;;font-size:13px">rigorous review, but I \
think you will have what to say about it already.</span></div> <div><font \
face="Helvetica Neue"><br></font></div><div><font face="Helvetica \
Neue"><br></font></div><div><font face="Helvetica Neue">Let&#39;s start from \
example:</font></div><div><a \
href="https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/gr \
oovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy#L30">https://github.com/bside \
up/groovy-core/blob/feature/macro_methods/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy#L30</a></div>
 <div><a href="https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subpr \
ojects/groovy-macro/src/main/java/org/codehaus/groovy/macro/runtime/MacroGroovyMethods \
.java#L68-84">https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subpro \
jects/groovy-macro/src/main/java/org/codehaus/groovy/macro/runtime/MacroGroovyMethods.java#L68-84</a></div>
 <div><br></div><div><br></div><div>Here you can see users side of this improvement. \
In simple words:</div><div>1) create extension methods class</div><div>2) create \
extension method with @Macro annotation on it</div><div> 3) use AST nodes as \
arguments, not types itself</div><div>4) ???</div><div>5) \
Profit!</div><div><br></div><div>Now, every call to @Macro extension method will be \
executed at compile time and will be transformed to returned expression.  </div> \
<div><br></div><div>Another good example - true inlines.  \
</div><div><br></div><div>something \
like:</div><div><br></div><div>@Macro</div><div>public static Expression \
inlineSomeCode(Object self, Expression argument) {</div> <div>     return new \
TernaryExpression(argument, argument, new \
ConstantExpression(null));</div><div>}</div><div><br></div><div><br></div><div>every:</div><div>inlineSomeCode(&quot;hello&quot;)</div><div><br></div><div>will \
be translated in-place to:</div> <div>&quot;hello&quot; ? &quot;hello&quot; : \
null</div><div><br></div><div>Without any additional Global AST transformations (only \
core macro extension subsystem&#39;s \
one)</div><div><br></div><div><br></div><div><br></div> <div>Implementation is pretty \
easy (except that fact that it&#39;s Grobal AST transformation with GDM \
lookup):</div><div><a \
href="https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/gr \
oovy-macro/src/main/java/org/codehaus/groovy/macro/transform/MacroTransformation.java# \
L49-92">https://github.com/bsideup/groovy-core/blob/feature/macro_methods/subprojects/ \
groovy-macro/src/main/java/org/codehaus/groovy/macro/transform/MacroTransformation.java#L49-92</a><br>
 </div><div><br></div><div><br></div><div><br></div><div>So please, I need your \
feedback. I&#39;m pretty close to complete this feature and send a new pull request \
instead of old one (about macro groovy)</div><div><br></div> \
<div><br></div><div><br></div><div>Thanks!</div><div><br></div>-- <br><div \
dir="ltr">Best regards,<div>Sergei Egorov</div></div> </div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic