Mercurial Hosting > luan
comparison src/luan/tools/CmdLine.java @ 86:6db8f286fa6c
_ENV is per module, not global
git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 08:03:51 +0000 |
parents | 4bf3d0c0b6b9 |
children | 6ca02b188dba |
comparison
equal
deleted
inserted
replaced
85:b2551f00bc51 | 86:6db8f286fa6c |
---|---|
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 BasicLib.make_standard(luan); | 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 } | |
18 boolean interactive = false; | 25 boolean interactive = false; |
19 boolean showVersion = false; | 26 boolean showVersion = false; |
20 int i = 0; | 27 int i = 0; |
21 if( args.length == 0 ) { | 28 if( args.length == 0 ) { |
22 interactive = true; | 29 interactive = true; |
33 } else if( arg.equals("-e") ) { | 40 } else if( arg.equals("-e") ) { |
34 if( ++i == args.length ) | 41 if( ++i == args.length ) |
35 error("'-e' needs argument"); | 42 error("'-e' needs argument"); |
36 String cmd = args[i]; | 43 String cmd = args[i]; |
37 try { | 44 try { |
38 LuanFunction fn = BasicLib.load(luan,cmd,"(command line)"); | 45 LuanFunction fn = BasicLib.load(luan,cmd,"(command line)",env); |
39 luan.call(fn,null,null); | 46 luan.call(fn,null,null); |
40 } catch(LuanException e) { | 47 } catch(LuanException e) { |
41 System.err.println("command line error: "+e.getMessage()); | 48 System.err.println("command line error: "+e.getMessage()); |
42 System.exit(-1); | 49 System.exit(-1); |
43 } | 50 } |
44 } else if( arg.equals("-") ) { | 51 } else if( arg.equals("-") ) { |
45 try { | 52 try { |
46 BasicLib.do_file(luan,"stdin"); | 53 BasicLib.do_file(luan,"stdin",env); |
47 } catch(LuanException e) { | 54 } catch(LuanException e) { |
48 System.err.println(e.getMessage()); | 55 System.err.println(e.getMessage()); |
49 System.exit(-1); | 56 System.exit(-1); |
50 } | 57 } |
51 System.exit(0); | 58 System.exit(0); |
63 System.arraycopy(args,1,varArgs,0,varArgs.length); | 70 System.arraycopy(args,1,varArgs,0,varArgs.length); |
64 LuanTable argsTable = new LuanTable(); | 71 LuanTable argsTable = new LuanTable(); |
65 for( int j=0; j<args.length; j++ ) { | 72 for( int j=0; j<args.length; j++ ) { |
66 argsTable.put( j, args[j] ); | 73 argsTable.put( j, args[j] ); |
67 } | 74 } |
68 luan.global().put("arg",argsTable); | 75 env.put("arg",argsTable); |
69 try { | 76 try { |
70 LuanFunction fn = BasicLib.load_file(luan,file); | 77 LuanFunction fn = BasicLib.load_file(luan,file,env); |
71 luan.call(fn,null,null,varArgs); | 78 luan.call(fn,null,null,varArgs); |
72 } catch(LuanException e) { | 79 } catch(LuanException e) { |
73 // System.err.println("error: "+e.getMessage()); | 80 // System.err.println("error: "+e.getMessage()); |
74 e.printStackTrace(); | 81 e.printStackTrace(); |
75 System.exit(-1); | 82 System.exit(-1); |
76 } | 83 } |
77 } | 84 } |
78 if( interactive ) | 85 if( interactive ) |
79 interactive(luan); | 86 interactive(luan,env); |
80 } | 87 } |
81 | 88 |
82 private static void error(String msg) { | 89 private static void error(String msg) { |
83 System.err.println(msg); | 90 System.err.println(msg); |
84 System.err.println("usage: java luan.CmdLine [options] [script [args]]"); | 91 System.err.println("usage: java luan.CmdLine [options] [script [args]]"); |
91 System.err.println(" -- stop handling options"); | 98 System.err.println(" -- stop handling options"); |
92 System.err.println(" - stop handling options and execute stdin"); | 99 System.err.println(" - stop handling options and execute stdin"); |
93 System.exit(-1); | 100 System.exit(-1); |
94 } | 101 } |
95 | 102 |
96 static void interactive(LuanState luan) { | 103 static void interactive(LuanState luan,LuanTable env) { |
97 while( true ) { | 104 while( true ) { |
98 System.out.print("> "); | 105 System.out.print("> "); |
99 String input = new Scanner(System.in).nextLine(); | 106 String input = new Scanner(System.in).nextLine(); |
100 try { | 107 try { |
101 Object[] rtn = luan.eval(input,"stdin"); | 108 Object[] rtn = luan.eval(input,"stdin",env); |
102 if( rtn.length > 0 ) | 109 if( rtn.length > 0 ) |
103 BasicLib.print(luan,rtn); | 110 BasicLib.print(luan,rtn); |
104 } catch(LuanException e) { | 111 } catch(LuanException e) { |
105 System.out.println(e.getMessage()); | 112 System.out.println(e.getMessage()); |
106 } | 113 } |