From 736c658c274404084b15b450b2ab72d0e7569586 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Fri, 3 Nov 2023 21:21:54 +0000 Subject: Backport changes in Chapter 5 to previous chapters and update Chapter5 documentation (#34) * Backport changes from chapter 5 to older chapters (#30) * Fix wrong indent * Backport to chapter 4 * Backport to chapter 3 * Backport to chapter 2 * Backport to chapter 1 * Add if statement to comment (#32) * Update visuals for chapters 1,2 & 3. Reenable drawing of edges from constants to Start for these chapters. * Update visuals for chapters 4. Revise text to explain the labeling of edges in graphs and that we omit edge from Constant to Start nodes from chapter 4. * Add code snippets for if statement parsing and the duplication and merging of ScopeNodes. Explain how scopeNodes are duplicated and merged. * Add constant check to minus node (#31) * Fix closing root block with } (#33) * Backport fix closing root block with } to chapter 4 and 3 (#35) * Show use-def edge from Phi to Region as dotted edge. Add example of Phi peephole. Update visuals. Add more examples. * Fix visuals in chapter 1 and README in chapter 4. * Remove subheading * Adjust spaces --------- Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> --- .../java/com/seaofnodes/simple/Chapter04Test.java | 113 ++++++++++++++------- 1 file changed, 74 insertions(+), 39 deletions(-) (limited to 'chapter04/src/test/java') diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java index ed999039..ebfe1399 100644 --- a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java +++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java @@ -15,44 +15,44 @@ public class Chapter04Test { @Test public void testChapter4Peephole() { - Parser parser = new Parser("return 1+arg+2; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 1+arg+2; #showGraph;"); ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3+3);", ret.print()); + assertEquals("return (arg+3);", ret.print()); } @Test public void testChapter4Peephole2() { - Parser parser = new Parser("return (1+arg)+2;", TypeInteger.BOT); + Parser parser = new Parser("return (1+arg)+2;"); ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3+3);", ret.print()); + assertEquals("return (arg+3);", ret.print()); } @Test public void testChapter4Add0() { - Parser parser = new Parser("return 0+arg;", TypeInteger.BOT); + Parser parser = new Parser("return 0+arg;"); ReturnNode ret = parser.parse(); - assertEquals("return Proj_3;", ret.print()); + assertEquals("return arg;", ret.print()); } @Test public void testChapter4AddAddMul() { - Parser parser = new Parser("return arg+0+arg;", TypeInteger.BOT); + Parser parser = new Parser("return arg+0+arg;"); ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3*2);", ret.print()); + assertEquals("return (arg*2);", ret.print()); } @Test public void testChapter4Peephole3() { - Parser parser = new Parser("return 1+arg+2+arg+3; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 1+arg+2+arg+3; #showGraph;"); ReturnNode ret = parser.parse(); - assertEquals("return ((Proj_3*2)+6);", ret.print()); + assertEquals("return ((arg*2)+6);", ret.print()); } @Test public void testChapter4Mul1() { - Parser parser = new Parser("return 1*arg;", TypeInteger.BOT); + Parser parser = new Parser("return 1*arg;"); ReturnNode ret = parser.parse(); - assertEquals("return Proj_3;", ret.print()); + assertEquals("return arg;", ret.print()); } @Test @@ -72,53 +72,63 @@ public class Chapter04Test { @Test public void testChapter4CompEq() { - Parser parser = new Parser("return 3==3; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 3==3; #showGraph;"); ReturnNode ret = parser.parse(); assertEquals("return 1;", ret.print()); } @Test public void testChapter4CompEq2() { - Parser parser = new Parser("return 3==4; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 3==4; #showGraph;"); ReturnNode ret = parser.parse(); assertEquals("return 0;", ret.print()); } @Test public void testChapter4CompNEq() { - Parser parser = new Parser("return 3!=3; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 3!=3; #showGraph;"); ReturnNode ret = parser.parse(); assertEquals("return 0;", ret.print()); } @Test public void testChapter4CompNEq2() { - Parser parser = new Parser("return 3!=4; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("return 3!=4; #showGraph;"); ReturnNode ret = parser.parse(); 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); + Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; #showGraph;"); ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3+3);", ret.print()); + assertEquals("return (arg+3);", ret.print()); } @Test public void testChapter4Bug2() { - Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;", TypeInteger.BOT); + Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;"); ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3+1);", ret.print()); + assertEquals("return (arg+1);", ret.print()); } @Test public void testChapter4Bug3() { - Parser parser = new Parser("int a=arg*2+1; a=a+-1; return a; #showGraph;", TypeInteger.BOT); - ReturnNode ret = parser.parse(); - assertEquals("return (Proj_3*2);", ret.print()); + try { + new Parser("inta=1; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'inta'",e.getMessage()); + } } + @Test + public void testChapter4Bug4() { + Parser parser = new Parser("return -arg;"); + ReturnNode ret = parser.parse(); + assertEquals("return (-arg);", ret.print()); + } + @Test public void testVarDecl() { Parser parser = new Parser("int a=1; return a;"); @@ -158,12 +168,14 @@ public class Chapter04Test { @Test public void testSelfAssign() { - exceptionRule.expect(RuntimeException.class); - exceptionRule.expectMessage("Undefined name 'a'"); - new Parser("int a=a; return a;").parse(); + try { + new Parser("int a=a; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'a'",e.getMessage()); + } } - @Test public void testChapter2ParseGrammar() { Node._disablePeephole = true; // disable peephole so we can observe full graph @@ -247,16 +259,22 @@ public class Chapter04Test { @Test public void testBad1() { - exceptionRule.expect(RuntimeException.class); - exceptionRule.expectMessage("Syntax error, expected =: "); - new Parser("ret").parse(); + try { + new Parser("ret").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected =: ",e.getMessage()); + } } @Test public void testBad2() { - exceptionRule.expect(RuntimeException.class); - exceptionRule.expectMessage("Syntax error: integer values cannot start with '0'"); - new Parser("return 0123;").parse(); + try { + new Parser("return 0123;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage()); + } } @Test @@ -267,9 +285,12 @@ public class Chapter04Test { @Test public void testBad4() { - exceptionRule.expect(RuntimeException.class); - exceptionRule.expectMessage("Syntax error, expected ;:"); - new Parser("return 100").parse(); + try { + new Parser("return 100").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected ;: ",e.getMessage()); + } } @Test @@ -280,8 +301,22 @@ public class Chapter04Test { @Test public void testBad6() { - exceptionRule.expect(RuntimeException.class); - exceptionRule.expectMessage("Syntax error, expected }:"); - new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse(); + try { + new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected }: ",e.getMessage()); + } } + + @Test + public void testBad7() { + try { + new Parser("return 1;}").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, unexpected }",e.getMessage()); + } + } + } -- cgit v1.2.3