comparison src/luan/tools/CmdLine.java @ 48:64ecb7a3aad7

rename Lua to Luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 28 Dec 2012 03:29:12 +0000
parents a443637829c1
children 8ede219cd111
comparison
equal deleted inserted replaced
47:659c7139e903 48:64ecb7a3aad7
4 import java.util.Scanner; 4 import java.util.Scanner;
5 import luan.lib.BasicLib; 5 import luan.lib.BasicLib;
6 import luan.lib.JavaLib; 6 import luan.lib.JavaLib;
7 import luan.lib.StringLib; 7 import luan.lib.StringLib;
8 import luan.lib.TableLib; 8 import luan.lib.TableLib;
9 import luan.Lua; 9 import luan.Luan;
10 import luan.LuaState; 10 import luan.LuanState;
11 import luan.LuaFunction; 11 import luan.LuanFunction;
12 import luan.LuaTable; 12 import luan.LuanTable;
13 import luan.LuaException; 13 import luan.LuanException;
14 import luan.interp.LuaCompiler; 14 import luan.interp.LuanCompiler;
15 15
16 16
17 public class CmdLine { 17 public class CmdLine {
18 18
19 public static void main(String[] args) { 19 public static void main(String[] args) {
20 LuaState lua = LuaCompiler.newLuaState(); 20 LuanState lua = LuanCompiler.newLuaState();
21 BasicLib.register(lua); 21 BasicLib.register(lua);
22 JavaLib.register(lua); 22 JavaLib.register(lua);
23 StringLib.register(lua); 23 StringLib.register(lua);
24 TableLib.register(lua); 24 TableLib.register(lua);
25 BasicLib.make_standard(lua); 25 BasicLib.make_standard(lua);
41 } else if( arg.equals("-e") ) { 41 } else if( arg.equals("-e") ) {
42 if( ++i == args.length ) 42 if( ++i == args.length )
43 error("'-e' needs argument"); 43 error("'-e' needs argument");
44 String cmd = args[i]; 44 String cmd = args[i];
45 try { 45 try {
46 LuaFunction fn = BasicLib.load(lua,cmd,"(command line)"); 46 LuanFunction fn = BasicLib.load(lua,cmd,"(command line)");
47 lua.call(fn,null,null); 47 lua.call(fn,null,null);
48 } catch(LuaException e) { 48 } catch(LuanException e) {
49 System.err.println("command line error: "+e.getMessage()); 49 System.err.println("command line error: "+e.getMessage());
50 System.exit(-1); 50 System.exit(-1);
51 } 51 }
52 } else if( arg.equals("-") ) { 52 } else if( arg.equals("-") ) {
53 try { 53 try {
54 BasicLib.do_file(lua,"stdin"); 54 BasicLib.do_file(lua,"stdin");
55 } catch(LuaException e) { 55 } catch(LuanException e) {
56 System.err.println(e.getMessage()); 56 System.err.println(e.getMessage());
57 System.exit(-1); 57 System.exit(-1);
58 } 58 }
59 System.exit(0); 59 System.exit(0);
60 } else { 60 } else {
62 } 62 }
63 i++; 63 i++;
64 } 64 }
65 } 65 }
66 if( showVersion ) 66 if( showVersion )
67 System.out.println(Lua.version); 67 System.out.println(Luan.version);
68 if( i < args.length ) { 68 if( i < args.length ) {
69 String file = args[i++]; 69 String file = args[i++];
70 Object[] varArgs = new Object[args.length-1]; 70 Object[] varArgs = new Object[args.length-1];
71 System.arraycopy(args,1,varArgs,0,varArgs.length); 71 System.arraycopy(args,1,varArgs,0,varArgs.length);
72 LuaTable argsTable = new LuaTable(); 72 LuanTable argsTable = new LuanTable();
73 for( int j=0; j<args.length; j++ ) { 73 for( int j=0; j<args.length; j++ ) {
74 argsTable.put( j, args[j] ); 74 argsTable.put( j, args[j] );
75 } 75 }
76 lua.global().put("arg",argsTable); 76 lua.global().put("arg",argsTable);
77 try { 77 try {
78 LuaFunction fn = BasicLib.load_file(lua,file); 78 LuanFunction fn = BasicLib.load_file(lua,file);
79 lua.call(fn,null,null,varArgs); 79 lua.call(fn,null,null,varArgs);
80 } catch(LuaException e) { 80 } catch(LuanException e) {
81 // System.err.println("error: "+e.getMessage()); 81 // System.err.println("error: "+e.getMessage());
82 e.printStackTrace(); 82 e.printStackTrace();
83 System.exit(-1); 83 System.exit(-1);
84 } 84 }
85 } 85 }
99 System.err.println(" -- stop handling options"); 99 System.err.println(" -- stop handling options");
100 System.err.println(" - stop handling options and execute stdin"); 100 System.err.println(" - stop handling options and execute stdin");
101 System.exit(-1); 101 System.exit(-1);
102 } 102 }
103 103
104 static void interactive(LuaState lua) { 104 static void interactive(LuanState lua) {
105 while( true ) { 105 while( true ) {
106 System.out.print("> "); 106 System.out.print("> ");
107 String input = new Scanner(System.in).nextLine(); 107 String input = new Scanner(System.in).nextLine();
108 try { 108 try {
109 LuaFunction fn = BasicLib.load(lua,input,"stdin"); 109 LuanFunction fn = BasicLib.load(lua,input,"stdin");
110 Object[] rtn = lua.call(fn,null,null); 110 Object[] rtn = lua.call(fn,null,null);
111 if( rtn.length > 0 ) 111 if( rtn.length > 0 )
112 BasicLib.print(lua,rtn); 112 BasicLib.print(lua,rtn);
113 } catch(LuaException e) { 113 } catch(LuanException e) {
114 System.out.println(e.getMessage()); 114 System.out.println(e.getMessage());
115 } 115 }
116 } 116 }
117 } 117 }
118 } 118 }