summaryrefslogtreecommitdiff
path: root/chapter04
diff options
context:
space:
mode:
Diffstat (limited to 'chapter04')
-rw-r--r--chapter04/README.md2
-rw-r--r--chapter04/docs/Makefile12
-rw-r--r--chapter04/docs/build-graphs.sh1
-rw-r--r--chapter04/src/main/java/com/seaofnodes/simple/Parser.java10
-rw-r--r--chapter04/src/test/java/com/seaofnodes/simple/Chapter01Test.java99
-rw-r--r--chapter04/src/test/java/com/seaofnodes/simple/Chapter02Test.java66
-rw-r--r--chapter04/src/test/java/com/seaofnodes/simple/Chapter03Test.java68
-rw-r--r--chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java227
8 files changed, 269 insertions, 216 deletions
diff --git a/chapter04/README.md b/chapter04/README.md
index 03c6c53f..1f300937 100644
--- a/chapter04/README.md
+++ b/chapter04/README.md
@@ -1,4 +1,4 @@
-# Chapter 4
+# Chapter 4: External Argument and Comparisons
In this chapter we extend the language grammar with following features:
diff --git a/chapter04/docs/Makefile b/chapter04/docs/Makefile
new file mode 100644
index 00000000..12659b8f
--- /dev/null
+++ b/chapter04/docs/Makefile
@@ -0,0 +1,12 @@
+GV := $(wildcard *.gv)
+SVG := $(patsubst %.gv,%.svg,$(GV))
+
+.PHONY: all
+all: $(SVG)
+
+%.svg: %.gv
+ dot -Tsvg $< > $@
+
+.PHONY: clean
+clean:
+ @rm -f $(SVG)
diff --git a/chapter04/docs/build-graphs.sh b/chapter04/docs/build-graphs.sh
deleted file mode 100644
index e736a386..00000000
--- a/chapter04/docs/build-graphs.sh
+++ /dev/null
@@ -1 +0,0 @@
-dot -Tsvg lattice.gv > lattice.svg
diff --git a/chapter04/src/main/java/com/seaofnodes/simple/Parser.java b/chapter04/src/main/java/com/seaofnodes/simple/Parser.java
index e42613e2..3359f14e 100644
--- a/chapter04/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter04/src/main/java/com/seaofnodes/simple/Parser.java
@@ -406,6 +406,7 @@ public class Parser {
String getAnyNextToken() {
if (isEOF()) return "";
if (isIdStart(peek())) return parseId();
+ if (isNumber(peek())) return parseNumberString();
if (isPunctuation(peek())) return parsePunctuation();
return String.valueOf(peek());
}
@@ -414,13 +415,16 @@ public class Parser {
boolean isNumber(char ch) {return Character.isDigit(ch);}
private Type parseNumber() {
- int start = _position;
- while (isNumber(nextChar())) ;
- String snum = new String(_input, start, --_position - start);
+ String snum = parseNumberString();
if (snum.length() > 1 && snum.charAt(0) == '0')
throw error("Syntax error: integer values cannot start with '0'");
return TypeInteger.constant(Long.parseLong(snum));
}
+ private String parseNumberString() {
+ int start = _position;
+ while (isNumber(nextChar())) ;
+ return new String(_input, start, --_position - start);
+ }
// First letter of an identifier
private boolean isIdStart(char ch) {
diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter01Test.java
new file mode 100644
index 00000000..3d568337
--- /dev/null
+++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -0,0 +1,99 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter01Test {
+
+ @Test
+ public void testSimpleProgram() {
+ Parser parser = new Parser("return 1;");
+ ReturnNode ret = parser.parse();
+ StartNode start = Parser.START;
+
+ assertTrue(ret.ctrl() instanceof ProjNode);
+ Node expr = ret.expr();
+ if( expr instanceof ConstantNode con ) {
+ assertEquals(start,con.in(0));
+ assertEquals(TypeInteger.constant(1), con._type);
+ } else {
+ fail();
+ }
+ }
+
+ @Test
+ public void testZero() {
+ Parser parser = new Parser("return 0;");
+ parser.parse();
+ StartNode start = Parser.START;
+ for( Node use : start._outputs )
+ if( use instanceof ConstantNode con )
+ assertEquals(TypeInteger.constant(0),con._type);
+ }
+
+ @Test
+ public void testBad1() {
+ try {
+ new Parser("ret").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected =: ",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testBad2() {
+ try {
+ new Parser("return 0123;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testNotBad3() {
+ // this test used to fail in chapter 1
+ assertEquals("return 12;", new Parser("return --12;").parse().print());
+ }
+
+ @Test
+ public void testBad4() {
+ try {
+ new Parser("return 100").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected ;: ",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testNotBad5() {
+ // this test used to fail in chapter 1
+ assertEquals("return -100;", new Parser("return -100;").parse().print());
+ }
+
+ @Test
+ public void testBad6() {
+ try {
+ new Parser("return100").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());
+ }
+ }
+
+}
diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter02Test.java
new file mode 100644
index 00000000..60ee8874
--- /dev/null
+++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -0,0 +1,66 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter02Test {
+
+ @Test
+ public void testParseGrammar() {
+ Parser parser = new Parser("return 1+2*3+-5;");
+ Node._disablePeephole = true; // disable peephole so we can observe full graph
+ ReturnNode ret = parser.parse();
+ assertEquals("return (1+((2*3)+(-5)));", ret.print());
+ GraphVisualizer gv = new GraphVisualizer();
+ System.out.println(gv.generateDotOutput(parser));
+ Node._disablePeephole = false;
+ }
+
+ @Test
+ public void testAddPeephole() {
+ Parser parser = new Parser("return 1+2;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testSubPeephole() {
+ Parser parser = new Parser("return 1-2;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return -1;", ret.print());
+ }
+
+ @Test
+ public void testMulPeephole() {
+ Parser parser = new Parser("return 2*3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 6;", ret.print());
+ }
+
+ @Test
+ public void testDivPeephole() {
+ Parser parser = new Parser("return 6/3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ }
+
+ @Test
+ public void testMinusPeephole() {
+ Parser parser = new Parser("return 6/-3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return -2;", ret.print());
+ }
+
+ @Test
+ public void testExample() {
+ Parser parser = new Parser("return 1+2*3+-5;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ GraphVisualizer gv = new GraphVisualizer();
+ System.out.println(gv.generateDotOutput(parser));
+ }
+
+}
diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter03Test.java
new file mode 100644
index 00000000..950d87fe
--- /dev/null
+++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -0,0 +1,68 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter03Test {
+
+ @Test
+ public void testVarDecl() {
+ Parser parser = new Parser("int a=1; return a;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 1;", ret.print());
+ }
+
+ @Test
+ public void testVarAdd() {
+ Parser parser = new Parser("int a=1; int b=2; return a+b;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testVarScope() {
+ Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 4;", ret.print());
+ }
+
+ @Test
+ public void testVarScopeNoPeephole() {
+ Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; #showGraph; } return c; #showGraph;");
+ Node._disablePeephole = true;
+ ReturnNode ret = parser.parse();
+ Node._disablePeephole = false;
+ assertEquals("return (1+3);", ret.print());
+ }
+
+ @Test
+ public void testVarDist() {
+ Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); #showGraph;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 8;", ret.print());
+ }
+
+ @Test
+ public void testSelfAssign() {
+ try {
+ new Parser("int a=a; return a;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'a'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testBad1() {
+ 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());
+ }
+ }
+
+}
diff --git a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 5757fc59..9f1535b3 100644
--- a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -2,61 +2,56 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
public class Chapter04Test {
- @Rule
- public ExpectedException exceptionRule = ExpectedException.none();
-
@Test
- public void testChapter4Peephole() {
+ public void testPeephole() {
Parser parser = new Parser("return 1+arg+2; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return (arg+3);", ret.print());
}
@Test
- public void testChapter4Peephole2() {
+ public void testPeephole2() {
Parser parser = new Parser("return (1+arg)+2;");
ReturnNode ret = parser.parse();
assertEquals("return (arg+3);", ret.print());
}
@Test
- public void testChapter4Add0() {
+ public void testAdd0() {
Parser parser = new Parser("return 0+arg;");
ReturnNode ret = parser.parse();
assertEquals("return arg;", ret.print());
}
@Test
- public void testChapter4AddAddMul() {
+ public void testAddAddMul() {
Parser parser = new Parser("return arg+0+arg;");
ReturnNode ret = parser.parse();
assertEquals("return (arg*2);", ret.print());
}
@Test
- public void testChapter4Peephole3() {
+ public void testPeephole3() {
Parser parser = new Parser("return 1+arg+2+arg+3; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return ((arg*2)+6);", ret.print());
}
@Test
- public void testChapter4Mul1() {
+ public void testMul1() {
Parser parser = new Parser("return 1*arg;");
ReturnNode ret = parser.parse();
assertEquals("return arg;", ret.print());
}
@Test
- public void testChapter4VarArg() {
+ public void testVarArg() {
Parser parser = new Parser("return arg; #showGraph;", TypeInteger.BOT);
ReturnNode ret = parser.parse();
assertTrue(ret.in(0) instanceof ProjNode);
@@ -64,56 +59,56 @@ public class Chapter04Test {
}
@Test
- public void testChapter4ConstantArg() {
+ public void testConstantArg() {
Parser parser = new Parser("return arg; #showGraph;", TypeInteger.constant(2));
ReturnNode ret = parser.parse();
assertEquals("return 2;", ret.print());
}
@Test
- public void testChapter4CompEq() {
+ public void testCompEq() {
Parser parser = new Parser("return 3==3; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return 1;", ret.print());
}
@Test
- public void testChapter4CompEq2() {
+ public void testCompEq2() {
Parser parser = new Parser("return 3==4; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return 0;", ret.print());
}
@Test
- public void testChapter4CompNEq() {
+ public void testCompNEq() {
Parser parser = new Parser("return 3!=3; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return 0;", ret.print());
}
@Test
- public void testChapter4CompNEq2() {
+ public void testCompNEq2() {
Parser parser = new Parser("return 3!=4; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return 1;", ret.print());
}
@Test
- public void testChapter4Bug1() {
+ public void testBug1() {
Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return (arg+3);", ret.print());
}
@Test
- public void testChapter4Bug2() {
+ public void testBug2() {
Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;");
ReturnNode ret = parser.parse();
assertEquals("return (arg+1);", ret.print());
}
@Test
- public void testChapter4Bug3() {
+ public void testBug3() {
try {
new Parser("inta=1; return a;").parse();
fail();
@@ -123,200 +118,10 @@ public class Chapter04Test {
}
@Test
- public void testChapter4Bug4() {
+ public void testBug4() {
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;");
- ReturnNode ret = parser.parse();
- assertEquals("return 1;", ret.print());
- }
-
- @Test
- public void testVarAdd() {
- Parser parser = new Parser("int a=1; int b=2; return a+b;");
- ReturnNode ret = parser.parse();
- assertEquals("return 3;", ret.print());
- }
-
- @Test
- public void testVarScope() {
- Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
- ReturnNode ret = parser.parse();
- assertEquals("return 4;", ret.print());
- }
-
- @Test
- public void testVarScopeNoPeephole() {
- Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; #showGraph; } return c; #showGraph;");
- Node._disablePeephole = true;
- ReturnNode ret = parser.parse();
- Node._disablePeephole = false;
- assertEquals("return (1+3);", ret.print());
- }
-
- @Test
- public void testVarDist() {
- Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); #showGraph;");
- ReturnNode ret = parser.parse();
- assertEquals("return 8;", ret.print());
- }
-
- @Test
- public void testSelfAssign() {
- try {
- new Parser("int a=a; return a;").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Undefined name 'a'",e.getMessage());
- }
- }
-
- @Test
- public void testChapter2ParseGrammar() {
- Parser parser = new Parser("return 1+2*3+-5;");
- Node._disablePeephole = true; // disable peephole so we can observe full graph
- ReturnNode ret = parser.parse();
- assertEquals("return (1+((2*3)+(-5)));", ret.print());
- GraphVisualizer gv = new GraphVisualizer();
- System.out.println(gv.generateDotOutput(parser));
- Node._disablePeephole = false;
- }
-
- @Test
- public void testChapter2AddPeephole() {
- Parser parser = new Parser("return 1+2;");
- ReturnNode ret = parser.parse();
- assertEquals("return 3;", ret.print());
- }
-
- @Test
- public void testChapter2SubPeephole() {
- Parser parser = new Parser("return 1-2;");
- ReturnNode ret = parser.parse();
- assertEquals("return -1;", ret.print());
- }
-
- @Test
- public void testChapter2MulPeephole() {
- Parser parser = new Parser("return 2*3;");
- ReturnNode ret = parser.parse();
- assertEquals("return 6;", ret.print());
- }
-
- @Test
- public void testChapter2DivPeephole() {
- Parser parser = new Parser("return 6/3;");
- ReturnNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
- }
-
- @Test
- public void testChapter2MinusPeephole() {
- Parser parser = new Parser("return 6/-3;");
- ReturnNode ret = parser.parse();
- assertEquals("return -2;", ret.print());
- }
-
- @Test
- public void testChapter2Example() {
- Parser parser = new Parser("return 1+2*3+-5;");
- ReturnNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
- GraphVisualizer gv = new GraphVisualizer();
- System.out.println(gv.generateDotOutput(parser));
- }
-
- @Test
- public void testSimpleProgram() {
- Parser parser = new Parser("return 1;");
- ReturnNode ret = parser.parse();
- StartNode start = Parser.START;
-
- assertTrue(ret.ctrl() instanceof ProjNode);
- Node expr = ret.expr();
- if( expr instanceof ConstantNode con ) {
- assertEquals(start,con.in(0));
- assertEquals(TypeInteger.constant(1), con._type);
- } else {
- fail();
- }
- }
-
- @Test
- public void testZero() {
- Parser parser = new Parser("return 0;");
- parser.parse();
- StartNode start = Parser.START;
- for( Node use : start._outputs )
- if( use instanceof ConstantNode con )
- assertEquals(TypeInteger.constant(0),con._type);
- }
-
- @Test
- public void testBad1() {
- try {
- new Parser("ret").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, expected =: ",e.getMessage());
- }
- }
-
- @Test
- public void testBad2() {
- try {
- new Parser("return 0123;").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage());
- }
- }
-
- @Test
- public void testNotBad3() {
- // this test used to fail in chapter 1
- assertEquals("return 12;", new Parser("return --12;").parse().print());
- }
-
- @Test
- public void testBad4() {
- try {
- new Parser("return 100").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, expected ;: ",e.getMessage());
- }
- }
-
- @Test
- public void testNotBad5() {
- // this test used to fail in chapter 1
- assertEquals("return -100;", new Parser("return -100;").parse().print());
- }
-
- @Test
- public void testBad6() {
- 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());
- }
- }
-
}