summaryrefslogtreecommitdiff
path: root/chapter01/src/test
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2023-09-19 09:19:37 -0700
committerCliff Click <cliffc@acm.org>2023-09-19 09:19:37 -0700
commit02f839e3eaab921aecd9c13b59047dd0551e132a (patch)
treebff164e39dfa9a9d42c89b041e5117b1d5c60fed /chapter01/src/test
parentcd4ac0ecebec7ac6eda30bdf1989318c14c64806 (diff)
downloadSimple-02f839e3eaab921aecd9c13b59047dd0551e132a.tar.gz
Simple-02f839e3eaab921aecd9c13b59047dd0551e132a.zip
Overhaul Lexer
Switched to bytes from chars, since we'll be parsing files eventually and typically compilers parse bytes from files (and not Java chars). Added a 'peek' to handle bad leading `--123` numbers. Used simpler number parsing; doesn't do doubles (yet). Added a filter for punctuation. Prior setup didn't handle multi-char punctuation like relation tests "<=". Switched the "EOF" marker char from 0 to Char.MAX_VALUE; harder to make mistakes, and simplifies the WS test. Switch javadoc comments to plain comments on private functions. Added tests for broken keyword, broken numbers, negative numbers.
Diffstat (limited to 'chapter01/src/test')
-rw-r--r--chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java45
1 files changed, 36 insertions, 9 deletions
diff --git a/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
index 9bc2fc1c..586c53de 100644
--- a/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
+++ b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
@@ -1,8 +1,8 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
-import org.junit.Assert;
import org.junit.Test;
+import static org.junit.Assert.*;
public class ParserTest {
@@ -10,23 +10,50 @@ public class ParserTest {
public void testSimpleProgram() {
Parser parser = new Parser();
StartNode startNode = parser.parse("return 1;");
- Assert.assertNotNull(startNode);
- Assert.assertEquals(2, startNode.nOuts());
+ assertNotNull(startNode);
+ assertEquals(2, startNode.nOuts());
for (int i = 0; i < startNode.nOuts(); i++) {
switch( startNode.out(i) ) {
case ReturnNode ret:
- Assert.assertEquals(2, ret.nIns());
- Assert.assertEquals(startNode, ret.in(0));
- Assert.assertTrue(ret.in(1) instanceof ConstantNode);
+ assertEquals(2, ret.nIns());
+ assertEquals(startNode, ret.in(0));
+ assertTrue(ret.in(1) instanceof ConstantNode);
break;
case ConstantNode con:
- Assert.assertEquals(1, con._value);
- Assert.assertEquals(1, con.nIns());
- Assert.assertEquals(startNode, con.in(0));
+ assertEquals(1, con._value);
+ assertEquals(1, con.nIns());
+ assertEquals(startNode, con.in(0));
break;
default:
throw new IllegalStateException();
}
}
}
+
+ @Test
+ public void testNegative() {
+ Parser parser = new Parser();
+ StartNode start = parser.parse("return -123;");
+ for( Node use : start._outputs )
+ if( use instanceof ConstantNode con )
+ assertEquals(-123,con._value);
+ }
+
+ @Test
+ public void testBad1() {
+ try { new Parser().parse("ret"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected return: ret",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad2() {
+ try { new Parser().parse("return 0123;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected nested expr or integer literal: 0123",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad3() {
+ try { new Parser().parse("return --12;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected ( got --: --",e.getMessage()); }
+ }
}