Skip to content

Instantly share code, notes, and snippets.

@Vindaar
Last active March 27, 2018 15:21
Show Gist options
  • Save Vindaar/39358d5cd110ace7c08066604a616036 to your computer and use it in GitHub Desktop.
Save Vindaar/39358d5cd110ace7c08066604a616036 to your computer and use it in GitHub Desktop.
macro setTabFields[T: (float | int), N: int](tab: Table[string, seq[seq[T]]],
names: array[N, string],
chip: int,
obj: untyped): untyped =
## taking some table `tab` sets all fields of the table taken from an array `names` to the
## fields of the object `obj` of the same names as the names in the table
result = newStmtList()
let namesImpl = names.symbol.getImpl
for name in namesImpl:
let field = parseExpr(name.strVal)
result.add quote do:
`tab`[`name`][`chip`].add `obj`.`field`
macro `+`[N, M: int](a: array[N, string], b: array[M, string]): untyped =
let aImpl = a.symbol.getImpl
let bImpl = b.symbol.getImpl
var tree = nnkBracket.newTree()
for x in aImpl:
tree.add quote do:
`x`
for x in bImpl:
tree.add quote do:
`x`
result = nnkStmtList.newTree(
tree
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment