changeset 30:8d8f4f5caef4

minor git-svn-id: https://luan-java.googlecode.com/svn/trunk@31 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 07 Dec 2012 20:22:19 +0000
parents 9bc66d09ea48
children 5cf15507d77e
files src/luan/CmdLine.java src/luan/interp/LuaCompiler.java src/luan/interp/LuaParser.java src/luan/lib/BasicLib.java
diffstat 4 files changed, 42 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/CmdLine.java	Fri Dec 07 09:33:31 2012 +0000
+++ b/src/luan/CmdLine.java	Fri Dec 07 20:22:19 2012 +0000
@@ -20,7 +20,7 @@
 			showVersion = true;
 		} else {
 			while( i < args.length ) {
-				String arg = args[i++];
+				String arg = args[i];
 				if( !arg.startsWith("-") || arg.equals("--") )
 					break;
 				if( arg.equals("-i") ) {
@@ -28,9 +28,9 @@
 				} else if( arg.equals("-v") ) {
 					showVersion = true;
 				} else if( arg.equals("-e") ) {
-					if( i == args.length )
+					if( ++i == args.length )
 						error("'-e' needs argument");
-					String cmd = args[i++];
+					String cmd = args[i];
 					try {
 						LuaFunction fn = BasicLib.load(lua,cmd);
 						fn.call(lua);
@@ -51,6 +51,7 @@
 				} else {
 					error("unrecognized option '"+arg+"'");
 				}
+				i++;
 			}
 		}
 		if( showVersion )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/LuaCompiler.java	Fri Dec 07 20:22:19 2012 +0000
@@ -0,0 +1,34 @@
+package luan.interp;
+
+import org.parboiled.Parboiled;
+import org.parboiled.errors.ErrorUtils;
+import org.parboiled.parserunners.ReportingParseRunner;
+import org.parboiled.parserunners.TracingParseRunner;
+import org.parboiled.support.ParsingResult;
+import luan.LuaFunction;
+import luan.LuaState;
+import luan.LuaException;
+
+
+public final class LuaCompiler {
+	private LuaCompiler() {}  // never
+
+	public static LuaFunction compile(LuaState lua,String src) throws LuaException {
+		LuaParser parser = Parboiled.createParser(LuaParser.class);
+		ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(src);
+//		ParsingResult<?> result = new TracingParseRunner(parser.Target()).run(src);
+		if( result.hasErrors() )
+			throw new LuaException( ErrorUtils.printParseErrors(result) );
+		Object resultValue = result.resultValue;
+		if( resultValue instanceof Expressions ) {
+			final Expressions expressions = (Expressions)resultValue;
+			return new LuaFunction() {
+				public Object[] call(LuaState lua,Object... args) throws LuaException {
+					return expressions.eval(lua);
+				}
+			};
+		}
+		Chunk chunk = (Chunk)resultValue;
+		return chunk.newClosure(lua);
+	}
+}
--- a/src/luan/interp/LuaParser.java	Fri Dec 07 09:33:31 2012 +0000
+++ b/src/luan/interp/LuaParser.java	Fri Dec 07 20:22:19 2012 +0000
@@ -21,7 +21,7 @@
 import luan.LuaState;
 
 
-public class LuaParser extends BaseParser<Object> {
+class LuaParser extends BaseParser<Object> {
 
 	static final class Frame {
 		final Frame parent;
@@ -105,7 +105,7 @@
 	}
 
 
-	public Rule Target() {
+	Rule Target() {
 		return Sequence(
 			Spaces(),
 			FirstOf(
--- a/src/luan/lib/BasicLib.java	Fri Dec 07 09:33:31 2012 +0000
+++ b/src/luan/lib/BasicLib.java	Fri Dec 07 20:22:19 2012 +0000
@@ -7,11 +7,6 @@
 import java.lang.reflect.Method;
 import java.util.Iterator;
 import java.util.Map;
-import org.parboiled.Parboiled;
-import org.parboiled.errors.ErrorUtils;
-import org.parboiled.parserunners.ReportingParseRunner;
-import org.parboiled.parserunners.TracingParseRunner;
-import org.parboiled.support.ParsingResult;
 import luan.Lua;
 import luan.LuaState;
 import luan.LuaTable;
@@ -19,9 +14,7 @@
 import luan.LuaFunction;
 import luan.LuaJavaFunction;
 import luan.LuaException;
-import luan.interp.LuaParser;
-import luan.interp.Expressions;
-import luan.interp.Chunk;
+import luan.interp.LuaCompiler;
 
 
 public class BasicLib {
@@ -58,22 +51,7 @@
 	}
 
 	public static LuaFunction load(LuaState lua,String ld) throws LuaException {
-		LuaParser parser = Parboiled.createParser(LuaParser.class);
-		ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(ld);
-//		ParsingResult<?> result = new TracingParseRunner(parser.Target()).run(ld);
-		if( result.hasErrors() )
-			throw new LuaException( ErrorUtils.printParseErrors(result) );
-		Object resultValue = result.resultValue;
-		if( resultValue instanceof Expressions ) {
-			final Expressions expressions = (Expressions)resultValue;
-			return new LuaFunction() {
-				public Object[] call(LuaState lua,Object... args) throws LuaException {
-					return expressions.eval(lua);
-				}
-			};
-		}
-		Chunk chunk = (Chunk)resultValue;
-		return chunk.newClosure(lua);
+		return LuaCompiler.compile(lua,ld);
 	}
 
 	public static String readAll(Reader in)