Last active
December 26, 2015 01:29
-
-
Save bamboo/7071426 to your computer and use it in GitHub Desktop.
Using assembly attributes to trigger code transformations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Spikes | |
import Boo.Lang.PatternMatching | |
import Boo.Lang.Compiler | |
import Boo.Lang.Compiler.Ast | |
import Boo.Lang.Compiler.MetaProgramming | |
class EnableResponsePatternAttribute(AbstractAstAttribute): | |
override def Apply(node as Node): | |
node.Accept(ResponseTransformer()) | |
class ResponseTransformer(DepthFirstTransformer): | |
override def OnExpressionStatement(node as ExpressionStatement): | |
match node.Expression: | |
case [| $target = response |]: | |
replacement = [| | |
yield responseRef = Response() | |
$target = responseRef.Value | |
|] | |
ReplaceCurrentNode replacement | |
otherwise: | |
pass | |
class Response: | |
property Value | |
def spike(): | |
code = [| | |
import Spikes | |
def run(): | |
r = response | |
print r | |
for r in run(): | |
r.Value = "pong" | |
[assembly: EnableResponsePattern] | |
|] | |
result = compile_(code, typeof(EnableResponsePatternAttribute).Assembly) | |
#print code.ToCodeString() | |
assembly = result.GeneratedAssembly | |
assert assembly is not null | |
assert assembly.EntryPoint is not null | |
assembly.EntryPoint.Invoke(null, (null,)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment