From 05887323c0cfd3fa1f950515cd239998c8738266 Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Sat, 5 Feb 2022 03:24:30 +0100 Subject: [PATCH] new test, bugfix --- bin/.placeholder | 1 + src/ndspkg/vm.nim | 2 ++ tests/closures.nds_d | 32 ++++++++++++++++++++++++++++++++ tests/collections.nds | 9 +++++++++ tests/precedence.nds | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 bin/.placeholder create mode 100644 tests/closures.nds_d create mode 100644 tests/collections.nds create mode 100644 tests/precedence.nds diff --git a/bin/.placeholder b/bin/.placeholder new file mode 100644 index 0000000..f4ee7b5 --- /dev/null +++ b/bin/.placeholder @@ -0,0 +1 @@ +ignoreme \ No newline at end of file diff --git a/src/ndspkg/vm.nim b/src/ndspkg/vm.nim index 7d1e41a..76dfbd2 100644 --- a/src/ndspkg/vm.nim +++ b/src/ndspkg/vm.nim @@ -235,6 +235,7 @@ proc run*(chunk: Chunk): InterpretResult = else: let start = stack.getIndexNeg(listLen - 1).addr var list = newListCopymem[NdValue](start, listLen) + stack.deleteTopN(listLen) stack.push(list.fromList()) of opCreateTable: let tblLen = readDU8() @@ -245,6 +246,7 @@ proc run*(chunk: Chunk): InterpretResult = if tbl[].tableSet(key, val): runtimeError("Attempt to redefine an existing value inside table declaration.") break + stack.deleteTopN(tblLen * 2) stack.push(tbl.fromTable()) of opLen: let res = stack.peek().getLength() diff --git a/tests/closures.nds_d b/tests/closures.nds_d new file mode 100644 index 0000000..287bca6 --- /dev/null +++ b/tests/closures.nds_d @@ -0,0 +1,32 @@ +//_d so the test doesn't run yet + +// capturing closures in lists: + +var f = funct() { + var y = 5; + var x = @[ + funct() print y, + funct() y = y + 1 + ]; + :result = x; +}; + +//expect:5.0 +f()[0](); +f()[1](); +//expect:6.0 +f()[0](); + +// capturing the result of a function: + +var f2 = funct() { + var x = { @f2 + :result = funct() { + print :f2; + }; + }; + x = 5; +}; + +//expect:5.0 +f2()(); \ No newline at end of file diff --git a/tests/collections.nds b/tests/collections.nds new file mode 100644 index 0000000..2bca596 --- /dev/null +++ b/tests/collections.nds @@ -0,0 +1,9 @@ +// a test about collections, WIP + +var returnlist = funct() { + :result = @[1, 2, 3, 4]; +}; + +//expect:3.0 +print returnlist()[2]; + diff --git a/tests/precedence.nds b/tests/precedence.nds new file mode 100644 index 0000000..158041e --- /dev/null +++ b/tests/precedence.nds @@ -0,0 +1,35 @@ +// test for testing the relative precedence of expressions + +// groupings + +//expect:15.0 +print 5 * (1 + 2); + +//expect:11.0 +print (5 * 2) + 1; + +//expect:-9.0 +print -((3 + 2) * 2) + 1; + +// calls + +// calls and indexes + +var returnlist = funct() { + :result = @[]; + :result[0] = 4; + :result[1] = 6; + :result[2] = 5; + :result[3] = 7; +}; + +//expect:5.0 +print returnlist()[2]; + +// priority over unary +var truesayer = funct() { + :result = true; +}; + +//expect:false +print !truesayer(); \ No newline at end of file