Version 11 by komaz
on Nov 22, 2010 19:22.

compared with
Version 12 by komaz
on Nov 22, 2010 19:34.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (4)

View Page History
{
loc := expr.loc
doMagic(type(loc, expr), slot(loc, expr), expr.src)
return LiteralExpr.makeNull(expr.loc, ns)
}
** then magic applied to type, otherwise - to method
**
abstract Void doMagic(TypeDef type, SlotDef? slot)
abstract Void doMagic(TypeDef type, SlotDef? slot, Str src := "")

... ton of helper methods for simplifying AST manipulation ...
}
{noformat}
The implementation of {{type}} and {{slot}} methods searching for appropriate definitions can be found [here|^MagicPlugin.fan].

Later, we can write plugins as simple like this:
{noformat}
class EchoMagic : MagicPlugin
{
new make(Compiler c) : super(c) {}

override Void doMagic(TypeDef t, SlotDef? s, Str src := "")
{
echo("type - $t")
echo("slot - $s")
echo("src - $src")
}
}
{noformat}

And use them like this:
{noformat}
@Magic { kind = Echo<||> }
class Sample
{
@Magic { kind = Echo<||> }
Void method() {}

@Magic { kind = Echo<||> }
Int slot := 1

Void main() { echo("hello, world!") }
}
{noformat}

h3. Conclusion

The DSL plugins seem to be powerful and dangerous, though quite complex tool, allowing to completely change the meaning of the source code. They strongly rely on Compiler API, which is quite big and likely to be changed. Also there's no clear understanding at which stage the DSL plugins are invoked, and sometimes it may be hard to predict all consequences of AST modifications, so it might be too risky to write something heavy using DSLs.

On the other hand, in some cases it can provide pretty elegant and robust solutions which can replace a ton of boilerplate code with full support of compile-time validation.

So, it seems to me that DSLs today are sort of black magic, and probably one day it should be revisited and well-organized.