topt_no_cursor.nim 7.6 KB

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