lib/min_stack.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 |
import tables, random import ../core/parser, ../core/value, ../core/interpreter, ../core/utils # Operations on the whole stack proc stack_module*(i: In)= i.define() .symbol("newstack") do (i: In): while i.stack.len > 0: discard i.pop .symbol("stack") do (i: In): i.push i.stack.newVal(i.scope) .symbol("unstack") do (i: In): var q: MinValue i.reqQuotation q i.stack = q.qVal .symbol("id") do (i: In): discard .symbol("pop") do (i: In): if i.stack.len < 1: raiseEmptyStack() discard i.pop .symbol("popop") do (i: In): if i.stack.len < 2: raiseEmptyStack() discard i.pop discard i.pop # (pop) dip .symbol("popd") do (i: In): i.push newVal(@["pop".newSym], i.scope) i.push "dip".newSym # ((pop) dip unquote) .symbol("k") do (i: In): i.push newVal(@["pop".newSym], i.scope) i.push "dip".newSym i.push "unquote".newSym .symbol("dup") do (i: In): i.push i.peek # (dup) dip .symbol("dupd") do (i: In): i.push newVal(@["dup".newSym], i.scope) i.push "dip".newSym #((dup) dip unquote) .symbol("w") do (i: In): i.push newVal(@["dup".newSym], i.scope) i.push "dip".newSym i.push "unquote".newSym .symbol("dip") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop i.unquote(q) i.push v .symbol("cleave") do (i: In): var q: MinValue i.reqQuotationOfQuotations q let v = i.pop for s in q.qVal: var s1 = s i.push v i.unquote(s1) .symbol("spread") do (i: In): var q: MinValue i.reqQuotationOfQuotations q var els = newSeq[MinValue](0) for el in 0..q.qVal.len-1: els.add i.pop var count = els.len-1 for s in q.qVal: var s1 = s i.push els[count] i.unquote(s1) count.dec .symbol("keep") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop i.push v i.unquote(q) i.push v .symbol("swap") do (i: In): i.reqStackSize 2 let a = i.pop let b = i.pop i.push a i.push b .symbol("nip") do (i: In): i.reqStackSize 2 let a = i.pop discard i.pop i.push a .symbol("over") do (i: In): i.reqStackSize 2 let a = i.pop let b = i.pop i.push b i.push a i.push b .symbol("pick") do (i: In): i.reqStackSize 3 let a = i.pop let b = i.pop let c = i.pop i.push c i.push b i.push a i.push c .symbol("rollup") do (i: In): i.reqStackSize 3 let first = i.pop let second = i.pop let third = i.pop i.push first i.push second i.push third .symbol("rolldown") do (i: In): i.reqStackSize 3 let first = i.pop let second = i.pop let third = i.pop i.push second i.push first i.push third # (swap) dip .symbol("swapd") do (i: In): i.push newVal(@["swap".newSym], i.scope) i.push "dip".newSym # ((swap) dip unquote) .symbol("c") do (i: In): i.push newVal(@["swap".newSym], i.scope) i.push "dip".newSym i.push "unquote".newSym .symbol("cons") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop q.qVal = @[v] & q.qVal i.push q # (() cons dip) .symbol("dig1") do (i: In): i.push newVal(@[], i.scope) i.push "cons".newSym i.push "dip".newSym # (() cons cons dip) .symbol("dig2") do (i: In): i.push newVal(@[], i.scope) i.push "cons".newSym i.push "cons".newSym i.push "dip".newSym # (() cons cons cons dip) .symbol("dig3") do (i: In): i.push newVal(@[], i.scope) i.push "cons".newSym i.push "cons".newSym i.push "cons".newSym i.push "dip".newSym # ((() cons) dip swap unquote) .symbol("bury1") do (i: In): i.push newVal(@[newVal(@[], i.scope), "cons".newSym], i.scope) i.push "dip".newSym i.push "swap".newSym i.push "unquote".newSym # ((() cons cons) dip swap unquote) .symbol("bury2") do (i: In): i.push newVal(@[newVal(@[], i.scope), "cons".newSym, "cons".newSym], i.scope) i.push "dip".newSym i.push "swap".newSym i.push "unquote".newSym # ((() cons cons cons) dip swap unquote) .symbol("bury3") do (i: In): i.push newVal(@[newVal(@[], i.scope), "cons".newSym, "cons".newSym, "cons".newSym], i.scope) i.push "dip".newSym i.push "swap".newSym i.push "unquote".newSym .symbol("swons") do (i: In): i.push "swap".newSym i.push "cons".newSym .symbol("sip") do (i: In): var a, b: MinValue i.reqTwoQuotations a, b i.push b i.unquote(a) i.push b .finalize("stack") |