Skip to content

Instantly share code, notes, and snippets.

@flaviut
Created April 28, 2014 03:02
Show Gist options
  • Save flaviut/11360767 to your computer and use it in GitHub Desktop.
Save flaviut/11360767 to your computer and use it in GitHub Desktop.
import macros, strutils
macro `:=`(a, b: expr): stmt {.immediate.} =
let tempVar = newIdentNode(!"tmp")
result = newLetStmt(tempVar, b)
var i = 0
for child in a.children:
var arrAccess = newNimNode(nnkBracketExpr)
.add(tempVar)
.add(newIntLitNode(i))
if child.kind == nnkExprColonExpr:
result.add(newVarStmt(child[0], arrAccess))
elif child.kind == nnkIdent:
result.add(newVarStmt(child, arrAccess))
else:
error("Wrong expr " & treeRepr(child))
inc i
(var1, var2, var3) := @[1, 2, 3]
dumpTree:
a[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment