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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
import critbits, tables, sequtils, algorithm import ../core/parser, ../core/value, ../core/interpreter, ../core/utils # Operations on sequences (data quotations) proc seq_module*(i: In)= let def = i.define() def.symbol("concat") do (i: In): let vals = i.expect("quot", "quot") let q1 = vals[0] let q2 = vals[1] let q = q2.qVal & q1.qVal i.push q.newVal(i.scope) def.symbol("first") do (i: In): let vals = i.expect("quot") let q = vals[0] if q.qVal.len == 0: raiseOutOfBounds("Quotation is empty") i.push q.qVal[0] def.symbol("rest") do (i: In): let vals = i.expect("quot") let q = vals[0] if q.qVal.len == 0: raiseOutOfBounds("Quotation is empty") i.push q.qVal[1..q.qVal.len-1].newVal(i.scope) def.symbol("append") do (i: In): let vals = i.expect("quot", "a") let q = vals[0] let v = vals[1] i.push newVal(q.qVal & v, i.scope) def.symbol("prepend") do (i: In): let vals = i.expect("quot", "a") let q = vals[0] let v = vals[1] i.push newVal(v & q.qVal, i.scope) def.symbol("get") do (i: In): let vals = i.expect("int", "quot") let index = vals[0] let q = vals[1] let ix = index.intVal if q.qVal.len < ix or ix < 0: raiseOutOfBounds("Index out of bounds") i.push q.qVal[ix.int] def.symbol("set") do (i: In): let vals = i.expect("int", "a", "quot") let index = vals[0] let val = vals[1] let q = vals[2] 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 def.symbol("remove") do (i: In): let vals = i.expect("int", "quot") let index = vals[0] let q = vals[1] 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) def.symbol("insert") do (i: In): let vals = i.expect("int", "a", "quot") let index = vals[0] let val = vals[1] let q = vals[2] 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) def.symbol("size") do (i: In): let vals = i.expect("quot") let q = vals[0] i.push q.qVal.len.newVal def.symbol("in?") do (i: In): let vals = i.expect("a", "quot") let v = vals[0] let q = vals[1] i.push q.qVal.contains(v).newVal def.symbol("map") do (i: In): let vals = i.expect("quot", "quot") var prog = vals[0] let list = vals[1] var res = newSeq[MinValue](0) for litem in list.qVal: i.push litem i.dequote(prog) res.add i.pop i.push res.newVal(i.scope) def.symbol("reverse") do (i: In): let vals = i.expect("quot") let q = vals[0] 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) def.symbol("filter") do (i: In): let vals = i.expect("quot", "quot") var filter = vals[0] let list = vals[1] var res = newSeq[MinValue](0) for e in list.qVal: i.push e i.dequote(filter) var check = i.pop if check.isBool and check.boolVal == true: res.add e i.push res.newVal(i.scope) def.symbol("reject") do (i: In): let vals = i.expect("quot", "quot") var filter = vals[0] let list = vals[1] var res = newSeq[MinValue](0) for e in list.qVal: i.push e i.dequote(filter) var check = i.pop if check.isBool and check.boolVal == false: res.add e i.push res.newVal(i.scope) def.symbol("any?") do (i: In): let vals = i.expect("quot", "quot") var filter = vals[0] let list = vals[1] for e in list.qVal: i.push e i.dequote(filter) var check = i.pop if check.isBool and check.boolVal == true: i.push true.newVal return i.push false.newVal def.symbol("all?") do (i: In): let vals = i.expect("quot", "quot") var filter = vals[0] let list = vals[1] for e in list.qVal: i.push e i.dequote(filter) var check = i.pop if check.isBool and check.boolVal == false: i.push false.newVal break i.push true.newVal def.symbol("sort") do (i: In): let vals = i.expect("quot", "quot") var cmp = vals[0] let list = vals[1] var i2 = i var minCmp = proc(a, b: MinValue): int {.closure.}= i2.push a i2.push b i2.dequote(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) def.symbol("shorten") do (i: In): let vals = i.expect("int", "quot") let n = vals[0] let q = vals[1] if n.intVal > q.qVal.len: raiseInvalid("Quotation is too short") i.push q.qVal[0..n.intVal.int-1].newVal(i.scope) def.symbol("find") do (i: In): let vals = i.expect("quot", "quot") var test = vals[0] let s = vals[1] var result: MinValue var res = -1 var c = 0 for el in s.qVal: i.push el i.dequote test result = i.pop if result.isBool and result.boolVal == true: res = c break c.inc i.push res.newVal def.symbol("reduce") do (i: In): let vals = i.expect("quot", "a", "quot") var q = vals[0] var acc = vals[1] let s = vals[2] for el in s.qVal: i.push acc i.push el i.dequote q acc = i.pop i.push acc def.symbol("map-reduce") do (i: In): let vals = i.expect("quot", "quot", "quot") var red = vals[0] var map = vals[1] let s = vals[2] if s.qVal.len == 0: raiseInvalid("Quotation must have at least one element") i.push s.qVal[0] i.dequote map var acc = i.pop for ix in 1..s.qVal.len-1: i.push s.qVal[ix] i.dequote map i.push acc i.dequote red acc = i.pop i.push acc def.symbol("partition") do (i: In): let vals = i.expect("quot", "quot") var test = vals[0] var s = vals[1] var tseq = newSeq[MinValue](0) var fseq = newSeq[MinValue](0) for el in s.qVal: i.push el i.dequote 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) def.symbol("slice") do (i: In): let vals = i.expect("int", "int", "quot") let finish = vals[0] let start = vals[1] let q = vals[2] 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) def.symbol("harvest") do (i: In): let vals = i.expect("quot") let q = vals[0] 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) def.symbol("flatten") do (i: In): let vals = i.expect("quot") let q = vals[0] 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 def.symbol("dhas?") do (i: In): let vals = i.expect("'sym", "dict") let k = vals[0] let d = vals[1] i.push d.dhas(k).newVal def.symbol("dget") do (i: In): let vals = i.expect("'sym", "dict") let k = vals[0] let d = vals[1] i.push d.dget(k) def.symbol("dset") do (i: In): let vals = i.expect("'sym", "a", "dict") let k = vals[0] let m = vals[1] let d = vals[2] i.push i.dset(d, k, m) def.symbol("ddel") do (i: In): let vals = i.expect("'sym", "dict") let k = vals[0] let d = vals[1] i.push i.ddel(d, k) def.symbol("dkeys") do (i: In): let vals = i.expect("dict") let d = vals[0] i.push i.keys(d) def.symbol("dvalues") do (i: In): let vals = i.expect("dict") let d = vals[0] i.push i.values(d) def.symbol("dsort") do (i: In): let vals = i.expect("dict") var d = vals[0] i.push d.qVal.sortedByIt(it.qVal[0].getString).newVal(i.scope) def.symbol("dpick") do (i: In): let vals = i.expect("quot", "dict") var q = vals[0] var d = vals[1] var res = newSeq[MinValue](0) for v in d.qVal: if q.qVal.contains v.qVal[0]: res.add v i.push res.newVal(i.scope) def.sigil("?") do (i: In): i.push("dhas?".newSym) def.sigil("/") do (i: In): i.push("dget".newSym) def.sigil("%") do (i: In): i.push("dset".newSym) def.finalize("seq") |