diff src/luan/lib/BasicLib.java @ 109:219e05867366

remove NumericForStmt; add BasicLib.range(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@110 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 04:30:29 +0000
parents 3c404a296995
children 2428ecfed375
line wrap: on
line diff
--- a/src/luan/lib/BasicLib.java	Fri May 23 03:21:54 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri May 23 04:30:29 2014 +0000
@@ -40,6 +40,7 @@
 				add( global, "load_file", LuanState.class, String.class );
 				add( global, "pairs", LuanState.class, LuanTable.class );
 				add( global, "print", LuanState.class, new Object[0].getClass() );
+				add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class );
 				add( global, "raw_equal", Object.class, Object.class );
 				add( global, "raw_get", LuanTable.class, Object.class );
 				add( global, "raw_len", LuanState.class, Object.class );
@@ -98,7 +99,7 @@
 
 	private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) {
 		return new LuanFunction() {
-			public Object[] call(LuanState luan,Object[] args) {
+			@Override public Object[] call(LuanState luan,Object[] args) {
 				if( !iter.hasNext() )
 					return LuanFunction.EMPTY;
 				Map.Entry<Object,Object> entry = iter.next();
@@ -200,4 +201,21 @@
 		return luan.JAVA.repr(v);
 	}
 
+	public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
+		final double step = stepV==null ? 1.0 : stepV;
+		if( step == 0.0 )
+			throw luan.JAVA.exception("bad argument #3 (step may not be zero)");
+		return new LuanFunction() {
+			double v = from;
+
+			@Override public Object[] call(LuanState luan,Object[] args) {
+				if( step > 0.0 && v > to || step < 0.0 && v < to )
+					return LuanFunction.EMPTY;
+				double rtn = v;
+				v += step;
+				return new Object[]{rtn};
+			}
+		};
+	}
+
 }