diff options
| author | Cliff Click <cliffc@acm.org> | 2024-11-07 16:30:43 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-11-23 09:03:20 -0800 |
| commit | efc81d5afc9983258db267894ce3f141962793a5 (patch) | |
| tree | e748baca01e0ed86c73fc195fbc66e630205807e /chapter17/src/test | |
| parent | 6d96985f9d297f6dee9ee6f877b9986dec797d4c (diff) | |
| download | Simple-efc81d5afc9983258db267894ce3f141962793a5.tar.gz Simple-efc81d5afc9983258db267894ce3f141962793a5.zip | |
var/val
Diffstat (limited to 'chapter17/src/test')
3 files changed, 76 insertions, 27 deletions
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java index 88dccc49..abb8b834 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java @@ -23,11 +23,12 @@ public class BrainFuckTest { Parser parser = new Parser( encoded + """ -int pc = 0, d = 0; -u8[] data = new u8[100], output = new u8[0]; +var pc = 0, d = 0; +u8[] output = new u8[0]; +val data = new u8[100]; while (pc < program#) { - int command = program[pc]; + var command = program[pc]; if (command == 62) { d++; } else if (command == 60) { @@ -37,9 +38,9 @@ while (pc < program#) { } else if (command == 45) { data[d]--; } else if (command == 46) { - u8[] old = output; + var old = output; output = new u8[output# + 1]; - int i = 0; + var i = 0; while (i < old#) output[i] = old[i++]; output[i] = data[d]; @@ -47,7 +48,7 @@ while (pc < program#) { data[d] = 42; } else if (command == 91) { if (data[d] == 0) { - int d = 1; + var d = 1; while (d > 0) { pc++; command = program[pc]; @@ -57,7 +58,7 @@ while (pc < program#) { } } else if (command == 93) { if (data[d]) { - int d = 1; + var d = 1; while (d > 0) { pc--; command = program[pc]; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 8161862f..4226c201 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -216,38 +216,36 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6 public void sieveOEratosthenes() { Parser parser = new Parser( """ -int[] ary = new int[arg]; -int[] primes = new int[arg]; -int nprimes = 0; -// Find primes -int j=2; -while( j*j < arg ) { - while( ary[j]==1 ) j = j + 1; - // j is now a prime - primes[nprimes] = j; nprimes = nprimes + 1; +int[] ary = new int[arg], primes = new int[arg]; +int nprimes = 0, p=2; +// Find primes while p^2 < arg +while( p*p < arg ) { + // skip marked non-primes + while( ary[p]==1 ) p = p + 1; + // p is now a prime + primes[nprimes] = p; nprimes = nprimes + 1; // Mark out the rest non-primes - int i = j + j; + int i = p + p; while( i < ary# ) { ary[i] = 1; - i = i + j; + i = i + p; } - j = j + 1; + p = p + 1; } -// Now just collect the remaining primes -while( j < arg ) { - if( ary[j] == 0 ) { - primes[nprimes] = j; nprimes = nprimes + 1; +// Now just collect the remaining primes, no more marking +while( p < arg ) { + if( ary[p] == 0 ) { + primes[nprimes] = p; nprimes = nprimes + 1; } - j = j + 1; + p = p + 1; } -// Shrink the result array to size +// Copy/shrink the result array int[] rez = new int[nprimes]; -j = 0; +int j = 0; while( j < nprimes ) { rez[j] = primes[j]; j = j + 1; } - return rez; """); StopNode stop = parser.parse().iterate(); diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java index 45e3ef0f..ffb9bfc2 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java @@ -88,6 +88,16 @@ return s.x; } @Test + public void testVar0() { + Parser parser = new Parser(""" +var d; +return d; +"""); + try { parser.parse().iterate(); fail(); } + catch( Exception e ) { assertEquals("Syntax error, expected expression: ;",e.getMessage()); } + } + + @Test public void testLinkedList2() { Parser parser = new Parser(""" struct LLI { LLI? next; int i; }; @@ -106,4 +116,44 @@ return sum; assertEquals(45L, Evaluator.evaluate(stop, 10)); } + @Test + public void sieveOEratosthenes() { + Parser parser = new Parser( +""" +val ary = new bool[arg], primes = new int[arg]; +var nprimes=0, p=2; +// Find primes while p^2 < arg +while( p*p < arg ) { + // 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# ) { + 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; +// Copy/shrink the result array +var rez = new int[nprimes], j = 0; +while( j < nprimes ) + rez[j] = primes[j++]; +return rez; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return [int];", stop.toString()); + Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20); + assertEquals("[int] { # :int; [] :int; }",obj.struct().toString()); + long nprimes = (Long)obj.fields()[0]; + long[] primes = new long[]{2,3,5,7,11,13,17,19}; + for( int i=0; i<nprimes; i++ ) + assertEquals(primes[i],(long)(Long)obj.fields()[i+1]); + } + } |
