Added ability for methods to have pragmas.

This commit is contained in:
Joey Payne 2015-10-07 23:02:43 -06:00
commit edc058c253

View file

@ -173,6 +173,8 @@ macro class*(head: untyped, body: untyped): untyped =
var procName = "" var procName = ""
setNodeName(n2, procName, typeName) setNodeName(n2, procName, typeName)
# add the base pragma when it's a base method
n2[4] = newNimNode(nnkPragma).add(ident"base")
else: else:
discard discard
@ -183,11 +185,16 @@ macro class*(head: untyped, body: untyped): untyped =
of nnkMethodDef, nnkProcDef: of nnkMethodDef, nnkProcDef:
# inject `self: T` into the arguments # inject `self: T` into the arguments
let n = copyNimTree(node) let n = copyNimTree(node)
# clear pragma since we forward declared already
n[4] = newEmptyNode()
n.params.insert(1, newIdentDefs(ident(objReference), typeName)) n.params.insert(1, newIdentDefs(ident(objReference), typeName))
# Copy the proc or method for inheritance # Copy the proc or method for inheritance
# ie: procName_ClassName() # ie: procName_ClassName()
let n2 = copyNimTree(node) let n2 = copyNimTree(node)
# clear pragma since we forward declared already
n2[4] = newEmptyNode()
n2.params.insert(1, newIdentDefs(ident(objReference), typeName)) n2.params.insert(1, newIdentDefs(ident(objReference), typeName))
let typeName = $(typeName.toStrLit()) let typeName = $(typeName.toStrLit())
@ -275,7 +282,7 @@ macro class*(head: untyped, body: untyped): untyped =
typeDecl[0][0][2][0][2] = recList typeDecl[0][0][2][0][2] = recList
result.insert(0, typeDecl) result.insert(0, typeDecl)
when isMainModule:
class Animal of RootObj: class Animal of RootObj:
var var
name: string name: string
@ -307,7 +314,6 @@ class Tiger of Cat:
# no need for super.super! # no need for super.super!
self.vocalizeAnimal() & "Rawr!" self.vocalizeAnimal() & "Rawr!"
if isMainModule:
var animals: seq[Animal] = @[] var animals: seq[Animal] = @[]
animals.add(new Dog(name: "Sparky", age: 10)) animals.add(new Dog(name: "Sparky", age: 10))
animals.add(new Cat(name: "Mitten", age: 10)) animals.add(new Cat(name: "Mitten", age: 10))