summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-03 14:03:48 -0700
committerCliff Click <cliffc@acm.org>2026-10-04 13:30:41 -0700
commit3476d99690805b3ebe69219b326e5e1b64ebd0a4 (patch)
treecb05c136d87603d292c44ac4e5df38d8136f1872 /src/test/java/com/seaofnodes/simple/BrainFuckTest.java
parent9a5ca20d2537179f3a8e01762c8db6b52a4db2d9 (diff)
downloadSimple-linear-chapter17b.tar.gz
Simple-linear-chapter17b.zip
Chapter 17b: Syntax Sugarlinear-chapter17b
Diffstat (limited to 'src/test/java/com/seaofnodes/simple/BrainFuckTest.java')
-rw-r--r--src/test/java/com/seaofnodes/simple/BrainFuckTest.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
new file mode 100644
index 00000000..154b74a2
--- /dev/null
+++ b/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -0,0 +1,72 @@
+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 java.nio.charset.StandardCharsets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class BrainFuckTest {
+
+ @Test
+ public void testBrainfuck() {
+ var program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.".getBytes(StandardCharsets.UTF_8);
+ var encoded = new StringBuilder("u8[] !program = new u8[").append(program.length).append("];");
+ for (int i = 0; i < program.length; i++) {
+ int value = program[i] & 0xFF;
+ encoded.append("program[").append(i).append("] = ").append(value).append(";");
+ }
+
+ Parser parser = new Parser(
+ encoded + """
+var d = 0;
+u8[] !output = new u8[0];
+u8[] !data = new u8[100];
+
+for( int pc = 0; pc < program#; pc++ ) {
+ var command = program[pc];
+ if (command == 62) {
+ d++;
+ } else if (command == 60) {
+ d--;
+ } else if (command == 43) {
+ data[d]++;
+ } else if (command == 45) {
+ data[d]--;
+ } else if (command == 46) {
+ // Output a byte; increase the output array size
+ var old = output;
+ output = new u8[output# + 1];
+ for( var i = 0; i < old#; i++ )
+ output[i] = old[i];
+ output[old#] = data[d]; // Add the extra byte on the end
+ } else if (command == 44) {
+ data[d] = 42;
+ } else if (command == 91) {
+ if (data[d] == 0) {
+ for( var d = 1; d > 0; ) {
+ command = program[++pc];
+ if (command == 91) d++;
+ if (command == 93) d--;
+ }
+ }
+ } else if (command == 93) {
+ if (data[d]) {
+ for( var d = 1; d > 0; ) {
+ command = program[--pc];
+ if (command == 93) d++;
+ if (command == 91) d--;
+ }
+ }
+ }
+}
+return output;
+ """);
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Hello World!\n", Evaluator.evaluate(stop, 0, 10000).toString());
+ }
+}