changeset 128:f0a4abe58593

minor git-svn-id: https://luan-java.googlecode.com/svn/trunk@129 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 08 Jun 2014 03:38:25 +0000
parents bcb3a09d0caf
children 486a0641bca4
files src/luan/LuanState.java src/luan/interp/LuanCompiler.java src/luan/interp/LuanParser.java src/luan/lib/BasicLib.java src/luan/lib/PackageLib.java src/luan/lib/init.luan
diffstat 6 files changed, 57 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/LuanState.java	Sun Jun 08 03:38:25 2014 +0000
@@ -120,8 +120,8 @@
 		}
 	}
 
-	public final Object eval(String cmd,String sourceName,boolean interactive) throws LuanException {
-		LuanFunction fn = BasicLib.load(this,cmd,sourceName,interactive);
+	public final Object eval(String cmd,String sourceName,boolean allowExpr) throws LuanException {
+		LuanFunction fn = BasicLib.load(this,cmd,sourceName,true,allowExpr);
 		return JAVA.call(fn,null);
 	}
 
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/interp/LuanCompiler.java
--- a/src/luan/interp/LuanCompiler.java	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/interp/LuanCompiler.java	Sun Jun 08 03:38:25 2014 +0000
@@ -13,7 +13,7 @@
 public final class LuanCompiler {
 	private LuanCompiler() {}  // never
 
-	public static LuanFunction compileModule(LuanState luan,LuanSource src) throws LuanException {
+	public static LuanFunction compileModule(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
 		UpValue.Getter envGetter = new UpValue.EnvGetter();
 		LuanParser parser = new LuanParser(src,envGetter);
 		for( Map.Entry<Object,Object> entry : luan.global() ) {
@@ -21,35 +21,36 @@
 			if( key instanceof String )
 				parser.addVar( (String)key, entry.getValue() );
 		}
+		FnDef fnDef = parse(luan,parser,allowExpr);
+		final Closure c = new Closure((LuanStateImpl)luan,fnDef);
+		return new LuanFunction() {
+			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+				Object rtn = c.call(luan,args);
+				if( rtn instanceof Object[] && ((Object[])rtn).length==0 )
+					rtn = c.upValues()[0].get();
+				return rtn;
+			}
+		};
+	}
+
+	public static LuanFunction compileGlobal(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
+		UpValue.Getter envGetter = UpValue.globalGetter;
+		LuanParser parser = new LuanParser(src,envGetter);
+		FnDef fnDef = parse(luan,parser,allowExpr);
+		return new Closure((LuanStateImpl)luan,fnDef);
+	}
+
+	private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException {
 		try {
-			FnDef fnDef = parser.RequiredModule();
-			final Closure c = new Closure((LuanStateImpl)luan,fnDef);
-			return new LuanFunction() {
-				@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-					Object rtn = c.call(luan,args);
-					if( rtn instanceof Object[] && ((Object[])rtn).length==0 )
-						rtn = c.upValues()[0].get();
-					return rtn;
-				}
-			};
+			if( allowExpr ) {
+				FnDef fnDef = parser.Expressions();
+				if( fnDef != null )
+					return fnDef;
+			}
+			return parser.RequiredModule();
 		} catch(ParseException e) {
 //e.printStackTrace();
-			LuanElement le = new LuanSource.CompilerElement(src);
-			throw luan.bit(le).exception( e.getFancyMessage() );
-		}
-	}
-
-	public static LuanFunction compileInteractive(LuanState luan,LuanSource src) throws LuanException {
-		UpValue.Getter envGetter = UpValue.globalGetter;
-		LuanParser parser = new LuanParser(src,envGetter);
-		try {
-			FnDef fnDef = parser.Expressions();
-			if( fnDef == null )
-				fnDef = parser.RequiredModule();
-			return new Closure((LuanStateImpl)luan,fnDef);
-		} catch(ParseException e) {
-//e.printStackTrace();
-			LuanElement le = new LuanSource.CompilerElement(src);
+			LuanElement le = new LuanSource.CompilerElement(parser.source);
 			throw luan.bit(le).exception( e.getFancyMessage() );
 		}
 	}
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/interp/LuanParser.java
--- a/src/luan/interp/LuanParser.java	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/interp/LuanParser.java	Sun Jun 08 03:38:25 2014 +0000
@@ -75,7 +75,7 @@
 	private static final String _ENV = "_ENV";
 	private static final UpValue.Getter[] NO_UP_VALUE_GETTERS = new UpValue.Getter[0];
 
-	private final LuanSource source;
+	final LuanSource source;
 	private Frame frame;
 	private final Parser parser;
 	private final boolean interactive;
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/lib/BasicLib.java
--- a/src/luan/lib/BasicLib.java	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Sun Jun 08 03:38:25 2014 +0000
@@ -40,7 +40,7 @@
 				add( global, "error", LuanState.class, Object.class );
 				add( global, "get_metatable", LuanState.class, Object.class );
 				add( global, "ipairs", LuanState.class, LuanTable.class );
-				add( global, "load", LuanState.class, String.class, String.class, Boolean.class );
+				add( global, "load", LuanState.class, String.class, String.class, Boolean.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() );
@@ -83,17 +83,21 @@
 		return Luan.type(obj);
 	}
 
-	public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean interactive) throws LuanException {
-		if( interactive!=null && interactive )
-			return LuanCompiler.compileInteractive(luan,new LuanSource(sourceName,text));
+	public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean useGlobal,Boolean allowExpr)
+		throws LuanException
+	{
+		if( allowExpr==null )
+			allowExpr = false;
+		if( useGlobal!=null && useGlobal )
+			return LuanCompiler.compileGlobal(luan,new LuanSource(sourceName,text),allowExpr);
 		else
-			return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text));
+			return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text),allowExpr);
 	}
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
 			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text();
-			return load(luan,src,fileName,false);
+			return load(luan,src,fileName,false,false);
 		} catch(IOException e) {
 			throw luan.JAVA.exception(e);
 		}
@@ -102,7 +106,7 @@
 	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);
+			return load(luan,src,path,false,false);
 		} catch(IOException e) {
 			throw luan.JAVA.exception(e);
 		}
@@ -244,7 +248,9 @@
 			int i = 0;
 
 			@Override public Object call(LuanState luan,Object[] unused) {
-				return i < args.length ? args[i++] : null;
+				if( ++i > args.length )
+					return LuanFunction.NOTHING;
+				return new Object[]{i,args[i-1]};
 			}
 		};
 	}
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/lib/PackageLib.java
--- a/src/luan/lib/PackageLib.java	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/lib/PackageLib.java	Sun Jun 08 03:38:25 2014 +0000
@@ -119,7 +119,7 @@
 			String urlStr = (String)args[1];
 			try {
 				String src = new IoLib.LuanUrl(urlStr).read_text();
-				LuanFunction fn = BasicLib.load(luan,src,urlStr,false);
+				LuanFunction fn = BasicLib.load(luan,src,urlStr,false,false);
 				return fn.call(luan,args);
 			} catch(IOException e) {
 				throw luan.JAVA.exception(e);
diff -r bcb3a09d0caf -r f0a4abe58593 src/luan/lib/init.luan
--- a/src/luan/lib/init.luan	Sat Jun 07 01:47:00 2014 +0000
+++ b/src/luan/lib/init.luan	Sun Jun 08 03:38:25 2014 +0000
@@ -2,7 +2,7 @@
 
 local function print(...)
 	local list = {}
-	for v in Basic.values(...) do
+	for _,v in Basic.values(...) do
 		list[#list+1] = to_string(v)
 		list[#list+1] = '\t'
 	end
@@ -27,10 +27,14 @@
 		return Io.read_console_line(prompt)
 	end
 	for line in console do
-		local fn = load(line,"stdin",true)
-		local result = Table.pack( fn() )
-		if result.n > 0 then
-			print( Table.unpack(result,1,result.n) )
+		try
+			local fn = load(line,"stdin",true,true)
+			local result = Table.pack( fn() )
+			if result.n > 0 then
+				print( Table.unpack(result,1,result.n) )
+			end
+		catch e do
+			print(e)
 		end
 	end
 end
@@ -75,14 +79,14 @@
 					return
 				end
 				local cmd = args[i]
-				local fn = load(cmd,"(command line)",true)
+				local fn = load(cmd,"(command line)",true,true)
 				local result = Table.pack( fn() )
 				if result.n > 0 then
 					print( Table.unpack(result,1,result.n) )
 				end
 			elseif arg == "-" then
 				local src = Io.stdin.read_text()
-				local fn = load(src,"stdin",false)
+				local fn = load(src,"stdin")
 				fn()
 				return
 			else