From 31ba4ecbc5214b3c25917fd97d030bfd39a784a3 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 30 Sep 2024 08:56:50 -0700 Subject: Fix 2 test cases --- .../main/java/com/seaofnodes/simple/Parser.java | 8 ++++++++ .../java/com/seaofnodes/simple/Chapter15Test.java | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/chapter15/src/main/java/com/seaofnodes/simple/Parser.java b/chapter15/src/main/java/com/seaofnodes/simple/Parser.java index 2b09e943..d9726d28 100644 --- a/chapter15/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter15/src/main/java/com/seaofnodes/simple/Parser.java @@ -489,13 +489,17 @@ public class Parser { // Convert the type name to a type. Type t0 = TYPES.get(tname); Type t1 = t0==null ? TypeStruct.BOT : t0; // Null: assume a forward ref type + // Nest arrays as needed while( match("[]") ) { Type t2 = TYPES.get("[]"+tname); if( t2==null ) { TYPES.put("[]"+tname,t1 = TypeStruct.makeAry(TypeInteger.BOT,ALIAS++,t1,ALIAS++)); START.addMemProj((TypeStruct)t1, _scope); // Insert memory edges + } else { + t1 = t2; } } + // Handle trailing '?' for nullable Type t2 = t1 instanceof TypeStruct obj ? TypeMemPtr.make(obj,match("?")) : t1; // Check no forward ref @@ -708,6 +712,10 @@ public class Parser { private Node newArray(Type t) { Node len = parseExpression().keep(); TypeStruct ary = (TypeStruct)TYPES.get("[]"+t.str()); + if( ary == null ) { + TYPES.put("[]"+t.str(), ary = TypeStruct.makeAry(TypeInteger.BOT,ALIAS++,t,ALIAS++)); + START.addMemProj(ary, _scope); // Insert memory edges + } int base = ary.aryBase (); int scale= ary.aryScale(); Node size = new AddNode(con(base),new ShlNode(len,con(scale)).peephole()).peephole(); diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 15465677..2955361a 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -33,6 +33,19 @@ return is[1]; assertEquals(0L, Evaluator.evaluate(stop, 0)); } + @Test + public void testBasic2() { + Parser parser = new Parser( +""" +int[] is = new int[2]; +int[] is2 = new int[2]; +return is[1]; +"""); + StopNode stop = parser.parse(false).iterate(true); + assertEquals("return 0;", stop.toString()); + assertEquals(0L, Evaluator.evaluate(stop, 0)); + } + @Test public void testRollingSum() { Parser parser = new Parser( @@ -67,4 +80,13 @@ return new flt; catch( Exception e ) { assertEquals("Cannot allocate a FltBot",e.getMessage()); } } + @Test + public void testBad1() { + Parser parser = new Parser( +""" +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()); } + } } -- cgit v1.2.3