std.qi 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. func Object(t, p=nil, o={}): return p !is nil? set_meta_table(p, get_meta_table(p) + t): set_meta_table(o, t)
  2. func is_object(o): return has_meta_table(o)
  3. func __class_wrapper(n, p, t, mt, st): return Object({
  4. "t": t,
  5. "mt": mt,
  6. "super": p,
  7. "__type": func (this) use (n): return n,
  8. "__str": func (this) use (n): return "<class " + n + ">",
  9. "__call": func (this, pargs) use (n, p) {
  10. var t = {}
  11. var mt = { "__type": func (this) use (n): n }
  12. if p {
  13. var i = 0
  14. for i < len(p) {
  15. t += p[i].t
  16. mt += p[i].mt
  17. i += 1
  18. }
  19. }
  20. t += this.t
  21. mt += this.mt
  22. mt.super = this.super
  23. var obj = set_meta_table(t, mt)
  24. if "constructor" in mt
  25. func_call(mt.constructor, [obj] + pargs)
  26. return obj
  27. }
  28. }, nil, st)
  29. class Error {
  30. msg = nil
  31. constructor (this, msg=nil) {
  32. this.msg = msg
  33. }
  34. __str (this): this.msg is nil? type(this): type(this) + ": " + this.msg
  35. }
  36. class AssertionError(Error)
  37. class TypeError(Error)
  38. class ValueError(Error)
  39. class IndexError(Error)
  40. class KeyError(Error)
  41. class RecursionError(Error)
  42. class NameError(Error)
  43. class IOError(Error)
  44. func isinstance(a, b): return type(a) == type(b)
  45. func head(l): return l[0]
  46. func tail(l): return slice(l, 1)
  47. func min(x, y): x < y? x: y
  48. func max(x, y): x > y? x: y
  49. func reverse(x) {
  50. if type(x) !in ("list", "string", "bytes")
  51. throw TypeError("expected first argument to be: list, string or bytes, but got: " + type(x))
  52. var r = []
  53. for var i = len(x)-1; i >= 0; i--
  54. list_push(r, x[i])
  55. if type(x) == "string"
  56. return list_join(r)
  57. elif type(x) == "bytes"
  58. return bytes(r)
  59. return r
  60. }
  61. set_pseudomethod("list.reverse", reverse)
  62. set_pseudomethod("string.reverse", reverse)
  63. set_pseudomethod("bytes.reverse", reverse)
  64. func range(f) {
  65. var t, s
  66. if len(arguments) >= 3 {
  67. t = arguments[1]
  68. s = arguments[2]
  69. } elif len(arguments) >= 2 {
  70. t = arguments[1]
  71. s = 1
  72. } else {
  73. t = f
  74. f = 0
  75. s = 1
  76. }
  77. if type(f) != "number"
  78. throw TypeError("expected first argument to be: number, but got: " + type(f))
  79. if type(t) != "number"
  80. throw TypeError("expected second argument to be: number, but got: " + type(t))
  81. if type(s) != "number"
  82. throw TypeError("expected third argument to be: number, but got: " + type(s))
  83. if f > t
  84. return reverse(range(t, f, s))
  85. var r = []
  86. for var i = f; i < t; i += s
  87. list_push(r, i)
  88. return r
  89. }
  90. let SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2
  91. func frewind(file)
  92. return file.rewind()
  93. func read_stdin() {
  94. return str(fread(STDIN, -1))
  95. }
  96. func file_read(filename) {
  97. let file = fopen(filename, "r")
  98. defer fclose(file)
  99. return str(fread(file, -1))
  100. }
  101. func file_write(filename, data) {
  102. let file = fopen(filename, "w")
  103. defer fclose(file)
  104. fwrite(file, bytes(data))
  105. }
  106. func is_defined(name) {
  107. if type(name) != "string"
  108. throw TypeError("expected first argument to be: string, but got: " + type(name))
  109. inline `qi_bool b = qi_find(state, qi_get(state, "name")->value.string) != NULL`
  110. inline `return qi_make_boolean(state, b)`
  111. }
  112. func is_table(a) {
  113. inline `return qi_make_boolean(state, qi_get(state, "a")->type == QI_TABLE)`
  114. }
  115. func list_remove(l, x, first=false) {
  116. if type(l) != "list"
  117. throw TypeError("expected first argument to be: list, but got: " + type(l))
  118. repeat:
  119. for var i = 0; i < len(l); i++
  120. if l[i] == x {
  121. list_delete(l, i)
  122. if first
  123. break
  124. goto repeat
  125. }
  126. }
  127. set_pseudomethod("list.remove", list_remove)
  128. func list_join(l) {
  129. if type(l) != "list"
  130. throw TypeError("expected first argumient to be: list, but got: " + type(l))
  131. var r = ""
  132. var s
  133. if len(arguments) == 1
  134. s = ""
  135. else
  136. s = arguments[1]
  137. if type(s) != "string"
  138. throw TypeError("expected second argument to be: string, but got: " + type(s))
  139. var first = true
  140. for var x of l {
  141. if type(x) != "string"
  142. throw TypeError("expected sequence item to be: string, but got: " + type(x))
  143. if s != "" && !first
  144. r += s
  145. r += x
  146. first = false
  147. }
  148. return r
  149. }
  150. set_pseudomethod("list.join", list_join)
  151. func list_pop_at(l, i) {
  152. if type(l) != "list"
  153. throw TypeError("expected first argument to be: list, but got: " + type(l))
  154. if type(i) != "number"
  155. throw TypeError("expected second argument to be: number, but got: " + type(i))
  156. var x = l[i]
  157. list_delete(l, i)
  158. return x
  159. }
  160. set_pseudomethod("list.popAt", list_pop_at)
  161. func __cmp(x, y): x > y? 1: x < y? -1: 0
  162. func list_sort(l, cmp=__cmp) {
  163. if type(l) != "list"
  164. throw TypeError("expected first argument to be: list, but got: " + type(l))
  165. if type(cmp) != "function"
  166. throw TypeError("expected second argument to be: function, but got: " + type(cmp))
  167. if len(l) == 0
  168. return l
  169. var z = len(l)
  170. for var i = 0; i < z - 1; i++
  171. for var j = 0; j < z - 1 - i; j++
  172. if cmp(l[j], l[j+1]) > 0 {
  173. let tmp = l[j]
  174. l[j] = l[j+1]
  175. l[j+1] = tmp
  176. }
  177. return l
  178. }
  179. func list_sorted(l, cmp=__cmp) {
  180. l = list_copy(l)
  181. return list_sort(l, cmp)
  182. }
  183. set_pseudomethod("list.sort", list_sort)
  184. set_pseudomethod("list.sorted", list_sorted)
  185. func list_shift(l) {
  186. if type(l) != "list"
  187. throw TypeError("expected first argument to be: list, but got: " + type(l))
  188. if is_empty(l)
  189. throw IndexError("shift from empty list")
  190. var a = l[0]
  191. list_delete(l, 0)
  192. return a
  193. }
  194. func list_unshift(l, x) {
  195. list_insert(l, 0, x)
  196. }
  197. set_pseudomethod("list.shift", list_shift)
  198. set_pseudomethod("list.unshift", list_unshift)
  199. set_pseudomethod("list.insert", list_insert)
  200. set_pseudomethod("list.delete", list_delete)
  201. func slice(l) {
  202. if type(l) !in ("list", "string", "bytes", "ustr")
  203. throw TypeError("expected first argument to be: list, string, bytes or ustr, but got: " + type(l))
  204. var r = []
  205. if len(arguments) == 2 {
  206. var f = arguments[1]
  207. if type(f) != "number"
  208. throw TypeError("expected second argument to be: number, but got: " + type(f))
  209. if f < 0
  210. f += len(l)
  211. for var i = f; i < len(l); i++
  212. list_push(r, l[i])
  213. } elif len(arguments) == 3 {
  214. var f = arguments[1], t = arguments[2]
  215. if type(f) != "number"
  216. throw TypeError("expected second argument to be: number, but got: " + type(f))
  217. if type(t) != "number"
  218. throw TypeError("expected third argument to be: number, but got: " + type(t))
  219. if f < 0
  220. f += len(l)
  221. if t < 0
  222. t += len(l)
  223. for var i = f; i < len(l) && i <= t; i++
  224. list_push(r, l[i])
  225. }
  226. if type(l) == "string"
  227. return list_join(r)
  228. elif type(l) == "bytes"
  229. return bytes(r)
  230. elif type(l) == "ustr"
  231. return ustr(r)
  232. return r
  233. }
  234. set_pseudomethod("list.slice", slice)
  235. set_pseudomethod("string.slice", slice)
  236. set_pseudomethod("bytes.slice", slice)
  237. let __slice = slice;
  238. func str_startswith(s, p) {
  239. if type(s) != "string"
  240. throw TypeError("expected first argument to be: string, but got: " + type(s))
  241. if len(s) < len(p)
  242. return false
  243. return slice(s, 0, len(p)-1) == p
  244. }
  245. set_pseudomethod("string.startsWith", str_startswith)
  246. func str_endswith(s, p) {
  247. if type(s) != "string"
  248. throw TypeError("expected first argument to be: string, but got: " + type(s))
  249. if len(s) < len(p)
  250. return false
  251. return slice(s, len(s) - len(p)) == p
  252. }
  253. set_pseudomethod("string.endsWith", str_endswith)
  254. func str_split(s) {
  255. if len(arguments) == 1 || arguments[1] == ""
  256. return list(s)
  257. if type(s) != "string"
  258. throw TypeError("expected first argument to be:!string, but got: " + type(s))
  259. var r = []
  260. var d = arguments[1]
  261. if type(d) != "string"
  262. throw TypeError("expected second argument to be: string, but got: " + type(s))
  263. var t = ""
  264. for var i = 0; i < len(s); i++ {
  265. if slice(s, i, i+len(d)-1) == d {
  266. list_push(r, t)
  267. t = ""
  268. i += len(d)-1
  269. continue
  270. }
  271. t += s[i]
  272. }
  273. if t != ""
  274. list_push(r, t)
  275. return r
  276. }
  277. set_pseudomethod("string.split", str_split)
  278. func str_replace(s, w, b) {
  279. if type(s) != "string"
  280. throw TypeError("expected first argument to be: string, but got: " + type(s))
  281. if type(w) != "string"
  282. throw TypeError("expected second argument to be: string, but got: " + type(w))
  283. if type(b) != "string"
  284. throw TypeError("expected third argument to be: string, but got: " + type(b))
  285. var r = ""
  286. for var i = 0; i < len(s); i++ {
  287. if slice(s, i, i+len(w)-1) == w {
  288. r += b
  289. i += len(w)-1
  290. continue
  291. }
  292. r += s[i]
  293. }
  294. return r
  295. }
  296. set_pseudomethod("string.replace", str_replace)
  297. func table_keys(t) {
  298. if type(t) != "table"
  299. throw TypeError("expected first argument to be: table, but got: " + type(t))
  300. var r = []
  301. for var k of t
  302. list_push(r, k)
  303. return r
  304. }
  305. set_pseudomethod("table.keys", table_keys)
  306. func table_values(t) {
  307. if type(t) != "table"
  308. throw TypeError("expected first argument to be: table, but got: " + type(t))
  309. var r = []
  310. for var k of t
  311. list_push(r, t[k])
  312. return r
  313. }
  314. set_pseudomethod("table.values", table_values)
  315. func reduce(f, xs) {
  316. if type(f) != "function"
  317. throw TypeError("expected first argument to be: function, but got: " + type(f))
  318. if type(xs) !in ("list", "tuple", "string", "bytes")
  319. throw TypeError("expected second argument to be: list, tuple, string or bytes, but got: " + type(xs))
  320. if len(xs) == 0
  321. throw ValueError("cannot reduce empty list")
  322. r = xs[0]
  323. for var x of slice(xs, 1)
  324. r = f(r, x)
  325. if type(xs) == "tuple"
  326. return tuple(r)
  327. elif type(xs) == "string"
  328. return list_join(r)
  329. elif type(xs) == "bytes"
  330. return bytes(r)
  331. return r
  332. }
  333. set_pseudomethod("list.reduce", func (xs, f): reduce(f, xs))
  334. set_pseudomethod("tuple.reduce", func (xs, f): reduce(f, xs))
  335. set_pseudomethod("string.reduce", func (xs, f): reduce(f, xs))
  336. set_pseudomethod("bytes.reduce", func (xs, f): reduce(f, xs))
  337. func sum(xs)
  338. return reduce(func (x, y): x + y, xs)
  339. set_pseudomethod("list.sum", sum)
  340. set_pseudomethod("tuple.sum", sum)
  341. func product(xs)
  342. return reduce(func (x, y): x * y, xs)
  343. set_pseudomethod("list.product", product)
  344. set_pseudomethod("tuple.product", product)
  345. func all(l): reduce(func (x, y): x && y, l)
  346. set_pseudomethod("list.all", all)
  347. set_pseudomethod("tuple.all", all)
  348. func any(l): reduce(func (x, y): x || y, l)
  349. set_pseudomethod("list.any", any)
  350. set_pseudomethod("tuple.any", any)
  351. func map(f, xs) {
  352. if type(f) != "function"
  353. throw TypeError("expected first argument to be: function, but got: " + type(f))
  354. if type(xs) !in ("list", "tuple", "string", "bytes")
  355. throw TypeError("expected second argument to be: list, tuple, string or bytes, but got: " + type(xs))
  356. if len(xs) == 0
  357. return xs
  358. var r = []
  359. for var x of xs
  360. list_push(r, f(x))
  361. if type(xs) == "tuple"
  362. return tuple(r)
  363. elif type(xs) == "string"
  364. return list_join(r)
  365. elif type(xs) == "bytes"
  366. return bytes(r)
  367. return r
  368. }
  369. set_pseudomethod("list.map", func (xs, f): map(f, xs))
  370. set_pseudomethod("tuple.map", func (xs, f): map(f, xs))
  371. set_pseudomethod("string.map", func (xs, f): map(f, xs))
  372. set_pseudomethod("bytes.map", func (xs, f): map(f, xs))
  373. func filter(f, xs) {
  374. if type(f) != "function"
  375. throw TypeError("expected first argument to be: function, but got: " + type(f))
  376. if type(xs) !in ("list", "tuple", "string", "bytes")
  377. throw TypeError("expected second argument to be: list, tuple, string or bytes, but got: " + type(xs))
  378. if len(xs) == 0
  379. return xs
  380. var r = []
  381. for var x of xs
  382. if f(x)
  383. list_push(r, x)
  384. if type(xs) == "tuple"
  385. return tuple(r)
  386. elif type(xs) == "string"
  387. return list_join(r)
  388. elif type(xs) == "bytes"
  389. return bytes(r)
  390. return r
  391. }
  392. set_pseudomethod("list.filter", func (xs, f): filter(f, xs))
  393. set_pseudomethod("tuple.filter", func (xs, f): filter(f, xs))
  394. set_pseudomethod("string.filter", func (xs, f): filter(f, xs))
  395. set_pseudomethod("bytes.filter", func (xs, f): filter(f, xs))
  396. func str_index(s, w) {
  397. if s == "" || w == ""
  398. return -1
  399. if type(s) != "string"
  400. throw TypeError("expected first argument to be: string, but got: " + type(s))
  401. if type(w) != "string"
  402. throw TypeError("expected second argument to be: string, but got: " + type(w))
  403. for var i = 0; i < len(s); i++
  404. if len(w) == 1 && s[i] == w
  405. return i
  406. elif slice(s, i, i+len(w)-1) == w
  407. return i
  408. return -1
  409. }
  410. set_pseudomethod("string.index", str_index)
  411. func str_lstrip(s, cs=" \t\n\r\x0b\x0c") {
  412. if type(s) != "string"
  413. throw TypeError("expected first argument to be: string, but got: " + type(s))
  414. if type(cs) != "string"
  415. throw TypeError("expected second argument to be: string, but got: " + type(cs))
  416. if s == ""
  417. return s
  418. var i
  419. for i = 0; s[i] in cs && i < len(s); i++
  420. pass
  421. return slice(s, i)
  422. }
  423. set_pseudomethod("string.lstrip", str_lstrip)
  424. func str_rstrip(s, cs=" \t\n\r\x0b\x0c") {
  425. if type(s) != "string"
  426. throw TypeError("expected first argument to be: string, but got: " + type(s))
  427. if type(cs) != "string"
  428. throw TypeError("expected second argument to be: string, but got: " + type(cs))
  429. if s == ""
  430. return s
  431. var k, i
  432. for k = 0, i = len(s)-1; s[i] in cs && i >= 0; k++
  433. i--
  434. return slice(s, 0, len(s)-k-1)
  435. }
  436. set_pseudomethod("string.rstrip", str_rstrip)
  437. func str_strip(s, cs=" \t\n\r\x0b\x0c") {
  438. if type(s) != "string"
  439. throw TypeError("expected first argument to be: string, but got: " + type(s))
  440. if type(cs) != "string"
  441. throw TypeError("expected second argument to be: string, but got: " + type(cs))
  442. return str_lstrip(str_rstrip(s, cs), cs)
  443. }
  444. set_pseudomethod("string.strip", str_strip)
  445. func zip() {
  446. if !arguments
  447. return []
  448. var l = map(len, arguments)
  449. l = reduce(min, l)
  450. var r = []
  451. for var i = 0; i < l; i++ {
  452. var t = []
  453. for var xs of arguments
  454. list_push(t, xs[i])
  455. list_push(r, t)
  456. }
  457. return r
  458. }
  459. func enumerate(l)
  460. if type(l) == "table"
  461. return zip(table_keys(l), table_values(l))
  462. else
  463. return zip(range(len(l)), l)
  464. func str_toupper(s) {
  465. if type(s) != "string"
  466. throw TypeError("expected first argument to be: string, but got: " + type(c))
  467. return map(func (c): c >= 'a' && c <= 'z'? chr(ord(c) - 32): c, s)
  468. }
  469. set_pseudomethod("string.toupper", str_toupper)
  470. func str_tolower(s) {
  471. if type(s) != "string"
  472. throw TypeError("expected first argument to be: string, but got: " + type(c))
  473. return map(func (c): c >= 'A' && c <= 'Z'? chr(ord(c) + 32): c, s)
  474. }
  475. set_pseudomethod("string.tolower", str_tolower)
  476. func hex(x) {
  477. if type(x) != "number"
  478. throw TypeError("expected first argument to be: number, but got: " + type(x))
  479. if x == 0
  480. return "0x0"
  481. let sgn = x < 0
  482. if sgn
  483. x = -x
  484. var r = ""
  485. for x > 0 {
  486. r = "0123456789abcdef"[x % 16] + r
  487. x //= 16
  488. }
  489. return (sgn? "-0x": "0x") + r
  490. }
  491. func oct(x) {
  492. if type(x) != "number"
  493. throw TypeError("expected first argument to be: number, but got: " + type(x))
  494. if x == 0
  495. return "0o0"
  496. let sgn = x < 0
  497. if sgn
  498. x = -x
  499. var r = ""
  500. for x > 0 {
  501. r = "01234567"[x % 8] + r
  502. x //= 8
  503. }
  504. return (sgn? "-0o": "0o") + r
  505. }
  506. func format(s) {
  507. if type(s) != "string"
  508. throw TypeError("expected first argument to be: string, but got: " + type(s))
  509. var r = ""
  510. var n = 1
  511. for var i = 0; i < len(s); i++
  512. switch s[i] {
  513. case '_'
  514. if i+1 < len(s) && s[i+1] == '_' {
  515. r += '_'
  516. i++
  517. continue
  518. }
  519. r += repr(arguments[n++])
  520. break
  521. default
  522. r += s[i]
  523. }
  524. return r
  525. }
  526. set_pseudomethod("string.format", format)
  527. func formatl(s, l) {
  528. if type(s) != "string"
  529. throw TypeError("expected first argument to be: string, but got: " + type(s))
  530. if type(l) != "list"
  531. throw TypeError("expected second argument to be: list, but got: " + type(l))
  532. return func_call(str_format, [s] + l)
  533. }
  534. set_pseudomethod("string.formatl", formatl)
  535. func formatd(s, t) {
  536. if type(s) != "string"
  537. throw TypeError("expected first argument to be: string, but got: " + type(s))
  538. var r = ""
  539. var n = 1
  540. for var i = 0; i < len(s); i++
  541. switch s[i] {
  542. case '{'
  543. if i+1 < len(s) && s[i+1] == '{' {
  544. r += '{'
  545. i++
  546. continue
  547. }
  548. var k = ''
  549. i++
  550. for i < len(s) && s[i] != '}'
  551. k += s[i++]
  552. if i >= len(s) || s[i] != '}'
  553. throw ValueError("unmatched { in format specifier")
  554. if !k
  555. throw ValueError("empty format key")
  556. r += repr(t[k])
  557. break
  558. default
  559. r += s[i]
  560. }
  561. return r
  562. }
  563. set_pseudomethod("string.formatd", formatd)
  564. func getch() return chr(fgetc(STDIN))
  565. func putch(c) fputc(STDOUT, c)
  566. func getline()
  567. return fgets(STDIN, 256)
  568. func input() {
  569. if len(arguments) > 0
  570. func_call(print, arguments)
  571. return str_rstrip(getline(), "\n\r")
  572. }
  573. func open(path, mode="r"): fopen(path, mode)
  574. set_pseudomethod("file.__enter", func () {})
  575. set_pseudomethod("file.__leave", func (f): fclose(f))
  576. set_pseudomethod("file.close", fclose)
  577. set_pseudomethod("file.flush", fflush)
  578. set_pseudomethod("file.seek", fseek)
  579. set_pseudomethod("file.getc", fgetc)
  580. set_pseudomethod("file.putc", fputc)
  581. set_pseudomethod("file.gets", fgets)
  582. set_pseudomethod("file.puts", fputs)
  583. set_pseudomethod("file.tell", ftell)
  584. set_pseudomethod("reference.deref", deref)
  585. set_pseudomethod("reference.set", ref_set)
  586. func assert(cond, msg=AssertionError())
  587. if !cond
  588. throw msg
  589. class ListIterator {
  590. constructor(this, l) {
  591. this.l = list(l)
  592. this.i = 0
  593. this.z = len(this.l)
  594. }
  595. __str(this): "<ListIterator>"
  596. __begin(this) {
  597. this.i = 0
  598. return this
  599. }
  600. __next(this) {
  601. if this.i < this.z
  602. return this.l[this.i++]
  603. }
  604. __end(this): this.i >= this.z
  605. }
  606. class OrderedTable {
  607. __data__ = nil
  608. constructor (this, v=nil) {
  609. this.__data__ = []
  610. if v !is nil {
  611. v = table(v)
  612. for var k of v
  613. this[k] = v[k]
  614. }
  615. }
  616. __type (this): "orderedtable"
  617. stringify (this, tempstack=[]) {
  618. if this in tempstack
  619. return "OrderedTable({...})"
  620. tempstack.push(this)
  621. var buf = "OrderedTable({"
  622. var f = true
  623. for var [k, v] of this.__data__ {
  624. if f
  625. f = false
  626. else buf += ", "
  627. k = k.replace(`\`, `\\`).replace(`"`, `\"`)
  628. if type(v) is "orderedtable"
  629. v = v.stringify(tempstack)
  630. buf += f`"${k}": ${v}`
  631. }
  632. buf += "})"
  633. return buf
  634. }
  635. __str (this) {
  636. return this.stringify([])
  637. }
  638. keys (this) {
  639. let keys = []
  640. for var [k, _] of this.__data__
  641. keys.push(k)
  642. return keys
  643. }
  644. values (this) {
  645. let values = []
  646. for var [_, v] of this.__data__
  647. values.push(v)
  648. return values
  649. }
  650. delete (this, k) {
  651. del this[k]
  652. }
  653. copy (this) {
  654. return OrderedTable(this.__data__)
  655. }
  656. get (this, k, d=nil) {
  657. k = str(k)
  658. for var [ok, v] of this.__data__
  659. if ok == k
  660. return v
  661. return d
  662. }
  663. __iter (this): ListIterator(this.keys())
  664. __len (this): len(this.__data__)
  665. __in (this, k) {
  666. k = str(k)
  667. for var [ok, _] of this.__data__
  668. if ok == k
  669. return true
  670. return false
  671. }
  672. __del (this, k) {
  673. k = str(k)
  674. for var [i, ok] of zip(range(len(this)), this.keys())
  675. if ok == k {
  676. del this.__data__[i]
  677. return
  678. }
  679. throw KeyError(k)
  680. }
  681. __index (this, k) {
  682. k = str(k)
  683. for var [ok, v] of this.__data__
  684. if ok == k
  685. return v
  686. throw KeyError(k)
  687. }
  688. __index_set (this, k, v) {
  689. k = str(k)
  690. for var [i, ok] of zip(range(len(this)), this.keys())
  691. if ok == k {
  692. this.__data__[i] = (ok, v)
  693. return
  694. }
  695. this.__data__.push((k, v))
  696. }
  697. }