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 |
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("at") do (i: In): var index, q: MinValue i.reqIntAndQuotation index, q if q.qVal.len-1 < index.intVal: raiseOutOfBounds("Insufficient items in quotation") i.push q.qVal[index.intVal.int] .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("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.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) # 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") |