std.qi 20 KB

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