Added templates and getAst in place of quote do. Also replace PNimrodNode.

This commit is contained in:
Joey Payne 2015-07-05 20:51:23 -06:00
commit f7b1b8cd66

View file

@ -8,17 +8,20 @@ macro new*(obj: untyped): untyped=
## and calls the init() method on it ## and calls the init() method on it
if obj.kind == nnkObjConstr: if obj.kind == nnkObjConstr:
# Only if the node is an object constructor # Only if the node is an object constructor
result = quote do: template init_object(obj)=
var init_obj = `obj` var init_obj = obj
when compiles(init_obj.init()): when compiles(init_obj.init()):
init_obj.init() init_obj.init()
init_obj init_obj
result = getAst(init_object(obj))
else: else:
# Otherwise, just revert to system.new # Otherwise, just revert to system.new
result = quote do: template init_object(obj)=
var init_obj = `obj` var init_obj = obj
system.new(init_obj) system.new(init_obj)
result = getAst(init_object(obj))
macro class*(head: untyped, body: untyped): untyped= macro class*(head: untyped, body: untyped): untyped=
# The macro is immediate so that it doesn't # The macro is immediate so that it doesn't
@ -29,7 +32,7 @@ macro class*(head: untyped, body: untyped): untyped=
let obj_reference = "self" let obj_reference = "self"
var export_class: bool = false # whether or not to export the class to other modules var export_class: bool = false # whether or not to export the class to other modules
var typeName, baseName: PNimrodNode var typeName, baseName: NimNode
if head.kind == nnkIdent: if head.kind == nnkIdent:
# `head` is expression `typeName` # `head` is expression `typeName`
@ -188,7 +191,7 @@ macro class*(head: untyped, body: untyped): untyped=
# simply call the class method from here # simply call the class method from here
# proc procName= # proc procName=
# procName_ClassName() # procName_ClassName()
var p: seq[PNimrodNode] = @[] var p: seq[NimNode] = @[]
for i in 1..n.params.len-1: for i in 1..n.params.len-1:
p.add(n.params[i][0]) p.add(n.params[i][0])
if is_assignment: if is_assignment:
@ -228,21 +231,23 @@ macro class*(head: untyped, body: untyped): untyped=
# Ident !"int" # Ident !"int"
# Empty # Empty
var type_decl: PNimrodNode var type_decl: NimNode
template declare_type_export(tname, bname)=
type tname* = ref object of bname
template declare_type(tname, bname)=
type tname = ref object of bname
if baseName == nil: if baseName == nil:
if export_class: if export_class:
type_decl = quote do: type_decl = getAst(declare_type_export(typeName, RootObj))
type `typeName`* = ref object of RootObj
else: else:
type_decl = quote do: type_decl = getAst(declare_type(typeName, RootObj))
type `typeName` = ref object of RootObj
else: else:
if export_class: if export_class:
type_decl = quote do: type_decl = getAst(declare_type_export(typeName, baseName))
type `typeName`* = ref object of `baseName`
else: else:
type_decl = quote do: type_decl = getAst(declare_type(typeName, baseName))
type `typeName` = ref object of `baseName`
# Inspect the tree structure: # Inspect the tree structure:
# #