std.qi 19 KB

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