std.qi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 file_read(filename) {
  50. let file = fopen(filename, "r")
  51. defer fclose(file)
  52. return str(fread(file, -1))
  53. }
  54. func file_write(filename, data) {
  55. let file = fopen(filename, "w")
  56. defer fclose(file)
  57. fwrite(file, bytes(data))
  58. }
  59. func is_defined(name) {
  60. if type(name) != "string"
  61. throw "expected first argument to be: string, but got: " + type(name)
  62. inline `qi_bool b = qi_find(state, qi_get(state, "name")->value.string) != NULL`
  63. inline `return qi_make_boolean(state, b)`
  64. }
  65. func list_remove(l, x, first=false) {
  66. if type(l) != "list"
  67. throw "expected first argument to be: list, but got: " + type(l)
  68. repeat:
  69. for var i = 0; i < len(l); i++
  70. if l[i] == x {
  71. list_delete(l, i)
  72. if first
  73. break
  74. goto repeat
  75. }
  76. }
  77. set_pseudomethod("list.remove", list_remove)
  78. func list_join(l) {
  79. if type(l) != "list"
  80. throw "expected first argument to be: list, but got: " + type(l)
  81. var r = ""
  82. var s
  83. if len(arguments) == 1
  84. s = ""
  85. else
  86. s = arguments[1]
  87. if type(s) != "string"
  88. throw "expected second argument to be: string, but got: " + type(s)
  89. var first = true
  90. for var x of l {
  91. if type(x) != "string"
  92. throw "expected sequence item to be: string, but got: " + type(x)
  93. if s != "" && !first
  94. r += s
  95. r += x
  96. first = false
  97. }
  98. return r
  99. }
  100. set_pseudomethod("list.join", list_join)
  101. func list_pop_at(l, i) {
  102. if type(l) != "list"
  103. throw "expected first argument to be: list, but got: " + type(l)
  104. if type(i) != "number"
  105. throw "expected second argument to be: number, but got: " + type(i)
  106. var x = l[i]
  107. list_delete(l, i)
  108. return x
  109. }
  110. set_pseudomethod("list.popAt", list_pop_at)
  111. func list_sort(l, cmp=func (x, y): x > y) {
  112. if type(l) != "list"
  113. throw "expected first argument to be: list, but got: " + type(l)
  114. if type(cmp) != "function"
  115. throw "expected second argument to be: function, but got: " + type(cmp)
  116. if len(l) == 0
  117. return l
  118. var z = len(l)
  119. for var i = 0; i < z - 1; i++
  120. for var j = 0; j < z - 1 - i; j++
  121. if cmp(l[j], l[j+1]) {
  122. let tmp = l[j]
  123. l[j] = l[j+1]
  124. l[j+1] = tmp
  125. }
  126. return l
  127. }
  128. func list_sorted(l, cmp=func (x, y): x > y) {
  129. l = list_copy(l)
  130. return list_sort(l, cmp)
  131. }
  132. set_pseudomethod("list.sort", list_sort)
  133. set_pseudomethod("list.sorted", list_sorted)
  134. func list_shift(l) {
  135. if type(l) != "list"
  136. throw "expected first argument to be: list, but got: " + type(l)
  137. if is_empty(l)
  138. throw "shift from empty list"
  139. var a = l[0]
  140. list_delete(l, 0)
  141. return a
  142. }
  143. func list_unshift(l, x) {
  144. list_insert(l, 0, x)
  145. }
  146. set_pseudomethod("list.shift", list_shift)
  147. set_pseudomethod("list.unshift", list_unshift)
  148. func slice(l) {
  149. if type(l) !in ("list", "string", "bytes", "ustr")
  150. throw "expected first argument to be: list, string, bytes or ustr, but got: " + type(l)
  151. var r = []
  152. if len(arguments) == 2 {
  153. var f = arguments[1]
  154. if type(f) != "number"
  155. throw "expected second argument to be: number, but got: " + type(f)
  156. if f < 0
  157. f += len(l)
  158. for var i = f; i < len(l); i++
  159. list_push(r, l[i])
  160. } elif len(arguments) == 3 {
  161. var f = arguments[1], t = arguments[2]
  162. if type(f) != "number"
  163. throw "expected second argument to be: number, but got: " + type(f)
  164. if type(t) != "number"
  165. throw "expected third argument to be: number, but got: " + type(t)
  166. if f < 0
  167. f += len(l)
  168. if t < 0
  169. t += len(l)
  170. for var i = f; i < len(l) && i <= t; i++
  171. list_push(r, l[i])
  172. }
  173. if type(l) == "string"
  174. return list_join(r)
  175. elif type(l) == "bytes"
  176. return bytes(r)
  177. elif type(l) == "ustr"
  178. return ustr(r)
  179. return r
  180. }
  181. set_pseudomethod("list.slice", slice)
  182. set_pseudomethod("string.slice", slice)
  183. set_pseudomethod("bytes.slice", slice)
  184. let __slice = slice;
  185. func str_startswith(s, p) {
  186. if type(s) != "string"
  187. throw "expected first argument to be: string, but got: " + type(s)
  188. if len(s) < len(p)
  189. return false
  190. return slice(s, 0, len(p)-1) == p
  191. }
  192. set_pseudomethod("string.startsWith", str_startswith)
  193. func str_endswith(s, p) {
  194. if type(s) != "string"
  195. throw "expected first argument to be: string, but got: " + type(s)
  196. if len(s) < len(p)
  197. return false
  198. return slice(s, len(s) - len(p)) == p
  199. }
  200. set_pseudomethod("string.endsWith", str_endswith)
  201. func str_split(s) {
  202. if len(arguments) == 1 || arguments[1] == ""
  203. return list(s)
  204. if type(s) != "string"
  205. throw "expected first argument to be:!string, but got: " + type(s)
  206. var r = []
  207. var d = arguments[1]
  208. if type(d) != "string"
  209. throw "expected second argument to be: string, but got: " + type(s)
  210. var t = ""
  211. for var i = 0; i < len(s); i++ {
  212. if slice(s, i, i+len(d)-1) == d {
  213. list_push(r, t)
  214. t = ""
  215. i += len(d)-1
  216. continue
  217. }
  218. t += s[i]
  219. }
  220. if t != ""
  221. list_push(r, t)
  222. return r
  223. }
  224. set_pseudomethod("string.split", str_split)
  225. func str_replace(s, w, b) {
  226. if type(s) != "string"
  227. throw "expected first argument to be: string, but got: " + type(s)
  228. if type(w) != "string"
  229. throw "expected second argument to be: string, but got: " + type(w)
  230. if type(b) != "string"
  231. throw "expected third argument to be: string, but got: " + type(b)
  232. var r = ""
  233. for var i = 0; i < len(s); i++ {
  234. if slice(s, i, i+len(w)-1) == w {
  235. r += b
  236. i += len(w)-1
  237. continue
  238. }
  239. r += s[i]
  240. }
  241. return r
  242. }
  243. set_pseudomethod("string.replace", str_replace)
  244. func table_keys(t) {
  245. if type(t) != "table"
  246. throw "expected first argument to be: table, but got: " + type(t)
  247. var r = []
  248. for var k of t
  249. list_push(r, k)
  250. return r
  251. }
  252. set_pseudomethod("table.keys", table_keys)
  253. func table_values(t) {
  254. if type(t) != "table"
  255. throw "expected first argument to be: table, but got: " + type(t)
  256. var r = []
  257. for var k of t
  258. list_push(r, t[k])
  259. return r
  260. }
  261. set_pseudomethod("table.values", table_values)
  262. func reduce(f, xs) {
  263. if type(f) != "function"
  264. throw "expected first argument to be: function, but got: " + type(f)
  265. if type(xs) !in ("list", "tuple", "string", "bytes")
  266. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  267. if len(xs) == 0
  268. throw "cannot reduce empty list"
  269. r = xs[0]
  270. for var x of slice(xs, 1)
  271. r = f(r, x)
  272. if type(xs) == "tuple"
  273. return tuple(r)
  274. elif type(xs) == "string"
  275. return list_join(r)
  276. elif type(xs) == "bytes"
  277. return bytes(r)
  278. return r
  279. }
  280. set_pseudomethod("list.reduce", func (xs, f): reduce(f, xs))
  281. set_pseudomethod("tuple.reduce", func (xs, f): reduce(f, xs))
  282. set_pseudomethod("string.reduce", func (xs, f): reduce(f, xs))
  283. set_pseudomethod("bytes.reduce", func (xs, f): reduce(f, xs))
  284. func sum(xs)
  285. return reduce(func (x, y): x + y, xs)
  286. set_pseudomethod("list.sum", sum)
  287. set_pseudomethod("tuple.sum", sum)
  288. func product(xs)
  289. return reduce(func (x, y): x * y, xs)
  290. set_pseudomethod("list.product", product)
  291. set_pseudomethod("tuple.product", product)
  292. func all(l): reduce(func (x, y): x && y, l)
  293. set_pseudomethod("list.all", all)
  294. set_pseudomethod("tuple.all", all)
  295. func any(l): reduce(func (x, y): x || y, l)
  296. set_pseudomethod("list.any", any)
  297. set_pseudomethod("tuple.any", any)
  298. func map(f, xs) {
  299. if type(f) != "function"
  300. throw "expected first argument to be: function, but got: " + type(f)
  301. if type(xs) !in ("list", "tuple", "string", "bytes")
  302. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  303. if len(xs) == 0
  304. return xs
  305. var r = []
  306. for var x of xs
  307. list_push(r, f(x))
  308. if type(xs) == "tuple"
  309. return tuple(r)
  310. elif type(xs) == "string"
  311. return list_join(r)
  312. elif type(xs) == "bytes"
  313. return bytes(r)
  314. return r
  315. }
  316. set_pseudomethod("list.map", func (xs, f): map(f, xs))
  317. set_pseudomethod("tuple.map", func (xs, f): map(f, xs))
  318. set_pseudomethod("string.map", func (xs, f): map(f, xs))
  319. set_pseudomethod("bytes.map", func (xs, f): map(f, xs))
  320. func filter(f, xs) {
  321. if type(f) != "function"
  322. throw "expected first argument to be: function, but got: " + type(f)
  323. if type(xs) !in ("list", "tuple", "string", "bytes")
  324. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  325. if len(xs) == 0
  326. return xs
  327. var r = []
  328. for var x of xs
  329. if f(x)
  330. list_push(r, x)
  331. if type(xs) == "tuple"
  332. return tuple(r)
  333. elif type(xs) == "string"
  334. return list_join(r)
  335. elif type(xs) == "bytes"
  336. return bytes(r)
  337. return r
  338. }
  339. set_pseudomethod("list.filter", func (xs, f): filter(f, xs))
  340. set_pseudomethod("tuple.filter", func (xs, f): filter(f, xs))
  341. set_pseudomethod("string.filter", func (xs, f): filter(f, xs))
  342. set_pseudomethod("bytes.filter", func (xs, f): filter(f, xs))
  343. func str_index(s, w) {
  344. if s == "" || w == ""
  345. return -1
  346. if type(s) != "string"
  347. throw "expected first argument to be: string, but got: " + type(s)
  348. if type(w) != "string"
  349. throw "expected second argument to be: string, but got: " + type(w)
  350. for var i = 0; i < len(s); i++
  351. if len(w) == 1 && s[i] == w
  352. return i
  353. elif slice(s, i, i+len(w)-1) == w
  354. return i
  355. return -1
  356. }
  357. set_pseudomethod("string.index", str_index)
  358. func str_lstrip(s, cs=" \t\n\r\x0b\x0c") {
  359. if type(s) != "string"
  360. throw "expected first argument to be: string, but got: " + type(s)
  361. if type(cs) != "string"
  362. throw "expected second argument to be: string, but got: " + type(cs)
  363. if s == ""
  364. return s
  365. for var i = 0; s[i] in cs && i < len(s); i++
  366. pass
  367. return slice(s, i)
  368. }
  369. set_pseudomethod("string.lstrip", str_lstrip)
  370. func str_rstrip(s, cs=" \t\n\r\x0b\x0c") {
  371. if type(s) != "string"
  372. throw "expected first argument to be: string, but got: " + type(s)
  373. if type(cs) != "string"
  374. throw "expected second argument to be: string, but got: " + type(cs)
  375. if s == ""
  376. return s
  377. for var k = 0, i = len(s)-1; s[i] in cs && i >= 0; k++
  378. i--
  379. return slice(s, 0, len(s)-k-1)
  380. }
  381. set_pseudomethod("string.rstrip", str_rstrip)
  382. func str_strip(s, cs=" \t\n\r\x0b\x0c") {
  383. if type(s) != "string"
  384. throw "expected first argument to be: string, but got: " + type(s)
  385. if type(cs) != "string"
  386. throw "expected second argument to be: string, but got: " + type(cs)
  387. return str_lstrip(str_rstrip(s, cs), cs)
  388. }
  389. set_pseudomethod("string.strip", str_strip)
  390. func table_get(t, k, d=nil) {
  391. if type(t) != "table"
  392. throw "expected first argument to be: table, but got: " + type(t)
  393. if type(k) != "string"
  394. throw "expected second argument to be: string, but got: " + type(k)
  395. if k !in t
  396. return d
  397. return t[k]
  398. }
  399. set_pseudomethod("table.get", table_get)
  400. func zip() {
  401. if !arguments
  402. return []
  403. var l = map(len, arguments)
  404. l = reduce(min, l)
  405. var r = []
  406. for var i = 0; i < l; i++ {
  407. var t = []
  408. for var xs of arguments
  409. list_push(t, xs[i])
  410. list_push(r, t)
  411. }
  412. return r
  413. }
  414. func enumerate(l)
  415. if type(l) == "table"
  416. return zip(table_keys(l), table_values(l))
  417. else
  418. return zip(range(len(l)), l)
  419. func str_toupper(s) {
  420. if type(s) != "string"
  421. throw "expected first argument to be: string, but got: " + type(c)
  422. return map(func (c): c >= 'a' && c <= 'z'? chr(ord(c) - 32): c, s)
  423. }
  424. set_pseudomethod("string.toupper", str_toupper)
  425. func str_tolower(s) {
  426. if type(s) != "string"
  427. throw "expected first argument to be: string, but got: " + type(c)
  428. return map(func (c): c >= 'A' && c <= 'Z'? chr(ord(c) + 32): c, s)
  429. }
  430. set_pseudomethod("string.tolower", str_tolower)
  431. func Object(t, p=nil): return p !is nil? set_meta_table(p, get_meta_table(p) + t): set_meta_table({}, t)
  432. func is_object(o): return has_meta_table(o)
  433. func __class_wrapper(n, p, t, mt, st): return Object(st + {
  434. t: t,
  435. mt: mt,
  436. super: p,
  437. __type: func (this) use (n): return n,
  438. __str: func (this) use (n): return "<class " + n + ">",
  439. __call: func (this, pargs) use (n, p) {
  440. var t = {}
  441. var mt = { __type: func (this) use (n): n }
  442. for var other of p {
  443. t += other.t
  444. mt += other.mt
  445. }
  446. t += this.t
  447. mt += this.mt
  448. mt.super = this.super
  449. obj = set_meta_table(t, mt)
  450. if "constructor" in mt
  451. func_call(mt.constructor, [obj] + pargs)
  452. return obj
  453. }
  454. })
  455. func format(s) {
  456. if type(s) != "string"
  457. throw "expected first argument to be: string, but got: " + type(s)
  458. var r = ""
  459. var n = 1
  460. for var i = 0; i < len(s); i++
  461. switch s[i] {
  462. case '_'
  463. if i+1 < len(s) && s[i+1] == '_' {
  464. r += '_'
  465. i++
  466. continue
  467. }
  468. r += repr(arguments[n++])
  469. break
  470. default
  471. r += s[i]
  472. }
  473. return r
  474. }
  475. set_pseudomethod("string.format", format)
  476. func formatl(s, l) {
  477. if type(s) != "string"
  478. throw "expected first argument to be: string, but got: " + type(s)
  479. if type(l) != "list"
  480. throw "expected second argument to be: list, but got: " + type(l)
  481. return func_call(str_format, [s] + l)
  482. }
  483. set_pseudomethod("string.formatl", formatl)
  484. func formatd(s, t) {
  485. if type(s) != "string"
  486. throw "expected first argument to be: string, but got: " + type(s)
  487. var r = ""
  488. var n = 1
  489. for var i = 0; i < len(s); i++
  490. switch s[i] {
  491. case '{'
  492. if i+1 < len(s) && s[i+1] == '{' {
  493. r += '{'
  494. i++
  495. continue
  496. }
  497. var k = ''
  498. i++
  499. for i < len(s) && s[i] != '}'
  500. k += s[i++]
  501. if i >= len(s) || s[i] != '}'
  502. throw "unmatched { in format specifier"
  503. if !k
  504. throw "empty format key"
  505. r += repr(t[k])
  506. break
  507. default
  508. r += s[i]
  509. }
  510. return r
  511. }
  512. set_pseudomethod("string.formatd", formatd)
  513. func getch() return chr(fgetc(STDIN))
  514. func putch(c) fputc(STDOUT, c)
  515. func getline()
  516. return fgets(STDIN, 256)
  517. func input() {
  518. if len(arguments) > 0
  519. func_call(print, arguments)
  520. return str_rstrip(getline(), "\n\r")
  521. }
  522. func open(path, mode="r"): fopen(path, mode)
  523. set_pseudomethod("file.__enter", func () {})
  524. set_pseudomethod("file.__leave", func (f): fclose(f))
  525. func assert(cond, msg="assertion failed")
  526. if !cond
  527. throw msg
  528. class Error {
  529. msg = nil
  530. constructor (this, msg) {
  531. this.msg = msg
  532. }
  533. __str (this): type(this) + ": " + this.msg
  534. }