lib/min_seq.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
import critbits, tables, sequtils, algorithm import ../core/parser, ../core/value, ../core/interpreter, ../core/utils # Operations on sequences (data quotations) proc seq_module*(i: In)= i.define() .symbol("concat") do (i: In): var q1, q2: MinValue i.reqTwoQuotations q1, q2 let q = q2.qVal & q1.qVal i.push q.newVal(i.scope) .symbol("first") do (i: In): var q: MinValue i.reqQuotation q if q.qVal.len == 0: raiseOutOfBounds("Quotation is empty") i.push q.qVal[0] .symbol("rest") do (i: In): var q: MinValue i.reqQuotation q if q.qVal.len == 0: raiseOutOfBounds("Quotation is empty") i.push q.qVal[1..q.qVal.len-1].newVal(i.scope) .symbol("append") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop i.push newVal(q.qVal & v, i.scope) .symbol("prepend") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop i.push newVal(v & q.qVal, i.scope) .symbol("get") do (i: In): var index, q: MinValue i.reqIntAndQuotation index, q let ix = index.intVal if q.qVal.len < ix or ix < 0: raiseOutOfBounds("Index out of bounds") i.push q.qVal[ix.int] .symbol("set") do (i: In): var val, index, q: MinValue i.reqInt index val = i.pop i.reqQuotation q let ix = index.intVal if q.qVal.len < ix or ix < 0: raiseOutOfBounds("Index out of bounds") q.qVal[ix.int] = val i.push q .symbol("remove") do (i: In): var index, q: MinValue i.reqIntAndQuotation index, q let ix = index.intVal if q.qVal.len < ix or ix < 0: raiseOutOfBounds("Index out of bounds") var res = newSeq[MinValue](0) for x in 0..q.qVal.len-1: if x == ix: continue res.add q.qVal[x] i.push res.newVal(i.scope) .symbol("insert") do (i: In): var val, index, q: MinValue i.reqInt index val = i.pop i.reqQuotation q let ix = index.intVal if q.qVal.len < ix or ix < 0: raiseOutOfBounds("Index out of bounds") var res = newSeq[MinValue](0) for x in 0..q.qVal.len-1: if x == ix: res.add val res.add q.qVal[x] i.push res.newVal(i.scope) .symbol("size") do (i: In): var q: MinValue i.reqQuotation q i.push q.qVal.len.newVal .symbol("in?") do (i: In): i.reqStackSize(2) let v = i.pop var q: MinValue i.reqQuotation q i.push q.qVal.contains(v).newVal .symbol("map") do (i: In): var prog, list: MinValue i.reqTwoQuotations prog, list var res = newSeq[MinValue](0) for litem in list.qVal: i.push litem i.unquote(prog) res.add i.pop i.push res.newVal(i.scope) .symbol("apply") do (i: In): var prog: MinValue i.reqQuotation prog i.apply prog .symbol("reverse") do (i: In): var q: MinValue i.reqQuotation q var res = newSeq[MinValue](0) for c in countdown(q.qVal.len-1, 0): res.add q.qVal[c] i.push res.newVal(i.scope) .symbol("filter") do (i: In): var filter, list: MinValue i.reqTwoQuotations filter, list var res = newSeq[MinValue](0) for e in list.qVal: i.push e i.unquote(filter) var check = i.pop if check.isBool and check.boolVal == true: res.add e i.push res.newVal(i.scope) .symbol("reject") do (i: In): var filter, list: MinValue i.reqTwoQuotations filter, list var res = newSeq[MinValue](0) for e in list.qVal: i.push e i.unquote(filter) var check = i.pop if check.isBool and check.boolVal == false: res.add e i.push res.newVal(i.scope) .symbol("any?") do (i: In): var filter, list: MinValue i.reqTwoQuotations filter, list for e in list.qVal: i.push e i.unquote(filter) var check = i.pop if check.isBool and check.boolVal == true: i.push true.newVal return i.push false.newVal .symbol("all?") do (i: In): var filter, list: MinValue i.reqTwoQuotations filter, list for e in list.qVal: i.push e i.unquote(filter) var check = i.pop if check.isBool and check.boolVal == false: i.push false.newVal break i.push true.newVal .symbol("sort") do (i: In): var cmp, list: MinValue i.reqTwoQuotations cmp, list var i2 = i var minCmp = proc(a, b: MinValue): int {.closure.}= i2.push a i2.push b i2.unquote(cmp) let r = i2.pop if r.isBool: if r.isBool and r.boolVal == true: return 1 else: return -1 else: raiseInvalid("Predicate quotation must return a boolean value") var qList = list.qVal sort[MinValue](qList, minCmp) i.push qList.newVal(i.scope) .symbol("shorten") do (i: In): var n, q: MinValue i.reqIntAndQuotation n, q if n.intVal > q.qVal.len: raiseInvalid("Quotation is too short") i.push q.qVal[0..n.intVal.int-1].newVal(i.scope) .symbol("find") do (i: In): var s, test, result: MinValue i.reqTwoQuotations test, s var res = -1 var c = 0 for el in s.qVal: i.push el i.unquote test result = i.pop if result.isBool and result.boolVal == true: res = c break c.inc i.push res.newVal .symbol("reduce") do (i: In): var s, q, acc: MinValue i.reqQuotation q acc = i.pop i.reqQuotation s for el in s.qVal: i.push acc i.push el i.unquote q acc = i.pop i.push acc .symbol("map-reduce") do (i: In): var s, map, red, acc: MinValue i.reqThreeQuotations red, map, s if s.qVal.len == 0: raiseInvalid("Quotation must have at least one element") i.push s.qVal[0] i.unquote map acc = i.pop for ix in 1..s.qVal.len-1: i.push s.qVal[ix] i.unquote map i.push acc i.unquote red acc = i.pop i.push acc .symbol("partition") do (i: In): var s, test: MinValue i.reqTwoQuotations test, s var tseq = newSeq[MinValue](0) var fseq = newSeq[MinValue](0) for el in s.qVal: i.push el i.unquote test let res = i.pop if res.isBool and res.boolVal == true: tseq.add el else: fseq.add el i.push tseq.newVal(i.scope) i.push fseq.newVal(i.scope) .symbol("slice") do (i: In): var start, finish, q: MinValue i.reqInt finish i.reqInt start i.reqQuotation q let st = start.intVal let fn = finish.intVal if st < 0 or fn > q.qVal.len-1: raiseOutOfBounds("Index out of bounds") elif fn < st: raiseInvalid("End index must be greater than start index") let rng = q.qVal[st.int..fn.int] i.push rng.newVal(i.scope) .symbol("harvest") do (i: In): var q: MinValue i.reqQuotation q var res = newSeq[MinValue](0) for el in q.qVal: if el.isQuotation and el.qVal.len == 0: continue res.add el i.push res.newVal(i.scope) .symbol("flatten") do (i: In): var q: MinValue i.reqQuotation q var res = newSeq[MinValue](0) for el in q.qVal: if el.isQuotation: for el2 in el.qVal: res.add el2 else: res.add el i.push res.newVal(i.scope) # Operations on dictionaries .symbol("dhas?") do (i: In): var d, k: MinValue i.reqStringLike k i.reqDictionary d i.push d.dhas(k).newVal .symbol("dget") do (i: In): var d, k: MinValue i.reqStringLike k i.reqDictionary d i.push d.dget(k) .symbol("dset") do (i: In): var d, k: MinValue i.reqStringLike k let m = i.pop i.reqDictionary d i.push i.dset(d, k, m) .symbol("ddel") do (i: In): var d, k: MinValue i.reqStringLike k i.reqDictionary d i.push i.ddel(d, k) .symbol("keys") do (i: In): var d: MinValue i.reqDictionary d i.push i.keys(d) .symbol("values") do (i: In): var d: MinValue i.reqDictionary d i.push i.values(d) .finalize("seq") |