Mercurial Hosting > luan
changeset 3:7a2cdbc5767f
add LuaState
git-svn-id: https://luan-java.googlecode.com/svn/trunk@4 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 16 Nov 2012 05:23:12 +0000 |
parents | 4da26b11d12a |
children | 24fd6381caca |
files | src/luan/Lua.java src/luan/LuaState.java src/luan/interp/AddExpr.java src/luan/interp/ConstExpr.java src/luan/interp/EnvExpr.java src/luan/interp/ExpList.java src/luan/interp/Expr.java src/luan/interp/FnCall.java src/luan/interp/IndexExpr.java src/luan/interp/LengthExpr.java src/luan/interp/SubExpr.java src/luan/interp/Values.java src/luan/interp/ValuesExpr.java src/luan/lib/BasicLib.java |
diffstat | 14 files changed, 125 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/Lua.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/Lua.java Fri Nov 16 05:23:12 2012 +0000 @@ -21,7 +21,11 @@ return obj.toString(); } - public static LuaNumber toNumber(Object obj) throws LuaException { + public static String checkString(Object obj) { + return toString(obj); + } + + public static LuaNumber toNumber(Object obj) { if( obj instanceof LuaNumber ) return (LuaNumber)obj; if( obj instanceof String ) { @@ -30,10 +34,17 @@ return new LuaNumber( Double.parseDouble(s) ); } catch(NumberFormatException e) {} } - throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); + return null; } - public static LuaFunction toFunction(Object obj) throws LuaException { + public static LuaNumber checkNumber(Object obj) throws LuaException { + LuaNumber n = toNumber(obj); + if( n == null ) + throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); + return n; + } + + public static LuaFunction checkFunction(Object obj) throws LuaException { if( obj instanceof LuaFunction ) return (LuaFunction)obj; throw new LuaException( "attempt to call a " + type(obj) + " value" );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/LuaState.java Fri Nov 16 05:23:12 2012 +0000 @@ -0,0 +1,11 @@ +package luan; + + +public class LuaState { + private final LuaTable env = new LuaTable(); + + public LuaTable env() { + return env; + } + +}
--- a/src/luan/interp/AddExpr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/AddExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -3,6 +3,7 @@ import luan.Lua; import luan.LuaNumber; import luan.LuaException; +import luan.LuaState; final class AddExpr extends BinaryOpExpr { @@ -11,9 +12,9 @@ super(op1,op2); } - @Override Object eval() throws LuaException { - double n1 = Lua.toNumber(op1.eval()).value(); - double n2 = Lua.toNumber(op2.eval()).value(); + @Override Object eval(LuaState lua) throws LuaException { + double n1 = Lua.checkNumber(op1.eval(lua)).value(); + double n2 = Lua.checkNumber(op2.eval(lua)).value(); return new LuaNumber( n1 + n2 ); } }
--- a/src/luan/interp/ConstExpr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/ConstExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -1,5 +1,7 @@ package luan.interp; +import luan.LuaState; + final class ConstExpr extends Expr { private final Object obj; @@ -8,7 +10,7 @@ this.obj = obj; } - @Override Object eval() { + @Override Object eval(LuaState lua) { return obj; } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/EnvExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -0,0 +1,11 @@ +package luan.interp; + +import luan.LuaState; + + +final class EnvExpr extends Expr { + + @Override Object eval(LuaState lua) { + return lua.env(); + } +}
--- a/src/luan/interp/ExpList.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/ExpList.java Fri Nov 16 05:23:12 2012 +0000 @@ -2,13 +2,15 @@ import java.util.List; import java.util.ArrayList; +import java.util.Collections; import luan.LuaException; +import luan.LuaState; final class ExpList extends Values { private interface Adder { - public void addTo(List<Object> list) throws LuaException; + public void addTo(LuaState lua,List<Object> list) throws LuaException; } private static class ExprAdder implements Adder { @@ -18,8 +20,8 @@ this.expr = expr; } - public void addTo(List<Object> list) throws LuaException { - list.add( expr.eval() ); + public void addTo(LuaState lua,List<Object> list) throws LuaException { + list.add( expr.eval(lua) ); } } @@ -31,8 +33,8 @@ this.values = values; } - public void addTo(List<Object> list) throws LuaException { - for( Object val : values.eval() ) { + public void addTo(LuaState lua,List<Object> list) throws LuaException { + for( Object val : values.eval(lua) ) { list.add( val ); } } @@ -50,21 +52,50 @@ adders.add( new ValuesAdder(values) ); } - ExpList build() { + Values build() { + if( adders.isEmpty() ) + return emptyExpList; + if( adders.size() == 1 ) { + Adder adder = adders.get(0); + if( adder instanceof ValuesAdder ) { + ValuesAdder va = (ValuesAdder)adder; + return va.values; + } + ExprAdder ea = (ExprAdder)adder; + return new SingleExpList(ea.expr); + } return new ExpList( adders.toArray(new Adder[0]) ); } } + private static final Values emptyExpList = new Values() { + List eval(LuaState lua) { + return Collections.emptyList(); + } + }; + + private static class SingleExpList extends Values { + private final Expr expr; + + SingleExpList(Expr expr) { + this.expr = expr; + } + + List eval(LuaState lua) throws LuaException { + return Collections.singletonList( expr.eval(lua) ); + } + } + private final Adder[] adders; private ExpList(Adder[] adders) { this.adders = adders; } - List eval() throws LuaException { + List eval(LuaState lua) throws LuaException { List<Object> list = new ArrayList<Object>(); for( Adder adder : adders ) { - adder.addTo(list); + adder.addTo(lua,list); } return list; }
--- a/src/luan/interp/Expr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/Expr.java Fri Nov 16 05:23:12 2012 +0000 @@ -1,8 +1,9 @@ package luan.interp; +import luan.LuaState; import luan.LuaException; abstract class Expr { - abstract Object eval() throws LuaException; + abstract Object eval(LuaState lua) throws LuaException; }
--- a/src/luan/interp/FnCall.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/FnCall.java Fri Nov 16 05:23:12 2012 +0000 @@ -5,6 +5,7 @@ import luan.Lua; import luan.LuaFunction; import luan.LuaException; +import luan.LuaState; final class FnCall extends Values { @@ -16,8 +17,8 @@ this.args = args; } - List eval() throws LuaException { - LuaFunction fn = Lua.toFunction( fnExpr.eval() ); - return Arrays.asList( fn.call( args.eval().toArray() ) ); + List eval(LuaState lua) throws LuaException { + LuaFunction fn = Lua.checkFunction( fnExpr.eval(lua) ); + return Arrays.asList( fn.call( args.eval(lua).toArray() ) ); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/IndexExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -0,0 +1,24 @@ +package luan.interp; + +import luan.Lua; +import luan.LuaException; +import luan.LuaState; +import luan.LuaTable; + + +final class IndexExpr extends BinaryOpExpr { + + IndexExpr(Expr op1,Expr op2) { + super(op1,op2); + } + + @Override Object eval(LuaState lua) throws LuaException { + Object t = op1.eval(lua); + if( t instanceof LuaTable ) { + LuaTable tbl = (LuaTable)t; + Object key = op2.eval(lua); + return tbl.get(key); + } + throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); + } +}
--- a/src/luan/interp/LengthExpr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/LengthExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -4,6 +4,7 @@ import luan.LuaNumber; import luan.LuaTable; import luan.LuaException; +import luan.LuaState; final class LengthExpr extends UnaryOpExpr { @@ -12,8 +13,8 @@ super(op); } - @Override Object eval() throws LuaException { - return new LuaNumber( length(op.eval()) ); + @Override Object eval(LuaState lua) throws LuaException { + return new LuaNumber( length(op.eval(lua)) ); } private static int length(Object obj) throws LuaException {
--- a/src/luan/interp/SubExpr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/SubExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -3,6 +3,7 @@ import luan.Lua; import luan.LuaNumber; import luan.LuaException; +import luan.LuaState; final class SubExpr extends BinaryOpExpr { @@ -11,9 +12,9 @@ super(op1,op2); } - @Override Object eval() throws LuaException { - double n1 = Lua.toNumber(op1.eval()).value(); - double n2 = Lua.toNumber(op2.eval()).value(); + @Override Object eval(LuaState lua) throws LuaException { + double n1 = Lua.checkNumber(op1.eval(lua)).value(); + double n2 = Lua.checkNumber(op2.eval(lua)).value(); return new LuaNumber( n1 - n2 ); } }
--- a/src/luan/interp/Values.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/Values.java Fri Nov 16 05:23:12 2012 +0000 @@ -2,8 +2,9 @@ import java.util.List; import luan.LuaException; +import luan.LuaState; abstract class Values { - abstract List eval() throws LuaException; + abstract List eval(LuaState lua) throws LuaException; }
--- a/src/luan/interp/ValuesExpr.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/interp/ValuesExpr.java Fri Nov 16 05:23:12 2012 +0000 @@ -3,6 +3,7 @@ import java.util.List; import luan.Lua; import luan.LuaException; +import luan.LuaState; final class ValuesExpr extends Expr { @@ -12,8 +13,8 @@ this.values = values; } - @Override Object eval() throws LuaException { - List list = values.eval(); + @Override Object eval(LuaState lua) throws LuaException { + List list = values.eval(lua); return list.isEmpty() ? null : list.get(0); } }
--- a/src/luan/lib/BasicLib.java Wed Nov 14 08:53:25 2012 +0000 +++ b/src/luan/lib/BasicLib.java Fri Nov 16 05:23:12 2012 +0000 @@ -23,7 +23,7 @@ for( int i=0; i<args.length; i++ ) { if( i > 0 ) System.out.print('\t'); - System.out.print( Lua.toString(args[i]) ); + System.out.print( Lua.checkString(args[i]) ); } System.out.println(); }