Added stack module.
h3rald h3rald@h3rald.com
Sat, 25 Mar 2017 10:04:51 +0100
5 files changed,
69 insertions(+),
51 deletions(-)
M
lib/min_lang.nim
→
lib/min_lang.nim
@@ -389,56 +389,6 @@ finally:
if hasFinally: i.unquote("<try-finally>", final) - # Operations on the whole stack - - .symbol("id") do (i: In): - discard - - .symbol("pop") do (i: In): - if i.stack.len < 1: - raiseEmptyStack() - discard i.pop - - .symbol("dup") do (i: In): - i.push i.peek - - .symbol("dip") do (i: In): - var q: MinValue - i.reqQuotation q - let v = i.pop - i.unquote("<dip>", q) - i.push v - - .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("sip") do (i: In): - var a, b: MinValue - i.reqTwoQuotations a, b - i.push b - i.unquote("<sip>", a) - i.push b - - .symbol("clear-stack") do (i: In): - while i.stack.len > 0: - discard i.pop - - .symbol("dump-stack") do (i: In): - echo i.dump - - .symbol("get-stack") do (i: In): - i.push i.stack.newVal(i.scope) - - .symbol("set-stack") do (i: In): - var q: MinValue - i.reqQuotation q - i.stack = q.qVal - # Operations on quotations or strings .symbol("concat") do (i: In):
A
lib/min_stack.nim
@@ -0,0 +1,64 @@
+import + tables, + random +import + ../core/parser, + ../core/value, + ../core/interpreter, + ../core/utils + +# Operations on the whole stack +proc stack_module*(i: In)= + + i.define("stack") + + .symbol("id") do (i: In): + discard + + .symbol("pop") do (i: In): + if i.stack.len < 1: + raiseEmptyStack() + discard i.pop + + .symbol("dup") do (i: In): + i.push i.peek + + .symbol("dip") do (i: In): + var q: MinValue + i.reqQuotation q + let v = i.pop + i.unquote("<dip>", q) + i.push v + + .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("sip") do (i: In): + var a, b: MinValue + i.reqTwoQuotations a, b + i.push b + i.unquote("<sip>", a) + i.push b + + .symbol("clear-stack") do (i: In): + while i.stack.len > 0: + discard i.pop + + .symbol("dump-stack") do (i: In): + echo i.dump + + .symbol("get-stack") do (i: In): + i.push i.stack.newVal(i.scope) + + .symbol("set-stack") do (i: In): + var q: MinValue + i.reqQuotation q + i.stack = q.qVal + + + .finalize()
M
min.nim
→
min.nim
@@ -19,6 +19,7 @@ core/interpreter,
core/utils import lib/min_lang, + lib/min_stack, lib/min_num, lib/min_str, lib/min_logic,@@ -112,6 +113,7 @@ MINIMHISTORY.writeFile("")
if not MINIMRC.fileExists: MINIMRC.writeFile("") i.lang_module + i.stack_module i.io_module i.logic_module i.num_module
M
prelude.min
→
prelude.min
@@ -1,4 +1,5 @@
; Imports +'stack import 'str import 'io import 'logic import@@ -14,7 +15,7 @@ ((pop) dip) :popd
((dup) dip) :dupd ((swap) dip) :swapd ((dup) dip i) :q -((zap) dip i) :k +((pop) dip i) :k ((cons) dip i) :b ((swap) dip i) :c ((dip) cons cons) :take
M
tests/all.min
→
tests/all.min
@@ -1,4 +1,5 @@
'lang load +;'stack load 'io load 'logic load 'num load