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 |
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 # ((dip) cons cons) .symbol("take") do (i: In): i.push newVal(@["dip".newSym], i.scope) i.push "cons".newSym i.push "cons".newSym .symbol("swap") do (i: In): if i.stack.len < 2: raiseEmptyStack() let a = i.pop let b = i.pop i.push a i.push b .symbol("rollup") do (i: In): if i.stack.len < 3: raiseEmptyStack() 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): if i.stack.len < 3: raiseEmptyStack() 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") |