topt_no_cursor.nim 7.6 KB

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