Mercurial Hosting > luan
changeset 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 | 7afa6df829f3 |
files | src/luan/interp/GenericForStmt.java src/luan/interp/LuanParser.java src/luan/interp/NumericForStmt.java src/luan/lib/BasicLib.java |
diffstat | 4 files changed, 23 insertions(+), 78 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/interp/GenericForStmt.java Fri May 23 03:21:54 2014 +0000 +++ b/src/luan/interp/GenericForStmt.java Fri May 23 04:30:29 2014 +0000 @@ -4,6 +4,7 @@ import luan.LuanException; import luan.LuanFunction; import luan.LuanSource; +import luan.LuanBit; final class GenericForStmt extends CodeImpl implements Stmt { @@ -22,9 +23,11 @@ @Override public void eval(LuanStateImpl luan) throws LuanException { LuanFunction iter = luan.bit(se).checkFunction( iterExpr.eval(luan) ); + LuanBit bit = luan.bit(iterExpr.se()); + String name = iterExpr.se().text(); try { while(true) { - Object[] vals = luan.bit(iterExpr.se()).call(iter,iterExpr.se().text()); + Object[] vals = bit.call(iter,name); if( vals.length==0 || vals[0]==null ) break; for( int i=0; i<nVars; i++ ) {
--- a/src/luan/interp/LuanParser.java Fri May 23 03:21:54 2014 +0000 +++ b/src/luan/interp/LuanParser.java Fri May 23 04:30:29 2014 +0000 @@ -223,7 +223,6 @@ || (stmt=ImportStmt()) != null || (stmt=BreakStmt()) != null || (stmt=GenericForStmt()) != null - || (stmt=NumericForStmt()) != null || (stmt=TryStmt()) != null || (stmt=DoStmt()) != null || (stmt=WhileStmt()) != null @@ -350,39 +349,6 @@ return parser.success(stmt); } - private Stmt NumericForStmt() throws ParseException { - int start = parser.begin(); - if( !Keyword("for") ) - return parser.failure(null); - String name = RequiredName(); - RequiredMatch( "=" ); - Spaces(); - Expr from = expr(RequiredExpr()); - Expr to; - Expr step; - if( parser.match(',') ) { - Spaces(); - to = expr(RequiredExpr()); - if( parser.match(',') ) { - Spaces(); - step = expr(RequiredExpr()); - } else { - step = new ConstExpr(1); - } - } else { - RequiredKeyword("to"); - to = expr(RequiredExpr()); - step = Keyword("step") ? expr(RequiredExpr()) : new ConstExpr(1); - } - addSymbol(name); // add "for" var to symbols - RequiredKeyword("do"); - Stmt loop = RequiredLoopBlock(); - RequiredKeyword("end"); - Stmt stmt = new NumericForStmt( se(start), symbolsSize()-1, from, to, step, loop ); - popSymbols(1); - return parser.success(stmt); - } - private Stmt TryStmt() throws ParseException { parser.begin(); if( !Keyword("try") )
--- a/src/luan/interp/NumericForStmt.java Fri May 23 03:21:54 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -package luan.interp; - -import luan.Luan; -import luan.LuanException; -import luan.LuanSource; -import luan.LuanBit; - - -final class NumericForStmt extends CodeImpl implements Stmt { - private final int iVar; - private final Expr fromExpr; - private final Expr toExpr; - private final Expr stepExpr; - private final Stmt block; - - NumericForStmt(LuanSource.Element se,int iVar,Expr fromExpr,Expr toExpr,Expr stepExpr,Stmt block) { - super(se); - this.iVar = iVar; - this.fromExpr = fromExpr; - this.toExpr = toExpr; - this.stepExpr = stepExpr; - this.block = block; - } - - @Override public void eval(LuanStateImpl luan) throws LuanException { - LuanBit bit = luan.bit(se); - double v = bit.checkNumber( fromExpr.eval(luan) ).doubleValue(); - double limit = bit.checkNumber( toExpr.eval(luan) ).doubleValue(); - double step = bit.checkNumber( stepExpr.eval(luan) ).doubleValue(); - try { - while( step > 0.0 && v <= limit || step < 0.0 && v >= limit ) { - luan.stackSet( iVar, v ); - block.eval(luan); - v += step; - } - } catch(BreakException e) { - } finally { - luan.stackClear(iVar,iVar+1); - } - } - -}
--- 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}; + } + }; + } + }