changeset 36:2a35154aec14

implement more basic lib functions git-svn-id: https://luan-java.googlecode.com/svn/trunk@37 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 18 Dec 2012 09:53:42 +0000
parents e51906de0f11
children 8a57ebfdfd78
files src/luan/CmdLine.java src/luan/Lua.java src/luan/LuaState.java src/luan/interp/BinaryOpExpr.java src/luan/interp/FnCall.java src/luan/interp/IndexExpr.java src/luan/interp/LenExpr.java src/luan/interp/LuaStateImpl.java src/luan/interp/SetTableEntry.java src/luan/interp/UnmExpr.java src/luan/interp/Utils.java src/luan/lib/BasicLib.java
diffstat 12 files changed, 82 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/CmdLine.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/CmdLine.java	Tue Dec 18 09:53:42 2012 +0000
@@ -1,6 +1,5 @@
 package luan;
 
-import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.Scanner;
 import luan.lib.BasicLib;
@@ -39,10 +38,8 @@
 						System.exit(-1);
 					}
 				} else if( arg.equals("-") ) {
-					String cmd = BasicLib.readAll(new InputStreamReader(System.in));
 					try {
-						LuaFunction fn = BasicLib.load(lua,cmd);
-						fn.call(lua);
+						BasicLib.dofile(lua,null);
 					} catch(LuaException e) {
 						System.err.println(e.getMessage());
 						System.exit(-1);
@@ -100,7 +97,7 @@
 				LuaFunction fn = BasicLib.load(lua,input);
 				Object[] rtn = fn.call(lua);
 				if( rtn.length > 0 )
-					BasicLib.print(rtn);
+					BasicLib.print(lua,rtn);
 			} catch(LuaException e) {
 				System.out.println(e.getMessage());
 			}
--- a/src/luan/Lua.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/Lua.java	Tue Dec 18 09:53:42 2012 +0000
@@ -20,12 +20,6 @@
 		return obj != null && !Boolean.FALSE.equals(obj);
 	}
 
-	public static String toString(Object obj) {
-		if( obj == null )
-			return "nil";
-		return obj.toString();
-	}
-
 	public static String asString(Object obj) {
 		if( obj instanceof String || obj instanceof LuaNumber )
 			return obj.toString();
@@ -40,12 +34,19 @@
 	}
 
 	public static LuaNumber toNumber(Object obj) {
+		return toNumber(obj,null);
+	}
+
+	public static LuaNumber toNumber(Object obj,Integer base) {
 		if( obj instanceof LuaNumber )
 			return (LuaNumber)obj;
 		if( obj instanceof String ) {
 			String s = (String)obj;
 			try {
-				return new LuaNumber( Double.parseDouble(s) );
+				if( base==null )
+					return new LuaNumber( Double.parseDouble(s) );
+				else
+					return new LuaNumber( Long.parseLong(s,base) );
 			} catch(NumberFormatException e) {}
 		}
 		return null;
--- a/src/luan/LuaState.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/LuaState.java	Tue Dec 18 09:53:42 2012 +0000
@@ -3,4 +3,5 @@
 
 public interface LuaState {
 	public LuaTable global();
+	public String toString(Object obj) throws LuaException;
 }
--- a/src/luan/interp/BinaryOpExpr.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/BinaryOpExpr.java	Tue Dec 18 09:53:42 2012 +0000
@@ -16,10 +16,10 @@
 	}
 
 	static final LuaFunction getBinHandler(String op,Object o1,Object o2) throws LuaException {
-		LuaFunction f1 = Utils.getHandler(op,o1);
+		LuaFunction f1 = Utils.getHandlerFunction(op,o1);
 		if( f1 != null )
 			return f1;
-		return Utils.getHandler(op,o2);
+		return Utils.getHandlerFunction(op,o2);
 	}
 
 	static final Object arithmetic(LuaStateImpl lua,String op,Object o1,Object o2) throws LuaException {
--- a/src/luan/interp/FnCall.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/FnCall.java	Tue Dec 18 09:53:42 2012 +0000
@@ -23,7 +23,7 @@
 			LuaFunction fn = (LuaFunction)o;
 			return fn.call( lua, args.eval(lua) );
 		}
-		Object h = Utils.getHandlerObject("__call",o);
+		Object h = Utils.getHandler("__call",o);
 		if( h != null )
 			return call(lua,h);
 		throw new LuaException( "attempt to call a " + Lua.type(o) + " value" );
--- a/src/luan/interp/IndexExpr.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/IndexExpr.java	Tue Dec 18 09:53:42 2012 +0000
@@ -23,11 +23,11 @@
 			Object value = tbl.get(key);
 			if( value != null )
 				return value;
-			h = Utils.getHandlerObject("__index",t);
+			h = Utils.getHandler("__index",t);
 			if( h==null )
 				return null;
 		} else {
-			h = Utils.getHandlerObject("__index",t);
+			h = Utils.getHandler("__index",t);
 			if( h==null )
 				throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
 		}
--- a/src/luan/interp/LenExpr.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/LenExpr.java	Tue Dec 18 09:53:42 2012 +0000
@@ -19,7 +19,7 @@
 			String s = (String)o;
 			return new LuaNumber( s.length() );
 		}
-		LuaFunction fn = Utils.getHandler("__len",o);
+		LuaFunction fn = Utils.getHandlerFunction("__len",o);
 		if( fn != null )
 			return Utils.first(fn.call(lua,o));
 		if( o instanceof LuaTable ) {
--- a/src/luan/interp/LuaStateImpl.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/LuaStateImpl.java	Tue Dec 18 09:53:42 2012 +0000
@@ -1,8 +1,10 @@
 package luan.interp;
 
+import luan.Lua;
 import luan.LuaState;
 import luan.LuaTable;
 import luan.LuaFunction;
+import luan.LuaException;
 
 
 final class LuaStateImpl implements LuaState {
@@ -12,6 +14,14 @@
 		return global;
 	}
 
+	@Override public String toString(Object obj) throws LuaException {
+		LuaFunction fn = Utils.getHandlerFunction("__tostring",obj);
+		if( fn != null )
+			return Lua.checkString( Utils.first( fn.call(this,obj) ) );
+		if( obj == null )
+			return "nil";
+		return obj.toString();
+	}
 
 	private static class Frame {
 		final Frame previousFrame;
--- a/src/luan/interp/SetTableEntry.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/SetTableEntry.java	Tue Dec 18 09:53:42 2012 +0000
@@ -26,12 +26,12 @@
 			Object old = table.put(key,value);
 			if( old != null )
 				return;
-			h = Utils.getHandlerObject("__newindex",t);
+			h = Utils.getHandler("__newindex",t);
 			if( h==null )
 				return;
 			table.put(key,old);
 		} else {
-			h = Utils.getHandlerObject("__newindex",t);
+			h = Utils.getHandler("__newindex",t);
 			if( h==null )
 				throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
 		}
--- a/src/luan/interp/UnmExpr.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/UnmExpr.java	Tue Dec 18 09:53:42 2012 +0000
@@ -18,7 +18,7 @@
 		LuaNumber n = Lua.toNumber(o);
 		if( n != null )
 			return new LuaNumber( -n.value() );
-		LuaFunction fn = Utils.getHandler("__unm",o);
+		LuaFunction fn = Utils.getHandlerFunction("__unm",o);
 		if( fn != null )
 			return Utils.first(fn.call(lua,o));
 		throw new LuaException("attempt to perform arithmetic on a "+Lua.type(o)+" value");
--- a/src/luan/interp/Utils.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/interp/Utils.java	Tue Dec 18 09:53:42 2012 +0000
@@ -13,13 +13,13 @@
 		return a.length==0 ? null : a[0];
 	}
 
-	static final Object getHandlerObject(String op,Object obj) throws LuaException {
+	static final Object getHandler(String op,Object obj) throws LuaException {
 		LuaTable t = Lua.getMetatable(obj);
 		return t==null ? null : t.get(op);
 	}
 
-	static final LuaFunction getHandler(String op,Object obj) throws LuaException {
-		Object f = getHandlerObject(op,obj);
+	static final LuaFunction getHandlerFunction(String op,Object obj) throws LuaException {
+		Object f = getHandler(op,obj);
 		if( f == null )
 			return null;
 		return Lua.checkFunction(f);
--- a/src/luan/lib/BasicLib.java	Tue Dec 18 07:05:58 2012 +0000
+++ b/src/luan/lib/BasicLib.java	Tue Dec 18 09:53:42 2012 +0000
@@ -3,6 +3,7 @@
 import java.io.File;
 import java.io.Reader;
 import java.io.FileReader;
+import java.io.InputStreamReader;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.Iterator;
@@ -27,8 +28,14 @@
 		add( t, "load", LuaState.class, String.class );
 		add( t, "loadfile", LuaState.class, String.class );
 		add( t, "pairs", LuaTable.class );
-		add( t, "print", new Object[0].getClass() );
+		add( t, "print", LuaState.class, new Object[0].getClass() );
+		add( t, "rawequal", Object.class, Object.class );
+		add( t, "rawget", LuaTable.class, Object.class );
+		add( t, "rawlen", Object.class );
+		add( t, "rawset", LuaTable.class, Object.class, Object.class );
 		add( t, "setmetatable", LuaTable.class, LuaTable.class );
+		add( t, "tonumber", Object.class, Integer.class );
+		add( t, "tostring", LuaState.class, Object.class );
 		add( t, "type", Object.class );
 		t.put( "_VERSION", Lua.version );
 	}
@@ -41,11 +48,11 @@
 		}
 	}
 
-	public static void print(Object... args) {
+	public static void print(LuaState lua,Object... args) throws LuaException {
 		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.toString(args[i]) );
 		}
 		System.out.println();
 	}
@@ -81,7 +88,12 @@
 
 
 	public static LuaFunction loadfile(LuaState lua,String fileName) throws LuaException,IOException {
-		return load(lua,read(new File(fileName)));
+		String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName));
+		return load(lua,src);
+	}
+
+	public static Object[] dofile(LuaState lua,String fileName) throws LuaException,IOException {
+		return loadfile(lua,fileName).call(lua);
 	}
 
 	private static class TableIter {
@@ -144,4 +156,37 @@
 		table.setMetatable(metatable);
 		return table;
 	}
+
+	public static boolean rawequal(Object v1,Object v2) {
+		return v1 == v2 || v1 != null && v1.equals(v2);
+	}
+
+	public static Object rawget(LuaTable table,Object index) {
+		return table.get(index);
+	}
+
+	public static LuaTable rawset(LuaTable table,Object index,Object value) {
+		table.put(index,value);
+		return table;
+	}
+
+	public static int rawlen(Object v) throws LuaException {
+		if( v instanceof String ) {
+			String s = (String)v;
+			return s.length();
+		}
+		if( v instanceof LuaTable ) {
+			LuaTable t = (LuaTable)v;
+			return t.length();
+		}
+		throw new LuaException( "bad argument #1 to 'rawlen' (table or string expected)" );
+	}
+
+	public static LuaNumber tonumber(Object e,Integer base) {
+		return Lua.toNumber(e,base);
+	}
+
+	public static String tostring(LuaState lua,Object v) throws LuaException {
+		return lua.toString(v);
+	}
 }