Hey guys,
I'm writing a python-to-nim transpiler and ran into a circular dependency problem for some procedure calls. Can I get some feedback/advice on a way to handle this?
Here are two sample procs:
# this proc adds a new IF statement tree to parent ast tree
proc addIf(nimTree: NimNode, pyNode: JsonNode) =
# some logic here
case pyNode["body"]["_type"]
of "For": # python for loop
nimTree.addFor(pyNode["body])
else: discard
# more logic here
# this proc adds a new For Loop statement tree to parent ast tree
proc addFor(nimTree: NimNode, pyNode: JsonNode) =
# logic here
case pyNode["body"]["_type"]
of "If": # python if statement
nimTree.addIf(pyNode["body"])
# more logic here
For some background, the way I'm currently approaching this problem:
Now I'm at the point where some procs are calling other procs... that call the previous proc, etc., like the example above.
I'm using the {.experimental:"codeReordering".} pragma, but this only applies to top-level code.
Any feedback/advice based on what you see?
If you need to see more examples, have any questions, etc. fire away!
The usual way to handle this problem is to just add forward declarations of your procedures:
proc addFor(nimTree: NimNode, pyNode: JsonNode)
# this proc adds a new IF statement tree to parent ast tree
proc addIf(nimTree: NimNode, pyNode: JsonNode) =
# some logic here
case pyNode["body"]["_type"]
of "For": # python for loop
nimTree.addFor(pyNode["body])
else: discard
# more logic here
# this proc adds a new For Loop statement tree to parent ast tree
proc addFor(nimTree: NimNode, pyNode: JsonNode) =
# logic here
case pyNode["body"]["_type"]
of "If": # python if statement
nimTree.addIf(pyNode["body"])
# more logic here
Thank you my good sir, so far this has been the solution.
I actually went back and read the entire manual again- I've been coding Nim for a year now and this entire time I thought forward declarations for procs were simply defining the entire proc, including its body, before using it somewhere ¯_(ツ)_/¯