Skip to content

Commit

Permalink
Fix issue #161
Browse files Browse the repository at this point in the history
Error adding 'null' and '5'
  • Loading branch information
cliffclick committed Feb 16, 2025
1 parent 4a9d57d commit bb0b25e
Show file tree
Hide file tree
Showing 31 changed files with 210 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
13 changes: 13 additions & 0 deletions chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Cannot store 3.14 into field IntBot i",e.getMessage()); }
}

@Test
public void testNullRef4() {
Parser parser = new Parser("-null-5/null-5");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
}

@Test public void testNullRef5() {
Parser parser = new Parser("return null+42;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
Parser parser = new Parser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
13 changes: 13 additions & 0 deletions chapter14/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
}

@Test
public void testNullRef4() {
Parser parser = new Parser("-null-5/null-5");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
}

@Test public void testNullRef5() {
Parser parser = new Parser("return null+42;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
Parser parser = new Parser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,9 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
13 changes: 13 additions & 0 deletions chapter15/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
}

@Test
public void testNullRef4() {
Parser parser = new Parser("-null-5/null-5");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
}

@Test public void testNullRef5() {
Parser parser = new Parser("return null+42;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
Parser parser = new Parser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,9 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
13 changes: 13 additions & 0 deletions chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
}

@Test
public void testNullRef4() {
Parser parser = new Parser("-null-5/null-5");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
}

@Test public void testNullRef5() {
Parser parser = new Parser("return null+42;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
Parser parser = new Parser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static boolean overflow( long x, long y ) {
public Node idealize () {
Node lhs = in(1);
Node rhs = in(2);
if( rhs.err() != null ) return null;
Type t2 = rhs._type;

// Add of 0. We do not check for (0+x) because this will already
Expand Down Expand Up @@ -184,4 +185,9 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
return null;
}
}
13 changes: 13 additions & 0 deletions chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); }
}

@Test
public void testNullRef4() {
Parser parser = new Parser("-null-5/null-5;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test public void testNullRef5() {
Parser parser = new Parser("return null+42;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
Parser parser = new Parser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ public class Chapter17Test {

@Test
public void testJig() {
Parser parser = new Parser("""
int i = 0;
i=i=1;
return i;
//return 3.14;
""");
Parser parser = new Parser("return 1;");
StopNode stop = parser.parse().iterate();
assertEquals("return 1;", stop.toString());
assertEquals(1L, Evaluator.evaluate(stop, 0));
Expand Down
2 changes: 1 addition & 1 deletion chapter18/src/main/java/com/seaofnodes/simple/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,6 @@ public static class ParseException extends RuntimeException {
public final Lexer _loc;
// file:line:charoff err
//String msg = "src:"+_line_number+":"+(_position-_line_start)+" "+errorMessage;
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; assert loc!=null; }
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.seaofnodes.simple.node;

import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.*;

import java.util.BitSet;

public class AddNode extends Node {
Expand Down Expand Up @@ -44,6 +44,7 @@ static boolean overflow( long x, long y ) {
public Node idealize () {
Node lhs = in(1);
Node rhs = in(2);
if( rhs.err()!=null ) return null;
Type t2 = rhs._type;

// Add of 0. We do not check for (0+x) because this will already
Expand Down Expand Up @@ -184,4 +185,10 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public Parser.ParseException err() {
if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null;
if( !(in(1)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(1)._type,null);
if( !(in(2)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(2)._type,null);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.seaofnodes.simple.node;

import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;

import java.util.BitSet;

public class MulNode extends Node {
Expand Down Expand Up @@ -60,4 +60,10 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public Parser.ParseException err() {
if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null;
if( !(in(1)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(1)._type,null);
if( !(in(2)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(2)._type,null);
return null;
}
}
13 changes: 13 additions & 0 deletions chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); }
}

@Test
public void testNullRef4() {
CodeGen code = new CodeGen("-null-5/null-5;");
try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test public void testNullRef5() {
CodeGen code = new CodeGen("return null+42;");
try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
CodeGen code = new CodeGen(
Expand Down
2 changes: 1 addition & 1 deletion chapter19/src/main/java/com/seaofnodes/simple/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,6 @@ public static class ParseException extends RuntimeException {
public final Lexer _loc;
// file:line:charoff err
//String msg = "src:"+_line_number+":"+(_position-_line_start)+" "+errorMessage;
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; assert loc!=null; }
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; }
}
}
11 changes: 8 additions & 3 deletions chapter19/src/main/java/com/seaofnodes/simple/node/AddNode.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.seaofnodes.simple.node;

import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.*;

import java.util.BitSet;

import static com.seaofnodes.simple.Parser.con;

public class AddNode extends Node {
public AddNode(Node lhs, Node rhs) { super(null, lhs, rhs); }

Expand Down Expand Up @@ -46,6 +44,7 @@ static boolean overflow( long x, long y ) {
public Node idealize () {
Node lhs = in(1);
Node rhs = in(2);
if( rhs.err()!=null ) return null;
Type t2 = rhs._type;

// Add of 0. We do not check for (0+x) because this will already
Expand Down Expand Up @@ -187,4 +186,10 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) {

@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
@Override Node copyF() { return new AddFNode(null,null); }
@Override public Parser.ParseException err() {
if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null;
if( !(in(1)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(1)._type,null);
if( !(in(2)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(2)._type,null);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ public Node idealize() {
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
@Override Node copyF() { return new MulFNode(null,null); }
@Override public Parser.ParseException err() {
if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null;
if( !(in(1)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(1)._type,null);
if( !(in(2)._type instanceof TypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(2)._type,null);
return null;
}
}
13 changes: 13 additions & 0 deletions chapter19/src/test/java/com/seaofnodes/simple/Chapter13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public void testNullRef3() {
catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); }
}

@Test
public void testNullRef4() {
CodeGen code = new CodeGen("-null-5/null-5;");
try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test public void testNullRef5() {
CodeGen code = new CodeGen("return null+42;");
try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
}

@Test
public void testEmpty() {
CodeGen code = new CodeGen(
Expand Down
2 changes: 1 addition & 1 deletion chapter20/src/main/java/com/seaofnodes/simple/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,6 @@ public static class ParseException extends RuntimeException {
public final Lexer _loc;
// file:line:charoff err
//String msg = "src:"+_line_number+":"+(_position-_line_start)+" "+errorMessage;
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; assert loc!=null; }
ParseException( String msg, Lexer loc ) { super(msg); _loc = loc; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public CodeGen( String src, TypeInteger arg, long workListSeed ) {
*/
private int _uid = 1;
public int UID() { return _uid; }
public int getUID() { return _uid++; }
public int getUID() {
return _uid++;
}

// Next available memory alias number
private int _alias = 2; // 0 is for control, 1 for memory
Expand Down
Loading

0 comments on commit bb0b25e

Please sign in to comment.