summaryrefslogtreecommitdiff
path: root/chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-01 10:33:55 -0800
committerCliff Click <cliffc@acm.org>2026-11-01 10:33:55 -0800
commitf4bd723d929e53475f7990374dca7b7dfcbbdc42 (patch)
tree7bb080119e7b88ad4ac9b30af019fe6a5e4ba9dd /chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
parent0ac0c9e10287972ac3af5a4ee43d26f0d5b318fe (diff)
downloadSimple-main.tar.gz
Simple-main.zip
Add Chapter 29 for tech debt cleanup with the parser splitmain
Keep Chapter 28b intact and carry the grammar-oriented parser package, Var move, IterPeeps move, and parser regression tests into Chapter 29. Defer privacy and exclude its experimental tests and fixture changes. Register the chapter in the indexes, IntelliJ modules and run configurations, Maven reactor, roadmap, and linear-history publisher. Windows and Linux each pass 573 tests, release builds, 199 allocation replays, and 29 native checks.
Diffstat (limited to 'chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java')
-rw-r--r--chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
new file mode 100644
index 00000000..c2713e4f
--- /dev/null
+++ b/chapter29/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -0,0 +1,87 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.isa.eval.EvalRisc5;
+
+import com.seaofnodes.isa.eval.EvalArm64;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class BrainFuckTest {
+
+ @Test public void testBrainFuck() throws IOException {
+ String brain_fuck = "Hello World!\n";
+ String src =
+"""
+// -*- mode: java; -*-
+val brain_fuck = { ->
+ var !program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
+ int 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 == '>') {
+ d++;
+ } else if (command == '<') {
+ d--;
+ } else if (command == '+') {
+ data[d]++;
+ } else if (command == '-') {
+ data[d]--;
+ } else if (command == '.') {
+ // Output a byte; increase the output array size
+ var old = output;
+ output = new u8[output# + 1];
+ for( int i = 0; i < old#; i++ )
+ output[i] = old[i];
+ output[old#] = data[d]; // Add the extra byte on the end
+ } else if (command == ',') {
+ data[d] = 42;
+ } else if (command == '[') {
+ if (data[d] == 0) {
+ for( int d = 1; d > 0; ) {
+ command = program[++pc];
+ if (command == 91) d++;
+ if (command == 93) d--;
+ }
+ }
+ } else if (command == ']') {
+ if (data[d]) {
+ for( int d = 1; d > 0; ) {
+ command = program[--pc];
+ if (command == 93) d++;
+ if (command == 91) d--;
+ }
+ }
+ }
+ }
+ return output;
+};
+""";
+ TestC.runC(src,"brain_fuck", brain_fuck, 40);
+
+ EvalRisc5 R5 = TestRisc5.build( src, "brain_fuck", 0, 25, false);
+ int trap = R5.step(100000);
+ assertEquals(0,trap);
+ int ptr = (int)R5.regs[com.seaofnodes.simple.node.cpus.riscv.riscv.A0];
+ assertEquals(brain_fuck.length(),R5.ld4s(ptr));
+ for( int i=0; i<brain_fuck.length(); i++ )
+ assertEquals(brain_fuck.charAt(i), R5.ld1z(ptr+4+i));
+
+ EvalArm64 A5 = TestArm64.build("brain_fuck", src, 0, 28, false);
+ int trap_arm = A5.step(100000);
+ assertEquals(0,trap_arm);
+ assertEquals(0,trap);
+ int ptr_arm = (int)A5.regs[com.seaofnodes.simple.node.cpus.arm.arm.X0];
+ assertEquals(brain_fuck.length(),A5.ld4s(ptr_arm));
+
+ for( int i=0; i<brain_fuck.length(); i++ )
+ assertEquals(brain_fuck.charAt(i), A5.ld1z(ptr_arm+4+i));
+ }
+
+}