diff options
| author | Thalia Archibald <thalia@archibald.dev> | 2024-06-21 12:58:51 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-21 20:58:51 +0100 |
| commit | fd8442075faa603b3956d51d81486424624c5355 (patch) | |
| tree | 029c1afb736ff0e350c2b032318e5fc376c7e1e4 /chapter03 | |
| parent | e5f4f7b85fbb91a293a7d89d7b85adfbaeeb51f4 (diff) | |
| download | Simple-fd8442075faa603b3956d51d81486424624c5355.tar.gz Simple-fd8442075faa603b3956d51d81486424624c5355.zip | |
Backport tests (#87)
* Fix unexpected number parse errors
* Add chapter titles
* Split test files by chapter
* Backport rename Fuzzer08Test to FuzzerWrap
* Replace graph build scripts with Makefile
* Backport Chapter05Test.testRegress1
* Backport require space after 'return'
* Backport check EOF after return statement
Diffstat (limited to 'chapter03')
| -rw-r--r-- | chapter03/README.md | 2 | ||||
| -rw-r--r-- | chapter03/docs/Makefile | 12 | ||||
| -rw-r--r-- | chapter03/src/main/java/com/seaofnodes/simple/Parser.java | 10 | ||||
| -rw-r--r-- | chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java | 99 | ||||
| -rw-r--r-- | chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java | 66 | ||||
| -rw-r--r-- | chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java | 138 |
6 files changed, 185 insertions, 142 deletions
diff --git a/chapter03/README.md b/chapter03/README.md index 7b0d22cb..0fb08e9a 100644 --- a/chapter03/README.md +++ b/chapter03/README.md @@ -1,4 +1,4 @@ -# Chapter 3 +# Chapter 3: Variable Declarations In this chapter we extend the language grammar to allow variable declarations. This allows us to write: diff --git a/chapter03/docs/Makefile b/chapter03/docs/Makefile new file mode 100644 index 00000000..12659b8f --- /dev/null +++ b/chapter03/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/chapter03/src/main/java/com/seaofnodes/simple/Parser.java b/chapter03/src/main/java/com/seaofnodes/simple/Parser.java index 9c7ebaca..4fb5b6ed 100644 --- a/chapter03/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter03/src/main/java/com/seaofnodes/simple/Parser.java @@ -363,6 +363,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()); } @@ -371,13 +372,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/chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java new file mode 100644 index 00000000..311ca23b --- /dev/null +++ b/chapter03/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; + + assertEquals(start, ret.ctrl()); + 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/chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java new file mode 100644 index 00000000..60ee8874 --- /dev/null +++ b/chapter03/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/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java index 965a100a..950d87fe 100644 --- a/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java +++ b/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java @@ -2,17 +2,12 @@ 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 Chapter03Test { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - @Test public void testVarDecl() { Parser parser = new Parser("int a=1; return a;"); @@ -61,131 +56,8 @@ public class Chapter03Test { } @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; - - assertEquals(start, ret.ctrl()); - 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 ) { @@ -193,14 +65,4 @@ public class Chapter03Test { } } - @Test - public void testBad7() { - try { - new Parser("return 1;}").parse(); - fail(); - } catch( RuntimeException e ) { - assertEquals("Syntax error, unexpected }",e.getMessage()); - } - } - } |
