Add nim glue for object construction
This commit is contained in:
parent
3763a095cb
commit
14529c3fe1
1 changed files with 29 additions and 0 deletions
|
|
@ -1,3 +1,32 @@
|
|||
import macros
|
||||
|
||||
proc getChildren(node: NimNode, nodeKind: NimNodeKind): seq[NimNode] =
|
||||
var stack: seq[NimNode] = @[node]
|
||||
result = @[]
|
||||
|
||||
while stack.len() > 0:
|
||||
let newNode = stack.pop()
|
||||
for i in 0 ..< newNode.len():
|
||||
let child = newNode[i]
|
||||
if child.kind == nodeKind:
|
||||
result.add(child)
|
||||
else:
|
||||
stack.add(child)
|
||||
|
||||
macro `@`*(objName: typed, fields: untyped): untyped =
|
||||
|
||||
let objImpl = objName.getImpl()
|
||||
let identDefs = objImpl.getChildren(nnkIdentDefs)
|
||||
|
||||
result = newNimNode(nnkObjConstr).add(objImpl[0])
|
||||
|
||||
for i in 0..<len(fields):
|
||||
let childLit = fields[i]
|
||||
let idDef = identDefs[i]
|
||||
|
||||
let colonExpr = newColonExpr(idDef[0], childLit)
|
||||
result.add(colonExpr)
|
||||
|
||||
proc preDec*[T: Ordinal | uint | uint64](x: var T; y = 1): T {.discardable.} =
|
||||
dec x, y
|
||||
result = x
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue