Mercurial Hosting > luan
changeset 120:8d7730a5e3b4
move standalone logic from Java to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@121 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 02 Jun 2014 04:43:45 +0000 |
parents | f1bf2890d80f |
children | cba7ed3f06cc |
files | src/luan/interp/LuanParser.java src/luan/lib/init.luan src/luan/tools/CmdLine.java |
diffstat | 3 files changed, 79 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/interp/LuanParser.java Sun Jun 01 09:38:40 2014 +0000 +++ b/src/luan/interp/LuanParser.java Mon Jun 02 04:43:45 2014 +0000 @@ -1210,6 +1210,7 @@ int nEquals = parser.currentIndex() - start; if( !parser.match('[') ) return parser.failure(null); + EndOfLine(); start = parser.currentIndex(); while( !LongBracketsEnd(nEquals) ) { if( !parser.anyChar() )
--- a/src/luan/lib/init.luan Sun Jun 01 09:38:40 2014 +0000 +++ b/src/luan/lib/init.luan Mon Jun 02 04:43:45 2014 +0000 @@ -35,3 +35,74 @@ end end + +local standalone_usage = [=[ +usage: java luan.CmdLine [options] [script [args]] +Available options are: + -e stat execute string 'stat' + -i enter interactive mode after executing 'script' + -v show version information + -- stop handling options + - stop handling options and execute stdin +]=] + +local function standalone_error(msg) + Io.stderr.write( msg, "\n", standalone_usage ) +end + +function Basic.standalone(...) + local args = {...} + local interactive = false + local showVersion = false + local i = 1 + if #args == 0 then + interactive = true + showVersion = true + else + while i <= #args do + local arg = args[i] + if arg.sub(1,1) ~= "-" or arg == "--" then + break + end + if arg == "-i" then + interactive = true + elseif arg == "-v" then + showVersion = true + elseif arg == "-e" then + i = i + 1 + if i == #args then + standalone_error "'-e' needs argument" + return + end + local cmd = args[i] + local fn = load(cmd,"(command line)",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) + fn() + return + else + standalone_error( "unrecognized option '"..arg.."'" ) + return + end + i = i + 1 + end + end + if showVersion then print(_VERSION) end + if i < #args then + local file = args[i] + _G.arg = {} + for j,v in ipairs(args) do + _G.arg[j-i] = v + end + local fn = load_file(file) + fn( Table.unpack(_G.arg) ) + end + if interactive then + Debug.debug("> ") + end +end
--- a/src/luan/tools/CmdLine.java Sun Jun 01 09:38:40 2014 +0000 +++ b/src/luan/tools/CmdLine.java Mon Jun 02 04:43:45 2014 +0000 @@ -14,99 +14,14 @@ public static void main(String[] args) { LuanState luan = LuanState.newStandard(); - boolean interactive = false; - boolean showVersion = false; - int i = 0; - if( args.length == 0 ) { - interactive = true; - showVersion = true; - } else { - while( i < args.length ) { - String arg = args[i]; - if( !arg.startsWith("-") || arg.equals("--") ) - break; - if( arg.equals("-i") ) { - interactive = true; - } else if( arg.equals("-v") ) { - showVersion = true; - } else if( arg.equals("-e") ) { - if( ++i == args.length ) - error("'-e' needs argument"); - String cmd = args[i]; - try { - LuanFunction fn = BasicLib.load(luan,cmd,"(command line)",false); - luan.JAVA.call(fn,null); - } catch(LuanException e) { - System.err.println("command line error: "+e.getMessage()); - System.exit(-1); - } - } else if( arg.equals("-") ) { - try { - BasicLib.do_file(luan,"stdin"); - } catch(LuanException e) { - System.err.println(e.getMessage()); - System.exit(-1); - } - System.exit(0); - } else { - error("unrecognized option '"+arg+"'"); - } - i++; - } + try { + LuanFunction standalone = (LuanFunction)luan.get("Basic.standalone"); + luan.JAVA.call(standalone,"standalone",args); + } catch(LuanException e) { + System.err.println(e.getMessage()); + System.exit(-1); } - if( showVersion ) - System.out.println(Luan.version); - if( i < args.length ) { - String file = args[i++]; - Object[] varArgs = new Object[args.length-1]; - System.arraycopy(args,1,varArgs,0,varArgs.length); - LuanTable argsTable = new LuanTable(); - for( int j=0; j<args.length; j++ ) { - argsTable.put( j, args[j] ); - } - luan.global().put("arg",argsTable); - try { - LuanFunction fn = BasicLib.load_file(luan,file); - luan.JAVA.call(fn,null,varArgs); - } catch(LuanException e) { -// System.err.println("error: "+e.getMessage()); - e.printStackTrace(); - System.exit(-1); - } - } - if( interactive ) - interactive(luan); - } - - private static void error(String msg) { - System.err.println(msg); - System.err.println("usage: java luan.CmdLine [options] [script [args]]"); - System.err.println("Available options are:"); - System.err.println(" -e stat execute string 'stat'"); - System.err.println(" -i enter interactive mode after executing 'script'"); -// System.err.println(" -l name require library 'name'"); - System.err.println(" -v show version information"); -// System.err.println(" -E ignore environment variables"); - System.err.println(" -- stop handling options"); - System.err.println(" - stop handling options and execute stdin"); - System.exit(-1); - } - - static void interactive(LuanState luan) { - Console console = System.console(); - LuanFunction print = (LuanFunction)luan.global().get("print"); - while( true ) { - String input = console.readLine("> "); - if( input==null ) - break; - try { - Object[] rtn = Luan.array(luan.eval(input,"stdin",true)); - if( rtn.length > 0 ) - luan.JAVA.call(print,"print",rtn); - } catch(LuanException e) { - System.out.println(e.getMessage()); - } - } + System.exit(0); } }