summaryrefslogtreecommitdiff
path: root/chapter17/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-11-11 09:55:48 -0800
committerCliff Click <cliffc@acm.org>2024-11-23 09:03:20 -0800
commit688d2230b93b03f2d4505346ef3b89b0f4c46445 (patch)
tree14a0fc88425a2f86a6f0a00f3c52334142d7d04c /chapter17/src/test/java/com/seaofnodes
parentc523a94308747083a08cc8e698d5f02c043c6127 (diff)
downloadSimple-688d2230b93b03f2d4505346ef3b89b0f4c46445.tar.gz
Simple-688d2230b93b03f2d4505346ef3b89b0f4c46445.zip
Allow C-style for loops
Diffstat (limited to 'chapter17/src/test/java/com/seaofnodes')
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java18
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java93
2 files changed, 83 insertions, 28 deletions
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
index de6852c7..28c3477c 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -23,11 +23,11 @@ public class BrainFuckTest {
Parser parser = new Parser(
encoded + """
-var pc = 0, d = 0;
+var d = 0;
u8[] !output = new u8[0];
val data = new u8[100];
-while (pc < program#) {
+for( int pc = 0; pc < program#; pc++ ) {
var command = program[pc];
if (command == 62) {
d++;
@@ -40,16 +40,14 @@ while (pc < program#) {
} else if (command == 46) {
var old = output;
output = new u8[output# + 1];
- var i = 0;
- while (i < old#)
- output[i] = old[i++];
- output[i] = data[d];
+ for( var i = 0; i < old#; i++ )
+ output[i] = old[i];
+ output[old#] = data[d];
} else if (command == 44) {
data[d] = 42;
} else if (command == 91) {
if (data[d] == 0) {
- var d = 1;
- while (d > 0) {
+ for( var d = 1; d > 0; ) {
pc++;
command = program[pc];
if (command == 91) d++;
@@ -58,8 +56,7 @@ while (pc < program#) {
}
} else if (command == 93) {
if (data[d]) {
- var d = 1;
- while (d > 0) {
+ for( var d = 1; d > 0; ) {
pc--;
command = program[pc];
if (command == 93) d++;
@@ -67,7 +64,6 @@ while (pc < program#) {
}
}
}
- pc++;
}
return output;
""");
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
index fe275ee9..d39cee2d 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
@@ -2,7 +2,6 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
-import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -162,6 +161,72 @@ return b ? b.next ? b.next.x : b.x; // parses "b ? (b.next ? b.next.x : b.x) : 0
}
+ @Test
+ public void testFor0() {
+ Parser parser = new Parser("""
+int sum=0;
+for( int i=0; i<arg; i++ )
+ sum = sum + i;
+return sum;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
+ assertEquals(3L, Evaluator.evaluate(stop, 3));
+ assertEquals(45L, Evaluator.evaluate(stop, 10));
+ }
+ @Test
+ public void testFor1() {
+ Parser parser = new Parser("""
+int sum=0, i=0;
+for( ; i<arg; i++ )
+ sum = sum + i;
+return sum;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
+ assertEquals(3L, Evaluator.evaluate(stop, 3));
+ assertEquals(45L, Evaluator.evaluate(stop, 10));
+ }
+ @Test
+ public void testFor2() {
+ Parser parser = new Parser("""
+int sum=0;
+for( int i=0; ; i++ ) {
+ if( i>=arg ) break;
+ sum = sum + i;
+}
+return sum;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
+ assertEquals(3L, Evaluator.evaluate(stop, 3));
+ assertEquals(45L, Evaluator.evaluate(stop, 10));
+ }
+ @Test
+ public void testFor3() {
+ Parser parser = new Parser("""
+int sum=0;
+for( int i=0; i<arg; )
+ sum = sum + i++;
+return sum;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
+ assertEquals(3L, Evaluator.evaluate(stop, 3));
+ assertEquals(45L, Evaluator.evaluate(stop, 10));
+ }
+ @Test
+ public void testFor4() {
+ Parser parser = new Parser("""
+int sum=0;
+for( int i=0; i<arg; i++ )
+ sum = sum + i;
+return i;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Undefined name 'i'",e.getMessage()); }
+ }
+
@Test
public void testLinkedList2() {
@@ -171,10 +236,8 @@ LLI? head = null;
while( arg-- )
head = new LLI { next=head; i=arg; };
int sum=0;
-while( head ) {
+for( ; head; head = head.next )
sum = sum + head.i;
- head = head.next;
-}
return sum;
""");
StopNode stop = parser.parse().iterate();
@@ -187,29 +250,25 @@ return sum;
Parser parser = new Parser(
"""
val ary = new bool[arg], primes = new int[arg];
-var nprimes=0, p=2;
+var nprimes=0, p=0;
// Find primes while p^2 < arg
-while( p*p < arg ) {
+for( p=2; p*p < arg; p++ ) {
// skip marked non-primes
while( ary[p] ) p++;
// p is now a prime
primes[nprimes++] = p;
// Mark out the rest non-primes
- int i = p + p;
- while( i < ary# ) {
+ for( int i = p + p; i < ary#; i = i + p )
ary[i] = true;
- i = i + p;
- }
- p++;
}
// Now just collect the remaining primes, no more marking
-while( p++ < arg )
- if( !ary[p-1] )
- primes[nprimes++] = p-1;
+for( ; p < arg; p++ )
+ if( !ary[p] )
+ primes[nprimes++] = p;
// Copy/shrink the result array
-var rez = new int[nprimes], j = 0;
-while( j < nprimes )
- rez[j] = primes[j++];
+var rez = new int[nprimes];
+for( int j=0; j<nprimes; j++ )
+ rez[j] = primes[j];
return rez;
""");
StopNode stop = parser.parse().iterate();