changeset 191:2456ef7ada02

minor git-svn-id: https://luan-java.googlecode.com/svn/trunk@192 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 01 Jul 2014 06:40:48 +0000
parents 04b86428dc50
children f2c6c22cddc0
files core/src/luan/LuanBit.java core/src/luan/impl/EqExpr.java core/src/luan/impl/IfStmt.java core/src/luan/impl/LuanParser.java core/src/luan/impl/NotExpr.java
diffstat 5 files changed, 76 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanBit.java	Thu Jun 26 18:20:54 2014 +0000
+++ b/core/src/luan/LuanBit.java	Tue Jul 01 06:40:48 2014 +0000
@@ -43,28 +43,48 @@
 
 	public String checkString(Object obj) throws LuanException {
 		String s = Luan.asString(obj);
-		if( s == null )
+		if( s != null )
+			return s;
+		if( el instanceof LuanSource.Element ) {
+			LuanSource.Element se = (LuanSource.Element)el;
+			throw exception( "attempt to use '"+se.text()+"' (a " + Luan.type(obj) + " value) as a string" );
+		} else {
 			throw exception( "attempt to use a " + Luan.type(obj) + " as a string" );
-		return s;
+		}
 	}
 
 	public Number checkNumber(Object obj) throws LuanException {
 		Number n = Luan.toNumber(obj);
-		if( n == null )
+		if( n != null )
+			return n;
+		if( el instanceof LuanSource.Element ) {
+			LuanSource.Element se = (LuanSource.Element)el;
+			throw exception( "attempt to perform arithmetic on '"+se.text()+"' (a " + Luan.type(obj) + " value)" );
+		} else {
 			throw exception( "attempt to perform arithmetic on a " + Luan.type(obj) + " value" );
-		return n;
+		}
 	}
 
 	public LuanFunction checkFunction(Object obj) throws LuanException {
 		if( obj instanceof LuanFunction )
 			return (LuanFunction)obj;
-		throw exception( "attempt to call a " + Luan.type(obj) + " value" );
+		if( el instanceof LuanSource.Element ) {
+			LuanSource.Element se = (LuanSource.Element)el;
+			throw exception( "attempt to call '"+se.text()+"' (a " + Luan.type(obj) + " value)" );
+		} else {
+			throw exception( "attempt to call a " + Luan.type(obj) + " value" );
+		}
 	}
 
 	public Boolean checkBoolean(Object obj) throws LuanException {
 		if( obj instanceof Boolean )
 			return (Boolean)obj;
-		throw exception( "attempt to use a " + Luan.type(obj) + " as a boolean" );
+		if( el instanceof LuanSource.Element ) {
+			LuanSource.Element se = (LuanSource.Element)el;
+			throw exception( "attempt to use '"+se.text()+"' (a " + Luan.type(obj) + " value) as a boolean" );
+		} else {
+			throw exception( "attempt to use a " + Luan.type(obj) + " as a boolean" );
+		}
 	}
 
 	public String toString(Object obj) throws LuanException {
--- a/core/src/luan/impl/EqExpr.java	Thu Jun 26 18:20:54 2014 +0000
+++ b/core/src/luan/impl/EqExpr.java	Tue Jul 01 06:40:48 2014 +0000
@@ -15,6 +15,10 @@
 	}
 
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
+		return eq(luan);
+	}
+
+	private boolean eq(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
 		if( o1 == o2 || o1 != null && o1.equals(o2) )
@@ -32,9 +36,13 @@
 			return false;
 		Object f = mt1.get("__eq");
 		if( f == null || !f.equals(mt2.get("__eq")) )
-			return null;
+			return false;
 		LuanBit bit = luan.bit(se);
 		LuanFunction fn = bit.checkFunction(f);
 		return Luan.toBoolean( Luan.first(bit.call(fn,"__eq",new Object[]{o1,o2})) );
 	}
+
+	@Override public String toString() {
+		return "(EqExpr "+op1+" "+op2+")";
+	}
 }
--- a/core/src/luan/impl/IfStmt.java	Thu Jun 26 18:20:54 2014 +0000
+++ b/core/src/luan/impl/IfStmt.java	Tue Jul 01 06:40:48 2014 +0000
@@ -18,7 +18,7 @@
 	}
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
-		if( luan.bit(se).checkBoolean( cnd.eval(luan) ) ) {
+		if( luan.bit(cnd.se()).checkBoolean( cnd.eval(luan) ) ) {
 			thenStmt.eval(luan);
 		} else {
 			elseStmt.eval(luan);
--- a/core/src/luan/impl/LuanParser.java	Thu Jun 26 18:20:54 2014 +0000
+++ b/core/src/luan/impl/LuanParser.java	Tue Jul 01 06:40:48 2014 +0000
@@ -600,7 +600,8 @@
 		if( exp==null )
 			return parser.failure(null);
 		while( Keyword("or",in) ) {
-			exp = new OrExpr( se(start), expr(exp), required(expr(AndExpr(in))) );
+			Expressions exp2 = AndExpr(in);
+			exp = new OrExpr( se(start), expr(exp), required(expr(exp2)) );
 		}
 		return parser.success(exp);
 	}
@@ -611,7 +612,8 @@
 		if( exp==null )
 			return parser.failure(null);
 		while( Keyword("and",in) ) {
-			exp = new AndExpr( se(start), expr(exp), required(expr(RelExpr(in))) );
+			Expressions exp2 = RelExpr(in);
+			exp = new AndExpr( se(start), expr(exp), required(expr(exp2)) );
 		}
 		return parser.success(exp);
 	}
@@ -624,22 +626,28 @@
 		while(true) {
 			if( parser.match("==") ) {
 				Spaces(in);
-				exp = new EqExpr( se(start), expr(exp), required(expr(ConcatExpr(in))) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new EqExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( parser.match("~=") ) {
 				Spaces(in);
-				exp = new NotExpr( se(start), new EqExpr( se(start), expr(exp), required(expr(ConcatExpr(in))) ) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new NotExpr( se(start), new EqExpr( se(start), expr(exp), required(expr(exp2)) ) );
 			} else if( parser.match("<=") ) {
 				Spaces(in);
-				exp = new LeExpr( se(start), expr(exp), required(expr(ConcatExpr(in))) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new LeExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( parser.match(">=") ) {
 				Spaces(in);
-				exp = new LeExpr( se(start), required(expr(ConcatExpr(in))), expr(exp) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new LeExpr( se(start), required(expr(exp2)), expr(exp) );
 			} else if( parser.match("<") ) {
 				Spaces(in);
-				exp = new LtExpr( se(start), expr(exp), required(expr(ConcatExpr(in))) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new LtExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( parser.match(">") ) {
 				Spaces(in);
-				exp = new LtExpr( se(start), required(expr(ConcatExpr(in))), expr(exp) );
+				Expressions exp2 = ConcatExpr(in);
+				exp = new LtExpr( se(start), required(expr(exp2)), expr(exp) );
 			} else
 				break;
 		}
@@ -653,7 +661,8 @@
 			return parser.failure(null);
 		if( parser.match("..") ) {
 			Spaces(in);
-			exp = new ConcatExpr( se(start), expr(exp), required(expr(ConcatExpr(in))) );
+			Expressions exp2 = ConcatExpr(in);
+			exp = new ConcatExpr( se(start), expr(exp), required(expr(exp2)) );
 		}
 		return parser.success(exp);
 	}
@@ -666,10 +675,12 @@
 		while(true) {
 			if( parser.match('+') ) {
 				Spaces(in);
-				exp = new AddExpr( se(start), expr(exp), required(expr(TermExpr(in))) );
+				Expressions exp2 = TermExpr(in);
+				exp = new AddExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( Minus() ) {
 				Spaces(in);
-				exp = new SubExpr( se(start), expr(exp), required(expr(TermExpr(in))) );
+				Expressions exp2 = TermExpr(in);
+				exp = new SubExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else
 				break;
 		}
@@ -689,13 +700,16 @@
 		while(true) {
 			if( parser.match('*') ) {
 				Spaces(in);
-				exp = new MulExpr( se(start), expr(exp), required(expr(UnaryExpr(in))) );
+				Expressions exp2 = UnaryExpr(in);
+				exp = new MulExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( parser.match('/') ) {
 				Spaces(in);
-				exp = new DivExpr( se(start), expr(exp), required(expr(UnaryExpr(in))) );
+				Expressions exp2 = UnaryExpr(in);
+				exp = new DivExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else if( Mod() ) {
 				Spaces(in);
-				exp = new ModExpr( se(start), expr(exp), required(expr(UnaryExpr(in))) );
+				Expressions exp2 = UnaryExpr(in);
+				exp = new ModExpr( se(start), expr(exp), required(expr(exp2)) );
 			} else
 				break;
 		}
@@ -711,15 +725,18 @@
 		int start = parser.begin();
 		if( parser.match('#') ) {
 			Spaces(in);
-			return parser.success( new LenExpr( se(start), required(expr(UnaryExpr(in))) ) );
+			Expressions exp = UnaryExpr(in);
+			return parser.success( new LenExpr( se(start), required(expr(exp)) ) );
 		}
 		if( Minus() ) {
 			Spaces(in);
-			return parser.success( new UnmExpr( se(start), required(expr(UnaryExpr(in))) ) );
+			Expressions exp = UnaryExpr(in);
+			return parser.success( new UnmExpr( se(start), required(expr(exp)) ) );
 		}
 		if( Keyword("not",in) ) {
 			Spaces(in);
-			return parser.success( new NotExpr( se(start), required(expr(UnaryExpr(in))) ) );
+			Expressions exp = UnaryExpr(in);
+			return parser.success( new NotExpr( se(start), required(expr(exp)) ) );
 		}
 		Expressions exp = PowExpr(in);
 		if( exp==null )
@@ -734,7 +751,8 @@
 			return parser.failure(null);
 		if( parser.match('^') ) {
 			Spaces(in);
-			exp = new ConcatExpr( se(start), expr(exp), required(expr(PowExpr(in))) );
+			Expressions exp2 = PowExpr(in);
+			exp = new ConcatExpr( se(start), expr(exp), required(expr(exp2)) );
 		}
 		return parser.success(exp);
 	}
--- a/core/src/luan/impl/NotExpr.java	Thu Jun 26 18:20:54 2014 +0000
+++ b/core/src/luan/impl/NotExpr.java	Tue Jul 01 06:40:48 2014 +0000
@@ -14,4 +14,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		return !Luan.toBoolean(op.eval(luan));
 	}
+
+	@Override public String toString() {
+		return "(NotExpr "+op+")";
+	}
 }