Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- proc parseVars(node: NimNode): (seq[string], NimNode) =
- ## Traverses the AST to find any identifiers
- ## and either replaces them with a constraint
- ## vararg access, or treats them as a Nim
- ## identifier if prefixed `u` or not alphanumeric.
- ##
- ## Return: The Variable names in AST First Occurence Order
- var tree = node.copy
- var queue = initHeapQueue[NimNode]()
- queue.push(tree)
- var variables = newTable[string, int]()
- while queue.len > 0:
- var current = queue.pop
- for i in 0..<current.len:
- var child = current[i]
- ## Find any Identifiers on the current Node
- if child.kind == nnkIdent:
- if child.strVal[0] == 'u':
- ## If tagged as a literal, keep it, but drop tag
- current[i] = ident(child.strVal.substr(1))
- elif child.strVal.isAlphaNumeric:
- ## Any untagged identifier is assumed a Variable
- ## Track its order in the AST
- if not variables.hasKeyOrPut(child.strVal, variables.len):
- result[0].add(child.strVal)
- ## Then replace the Identifier with an Array Access
- ## corresponding to its location in the constraint
- current[i] = newNimNode(nnkBracketExpr).add(
- ident("x"),
- newIntLitNode(variables[child.strVal])
- )
- echo tree.treeRepr
- else:
- queue.push(child)
- return (result[0], tree)
Advertisement
Add Comment
Please, Sign In to add comment