Mercurial Hosting > luan
comparison src/luan/tools/CmdLine.java @ 40:e3624b7cd603
implement stack trace
git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 21 Dec 2012 10:45:54 +0000 |
parents | e5bcb1eeafc1 |
children | 786699c78837 |
comparison
equal
deleted
inserted
replaced
39:e5bcb1eeafc1 | 40:e3624b7cd603 |
---|---|
13 import luan.interp.LuaCompiler; | 13 import luan.interp.LuaCompiler; |
14 | 14 |
15 | 15 |
16 public class CmdLine { | 16 public class CmdLine { |
17 | 17 |
18 public static void main(String[] args) throws Exception { | 18 public static void main(String[] args) { |
19 LuaState lua = LuaCompiler.newLuaState(); | 19 LuaState lua = LuaCompiler.newLuaState(); |
20 BasicLib.register(lua); | 20 BasicLib.register(lua); |
21 JavaLib.register(lua); | 21 JavaLib.register(lua); |
22 BasicLib.make_standard(lua); | |
22 boolean interactive = false; | 23 boolean interactive = false; |
23 boolean showVersion = false; | 24 boolean showVersion = false; |
24 int i = 0; | 25 int i = 0; |
25 if( args.length == 0 ) { | 26 if( args.length == 0 ) { |
26 interactive = true; | 27 interactive = true; |
37 } else if( arg.equals("-e") ) { | 38 } else if( arg.equals("-e") ) { |
38 if( ++i == args.length ) | 39 if( ++i == args.length ) |
39 error("'-e' needs argument"); | 40 error("'-e' needs argument"); |
40 String cmd = args[i]; | 41 String cmd = args[i]; |
41 try { | 42 try { |
42 LuaFunction fn = BasicLib.load(lua,cmd); | 43 LuaFunction fn = BasicLib.load(lua,cmd,"(command line)"); |
43 fn.call(lua); | 44 lua.call(fn,null,null); |
44 } catch(LuaException e) { | 45 } catch(LuaException e) { |
45 System.err.println("command line error: "+e.getMessage()); | 46 System.err.println("command line error: "+e.getMessage()); |
46 System.exit(-1); | 47 System.exit(-1); |
47 } | 48 } |
48 } else if( arg.equals("-") ) { | 49 } else if( arg.equals("-") ) { |
49 try { | 50 try { |
50 BasicLib.do_file(lua,null); | 51 BasicLib.do_file(lua,"stdin"); |
51 } catch(LuaException e) { | 52 } catch(LuaException e) { |
52 System.err.println(e.getMessage()); | 53 System.err.println(e.getMessage()); |
53 System.exit(-1); | 54 System.exit(-1); |
54 } | 55 } |
55 System.exit(0); | 56 System.exit(0); |
70 argsTable.put( new LuaNumber(j), args[j] ); | 71 argsTable.put( new LuaNumber(j), args[j] ); |
71 } | 72 } |
72 lua.global().put("arg",argsTable); | 73 lua.global().put("arg",argsTable); |
73 try { | 74 try { |
74 LuaFunction fn = BasicLib.load_file(lua,file); | 75 LuaFunction fn = BasicLib.load_file(lua,file); |
75 fn.call(lua,varArgs); | 76 lua.call(fn,null,null,varArgs); |
76 } catch(LuaException e) { | 77 } catch(LuaException e) { |
77 // System.err.println(e.getMessage()); | 78 System.err.println(e); |
78 e.printStackTrace(); | 79 // e.printStackTrace(); |
79 System.exit(-1); | 80 System.exit(-1); |
80 } | 81 } |
81 } | 82 } |
82 if( interactive ) | 83 if( interactive ) |
83 interactive(lua); | 84 interactive(lua); |
100 static void interactive(LuaState lua) { | 101 static void interactive(LuaState lua) { |
101 while( true ) { | 102 while( true ) { |
102 System.out.print("> "); | 103 System.out.print("> "); |
103 String input = new Scanner(System.in).nextLine(); | 104 String input = new Scanner(System.in).nextLine(); |
104 try { | 105 try { |
105 LuaFunction fn = BasicLib.load(lua,input); | 106 LuaFunction fn = BasicLib.load(lua,input,"stdin"); |
106 Object[] rtn = fn.call(lua); | 107 Object[] rtn = lua.call(fn,null,null); |
107 if( rtn.length > 0 ) | 108 if( rtn.length > 0 ) |
108 BasicLib.print(lua,rtn); | 109 BasicLib.print(lua,rtn); |
109 } catch(LuaException e) { | 110 } catch(LuaException e) { |
110 System.out.println(e.getMessage()); | 111 System.out.println(e.getMessage()); |
111 } | 112 } |