changeset 694:b620b8e1010f

remove Luan.load() allow_expression param and add Luan.eval()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Apr 2016 20:56:08 -0600
parents ca169567ce07
children 5042e487d717
files core/src/luan/Luan.java core/src/luan/LuanState.java core/src/luan/impl/LuanCompiler.java core/src/luan/impl/LuanParser.java core/src/luan/impl/ParseException.java core/src/luan/impl/Parser.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/Io.luan core/src/luan/modules/Luan.luan core/src/luan/modules/PackageLuan.java core/src/luan/modules/RpcLuan.java http/src/luan/modules/http/Shell_mod.luan
diffstat 12 files changed, 66 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/Luan.java
--- a/core/src/luan/Luan.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/Luan.java	Wed Apr 20 20:56:08 2016 -0600
@@ -2,6 +2,7 @@
 
 import java.util.List;
 import luan.modules.BasicLuan;
+import luan.impl.LuanCompiler;
 
 
 public final class Luan {
@@ -139,6 +140,17 @@
 		return checkFunction(f);
 	}
 
+	public static LuanFunction load(String text,String sourceName,LuanTable env)
+		throws LuanException
+	{
+		return LuanCompiler.compile(text,sourceName,env);
+	}
+
+	public static LuanFunction load(String text,String sourceName)
+		throws LuanException
+	{
+		return load(text,sourceName,null);
+	}
 
 
 	private Luan() {}  // never
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/LuanState.java	Wed Apr 20 20:56:08 2016 -0600
@@ -52,7 +52,7 @@
 		}
 		onClose.clear();
 	}
-
+/*
 	public final Object eval(String cmd) throws LuanException {
 		return eval(cmd,new LuanTable());
 	}
@@ -61,7 +61,7 @@
 		LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
 		return fn.call(this);
 	}
-
+*/
 
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/impl/LuanCompiler.java
--- a/core/src/luan/impl/LuanCompiler.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/impl/LuanCompiler.java	Wed Apr 20 20:56:08 2016 -0600
@@ -14,12 +14,18 @@
 public final class LuanCompiler {
 	private LuanCompiler() {}  // never
 
-	public static LuanFunction compile(String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException {
-		LuanParser parser = new LuanParser(sourceName,sourceText);
+	public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException {
+		LuanParser parser = new LuanParser(sourceText,sourceName);
 		parser.addVar( "java" );
 		parser.addVar( "require" );
 		if( env != null )  parser.addVar( "_ENV" );
-		Class fnClass = parse(parser,allowExpr);
+		Class fnClass;
+		try {
+			fnClass = parser.RequiredModule();
+		} catch(ParseException e) {
+//e.printStackTrace();
+			throw new LuanException( e.getFancyMessage() );
+		}
 		LuanJava java;
 		if( env == null ) {
 			java = new LuanJava();
@@ -48,20 +54,6 @@
 		return closure;
 	}
 
-	private static Class parse(LuanParser parser,boolean allowExpr) throws LuanException {
-		try {
-			if( allowExpr ) {
-				Class fnClass = parser.Expression();
-				if( fnClass != null )
-					return fnClass;
-			}
-			return parser.RequiredModule();
-		} catch(ParseException e) {
-//e.printStackTrace();
-			throw new LuanException( e.getFancyMessage() );
-		}
-	}
-
 	public static String toJava(String sourceName,String sourceText) throws LuanException {
 		LuanParser parser = new LuanParser(sourceName,sourceText);
 		parser.addVar( "java" );
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Wed Apr 20 20:56:08 2016 -0600
@@ -187,9 +187,9 @@
 	private final Parser parser;
 	private final Stmts top;
 
-	LuanParser(String sourceName,String sourceText) {
+	LuanParser(String sourceText,String sourceName) {
 		this.frame = new Frame();
-		this.parser = new Parser(sourceName,sourceText);
+		this.parser = new Parser(sourceText,sourceName);
 		this.top = new Stmts();
 	}
 
@@ -247,7 +247,7 @@
 	private Expr newFnExp(Stmts stmt,String name) {
 		return toFnExp( stmt, frame.upValueSymbols, name );
 	}
-
+/*
 	Class Expression() throws ParseException {
 		Spaces();
 		parser.begin();
@@ -261,7 +261,7 @@
 		}
 		return parser.failure(null);
 	}
-
+*/
 	Class RequiredModule() throws ParseException {
 		GetRequiredModule();
 		return newFnClass(top);
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/impl/ParseException.java
--- a/core/src/luan/impl/ParseException.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/impl/ParseException.java	Wed Apr 20 20:56:08 2016 -0600
@@ -8,11 +8,11 @@
 	public final int iCurrent;
 	public final int iHigh;
 
-	ParseException(String msg,String sourceName,String text,int iCurrent,int iHigh) {
+	ParseException(String msg,String text,String sourceName,int iCurrent,int iHigh) {
 		super(msg);
 //		this.src = src;
+		this.text = text;
 		this.sourceName = sourceName;
-		this.text = text;
 		this.iCurrent = iCurrent;
 		this.iHigh = iHigh;
 //System.out.println("iCurrent = "+iCurrent);
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/impl/Parser.java
--- a/core/src/luan/impl/Parser.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/impl/Parser.java	Wed Apr 20 20:56:08 2016 -0600
@@ -9,17 +9,17 @@
 	}
 
 //	private final LuanSource src;
+	public final String text;
 	public final String sourceName;
-	public final String text;
 	private final int len;
 	private Frame[] stack = new Frame[256];
 	private int frame = 0;
 	private int iHigh;
 
-	public Parser(String sourceName,String text) {
+	public Parser(String text,String sourceName) {
 //		this.src = src;
+		this.text = text;
 		this.sourceName = sourceName;
-		this.text = text;
 		this.len = text.length();
 		stack[0] = new Frame();
 	}
@@ -78,7 +78,7 @@
 	}
 
 	public ParseException exception(String msg) {
-		return new ParseException(msg,sourceName,text,i(),iHigh);
+		return new ParseException(msg,text,sourceName,i(),iHigh);
 	}
 
 	public ParseException exception() {
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Wed Apr 20 20:56:08 2016 -0600
@@ -15,7 +15,6 @@
 import luan.LuanException;
 import luan.LuanMethod;
 import luan.LuanMeta;
-import luan.impl.LuanCompiler;
 
 
 public final class BasicLuan {
@@ -24,12 +23,12 @@
 		return Luan.type(obj);
 	}
 
-	public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env,Boolean allowExpr)
+	public static LuanFunction load(String text,String sourceName,LuanTable env)
 		throws LuanException
 	{
-		if( allowExpr==null )
-			allowExpr = false;
-		return LuanCompiler.compile(sourceName,text,env,allowExpr);
+		Utils.checkNotNull(text);
+		Utils.checkNotNull(sourceName,1);
+		return Luan.load(text,sourceName,env);
 	}
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
@@ -41,7 +40,7 @@
 		String src = PackageLuan.read(luan,fileName);
 		if( src == null )
 			throw new LuanException("file '"+fileName+"' not found" );
-		return load(luan,src,fileName,null,false);
+		return load(src,fileName,null);
 	}
 
 	public static LuanFunction pairs(final LuanState luan,final LuanTable t) throws LuanException {
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/modules/Io.luan	Wed Apr 20 20:56:08 2016 -0600
@@ -159,12 +159,20 @@
 	for line in console do
 		try {
 			function()
-				local fn = load(line,"stdin",env,true)
+				local fn
+				try {
+					function()
+						fn = load("return "..line,"stdin",env)
+					end
+					catch = function(e)
+						fn = load(line,"stdin",env)
+					end
+				}
 				M.print( fn() )
-			end;
+			end
 			catch = function(e)
 				M.print(e)
-			end;
+			end
 		}
 	end
 end
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/modules/Luan.luan
--- a/core/src/luan/modules/Luan.luan	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/modules/Luan.luan	Wed Apr 20 20:56:08 2016 -0600
@@ -43,4 +43,8 @@
 	return v or M.error(message or "assertion failed!")
 end
 
+function M.eval(s,source_name)
+	return M.load( "return "..s, source_name or "eval" )()
+end
+
 return M
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/modules/PackageLuan.java
--- a/core/src/luan/modules/PackageLuan.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/modules/PackageLuan.java	Wed Apr 20 20:56:08 2016 -0600
@@ -48,7 +48,7 @@
 				String src = read(luan,modName);
 				if( src == null )
 					return null;
-				LuanFunction loader = BasicLuan.load(luan,src,modName,null,false);
+				LuanFunction loader = Luan.load(src,modName);
 				mod = Luan.first(
 					loader.call(luan,new Object[]{modName})
 				);
diff -r ca169567ce07 -r b620b8e1010f core/src/luan/modules/RpcLuan.java
--- a/core/src/luan/modules/RpcLuan.java	Tue Apr 19 15:54:11 2016 -0600
+++ b/core/src/luan/modules/RpcLuan.java	Wed Apr 20 20:56:08 2016 -0600
@@ -139,7 +139,7 @@
 			return readBinary(in,readInt(in));
 		case TABLE:
 			String s = readString(in);
-			LuanFunction fn = BasicLuan.load(luan,s,"rpc-reader",null,true);
+			LuanFunction fn = Luan.load("return "+s,"rpc-reader");
 			return fn.call(luan);
 		default:
 			throw new LuanException( "invalid type: " + type );
diff -r ca169567ce07 -r b620b8e1010f http/src/luan/modules/http/Shell_mod.luan
--- a/http/src/luan/modules/http/Shell_mod.luan	Tue Apr 19 15:54:11 2016 -0600
+++ b/http/src/luan/modules/http/Shell_mod.luan	Wed Apr 20 20:56:08 2016 -0600
@@ -30,13 +30,21 @@
 			print( "% "..cmd )
 			try {
 				function()
-					local line = load(cmd,"<web_shell>",M.env,true)
+					local line
+					try {
+						function()
+							line = load("return "..cmd,"<web_shell>",M.env)
+						end
+						catch = function(e)
+							line = load(cmd,"<web_shell>",M.env)
+						end
+					}
 					print( line() )
-				end;
+				end
 				catch = function(e)
 					Io.print_to(Io.stderr,e)
 					print(e)
-				end;
+				end
 			}
 		end
 	end