topt_no_cursor.nim 7.5 KB

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