From f13a809597991b705db6f185bd356c34eca967be Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 11 Nov 2024 11:39:32 -0800 Subject: Allow pre-inc/dec on variables --- .../src/test/java/com/seaofnodes/simple/BrainFuckTest.java | 6 ++---- .../src/test/java/com/seaofnodes/simple/Chapter08Test.java | 2 +- .../src/test/java/com/seaofnodes/simple/Chapter11Test.java | 2 +- .../src/test/java/com/seaofnodes/simple/Chapter17Test.java | 12 ++++++++++++ 4 files changed, 16 insertions(+), 6 deletions(-) (limited to 'chapter17/src/test/java/com/seaofnodes') diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java index 28c3477c..0c2509bf 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java @@ -48,8 +48,7 @@ for( int pc = 0; pc < program#; pc++ ) { } else if (command == 91) { if (data[d] == 0) { for( var d = 1; d > 0; ) { - pc++; - command = program[pc]; + command = program[++pc]; if (command == 91) d++; if (command == 93) d--; } @@ -57,8 +56,7 @@ for( int pc = 0; pc < program#; pc++ ) { } else if (command == 93) { if (data[d]) { for( var d = 1; d > 0; ) { - pc--; - command = program[pc]; + command = program[--pc]; if (command == 93) d++; if (command == 91) d--; } diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter08Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter08Test.java index 29fd09dc..bb3bd674 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter08Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter08Test.java @@ -138,7 +138,7 @@ return arg; @Test public void testRegress2() { - Parser parser = new Parser("if(1) return 0; else while(arg>--arg) arg=arg+1; return 0;"); + Parser parser = new Parser("if(1) return 0; else while(arg>- -arg) arg=arg+1; return 0;"); StopNode stop = parser.parse().iterate(); assertEquals("return 0;", stop.toString()); } diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java index 11424720..ddcc9e63 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java @@ -292,7 +292,7 @@ else { } """); StopNode stop = parser.parse().iterate(); - assertEquals("Stop[ return 9; return 0; return 0; ]", stop.toString()); + assertEquals("Stop[ return 9; return 0; return 0; return 0; return 0; return 0; return 0; return 0; ]", stop.toString()); } diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java index d39cee2d..3773f87f 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java @@ -3,6 +3,7 @@ package com.seaofnodes.simple; import com.seaofnodes.simple.evaluator.Evaluator; import com.seaofnodes.simple.node.StopNode; import org.junit.Test; +import org.junit.Ignore; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -87,6 +88,17 @@ return s.x; assertEquals(65535L, Evaluator.evaluate(stop, 2)); } + @Test + public void testInc6() { + Parser parser = new Parser(""" +return --arg; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return (arg+-1);", stop.toString()); + assertEquals(-1L, Evaluator.evaluate(stop, 0)); + } + + @Test public void testVar0() { Parser parser = new Parser(""" -- cgit v1.2.3