changeset 151:c9100f29fae0

conditions must be type boolean git-svn-id: https://luan-java.googlecode.com/svn/trunk@152 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 16 Jun 2014 10:37:06 +0000
parents f35c50027985
children a9391f914aba
files src/luan/LuanBit.java src/luan/interp/IfStmt.java src/luan/interp/LuanParser.java src/luan/interp/RepeatStmt.java src/luan/interp/WhileStmt.java
diffstat 5 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanBit.java	Mon Jun 16 10:11:48 2014 +0000
+++ b/src/luan/LuanBit.java	Mon Jun 16 10:37:06 2014 +0000
@@ -61,6 +61,12 @@
 		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" );
+	}
+
 	public String toString(Object obj) throws LuanException {
 		LuanFunction fn = getHandlerFunction("__tostring",obj);
 		if( fn != null )
--- a/src/luan/interp/IfStmt.java	Mon Jun 16 10:11:48 2014 +0000
+++ b/src/luan/interp/IfStmt.java	Mon Jun 16 10:37:06 2014 +0000
@@ -2,21 +2,23 @@
 
 import luan.Luan;
 import luan.LuanException;
+import luan.LuanSource;
 
 
-final class IfStmt implements Stmt {
+final class IfStmt extends CodeImpl implements Stmt {
 	private final Expr cnd;
 	final Stmt thenStmt;
 	final Stmt elseStmt;
 
-	IfStmt(Expr cnd,Stmt thenStmt,Stmt elseStmt) {
+	IfStmt(LuanSource.Element se,Expr cnd,Stmt thenStmt,Stmt elseStmt) {
+		super(se);
 		this.cnd = cnd;
 		this.thenStmt = thenStmt;
 		this.elseStmt = elseStmt;
 	}
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
-		if( Luan.toBoolean( cnd.eval(luan) ) ) {
+		if( luan.bit(se).checkBoolean( cnd.eval(luan) ) ) {
 			thenStmt.eval(luan);
 		} else {
 			elseStmt.eval(luan);
--- a/src/luan/interp/LuanParser.java	Mon Jun 16 10:11:48 2014 +0000
+++ b/src/luan/interp/LuanParser.java	Mon Jun 16 10:37:06 2014 +0000
@@ -451,24 +451,24 @@
 	}
 
 	private Stmt WhileStmt() throws ParseException {
-		parser.begin();
+		int start = parser.begin();
 		if( !Keyword("while",In.NOTHING) )
 			return parser.failure(null);
 		Expr cnd = expr(RequiredExpr(In.NOTHING));
 		RequiredKeyword("do",In.NOTHING);
 		Stmt loop = RequiredLoopBlock();
 		RequiredKeyword("end",In.NOTHING);
-		return parser.success( new WhileStmt(cnd,loop) );
+		return parser.success( new WhileStmt(se(start),cnd,loop) );
 	}
 
 	private Stmt RepeatStmt() throws ParseException {
-		parser.begin();
+		int start = parser.begin();
 		if( !Keyword("repeat",In.NOTHING) )
 			return parser.failure(null);
 		Stmt loop = RequiredLoopBlock();
 		RequiredKeyword("until",In.NOTHING);
 		Expr cnd = expr(RequiredExpr(In.NOTHING));
-		return parser.success( new RepeatStmt(loop,cnd) );
+		return parser.success( new RepeatStmt(se(start),loop,cnd) );
 	}
 
 	private Stmt RequiredLoopBlock() throws ParseException {
@@ -486,6 +486,7 @@
 	}
 
 	private Stmt IfStmt2() throws ParseException {
+		int start = parser.currentIndex();
 		Expr cnd = expr(RequiredExpr(In.NOTHING));
 		RequiredKeyword("then",In.NOTHING);
 		Stmt thenBlock = RequiredBlock();
@@ -496,7 +497,7 @@
 			elseBlock = Keyword("else",In.NOTHING) ? RequiredBlock() : Stmt.EMPTY;
 			RequiredKeyword("end",In.NOTHING);
 		}
-		return new IfStmt(cnd,thenBlock,elseBlock);
+		return new IfStmt(se(start),cnd,thenBlock,elseBlock);
 	}
 
 	private Stmt SetStmt() throws ParseException {
--- a/src/luan/interp/RepeatStmt.java	Mon Jun 16 10:11:48 2014 +0000
+++ b/src/luan/interp/RepeatStmt.java	Mon Jun 16 10:37:06 2014 +0000
@@ -2,13 +2,15 @@
 
 import luan.Luan;
 import luan.LuanException;
+import luan.LuanSource;
 
 
-final class RepeatStmt implements Stmt {
+final class RepeatStmt extends CodeImpl implements Stmt {
 	private final Stmt doStmt;
 	private final Expr cnd;
 
-	RepeatStmt(Stmt doStmt,Expr cnd) {
+	RepeatStmt(LuanSource.Element se,Stmt doStmt,Expr cnd) {
+		super(se);
 		this.doStmt = doStmt;
 		this.cnd = cnd;
 	}
@@ -17,7 +19,7 @@
 		try {
 			do {
 				doStmt.eval(luan);
-			} while( !Luan.toBoolean( cnd.eval(luan) ) );
+			} while( !luan.bit(se).checkBoolean( cnd.eval(luan) ) );
 		} catch(BreakException e) {}
 	}
 }
--- a/src/luan/interp/WhileStmt.java	Mon Jun 16 10:11:48 2014 +0000
+++ b/src/luan/interp/WhileStmt.java	Mon Jun 16 10:37:06 2014 +0000
@@ -2,20 +2,22 @@
 
 import luan.Luan;
 import luan.LuanException;
+import luan.LuanSource;
 
 
-final class WhileStmt implements Stmt {
+final class WhileStmt extends CodeImpl implements Stmt {
 	private final Expr cnd;
 	private final Stmt doStmt;
 
-	WhileStmt(Expr cnd,Stmt doStmt) {
+	WhileStmt(LuanSource.Element se,Expr cnd,Stmt doStmt) {
+		super(se);
 		this.cnd = cnd;
 		this.doStmt = doStmt;
 	}
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
 		try {
-			while( Luan.toBoolean( cnd.eval(luan) ) ) {
+			while( luan.bit(se).checkBoolean( cnd.eval(luan) ) ) {
 				doStmt.eval(luan);
 			}
 		} catch(BreakException e) {}