topt_no_cursor.nim 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. discard """
  2. nimoutFull: true
  3. cmd: '''nim c -r --warnings:off --hints:off --mm:arc --expandArc:newTarget --expandArc:delete --expandArc:p1 --expandArc:tt --hint:Performance:off --assertions:off --expandArc:extractConfig --expandArc:mergeShadowScope --expandArc:check $file'''
  4. nimout: '''
  5. --expandArc: newTarget
  6. var
  7. splat
  8. :tmp
  9. :tmp_1
  10. splat = splitDrive do:
  11. let blitTmp = path
  12. blitTmp
  13. :tmp = splat.drive
  14. `=wasMoved`(splat.drive)
  15. :tmp_1 = splat.path_1
  16. `=wasMoved`(splat.path_1)
  17. result = (
  18. let blitTmp_1 = :tmp
  19. blitTmp_1,
  20. let blitTmp_2 = :tmp_1
  21. blitTmp_2)
  22. `=destroy`(splat)
  23. -- end of expandArc ------------------------
  24. --expandArc: delete
  25. var
  26. sibling
  27. saved
  28. `=copy`(sibling, target.parent.left)
  29. `=copy`(saved, sibling.right)
  30. `=copy`(sibling.right, saved.left)
  31. `=sink`(sibling.parent, saved)
  32. `=destroy`(sibling)
  33. -- end of expandArc ------------------------
  34. --expandArc: p1
  35. var
  36. lresult
  37. lvalue
  38. lnext
  39. tmpTupleAsgn
  40. result = default(Maybe)
  41. lresult = @[123]
  42. tmpTupleAsgn = (
  43. let blitTmp = lresult
  44. blitTmp, ";")
  45. lvalue = tmpTupleAsgn[0]
  46. lnext = tmpTupleAsgn[1]
  47. `=sink`(result.value, move lvalue)
  48. `=destroy`(lnext)
  49. `=destroy_1`(lvalue)
  50. -- end of expandArc ------------------------
  51. --expandArc: tt
  52. var
  53. it_cursor
  54. a
  55. :tmpD
  56. :tmpD_1
  57. :tmpD_2
  58. try:
  59. it_cursor = x
  60. a = (
  61. :tmpD = `=dup`(it_cursor.key)
  62. :tmpD,
  63. :tmpD_1 = `=dup`(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. {.push, overflowChecks: false.}
  91. inc(i, 1)
  92. {.pop.}
  93. finally:
  94. `=destroy`(splitted)
  95. finally:
  96. `=destroy_1`(lan_ip)
  97. -- end of expandArc ------------------------
  98. --expandArc: mergeShadowScope
  99. var shadowScope
  100. `=copy`(shadowScope, c.currentScope)
  101. rawCloseScope(c)
  102. block :tmp:
  103. var sym
  104. var i = 0
  105. let L = len(shadowScope.symbols)
  106. block :tmp_1:
  107. while i < L:
  108. var :tmpD
  109. sym = shadowScope.symbols[i]
  110. addInterfaceDecl(c):
  111. :tmpD = `=dup`(sym)
  112. :tmpD
  113. {.push, overflowChecks: false.}
  114. inc(i, 1)
  115. {.pop.}
  116. `=destroy`(shadowScope)
  117. -- end of expandArc ------------------------
  118. --expandArc: check
  119. var par
  120. this.isValid = fileExists(this.value)
  121. if dirExists(this.value):
  122. var :tmpD
  123. par = (dir:
  124. :tmpD = `=dup`(this.value)
  125. :tmpD, front: "") else:
  126. var
  127. :tmpD_1
  128. :tmpD_2
  129. :tmpD_3
  130. par = (dir_1: parentDir(this.value), front_1:
  131. :tmpD_1 = `=dup`(
  132. :tmpD_3 = splitDrive do:
  133. :tmpD_2 = `=dup`(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. result = default(Maybe)
  207. let lresult = @[123]
  208. var lvalue: seq[int]
  209. var lnext: string
  210. (lvalue, lnext) = (lresult, ";")
  211. result.value = move lvalue
  212. proc tissue15130 =
  213. doAssert p1().value == @[123]
  214. tissue15130()
  215. type
  216. KeyValue = tuple[key, val: seq[int]]
  217. proc tt(x: KeyValue) =
  218. var it = x
  219. let a = (it.key, it.val)
  220. echo a
  221. proc encodedQuery =
  222. var query: seq[KeyValue] = @[]
  223. query.add (key: @[1], val: @[2])
  224. for elem in query:
  225. elem.tt()
  226. encodedQuery()
  227. # bug #15147
  228. proc s(input: string): (string, string) =
  229. result = (";", "")
  230. proc charmatch(input: string): (string, string) =
  231. result = ("123", input[0 .. input.high])
  232. proc plus(input: string) =
  233. var
  234. lvalue, rvalue: string # cursors
  235. lnext: string # must be cursor!!!
  236. rnext: string # cursor
  237. let lresult = charmatch(input)
  238. (lvalue, lnext) = lresult
  239. let rresult = s(lnext)
  240. (rvalue, rnext) = rresult
  241. plus("123;")
  242. func substrEq(s: string, pos: int, substr: string): bool =
  243. var i = 0
  244. var length = substr.len
  245. while i < length and pos+i < s.len and s[pos+i] == substr[i]:
  246. inc i
  247. return i == length
  248. template stringHasSep(s: string, index: int, sep: string): bool =
  249. s.substrEq(index, sep)
  250. template splitCommon(s, sep, maxsplit, sepLen) =
  251. var last = 0
  252. var splits = maxsplit
  253. while last <= len(s):
  254. var first = last
  255. while last < len(s) and not stringHasSep(s, last, sep):
  256. inc(last)
  257. if splits == 0: last = len(s)
  258. yield substr(s, first, last-1)
  259. if splits == 0: break
  260. dec(splits)
  261. inc(last, sepLen)
  262. iterator split(s: string, sep: string, maxsplit = -1): string =
  263. splitCommon(s, sep, maxsplit, sep.len)
  264. template accResult(iter: untyped) =
  265. result = @[]
  266. for x in iter: add(result, x)
  267. func split*(s: string, sep: string, maxsplit = -1): seq[string] =
  268. accResult(split(s, sep, maxsplit))
  269. let txt = @["opt 192.168.0.1", "static_lease 192.168.0.1"]
  270. # bug #17033
  271. proc extractConfig() =
  272. var lan_ip = ""
  273. for line in txt:
  274. let splitted = line.split(" ")
  275. if splitted[0] == "opt":
  276. lan_ip = splitted[1] # "borrow" is conditional and inside a loop.
  277. # Not good enough...
  278. # we need a flag that live-ranges are disjoint
  279. echo lan_ip
  280. echo splitted[1] # Without this line everything works
  281. extractConfig()
  282. type
  283. Symbol = ref object
  284. name: string
  285. Scope = ref object
  286. parent: Scope
  287. symbols: seq[Symbol]
  288. PContext = ref object
  289. currentScope: Scope
  290. proc rawCloseScope(c: PContext) =
  291. c.currentScope = c.currentScope.parent
  292. proc addInterfaceDecl(c: PContext; s: Symbol) =
  293. c.currentScope.symbols.add s
  294. proc mergeShadowScope*(c: PContext) =
  295. let shadowScope = c.currentScope
  296. c.rawCloseScope
  297. for sym in shadowScope.symbols:
  298. c.addInterfaceDecl(sym)
  299. mergeShadowScope(PContext(currentScope: Scope(parent: Scope())))
  300. type
  301. Foo = ref object
  302. isValid*: bool
  303. value*: string
  304. matchDirs*: seq[string]
  305. proc getSubDirs(parent, front: string): seq[string] = @[]
  306. method check(this: Foo) {.base.} =
  307. this.isValid = fileExists(this.value)
  308. let par = if dirExists(this.value): (dir: this.value, front: "")
  309. else: (dir: parentDir(this.value), front: splitDrive(this.value).path)
  310. if dirExists(par.dir):
  311. this.matchDirs = getSubDirs(par.dir, par.front)
  312. else:
  313. this.matchDirs = @[]
  314. check(Foo())