comparison src/luan/tools/CmdLine.java @ 108:3c404a296995

make Package module more standard; return _ENV by default; add "import" statement; git-svn-id: https://luan-java.googlecode.com/svn/trunk@109 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 03:21:54 +0000
parents 754e6030c029
children 2428ecfed375
comparison
equal deleted inserted replaced
107:dbf459397217 108:3c404a296995
12 12
13 public class CmdLine { 13 public class CmdLine {
14 14
15 public static void main(String[] args) { 15 public static void main(String[] args) {
16 LuanState luan = LuanState.newStandard(); 16 LuanState luan = LuanState.newStandard();
17 LuanTable env;
18 try {
19 env = luan.newEnvironment();
20 } catch(LuanException e) {
21 System.err.println("command line error: "+e.getMessage());
22 System.exit(-1);
23 throw new RuntimeException(); // never
24 }
25 boolean interactive = false; 17 boolean interactive = false;
26 boolean showVersion = false; 18 boolean showVersion = false;
27 int i = 0; 19 int i = 0;
28 if( args.length == 0 ) { 20 if( args.length == 0 ) {
29 interactive = true; 21 interactive = true;
40 } else if( arg.equals("-e") ) { 32 } else if( arg.equals("-e") ) {
41 if( ++i == args.length ) 33 if( ++i == args.length )
42 error("'-e' needs argument"); 34 error("'-e' needs argument");
43 String cmd = args[i]; 35 String cmd = args[i];
44 try { 36 try {
45 LuanFunction fn = BasicLib.load(luan,cmd,"(command line)",env); 37 LuanFunction fn = BasicLib.load(luan,cmd,"(command line)",false);
46 luan.JAVA.call(fn,null); 38 luan.JAVA.call(fn,null);
47 } catch(LuanException e) { 39 } catch(LuanException e) {
48 System.err.println("command line error: "+e.getMessage()); 40 System.err.println("command line error: "+e.getMessage());
49 System.exit(-1); 41 System.exit(-1);
50 } 42 }
51 } else if( arg.equals("-") ) { 43 } else if( arg.equals("-") ) {
52 try { 44 try {
53 BasicLib.do_file(luan,"stdin",env); 45 BasicLib.do_file(luan,"stdin");
54 } catch(LuanException e) { 46 } catch(LuanException e) {
55 System.err.println(e.getMessage()); 47 System.err.println(e.getMessage());
56 System.exit(-1); 48 System.exit(-1);
57 } 49 }
58 System.exit(0); 50 System.exit(0);
70 System.arraycopy(args,1,varArgs,0,varArgs.length); 62 System.arraycopy(args,1,varArgs,0,varArgs.length);
71 LuanTable argsTable = new LuanTable(); 63 LuanTable argsTable = new LuanTable();
72 for( int j=0; j<args.length; j++ ) { 64 for( int j=0; j<args.length; j++ ) {
73 argsTable.put( j, args[j] ); 65 argsTable.put( j, args[j] );
74 } 66 }
75 env.put("arg",argsTable); 67 luan.global().put("arg",argsTable);
76 try { 68 try {
77 LuanFunction fn = BasicLib.load_file(luan,file,env); 69 LuanFunction fn = BasicLib.load_file(luan,file);
78 luan.JAVA.call(fn,null,varArgs); 70 luan.JAVA.call(fn,null,varArgs);
79 } catch(LuanException e) { 71 } catch(LuanException e) {
80 // System.err.println("error: "+e.getMessage()); 72 // System.err.println("error: "+e.getMessage());
81 e.printStackTrace(); 73 e.printStackTrace();
82 System.exit(-1); 74 System.exit(-1);
83 } 75 }
84 } 76 }
85 if( interactive ) 77 if( interactive )
86 interactive(luan,env); 78 interactive(luan);
87 } 79 }
88 80
89 private static void error(String msg) { 81 private static void error(String msg) {
90 System.err.println(msg); 82 System.err.println(msg);
91 System.err.println("usage: java luan.CmdLine [options] [script [args]]"); 83 System.err.println("usage: java luan.CmdLine [options] [script [args]]");
98 System.err.println(" -- stop handling options"); 90 System.err.println(" -- stop handling options");
99 System.err.println(" - stop handling options and execute stdin"); 91 System.err.println(" - stop handling options and execute stdin");
100 System.exit(-1); 92 System.exit(-1);
101 } 93 }
102 94
103 static void interactive(LuanState luan,LuanTable env) { 95 static void interactive(LuanState luan) {
104 try { 96 try {
105 ConsoleReader console = new ConsoleReader(); 97 ConsoleReader console = new ConsoleReader();
106 console.setDefaultPrompt("> "); 98 console.setDefaultPrompt("> ");
107 while( true ) { 99 while( true ) {
108 String input = console.readLine(); 100 String input = console.readLine();
109 if( input==null ) 101 if( input==null )
110 break; 102 break;
111 try { 103 try {
112 Object[] rtn = luan.eval(input,"stdin",env); 104 Object[] rtn = luan.eval(input,"stdin",true);
113 if( rtn.length > 0 ) 105 if( rtn.length > 0 )
114 BasicLib.print(luan,rtn); 106 BasicLib.print(luan,rtn);
115 } catch(LuanException e) { 107 } catch(LuanException e) {
116 System.out.println(e.getMessage()); 108 System.out.println(e.getMessage());
117 } 109 }