std.qi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 `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 map(f, xs) {
  291. if type(f) != "function"
  292. throw "expected first argument to be: function, but got: " + type(f)
  293. if type(xs) !in ("list", "tuple", "string", "bytes")
  294. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  295. if len(xs) == 0
  296. return xs
  297. var r = []
  298. for var x of xs
  299. list_push(r, f(x))
  300. if type(xs) == "tuple"
  301. return tuple(r)
  302. elif type(xs) == "string"
  303. return list_join(r)
  304. elif type(xs) == "bytes"
  305. return bytes(r)
  306. return r
  307. }
  308. set_pseudomethod("list.map", func (xs, f): map(f, xs))
  309. set_pseudomethod("tuple.map", func (xs, f): map(f, xs))
  310. set_pseudomethod("string.map", func (xs, f): map(f, xs))
  311. set_pseudomethod("bytes.map", func (xs, f): map(f, xs))
  312. func filter(f, xs) {
  313. if type(f) != "function"
  314. throw "expected first argument to be: function, but got: " + type(f)
  315. if type(xs) !in ("list", "tuple", "string", "bytes")
  316. throw "expected second argument to be: list, tuple, string or bytes, but got: " + type(xs)
  317. if len(xs) == 0
  318. return xs
  319. var r = []
  320. for var x of xs
  321. if f(x)
  322. list_push(r, x)
  323. if type(xs) == "tuple"
  324. return tuple(r)
  325. elif type(xs) == "string"
  326. return list_join(r)
  327. elif type(xs) == "bytes"
  328. return bytes(r)
  329. return r
  330. }
  331. set_pseudomethod("list.filter", func (xs, f): filter(f, xs))
  332. set_pseudomethod("tuple.filter", func (xs, f): filter(f, xs))
  333. set_pseudomethod("string.filter", func (xs, f): filter(f, xs))
  334. set_pseudomethod("bytes.filter", func (xs, f): filter(f, xs))
  335. func str_index(s, w) {
  336. if s == "" || w == ""
  337. return -1
  338. if type(s) != "string"
  339. throw "expected first argument to be: string, but got: " + type(s)
  340. if type(w) != "string"
  341. throw "expected second argument to be: string, but got: " + type(w)
  342. for var i = 0; i < len(s); i++
  343. if len(w) == 1 && s[i] == w
  344. return i
  345. elif slice(s, i, i+len(w)-1) == w
  346. return i
  347. return -1
  348. }
  349. set_pseudomethod("string.index", str_index)
  350. func str_lstrip(s, cs=" \t\n\r\x0b\x0c") {
  351. if type(s) != "string"
  352. throw "expected first argument to be: string, but got: " + type(s)
  353. if type(cs) != "string"
  354. throw "expected second argument to be: string, but got: " + type(cs)
  355. if s == ""
  356. return s
  357. for var i = 0; s[i] in cs && i < len(s); i++
  358. pass
  359. return slice(s, i)
  360. }
  361. set_pseudomethod("string.lstrip", str_lstrip)
  362. func str_rstrip(s, cs=" \t\n\r\x0b\x0c") {
  363. if type(s) != "string"
  364. throw "expected first argument to be: string, but got: " + type(s)
  365. if type(cs) != "string"
  366. throw "expected second argument to be: string, but got: " + type(cs)
  367. if s == ""
  368. return s
  369. for var k = 0, i = len(s)-1; s[i] in cs && i >= 0; k++
  370. i--
  371. return slice(s, 0, len(s)-k-1)
  372. }
  373. set_pseudomethod("string.rstrip", str_rstrip)
  374. func str_strip(s, cs=" \t\n\r\x0b\x0c") {
  375. if type(s) != "string"
  376. throw "expected first argument to be: string, but got: " + type(s)
  377. if type(cs) != "string"
  378. throw "expected second argument to be: string, but got: " + type(cs)
  379. return str_lstrip(str_rstrip(s, cs), cs)
  380. }
  381. set_pseudomethod("string.strip", str_strip)
  382. func table_get(t, k, d=nil) {
  383. if type(t) != "table"
  384. throw "expected first argument to be: table, but got: " + type(t)
  385. if type(k) != "string"
  386. throw "expected second argument to be: string, but got: " + type(k)
  387. if k !in t
  388. return d
  389. return t[k]
  390. }
  391. set_pseudomethod("table.get", table_get)
  392. func zip() {
  393. if !arguments
  394. return []
  395. var l = map(len, arguments)
  396. l = reduce(min, l)
  397. var r = []
  398. for var i = 0; i < l; i++ {
  399. var t = []
  400. for var xs of arguments
  401. list_push(t, xs[i])
  402. list_push(r, t)
  403. }
  404. return r
  405. }
  406. func enumerate(l)
  407. if type(l) == "table"
  408. return zip(table_keys(l), table_values(l))
  409. else
  410. return zip(range(len(l)), l)
  411. func str_toupper(s) {
  412. if type(s) != "string"
  413. throw "expected first argument to be: string, but got: " + type(c)
  414. return map(func (c): c >= 'a' && c <= 'z'? chr(ord(c) - 32): c, s)
  415. }
  416. set_pseudomethod("string.toupper", str_toupper)
  417. func str_tolower(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.tolower", str_tolower)
  423. func Object(t, p=nil): return p !is nil? set_meta_table(p, get_meta_table(p) + t): set_meta_table({}, t)
  424. func is_object(o): return has_meta_table(o)
  425. func __class_wrapper(n, p, t, mt, st): return Object(st + {
  426. t: t,
  427. mt: mt,
  428. super: [],
  429. __type: func (this) use (n): return n,
  430. __str: func (this) use (n): return "<class " + n + ">",
  431. __call: func (this, pargs) use (p) {
  432. var t = {}
  433. var mt = {}
  434. for var other of p {
  435. t += other.t
  436. mt += other.mt
  437. list_push(this.super, other)
  438. }
  439. if len(this.super) == 1
  440. this.super = this.super[0]
  441. t += this.t
  442. mt += this.mt
  443. t.super = this.super
  444. obj = set_meta_table(t, mt)
  445. if "constructor" in mt
  446. func_call(mt.constructor, [obj] + pargs)
  447. return obj
  448. }
  449. })
  450. func format(s) {
  451. if type(s) != "string"
  452. throw "expected first argument to be: string, but got: " + type(s)
  453. var r = ""
  454. var n = 1
  455. for var i = 0; i < len(s); i++
  456. switch s[i] {
  457. case '_'
  458. if i+1 < len(s) && s[i+1] == '_' {
  459. r += '_'
  460. i++
  461. continue
  462. }
  463. r += repr(arguments[n++])
  464. break
  465. default
  466. r += s[i]
  467. }
  468. return r
  469. }
  470. set_pseudomethod("string.format", format)
  471. func formatl(s, l) {
  472. if type(s) != "string"
  473. throw "expected first argument to be: string, but got: " + type(s)
  474. if type(l) != "list"
  475. throw "expected second argument to be: list, but got: " + type(l)
  476. return func_call(str_format, [s] + l)
  477. }
  478. set_pseudomethod("string.formatl", formatl)
  479. func formatd(s, t) {
  480. if type(s) != "string"
  481. throw "expected first argument to be: string, but got: " + type(s)
  482. var r = ""
  483. var n = 1
  484. for var i = 0; i < len(s); i++
  485. switch s[i] {
  486. case '{'
  487. if i+1 < len(s) && s[i+1] == '{' {
  488. r += '{'
  489. i++
  490. continue
  491. }
  492. var k = ''
  493. i++
  494. for i < len(s) && s[i] != '}'
  495. k += s[i++]
  496. if i >= len(s) || s[i] != '}'
  497. throw "unmatched { in format specifier"
  498. if !k
  499. throw "empty format key"
  500. r += repr(t[k])
  501. break
  502. default
  503. r += s[i]
  504. }
  505. return r
  506. }
  507. set_pseudomethod("string.formatd", formatd)
  508. func getch() return chr(fgetc(STDIN))
  509. func putch(c) fputc(STDOUT, c)
  510. func getline()
  511. return fgets(STDIN, 256)
  512. func input() {
  513. if len(arguments) > 0
  514. func_call(print, arguments)
  515. return str_rstrip(getline(), "\n\r")
  516. }
  517. func open(path, mode="r"): fopen(path, mode)
  518. func assert(cond, msg="assertion failed")
  519. if !cond
  520. throw msg