-
-
Save zacharycarter/9dd9d4399e418589b4a44eb368373706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro genSetProc(t: typedesc): untyped = | |
let | |
tImpl = t.getImpl | |
tsym = tImpl[0] | |
procName = ident("set" & $tSym) | |
doAssert tImpl[2].kind == nnkObjectTy | |
var | |
idxNameType = newSeq[(int, string, NimNode)]() | |
idx = 1 | |
for ch in tImpl[2][2]: | |
case ch.kind | |
of nnkEmpty: | |
discard | |
of nnkIdentDefs: | |
let (name, dtype) = extractNameType(ch) | |
idxNameType.add (idx, name, dtype) | |
else: | |
error("genSetProc ??? " & $ch.treeRepr) | |
inc idx | |
echo idxNameType.repr | |
let | |
oId = ident"o" | |
aId = ident"a" | |
icId = ident"ic" | |
mId = ident"m" | |
pnId = ident"pn" | |
sId = ident"s" | |
var procBody = newStmtList() | |
procBody.add quote do: | |
var `pnId` = nkObjConstr.newTree() | |
`pnId`.add nkEmpty.newNode() | |
let resId = ident"result" | |
for (idx, name, dtype) in idxNameType: | |
procBody.add quote do: | |
var `sId` = newSym(skField, `icId`.getIdent(`name`), `mId`, `mId`.info) | |
case dtype.kind | |
of nnkSym, nnkIdent: | |
var nId = ident"name" | |
procBody.add quote do: | |
`pnId`.add nkExprColonExpr.newTree( | |
newSymNode(`sId`), | |
nkStrLit.newStrNode(`oId`.`nId`) | |
) | |
of nnkBracketExpr: | |
discard | |
# procBody.add handleSeq(nId, idx, name, dtype) | |
else: | |
echo "No was ", dtype.kind | |
discard | |
procBody.add quote do: | |
setResult(`aId`, `pnId`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment