Skip to content

Instantly share code, notes, and snippets.

@bamboo
Last active December 26, 2015 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bamboo/7071426 to your computer and use it in GitHub Desktop.
Save bamboo/7071426 to your computer and use it in GitHub Desktop.
Using assembly attributes to trigger code transformations
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