std.qi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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")
  150. throw "expected first argument to be: list, string or bytes, 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. return r
  178. }
  179. set_pseudomethod("list.slice", slice)
  180. set_pseudomethod("string.slice", slice)
  181. set_pseudomethod("bytes.slice", slice)
  182. let __slice = slice;
  183. func str_startswith(s, p) {
  184. if type(s) != "string"
  185. throw "expected first argument to be: string, but got: " + type(s)
  186. if len(s) < len(p)
  187. return false
  188. return slice(s, 0, len(p)-1) == p
  189. }
  190. set_pseudomethod("string.startsWith", str_startswith)
  191. func str_endswith(s, p) {
  192. if type(s) != "string"
  193. throw "expected first argument to be: string, but got: " + type(s)
  194. if len(s) < len(p)
  195. return false
  196. return slice(s, len(s) - len(p)) == p
  197. }
  198. set_pseudomethod("string.endsWith", str_endswith)
  199. func str_split(s) {
  200. if len(arguments) == 1 || arguments[1] == ""
  201. return list(s)
  202. if type(s) != "string"
  203. throw "expected first argument to be:!string, but got: " + type(s)
  204. var r = []
  205. var d = arguments[1]
  206. if type(d) != "string"
  207. throw "expected second argument to be: string, but got: " + type(s)
  208. var t = ""
  209. for var i = 0; i < len(s); i++ {
  210. if slice(s, i, i+len(d)-1) == d {
  211. list_push(r, t)
  212. t = ""
  213. i += len(d)-1
  214. continue
  215. }
  216. t += s[i]
  217. }
  218. if t != ""
  219. list_push(r, t)
  220. return r
  221. }
  222. set_pseudomethod("string.split", str_split)
  223. func str_replace(s, w, b) {
  224. if type(s) != "string"
  225. throw "expected first argument to be: string, but got: " + type(s)
  226. if type(w) != "string"
  227. throw "expected second argument to be: string, but got: " + type(w)
  228. if type(b) != "string"
  229. throw "expected third argument to be: string, but got: " + type(b)
  230. var r = ""
  231. for var i = 0; i < len(s); i++ {
  232. if slice(s, i, i+len(w)-1) == w {
  233. r += b
  234. i += len(w)-1
  235. continue
  236. }
  237. r += s[i]
  238. }
  239. return r
  240. }
  241. set_pseudomethod("string.replace", str_replace)
  242. func table_keys(t) {
  243. if type(t) != "table"
  244. throw "expected first argument to be: table, but got: " + type(t)
  245. var r = []
  246. for var k of t
  247. list_push(r, k)
  248. return r
  249. }
  250. set_pseudomethod("table.keys", table_keys)
  251. func table_values(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, t[k])
  257. return r
  258. }
  259. set_pseudomethod("table.values", table_values)
  260. func reduce(f, xs) {
  261. if type(f) != "function"
  262. throw "expected first argument to be: function, but got: " + type(f)
  263. if type(xs) !in ("list", "tuple", "string", "bytes")
  264. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  265. if len(xs) == 0
  266. throw "cannot reduce empty list"
  267. r = xs[0]
  268. for var x of slice(xs, 1)
  269. r = f(r, x)
  270. if type(xs) == "tuple"
  271. return tuple(r)
  272. elif type(xs) == "string"
  273. return list_join(r)
  274. elif type(xs) == "bytes"
  275. return bytes(r)
  276. return r
  277. }
  278. set_pseudomethod("list.reduce", func (xs, f): reduce(f, xs))
  279. set_pseudomethod("tuple.reduce", func (xs, f): reduce(f, xs))
  280. set_pseudomethod("string.reduce", func (xs, f): reduce(f, xs))
  281. set_pseudomethod("bytes.reduce", func (xs, f): reduce(f, xs))
  282. func sum(xs)
  283. return reduce(func (x, y): x + y, xs)
  284. set_pseudomethod("list.sum", sum)
  285. set_pseudomethod("tuple.sum", sum)
  286. func product(xs)
  287. return reduce(func (x, y): x * y, xs)
  288. set_pseudomethod("list.product", product)
  289. set_pseudomethod("tuple.product", product)
  290. func all(l): reduce(func (x, y): x && y, l)
  291. set_pseudomethod("list.all", all)
  292. set_pseudomethod("tuple.all", all)
  293. func any(l): reduce(func (x, y): x || y, l)
  294. set_pseudomethod("list.any", any)
  295. set_pseudomethod("tuple.any", any)
  296. func map(f, xs) {
  297. if type(f) != "function"
  298. throw "expected first argument to be: function, but got: " + type(f)
  299. if type(xs) !in ("list", "tuple", "string", "bytes")
  300. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  301. if len(xs) == 0
  302. return xs
  303. var r = []
  304. for var x of xs
  305. list_push(r, f(x))
  306. if type(xs) == "tuple"
  307. return tuple(r)
  308. elif type(xs) == "string"
  309. return list_join(r)
  310. elif type(xs) == "bytes"
  311. return bytes(r)
  312. return r
  313. }
  314. set_pseudomethod("list.map", func (xs, f): map(f, xs))
  315. set_pseudomethod("tuple.map", func (xs, f): map(f, xs))
  316. set_pseudomethod("string.map", func (xs, f): map(f, xs))
  317. set_pseudomethod("bytes.map", func (xs, f): map(f, xs))
  318. func filter(f, xs) {
  319. if type(f) != "function"
  320. throw "expected first argument to be: function, but got: " + type(f)
  321. if type(xs) !in ("list", "tuple", "string", "bytes")
  322. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  323. if len(xs) == 0
  324. return xs
  325. var r = []
  326. for var x of xs
  327. if f(x)
  328. list_push(r, x)
  329. if type(xs) == "tuple"
  330. return tuple(r)
  331. elif type(xs) == "string"
  332. return list_join(r)
  333. elif type(xs) == "bytes"
  334. return bytes(r)
  335. return r
  336. }
  337. set_pseudomethod("list.filter", func (xs, f): filter(f, xs))
  338. set_pseudomethod("tuple.filter", func (xs, f): filter(f, xs))
  339. set_pseudomethod("string.filter", func (xs, f): filter(f, xs))
  340. set_pseudomethod("bytes.filter", func (xs, f): filter(f, xs))
  341. func str_index(s, w) {
  342. if s == "" || w == ""
  343. return -1
  344. if type(s) != "string"
  345. throw "expected first argument to be: string, but got: " + type(s)
  346. if type(w) != "string"
  347. throw "expected second argument to be: string, but got: " + type(w)
  348. for var i = 0; i < len(s); i++
  349. if len(w) == 1 && s[i] == w
  350. return i
  351. elif slice(s, i, i+len(w)-1) == w
  352. return i
  353. return -1
  354. }
  355. set_pseudomethod("string.index", str_index)
  356. func str_lstrip(s, cs=" \t\n\r\x0b\x0c") {
  357. if type(s) != "string"
  358. throw "expected first argument to be: string, but got: " + type(s)
  359. if type(cs) != "string"
  360. throw "expected second argument to be: string, but got: " + type(cs)
  361. if s == ""
  362. return s
  363. for var i = 0; s[i] in cs && i < len(s); i++
  364. pass
  365. return slice(s, i)
  366. }
  367. set_pseudomethod("string.lstrip", str_lstrip)
  368. func str_rstrip(s, cs=" \t\n\r\x0b\x0c") {
  369. if type(s) != "string"
  370. throw "expected first argument to be: string, but got: " + type(s)
  371. if type(cs) != "string"
  372. throw "expected second argument to be: string, but got: " + type(cs)
  373. if s == ""
  374. return s
  375. for var k = 0, i = len(s)-1; s[i] in cs && i >= 0; k++
  376. i--
  377. return slice(s, 0, len(s)-k-1)
  378. }
  379. set_pseudomethod("string.rstrip", str_rstrip)
  380. func str_strip(s, cs=" \t\n\r\x0b\x0c") {
  381. if type(s) != "string"
  382. throw "expected first argument to be: string, but got: " + type(s)
  383. if type(cs) != "string"
  384. throw "expected second argument to be: string, but got: " + type(cs)
  385. return str_lstrip(str_rstrip(s, cs), cs)
  386. }
  387. set_pseudomethod("string.strip", str_strip)
  388. func table_get(t, k, d=nil) {
  389. if type(t) != "table"
  390. throw "expected first argument to be: table, but got: " + type(t)
  391. if type(k) != "string"
  392. throw "expected second argument to be: string, but got: " + type(k)
  393. if k !in t
  394. return d
  395. return t[k]
  396. }
  397. set_pseudomethod("table.get", table_get)
  398. func zip() {
  399. if !arguments
  400. return []
  401. var l = map(len, arguments)
  402. l = reduce(min, l)
  403. var r = []
  404. for var i = 0; i < l; i++ {
  405. var t = []
  406. for var xs of arguments
  407. list_push(t, xs[i])
  408. list_push(r, t)
  409. }
  410. return r
  411. }
  412. func enumerate(l)
  413. if type(l) == "table"
  414. return zip(table_keys(l), table_values(l))
  415. else
  416. return zip(range(len(l)), l)
  417. func str_toupper(s) {
  418. if type(s) != "string"
  419. throw "expected first argument to be: string, but got: " + type(c)
  420. return map(func (c): c >= 'a' && c <= 'z'? chr(ord(c) - 32): c, s)
  421. }
  422. set_pseudomethod("string.toupper", str_toupper)
  423. func str_tolower(s) {
  424. if type(s) != "string"
  425. throw "expected first argument to be: string, but got: " + type(c)
  426. return map(func (c): c >= 'A' && c <= 'Z'? chr(ord(c) + 32): c, s)
  427. }
  428. set_pseudomethod("string.tolower", str_tolower)
  429. func Object(t, p=nil): return p !is nil? set_meta_table(p, get_meta_table(p) + t): set_meta_table({}, t)
  430. func is_object(o): return has_meta_table(o)
  431. func __class_wrapper(n, p, t, mt, st): return Object(st + {
  432. t: t,
  433. mt: mt,
  434. super: [],
  435. __type: func (this) use (n): return n,
  436. __str: func (this) use (n): return "<class " + n + ">",
  437. __call: func (this, pargs) use (p) {
  438. var t = {}
  439. var mt = {}
  440. for var other of p {
  441. t += other.t
  442. mt += other.mt
  443. list_push(this.super, other)
  444. }
  445. if len(this.super) == 1
  446. this.super = this.super[0]
  447. t += this.t
  448. mt += this.mt
  449. t.super = this.super
  450. obj = set_meta_table(t, mt)
  451. if "constructor" in mt
  452. func_call(mt.constructor, [obj] + pargs)
  453. return obj
  454. }
  455. })
  456. func format(s) {
  457. if type(s) != "string"
  458. throw "expected first argument to be: string, but got: " + type(s)
  459. var r = ""
  460. var n = 1
  461. for var i = 0; i < len(s); i++
  462. switch s[i] {
  463. case '_'
  464. if i+1 < len(s) && s[i+1] == '_' {
  465. r += '_'
  466. i++
  467. continue
  468. }
  469. r += repr(arguments[n++])
  470. break
  471. default
  472. r += s[i]
  473. }
  474. return r
  475. }
  476. set_pseudomethod("string.format", format)
  477. func formatl(s, l) {
  478. if type(s) != "string"
  479. throw "expected first argument to be: string, but got: " + type(s)
  480. if type(l) != "list"
  481. throw "expected second argument to be: list, but got: " + type(l)
  482. return func_call(str_format, [s] + l)
  483. }
  484. set_pseudomethod("string.formatl", formatl)
  485. func formatd(s, t) {
  486. if type(s) != "string"
  487. throw "expected first argument to be: string, but got: " + type(s)
  488. var r = ""
  489. var n = 1
  490. for var i = 0; i < len(s); i++
  491. switch s[i] {
  492. case '{'
  493. if i+1 < len(s) && s[i+1] == '{' {
  494. r += '{'
  495. i++
  496. continue
  497. }
  498. var k = ''
  499. i++
  500. for i < len(s) && s[i] != '}'
  501. k += s[i++]
  502. if i >= len(s) || s[i] != '}'
  503. throw "unmatched { in format specifier"
  504. if !k
  505. throw "empty format key"
  506. r += repr(t[k])
  507. break
  508. default
  509. r += s[i]
  510. }
  511. return r
  512. }
  513. set_pseudomethod("string.formatd", formatd)
  514. func getch() return chr(fgetc(STDIN))
  515. func putch(c) fputc(STDOUT, c)
  516. func getline()
  517. return fgets(STDIN, 256)
  518. func input() {
  519. if len(arguments) > 0
  520. func_call(print, arguments)
  521. return str_rstrip(getline(), "\n\r")
  522. }
  523. func open(path, mode="r"): fopen(path, mode)
  524. func assert(cond, msg="assertion failed")
  525. if !cond
  526. throw msg