topt_no_cursor.nim 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. discard """
  2. output: '''(repo: "", package: "meo", ext: "")
  3. doing shady stuff...
  4. 3
  5. 6
  6. (@[1], @[2])
  7. 192.168.0.1
  8. 192.168.0.1
  9. 192.168.0.1
  10. 192.168.0.1'''
  11. cmd: '''nim c --gc:arc --expandArc:newTarget --expandArc:delete --expandArc:p1 --expandArc:tt --hint:Performance:off --assertions:off --expandArc:extractConfig --expandArc:mergeShadowScope --expandArc:check $file'''
  12. nimout: '''--expandArc: newTarget
  13. var
  14. splat
  15. :tmp
  16. :tmp_1
  17. :tmp_2
  18. splat = splitFile(path)
  19. :tmp = splat.dir
  20. wasMoved(splat.dir)
  21. :tmp_1 = splat.name
  22. wasMoved(splat.name)
  23. :tmp_2 = splat.ext
  24. wasMoved(splat.ext)
  25. result = (
  26. let blitTmp = :tmp
  27. blitTmp,
  28. let blitTmp_1 = :tmp_1
  29. blitTmp_1,
  30. let blitTmp_2 = :tmp_2
  31. blitTmp_2)
  32. `=destroy`(splat)
  33. -- end of expandArc ------------------------
  34. --expandArc: delete
  35. var
  36. sibling
  37. saved
  38. `=copy`(sibling, target.parent.left)
  39. `=copy`(saved, sibling.right)
  40. `=copy`(sibling.right, saved.left)
  41. `=sink`(sibling.parent, saved)
  42. `=destroy`(sibling)
  43. -- end of expandArc ------------------------
  44. --expandArc: p1
  45. var
  46. lresult
  47. lvalue
  48. lnext
  49. _
  50. lresult = @[123]
  51. _ = (
  52. let blitTmp = lresult
  53. blitTmp, ";")
  54. lvalue = _[0]
  55. lnext = _[1]
  56. result.value = move lvalue
  57. `=destroy`(lnext)
  58. `=destroy_1`(lvalue)
  59. -- end of expandArc ------------------------
  60. --expandArc: tt
  61. var
  62. it_cursor
  63. a
  64. :tmpD
  65. :tmpD_1
  66. :tmpD_2
  67. try:
  68. it_cursor = x
  69. a = (
  70. wasMoved(:tmpD)
  71. `=copy`(:tmpD, it_cursor.key)
  72. :tmpD,
  73. wasMoved(:tmpD_1)
  74. `=copy`(:tmpD_1, it_cursor.val)
  75. :tmpD_1)
  76. echo [
  77. :tmpD_2 = `$`(a)
  78. :tmpD_2]
  79. finally:
  80. `=destroy`(:tmpD_2)
  81. `=destroy_1`(a)
  82. -- end of expandArc ------------------------
  83. --expandArc: extractConfig
  84. var lan_ip
  85. try:
  86. lan_ip = ""
  87. block :tmp:
  88. var line
  89. var i = 0
  90. let L = len(txt)
  91. block :tmp_1:
  92. while i < L:
  93. var splitted
  94. try:
  95. line = txt[i]
  96. splitted = split(line, " ", -1)
  97. if splitted[0] == "opt":
  98. `=copy`(lan_ip, splitted[1])
  99. echo [lan_ip]
  100. echo [splitted[1]]
  101. inc(i, 1)
  102. finally:
  103. `=destroy`(splitted)
  104. finally:
  105. `=destroy_1`(lan_ip)
  106. --expandArc: mergeShadowScope
  107. var shadowScope
  108. `=copy`(shadowScope, c.currentScope)
  109. rawCloseScope(c)
  110. block :tmp:
  111. var sym
  112. var i = 0
  113. let L = len(shadowScope.symbols)
  114. block :tmp_1:
  115. while i < L:
  116. var :tmpD
  117. sym = shadowScope.symbols[i]
  118. addInterfaceDecl(c):
  119. wasMoved(:tmpD)
  120. `=copy_1`(:tmpD, sym)
  121. :tmpD
  122. inc(i, 1)
  123. `=destroy`(shadowScope)
  124. -- end of expandArc ------------------------
  125. --expandArc: check
  126. var par
  127. this.isValid = fileExists(this.value)
  128. if dirExists(this.value):
  129. var :tmpD
  130. par = (dir:
  131. wasMoved(:tmpD)
  132. `=copy`(:tmpD, this.value)
  133. :tmpD, front: "") else:
  134. var
  135. :tmpD_1
  136. :tmpD_2
  137. :tmpD_3
  138. par = (dir_1: parentDir(this.value), front_1:
  139. wasMoved(:tmpD_1)
  140. `=copy`(:tmpD_1,
  141. :tmpD_3 = splitPath do:
  142. wasMoved(:tmpD_2)
  143. `=copy`(:tmpD_2, this.value)
  144. :tmpD_2
  145. :tmpD_3.tail)
  146. :tmpD_1)
  147. `=destroy`(:tmpD_3)
  148. if dirExists(par.dir):
  149. `=sink`(this.matchDirs, getSubDirs(par.dir, par.front))
  150. else:
  151. `=sink`(this.matchDirs, [])
  152. `=destroy`(par)
  153. -- end of expandArc ------------------------'''
  154. """
  155. import os
  156. type Target = tuple[repo, package, ext: string]
  157. proc newTarget*(path: string): Target =
  158. let splat = path.splitFile
  159. result = (repo: splat.dir, package: splat.name, ext: splat.ext)
  160. echo newTarget("meo")
  161. type
  162. Node = ref object
  163. left, right, parent: Node
  164. value: int
  165. proc delete(target: var Node) =
  166. var sibling = target.parent.left # b3
  167. var saved = sibling.right # b3.right -> r4
  168. sibling.right = saved.left # b3.right -> r4.left = nil
  169. sibling.parent = saved # b3.parent -> r5 = r4
  170. #[after this proc:
  171. b 5
  172. / \
  173. b 3 b 6
  174. ]#
  175. #[before:
  176. r 5
  177. / \
  178. b 3 b 6 - to delete
  179. / \
  180. empty r 4
  181. ]#
  182. proc main =
  183. var five = Node(value: 5)
  184. var six = Node(value: 6)
  185. six.parent = five
  186. five.right = six
  187. var three = Node(value: 3)
  188. three.parent = five
  189. five.left = three
  190. var four = Node(value: 4)
  191. four.parent = three
  192. three.right = four
  193. echo "doing shady stuff..."
  194. delete(six)
  195. # need both of these echos
  196. echo five.left.value
  197. echo five.right.value
  198. main()
  199. type
  200. Maybe = object
  201. value: seq[int]
  202. proc p1(): Maybe =
  203. let lresult = @[123]
  204. var lvalue: seq[int]
  205. var lnext: string
  206. (lvalue, lnext) = (lresult, ";")
  207. result.value = move lvalue
  208. proc tissue15130 =
  209. doAssert p1().value == @[123]
  210. tissue15130()
  211. type
  212. KeyValue = tuple[key, val: seq[int]]
  213. proc tt(x: KeyValue) =
  214. var it = x
  215. let a = (it.key, it.val)
  216. echo a
  217. proc encodedQuery =
  218. var query: seq[KeyValue]
  219. query.add (key: @[1], val: @[2])
  220. for elem in query:
  221. elem.tt()
  222. encodedQuery()
  223. # bug #15147
  224. proc s(input: string): (string, string) =
  225. result = (";", "")
  226. proc charmatch(input: string): (string, string) =
  227. result = ("123", input[0 .. input.high])
  228. proc plus(input: string) =
  229. var
  230. lvalue, rvalue: string # cursors
  231. lnext: string # must be cursor!!!
  232. rnext: string # cursor
  233. let lresult = charmatch(input)
  234. (lvalue, lnext) = lresult
  235. let rresult = s(lnext)
  236. (rvalue, rnext) = rresult
  237. plus("123;")
  238. func substrEq(s: string, pos: int, substr: string): bool =
  239. var i = 0
  240. var length = substr.len
  241. while i < length and pos+i < s.len and s[pos+i] == substr[i]:
  242. inc i
  243. return i == length
  244. template stringHasSep(s: string, index: int, sep: string): bool =
  245. s.substrEq(index, sep)
  246. template splitCommon(s, sep, maxsplit, sepLen) =
  247. var last = 0
  248. var splits = maxsplit
  249. while last <= len(s):
  250. var first = last
  251. while last < len(s) and not stringHasSep(s, last, sep):
  252. inc(last)
  253. if splits == 0: last = len(s)
  254. yield substr(s, first, last-1)
  255. if splits == 0: break
  256. dec(splits)
  257. inc(last, sepLen)
  258. iterator split(s: string, sep: string, maxsplit = -1): string =
  259. splitCommon(s, sep, maxsplit, sep.len)
  260. template accResult(iter: untyped) =
  261. result = @[]
  262. for x in iter: add(result, x)
  263. func split*(s: string, sep: string, maxsplit = -1): seq[string] =
  264. accResult(split(s, sep, maxsplit))
  265. let txt = @["opt 192.168.0.1", "static_lease 192.168.0.1"]
  266. # bug #17033
  267. proc extractConfig() =
  268. var lan_ip = ""
  269. for line in txt:
  270. let splitted = line.split(" ")
  271. if splitted[0] == "opt":
  272. lan_ip = splitted[1] # "borrow" is conditional and inside a loop.
  273. # Not good enough...
  274. # we need a flag that live-ranges are disjoint
  275. echo lan_ip
  276. echo splitted[1] # Without this line everything works
  277. extractConfig()
  278. type
  279. Symbol = ref object
  280. name: string
  281. Scope = ref object
  282. parent: Scope
  283. symbols: seq[Symbol]
  284. PContext = ref object
  285. currentScope: Scope
  286. proc rawCloseScope(c: PContext) =
  287. c.currentScope = c.currentScope.parent
  288. proc addInterfaceDecl(c: PContext; s: Symbol) =
  289. c.currentScope.symbols.add s
  290. proc mergeShadowScope*(c: PContext) =
  291. let shadowScope = c.currentScope
  292. c.rawCloseScope
  293. for sym in shadowScope.symbols:
  294. c.addInterfaceDecl(sym)
  295. mergeShadowScope(PContext(currentScope: Scope(parent: Scope())))
  296. type
  297. Foo = ref object
  298. isValid*: bool
  299. value*: string
  300. matchDirs*: seq[string]
  301. proc getSubDirs(parent, front: string): seq[string] = @[]
  302. method check(this: Foo) {.base.} =
  303. this.isValid = fileExists(this.value)
  304. let par = if dirExists(this.value): (dir: this.value, front: "")
  305. else: (dir: parentDir(this.value), front: splitPath(this.value).tail)
  306. if dirExists(par.dir):
  307. this.matchDirs = getSubDirs(par.dir, par.front)
  308. else:
  309. this.matchDirs = @[]
  310. check(Foo())