Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import ../stint/private/datatypes
  2. import macros
  3.  
  4. var a: UintImpl[UintImpl[uint64]]
  5.  
  6. proc asWordsImpl(x: NimNode, current_path: NimNode, result: var NimNode) =
  7. ## Transforms an UintImpl/IntImpl into an array of words
  8. ## at compile-time. Recursve implementation.
  9. ## Result is from most significant word to least significant
  10.  
  11. let
  12. node = x.getTypeInst
  13.  
  14. if node.kind == nnkBracketExpr:
  15. assert eqIdent(node[0], "UintImpl") or eqIdent(node[0], "IntImpl")
  16.  
  17. let hi = nnkDotExpr.newTree(current_path, newIdentNode("hi"))
  18. let lo = nnkDotExpr.newTree(current_path, newIdentNode("lo"))
  19. asWordsImpl(node[1], hi, result)
  20. asWordsImpl(node[1], lo, result)
  21. else:
  22. result.add current_path
  23.  
  24. macro asWords*(x: UintImpl or IntImpl, idx: static[int]): untyped =
  25.  
  26. var words = nnkBracket.newTree()
  27. asWordsImpl(x, x, words)
  28.  
  29. result = words[idx]
  30.  
  31. echo a.asWords(2)
  32.  
  33. # macro asWords*(x: ForLoopStmt): untyped =
  34. # ## This unrolls the body of the for loop
  35. # ## and applies it for each word.
  36.  
  37. # echo x.treerepr
  38.  
  39. # # ##### Tree representation
  40. # # for word_a, word_b in asWords(a, b):
  41. # # discard
  42.  
  43. # # ForStmt
  44. # # Ident "word_a"
  45. # # Ident "word_b"
  46. # # Call
  47. # # Ident "asWords"
  48. # # Ident "a"
  49. # # Ident "b"
  50. # # StmtList
  51. # # DiscardStmt
  52. # # Empty
  53.  
  54. # # 1. Get the words variable idents and multiprecision ints ident to iterate on
  55. # var wordsIdents = nnkBracket.newTree
  56. # var idx = 0
  57. # while x[idx].kind == nnkIdent:
  58. # wordsIdents.add x[idx]
  59. # inc idx
  60.  
  61. # var stintsIdents = nnkBracket.newTree
  62. # idx = 1
  63. # while idx < x[wordsIdents.len].len and x[wordsIdents.len][idx].kind == nnkIdent:
  64. # stintsIdents.add x[wordsIdents.len][idx]
  65. # inc idx
  66.  
  67. # assert wordsIdents.len == stintsIdents.len, "The number of loop variables and multiprecision integers t iterate on must be the same"
  68.  
  69. # # 2. For each stints, get the words from most significant to least significant
  70. # var words = nnkBracket.newTree
  71. # for ident in stintsIdents:
  72. # var wordList = nnkBracket.newTree()
  73. # asWordsImpl(ident, ident, wordList)
  74. # words.add wordList
  75.  
  76. # result = words[0]
  77.  
  78. # for word_a, word_b in asWords(a, a):
  79. # discard
Add Comment
Please, Sign In to add comment