pluginsupport.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Plugin support for the Nim compiler. Right now they
  10. ## need to be build with the compiler, no DLL support.
  11. import ast, semdata, idents
  12. type
  13. Transformation* = proc (c: PContext; n: PNode): PNode {.nimcall.}
  14. Plugin = ref object
  15. fn, module, package: PIdent
  16. t: Transformation
  17. next: Plugin
  18. proc pluginMatches(p: Plugin; s: PSym): bool =
  19. if s.name.id != p.fn.id:
  20. return false
  21. let module = s.skipGenericOwner
  22. if module == nil or module.kind != skModule or
  23. module.name.id != p.module.id:
  24. return false
  25. let package = module.owner
  26. if package == nil or package.kind != skPackage or
  27. package.name.id != p.package.id:
  28. return false
  29. return true
  30. var head: Plugin
  31. proc getPlugin*(fn: PSym): Transformation =
  32. var it = head
  33. while it != nil:
  34. if pluginMatches(it, fn): return it.t
  35. it = it.next
  36. proc registerPlugin*(package, module, fn: string; t: Transformation) =
  37. let oldHead = head
  38. head = Plugin(fn: getIdent(fn), module: getIdent(module),
  39. package: getIdent(package), t: t, next: oldHead)