Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Boo.Lang.Compiler
- import Boo.Lang.Compiler.Ast
- import Boo.Lang.Compiler.TypeSystem
- import Boo.Lang.Compiler.TypeSystem.Services
- import Boo.Lang.Compiler.MetaProgramming
- import Boo.Lang.Environments
- macro delegate:
- case [| delegate $field as $fieldType |]:
- enclosingType = delegate.GetAncestor of TypeDefinition()
- Context.Parameters.Pipeline.AfterStep += do (sender, args):
- if args.Step isa Boo.Lang.Compiler.Steps.MacroAndAttributeExpansion:
- implementDelegationOf field, fieldType, enclosingType
- def implementDelegationOf(field as ReferenceExpression, fieldType as TypeReference, typeDef as TypeDefinition):
- for member in membersOf(fieldType):
- match member:
- case IMethod(Name: methodName, ReturnType: returnType):
- newMethod = [|
- def $methodName() as $returnType:
- $(field.CloneNode()).$methodName()
- |]
- typeDef.Members.Add(newMethod)
- def membersOf(typeRef as TypeReference):
- my(NameResolutionService).ResolveTypeReference(typeRef)
- return (typeRef.Entity cast INamespace).GetMembers()
- [extension] def op_Implicit(type as IType) as TypeReference:
- return my(BooCodeBuilder).CreateTypeReference(type)
- code = [|
- class Foo:
- def Bar():
- print "Foo.Bar"
- def Baz():
- print "Foo.Baz"
- class Gazonk:
- delegate foo as Foo
- foo = Foo()
- |]
- result = compile_(code, System.Reflection.Assembly.GetExecutingAssembly())
- print result.CompileUnit.ToCodeString()
- assert len(result.Errors) == 0, result.Errors.ToString(true)
- gazonk as duck = result.GeneratedAssembly.GetType("Gazonk")()
- gazonk.Bar()
- gazonk.Baz()
Add Comment
Please, Sign In to add comment