changeset 116:1ff1c32417eb

more IoLib work and added init.luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@117 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 30 May 2014 04:55:37 +0000
parents eacf6ce1b47d
children e935581cf9fb
files src/luan/LuanState.java src/luan/init.luan src/luan/interp/LuanParser.java src/luan/lib/BasicLib.java src/luan/lib/IoLib.java src/luan/lib/Utils.java
diffstat 6 files changed, 173 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Thu May 29 09:26:44 2014 +0000
+++ b/src/luan/LuanState.java	Fri May 30 04:55:37 2014 +0000
@@ -115,6 +115,7 @@
 			luan.load(HtmlLib.NAME,HtmlLib.LOADER);
 			luan.load(BinaryLib.NAME,BinaryLib.LOADER);
 			luan.load(IoLib.NAME,IoLib.LOADER);
+			BasicLib.do_java_resource(luan,"luan/init.luan");
 			return luan;
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/init.luan	Fri May 30 04:55:37 2014 +0000
@@ -0,0 +1,15 @@
+--Io.stdout.write "this is init.luan\n"
+
+function _G.print(...)
+	local list = {}
+	for v in Basic.values(...) do
+		list[#list+1] = to_string(v)
+		list[#list+1] = '\t'
+	end
+	if #list == 0 then
+		Io.stdout.write( '\n' )
+	else
+		list[#list] = '\n'
+		Io.stdout.write( Table.unpack(list) )
+	end
+end
--- a/src/luan/interp/LuanParser.java	Thu May 29 09:26:44 2014 +0000
+++ b/src/luan/interp/LuanParser.java	Fri May 30 04:55:37 2014 +0000
@@ -820,7 +820,6 @@
 	private Var VarExt(boolean inParens,int start,Code exp1) throws ParseException {
 		parser.begin();
 		Expr exp2 = SubExpr(inParens);
-		exp2 = SubExpr(inParens);
 		if( exp2 != null )
 			return parser.success(indexVar(start,expr(exp1),exp2));
 		if( parser.match('.') ) {
--- a/src/luan/lib/BasicLib.java	Thu May 29 09:26:44 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri May 30 04:55:37 2014 +0000
@@ -41,7 +41,7 @@
 				add( global, "load", LuanState.class, String.class, String.class, Boolean.class );
 				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, "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 );
@@ -53,6 +53,7 @@
 				add( global, "to_string", LuanState.class, Object.class );
 				add( global, "type", Object.class );
 				global.put( "_VERSION", Luan.version );
+				add( module, "values", new Object[0].getClass() );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -63,10 +64,10 @@
 	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
 		t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) );
 	}
-
+/*
 	public static void print(LuanState luan,Object... args) throws LuanException {
 		LuanFunction write = (LuanFunction)luan.get("Io.stdout.write");
-		List list = new ArrayList();
+		List<Object> list = new ArrayList<Object>();
 		for( int i=0; i<args.length; i++ ) {
 			if( i > 0 )
 				list.add("\t");
@@ -75,7 +76,7 @@
 		list.add("\n");
 		write.call(luan,list.toArray());
 	}
-
+*/
 	public static String type(Object obj) {
 		return Luan.type(obj);
 	}
@@ -88,16 +89,34 @@
 	}
 
 
-	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException, IOException {
-		String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName));
-		return load(luan,src,fileName,false);
+	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
+		try {
+			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName));
+			return load(luan,src,fileName,false);
+		} catch(IOException e) {
+			throw luan.JAVA.exception(e);
+		}
 	}
 
-	public static Object do_file(LuanState luan,String fileName) throws LuanException, IOException {
+	public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException {
+		try {
+			String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text();
+			return load(luan,src,path,false);
+		} catch(IOException e) {
+			throw luan.JAVA.exception(e);
+		}
+	}
+
+	public static Object do_file(LuanState luan,String fileName) throws LuanException {
 		LuanFunction fn = load_file(luan,fileName);
 		return luan.JAVA.call(fn,null);
 	}
 
+	public static Object do_java_resource(LuanState luan,String path) throws LuanException {
+		LuanFunction fn = load_java_resource(luan,path);
+		return luan.JAVA.call(fn,null);
+	}
+
 	private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) {
 		return new LuanFunction() {
 			@Override public Object[] call(LuanState luan,Object[] args) {
@@ -219,4 +238,14 @@
 		};
 	}
 
+	public static LuanFunction values(final Object... args) throws LuanException {
+		return new LuanFunction() {
+			int i = 0;
+
+			@Override public Object call(LuanState luan,Object[] unused) {
+				return i < args.length ? args[i++] : null;
+			}
+		};
+	}
+
 }
--- a/src/luan/lib/IoLib.java	Thu May 29 09:26:44 2014 +0000
+++ b/src/luan/lib/IoLib.java	Fri May 30 04:55:37 2014 +0000
@@ -1,5 +1,6 @@
 package luan.lib;
 
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.io.Reader;
@@ -8,9 +9,12 @@
 import java.io.FileWriter;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
+import java.net.URL;
+import java.net.MalformedURLException;
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
@@ -27,6 +31,9 @@
 			LuanTable module = new LuanTable();
 			try {
 				add( module, "file", String.class );
+				add( module, "java_resource_to_url", String.class );
+				add( module, "url", String.class );
+				add( module, "java_resource", String.class );
 
 				LuanTable stdin = new LuanTable();
 				stdin.put( "read_text", new LuanJavaFunction(
@@ -38,6 +45,9 @@
 				stdin.put( "read_lines", new LuanJavaFunction(
 					IoLib.class.getMethod( "stdin_read_lines" ), null
 				) );
+				stdin.put( "read_blocks", new LuanJavaFunction(
+					IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null
+				) );
 				module.put( "stdin", stdin );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -92,6 +102,11 @@
 		public LuanFunction read_lines() throws IOException {
 			return lines(new BufferedReader(new FileReader(file)));
 		}
+
+		public LuanFunction read_blocks(Integer blockSize) throws IOException {
+			int n = blockSize!=null ? blockSize : Utils.bufSize;
+			return blocks(new FileInputStream(file),n);
+		}
 	}
 
 	public static LuanTable file(String name) {
@@ -116,6 +131,9 @@
 			tbl.put( "read_lines", new LuanJavaFunction(
 				LuanFile.class.getMethod( "read_lines" ), file
 			) );
+			tbl.put( "read_blocks", new LuanJavaFunction(
+				LuanFile.class.getMethod( "read_blocks", Integer.class ), file
+			) );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
@@ -123,18 +141,87 @@
 	}
 
 
-	public String stdin_read_text() throws IOException {
+	public static final class LuanUrl {
+		private final URL url;
+
+		LuanUrl(String s) throws MalformedURLException {
+			this.url = new URL(s);
+		}
+
+		public String read_text() throws IOException {
+			Reader in = new InputStreamReader(url.openStream());
+			String s = Utils.readAll(in);
+			in.close();
+			return s;
+		}
+
+		public byte[] read_binary() throws IOException {
+			InputStream in = url.openStream();
+			byte[] a = Utils.readAll(in);
+			in.close();
+			return a;
+		}
+
+		public LuanFunction read_lines() throws IOException {
+			return lines(new BufferedReader(new InputStreamReader(url.openStream())));
+		}
+
+		public LuanFunction read_blocks(Integer blockSize) throws IOException {
+			int n = blockSize!=null ? blockSize : Utils.bufSize;
+			return blocks(url.openStream(),n);
+		}
+	}
+
+	public static LuanTable url(String s) throws MalformedURLException {
+		LuanTable tbl = new LuanTable();
+		LuanUrl url = new LuanUrl(s);
+		try {
+			tbl.put( "read_text", new LuanJavaFunction(
+				LuanUrl.class.getMethod( "read_text" ), url
+			) );
+			tbl.put( "read_binary", new LuanJavaFunction(
+				LuanUrl.class.getMethod( "read_binary" ), url
+			) );
+			tbl.put( "read_lines", new LuanJavaFunction(
+				LuanUrl.class.getMethod( "read_lines" ), url
+			) );
+			tbl.put( "read_blocks", new LuanJavaFunction(
+				LuanUrl.class.getMethod( "read_blocks", Integer.class ), url
+			) );
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+		return tbl;
+	}
+
+
+
+	public static String stdin_read_text() throws IOException {
 		return Utils.readAll(new InputStreamReader(System.in));
 	}
 
-	public byte[] stdin_read_binary() throws IOException {
+	public static byte[] stdin_read_binary() throws IOException {
 		return Utils.readAll(System.in);
 	}
 
-	public LuanFunction stdin_read_lines() throws IOException {
+	public static LuanFunction stdin_read_lines() throws IOException {
 		return lines(new BufferedReader(new InputStreamReader(System.in)));
 	}
 
+	public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException {
+		int n = blockSize!=null ? blockSize : Utils.bufSize;
+		return blocks(System.in,n);
+	}
+
+	public static String java_resource_to_url(String path) throws IOException {
+		URL url = ClassLoader.getSystemResource(path);
+		return url==null ? null : url.toString();
+	}
+
+	public static LuanTable java_resource(String path) throws IOException {
+		return url(java_resource_to_url(path));
+	}
+
 
 	public interface LuanWriter {
 		public void write(LuanState luan,Object... args) throws LuanException, IOException;
@@ -204,11 +291,13 @@
 		return writer;
 	}
 
-	public static LuanFunction lines(final BufferedReader in) {
+	static LuanFunction lines(final BufferedReader in) {
 		return new LuanFunction() {
-			public Object call(LuanState luan,Object[] args) throws LuanException {
+			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
 				try {
-					if( args.length==1 && "close".equals(args[0]) ) {
+					if( args.length > 0 ) {
+						if( args.length > 1 || !"close".equals(args[0]) )
+							throw luan.JAVA.exception( "the only argument allowed is 'close'" );
 						in.close();
 						return null;
 					}
@@ -223,4 +312,28 @@
 		};
 	}
 
+	static LuanFunction blocks(final InputStream in,final int blockSize) {
+		return new LuanFunction() {
+			final byte[] a = new byte[blockSize];
+
+			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+				try {
+					if( args.length > 0 ) {
+						if( args.length > 1 || !"close".equals(args[0]) )
+							throw luan.JAVA.exception( "the only argument allowed is 'close'" );
+						in.close();
+						return null;
+					}
+					if( in.read(a) == -1 ) {
+						in.close();
+						return null;
+					}
+					return a;
+				} catch(IOException e) {
+					throw luan.JAVA.exception(e);
+				}
+			}
+		};
+	}
+
 }
--- a/src/luan/lib/Utils.java	Thu May 29 09:26:44 2014 +0000
+++ b/src/luan/lib/Utils.java	Fri May 30 04:55:37 2014 +0000
@@ -22,7 +22,7 @@
 public final class Utils {
 	private Utils() {}  // never
 
-	private static final int bufSize = 8192;
+	static final int bufSize = 8192;
 
 	public static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException {
 		if( v == null )