Guest User

Untitled

a guest
Aug 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import Boo.Lang.Compiler
  2. import Boo.Lang.Compiler.Ast
  3. import Boo.Lang.Compiler.TypeSystem
  4. import Boo.Lang.Compiler.TypeSystem.Services
  5. import Boo.Lang.Compiler.MetaProgramming
  6. import Boo.Lang.Environments
  7.  
  8. macro delegate:
  9. case [| delegate $field as $fieldType |]:
  10. enclosingType = delegate.GetAncestor of TypeDefinition()
  11. Context.Parameters.Pipeline.AfterStep += do (sender, args):
  12. if args.Step isa Boo.Lang.Compiler.Steps.MacroAndAttributeExpansion:
  13. implementDelegationOf field, fieldType, enclosingType
  14.  
  15. def implementDelegationOf(field as ReferenceExpression, fieldType as TypeReference, typeDef as TypeDefinition):
  16.  
  17. for member in membersOf(fieldType):
  18. match member:
  19. case IMethod(Name: methodName, ReturnType: returnType):
  20. newMethod = [|
  21. def $methodName() as $returnType:
  22. $(field.CloneNode()).$methodName()
  23. |]
  24. typeDef.Members.Add(newMethod)
  25.  
  26. def membersOf(typeRef as TypeReference):
  27. my(NameResolutionService).ResolveTypeReference(typeRef)
  28. return (typeRef.Entity cast INamespace).GetMembers()
  29.  
  30. [extension] def op_Implicit(type as IType) as TypeReference:
  31. return my(BooCodeBuilder).CreateTypeReference(type)
  32.  
  33. code = [|
  34. class Foo:
  35. def Bar():
  36. print "Foo.Bar"
  37. def Baz():
  38. print "Foo.Baz"
  39.  
  40. class Gazonk:
  41. delegate foo as Foo
  42. foo = Foo()
  43. |]
  44.  
  45. result = compile_(code, System.Reflection.Assembly.GetExecutingAssembly())
  46. print result.CompileUnit.ToCodeString()
  47. assert len(result.Errors) == 0, result.Errors.ToString(true)
  48.  
  49. gazonk as duck = result.GeneratedAssembly.GetType("Gazonk")()
  50. gazonk.Bar()
  51. gazonk.Baz()
Add Comment
Please, Sign In to add comment