summaryrefslogtreecommitdiff
path: root/chapter15/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-10-01 10:36:59 -0700
committerCliff Click <cliffc@acm.org>2024-10-06 08:56:22 -0700
commitbd4c0c8d9fd33b35a0c4d98ca2dd641e3fafbfd3 (patch)
tree9f3a191348482bc6942cb1f256d112059a122776 /chapter15/src/test/java/com/seaofnodes
parente315432bbd1ffa4e2dd562c4f4fc454ea1ba2464 (diff)
downloadSimple-bd4c0c8d9fd33b35a0c4d98ca2dd641e3fafbfd3.tar.gz
Simple-bd4c0c8d9fd33b35a0c4d98ca2dd641e3fafbfd3.zip
Fix parsing forward ref arrays
Diffstat (limited to 'chapter15/src/test/java/com/seaofnodes')
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java38
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java4
2 files changed, 36 insertions, 6 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index 6ba6eed4..62e865aa 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -61,6 +61,36 @@ return a[0];
}
@Test
+ public void testBasic4() {
+ Parser parser = new Parser(
+"""
+struct A { int i; }
+A[] a = new A[2];
+return a;
+""");
+ StopNode stop = parser.parse(false).iterate(false);
+ assertEquals("return A[];", stop.toString());
+ assertEquals("Obj<A[]> {\n #=2\n []=null\n}", Evaluator.evaluate(stop, 0).toString());
+ }
+
+ @Test
+ public void testTree() {
+ Parser parser = new Parser(
+"""
+// Can we define a forward-reference array?
+struct Tree { Tree[] _kids; }
+Tree root = new Tree;
+root._kids = new Tree[2];
+root._kids[0] = new Tree;
+return root;
+""");
+ StopNode stop = parser.parse(false).iterate(false);
+ assertEquals("return Tree;", stop.toString());
+ assertEquals("Obj<Tree> {\n _kids=Obj<Tree[]> {\n #=2\n []=Obj<Tree> {\n _kids=null\n}\n}\n}", Evaluator.evaluate(stop, 0).toString());
+ }
+
+
+ @Test
public void testRollingSum() {
Parser parser = new Parser(
"""
@@ -123,9 +153,9 @@ while( j < nprimes ) {
return rez;
""");
StopNode stop = parser.parse(false).iterate(true);
- assertEquals("return []int;", stop.toString());
+ assertEquals("return int[];", stop.toString());
Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20);
- assertEquals("[]int {\n # :int;\n [] :int;\n}",obj.struct().toString());
+ assertEquals("int[] {\n # :int;\n [] :int;\n}",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++ )
@@ -140,7 +170,7 @@ return rez;
return new flt;
""");
try { parser.parse(false).iterate(false); fail(); }
- catch( Exception e ) { assertEquals("Cannot allocate a FltBot",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Cannot allocate a flt",e.getMessage()); }
}
@Test
@@ -150,7 +180,7 @@ return new flt;
int is = new int[2];
""");
try { parser.parse(false).iterate(false); fail(); }
- catch( Exception e ) { assertEquals("Type *[]int is not of declared type int",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Type *int[] is not of declared type int",e.getMessage()); }
}
@Test
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java b/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
index 324dd42d..7cd854bf 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
+++ b/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
@@ -109,8 +109,8 @@ public class ScriptGenerator {
static {
new Parser("", TypeInteger.BOT);
- for(var e : Parser.TYPES.entrySet() ) {
- if (e.getValue() instanceof TypeInteger) {
+ for( var e : Parser.TYPES.entrySet() ) {
+ if( e.getValue() instanceof TypeInteger ) {
INTTYPES.add(new TypeInt(e.getKey()));
}
}