summary refs log tree commit diff stats
path: root/compiler/pluginsupport.nim
blob: 19a0bc84d2ea6659d2a85e0b3b7e3ec92f52ec6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Plugin support for the Nim compiler. Right now they
## need to be build with the compiler, no DLL support.

import ast, semdata, idents

type
  Transformation* = proc (c: PContext; n: PNode): PNode {.nimcall.}
  Plugin = ref object
    fn, module, package: PIdent
    t: Transformation
    next: Plugin

proc pluginMatches(p: Plugin; s: PSym): bool =
  if s.name.id != p.fn.id:
    return false
  let module = s.skipGenericOwner
  if module == nil or module.kind != skModule or
      module.name.id != p.module.id:
    return false
  let package = module.owner
  if package == nil or package.kind != skPackage or
      package.name.id != p.package.id:
    return false
  return true

var head: Plugin

proc getPlugin*(fn: PSym): Transformation =
  var it = head
  while it != nil:
    if pluginMatches(it, fn): return it.t
    it = it.next

proc registerPlugin*(package, module, fn: string; t: Transformation) =
  let oldHead = head
  head = Plugin(fn: getIdent(fn), module: getIdent(module),
                 package: getIdent(package), t: t, next: oldHead)