Mercurial Hosting > luan
changeset 2:4da26b11d12a
start interp
git-svn-id: https://luan-java.googlecode.com/svn/trunk@3 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 14 Nov 2012 08:53:25 +0000 |
parents | 2df768b926aa |
children | 7a2cdbc5767f |
files | src/luan/Lua.java src/luan/LuaException.java src/luan/LuaJavaFunction.java src/luan/LuaNumber.java src/luan/interp/AddExpr.java src/luan/interp/BinaryOpExpr.java src/luan/interp/ConstExpr.java src/luan/interp/ExpList.java src/luan/interp/Expr.java src/luan/interp/FnCall.java src/luan/interp/LengthExpr.java src/luan/interp/SubExpr.java src/luan/interp/UnaryOpExpr.java src/luan/interp/Values.java src/luan/interp/ValuesExpr.java src/luan/lib/BasicLib.java |
diffstat | 16 files changed, 281 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
diff -r 2df768b926aa -r 4da26b11d12a src/luan/Lua.java --- a/src/luan/Lua.java Tue Nov 13 05:54:32 2012 +0000 +++ b/src/luan/Lua.java Wed Nov 14 08:53:25 2012 +0000 @@ -3,12 +3,6 @@ public class Lua { - public static String toString(Object obj) { - if( obj == null ) - return "nil"; - return obj.toString(); - } - public static String type(Object obj) { if( obj == null ) return "nil"; @@ -21,19 +15,13 @@ return "userdata"; } - public static int length(Object obj) throws LuaException { - if( obj instanceof String ) { - String s = (String)obj; - return s.length(); - } - if( obj instanceof LuaTable ) { - LuaTable t = (LuaTable)obj; - return t.length(); - } - throw new LuaException( "attempt to get length of a " + type(obj) + " value" ); + public static String toString(Object obj) { + if( obj == null ) + return "nil"; + return obj.toString(); } - static LuaNumber toNumber(Object obj) throws LuaException { + public static LuaNumber toNumber(Object obj) throws LuaException { if( obj instanceof LuaNumber ) return (LuaNumber)obj; if( obj instanceof String ) { @@ -45,12 +33,10 @@ throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); } - static LuaNumber add(Object n1,Object n2) throws LuaException { - return new LuaNumber( toNumber(n1).n + toNumber(n2).n ); - } - - static LuaNumber sub(Object n1,Object n2) throws LuaException { - return new LuaNumber( toNumber(n1).n - toNumber(n2).n ); + public static LuaFunction toFunction(Object obj) throws LuaException { + if( obj instanceof LuaFunction ) + return (LuaFunction)obj; + throw new LuaException( "attempt to call a " + type(obj) + " value" ); } }
diff -r 2df768b926aa -r 4da26b11d12a src/luan/LuaException.java --- a/src/luan/LuaException.java Tue Nov 13 05:54:32 2012 +0000 +++ b/src/luan/LuaException.java Wed Nov 14 08:53:25 2012 +0000 @@ -3,7 +3,7 @@ public class LuaException extends Exception { - LuaException(String msg) { + public LuaException(String msg) { super(msg); }
diff -r 2df768b926aa -r 4da26b11d12a src/luan/LuaJavaFunction.java --- a/src/luan/LuaJavaFunction.java Tue Nov 13 05:54:32 2012 +0000 +++ b/src/luan/LuaJavaFunction.java Wed Nov 14 08:53:25 2012 +0000 @@ -17,7 +17,7 @@ argConverters = getArgConverters(method); } - public Object[] call(Object... args) { + @Override public Object[] call(Object... args) { args = fixArgs(args); Object rtn; try {
diff -r 2df768b926aa -r 4da26b11d12a src/luan/LuaNumber.java --- a/src/luan/LuaNumber.java Tue Nov 13 05:54:32 2012 +0000 +++ b/src/luan/LuaNumber.java Wed Nov 14 08:53:25 2012 +0000 @@ -1,13 +1,17 @@ package luan; -public class LuaNumber { +public final class LuaNumber { final double n; public LuaNumber(double n) { this.n = n; } + public double value() { + return n; + } + // convert to Lua format @Override public String toString() { String s = Double.toString(n);
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/AddExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/AddExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,19 @@ +package luan.interp; + +import luan.Lua; +import luan.LuaNumber; +import luan.LuaException; + + +final class AddExpr extends BinaryOpExpr { + + AddExpr(Expr op1,Expr op2) { + super(op1,op2); + } + + @Override Object eval() throws LuaException { + double n1 = Lua.toNumber(op1.eval()).value(); + double n2 = Lua.toNumber(op2.eval()).value(); + return new LuaNumber( n1 + n2 ); + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/BinaryOpExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/BinaryOpExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,12 @@ +package luan.interp; + + +abstract class BinaryOpExpr extends Expr { + final Expr op1; + final Expr op2; + + BinaryOpExpr(Expr op1,Expr op2) { + this.op1 = op1; + this.op2 = op2; + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/ConstExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/ConstExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,14 @@ +package luan.interp; + + +final class ConstExpr extends Expr { + private final Object obj; + + ConstExpr(Object obj) { + this.obj = obj; + } + + @Override Object eval() { + return obj; + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/ExpList.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/ExpList.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,71 @@ +package luan.interp; + +import java.util.List; +import java.util.ArrayList; +import luan.LuaException; + + +final class ExpList extends Values { + + private interface Adder { + public void addTo(List<Object> list) throws LuaException; + } + + private static class ExprAdder implements Adder { + private final Expr expr; + + ExprAdder(Expr expr) { + this.expr = expr; + } + + public void addTo(List<Object> list) throws LuaException { + list.add( expr.eval() ); + } + + } + + private static class ValuesAdder implements Adder { + private final Values values; + + ValuesAdder(Values values) { + this.values = values; + } + + public void addTo(List<Object> list) throws LuaException { + for( Object val : values.eval() ) { + list.add( val ); + } + } + + } + + static class Builder { + private final List<Adder> adders = new ArrayList<Adder>(); + + void add(Expr expr) { + adders.add( new ExprAdder(expr) ); + } + + void add(Values values) { + adders.add( new ValuesAdder(values) ); + } + + ExpList build() { + return new ExpList( adders.toArray(new Adder[0]) ); + } + } + + private final Adder[] adders; + + private ExpList(Adder[] adders) { + this.adders = adders; + } + + List eval() throws LuaException { + List<Object> list = new ArrayList<Object>(); + for( Adder adder : adders ) { + adder.addTo(list); + } + return list; + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/Expr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/Expr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,8 @@ +package luan.interp; + +import luan.LuaException; + + +abstract class Expr { + abstract Object eval() throws LuaException; +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/FnCall.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/FnCall.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,23 @@ +package luan.interp; + +import java.util.Arrays; +import java.util.List; +import luan.Lua; +import luan.LuaFunction; +import luan.LuaException; + + +final class FnCall extends Values { + private final Expr fnExpr; + private final Values args; + + FnCall(Expr fnExpr,Values args) { + this.fnExpr = fnExpr; + this.args = args; + } + + List eval() throws LuaException { + LuaFunction fn = Lua.toFunction( fnExpr.eval() ); + return Arrays.asList( fn.call( args.eval().toArray() ) ); + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/LengthExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/LengthExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,30 @@ +package luan.interp; + +import luan.Lua; +import luan.LuaNumber; +import luan.LuaTable; +import luan.LuaException; + + +final class LengthExpr extends UnaryOpExpr { + + LengthExpr(Expr op) { + super(op); + } + + @Override Object eval() throws LuaException { + return new LuaNumber( length(op.eval()) ); + } + + private static int length(Object obj) throws LuaException { + if( obj instanceof String ) { + String s = (String)obj; + return s.length(); + } + if( obj instanceof LuaTable ) { + LuaTable t = (LuaTable)obj; + return t.length(); + } + throw new LuaException( "attempt to get length of a " + Lua.type(obj) + " value" ); + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/SubExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/SubExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,19 @@ +package luan.interp; + +import luan.Lua; +import luan.LuaNumber; +import luan.LuaException; + + +final class SubExpr extends BinaryOpExpr { + + SubExpr(Expr op1,Expr op2) { + super(op1,op2); + } + + @Override Object eval() throws LuaException { + double n1 = Lua.toNumber(op1.eval()).value(); + double n2 = Lua.toNumber(op2.eval()).value(); + return new LuaNumber( n1 - n2 ); + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/UnaryOpExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/UnaryOpExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,10 @@ +package luan.interp; + + +abstract class UnaryOpExpr extends Expr { + final Expr op; + + UnaryOpExpr(Expr op) { + this.op = op; + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/Values.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/Values.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,9 @@ +package luan.interp; + +import java.util.List; +import luan.LuaException; + + +abstract class Values { + abstract List eval() throws LuaException; +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/interp/ValuesExpr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/ValuesExpr.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,19 @@ +package luan.interp; + +import java.util.List; +import luan.Lua; +import luan.LuaException; + + +final class ValuesExpr extends Expr { + private final Values values; + + ValuesExpr(Values values) { + this.values = values; + } + + @Override Object eval() throws LuaException { + List list = values.eval(); + return list.isEmpty() ? null : list.get(0); + } +}
diff -r 2df768b926aa -r 4da26b11d12a src/luan/lib/BasicLib.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/BasicLib.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,31 @@ +package luan.lib; + +import luan.Lua; +import luan.LuaTable; +import luan.LuaJavaFunction; + + +public class BasicLib { + + public static void register(LuaTable t) { + add( t, "print", new Object[0].getClass() ); + } + + private static void add(LuaTable t,String method,Class<?>... parameterTypes) { + try { + t.set( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + public static void print(Object... args) { + for( int i=0; i<args.length; i++ ) { + if( i > 0 ) + System.out.print('\t'); + System.out.print( Lua.toString(args[i]) ); + } + System.out.println(); + } + +}