From 0049146baf2502809ed3f29b07cc5f90513cb747 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 26 Oct 2023 14:28:36 -0700 Subject: Replace Parser scopes with ScopeNode (#14) Fixes #12 * Replace Parser scopes with ScopeNode Basically move the Stack of HashMaps to a ScopeNode, with wrappers. The HashMap maps String names to Integer indices; inputs to the ScopeNode. This way, the ScopeNode keeps alive Nodes that are only alive because they are alive in the HashMap. Fix bug isDead -> isUnused in deadCodeElim. Support routines for adding/removing edges in ScopeNode. Set $ctrl to null after a Return; there is no control. Regression tests. * One more test case --- .../java/com/seaofnodes/simple/Chapter04Test.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'chapter04/src/test/java/com') diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java index 988e3608..8cc78af0 100644 --- a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java +++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java @@ -98,6 +98,20 @@ public class Chapter04Test { assertEquals("return 1;", ret.print()); } + @Test + public void testChapter4Bug1() { + Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; #showGraph;", TypeInteger.BOT); + ReturnNode ret = parser.parse(); + assertEquals("return (Proj_3+3);", ret.print()); + } + + @Test + public void testChapter4Bug2() { + Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;", TypeInteger.BOT); + ReturnNode ret = parser.parse(); + assertEquals("return (Proj_3+1);", ret.print()); + } + @Test public void testVarDecl() { Parser parser = new Parser("int a=1; return a;"); @@ -135,6 +149,13 @@ public class Chapter04Test { assertEquals("return 8;", ret.print()); } + @Test + public void testSelfAssign() { + exceptionRule.expect(RuntimeException.class); + exceptionRule.expectMessage("Undefined name 'a'"); + new Parser("int a=a; return a;").parse(); + } + @Test public void testChapter2ParseGrammar() { -- cgit v1.2.3