Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.ruby.shell/src/com/oracle/truffle/ruby/shell/Shell.java @ 13654:32af063cc29c
Don't create null guards for values that are already known to be non-null.
author | Gilles Duboscq <duboscq@ssw.jku.at> |
---|---|
date | Thu, 16 Jan 2014 12:18:29 +0100 |
parents | 497fada09efb |
children | 3f27e57439ed |
rev | line source |
---|---|
13514 | 1 /* |
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This | |
3 * code is released under a tri EPL/GPL/LGPL license. You can use it, | |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.shell; | |
11 | |
12 import java.io.*; | |
13 | |
14 import jline.console.*; | |
15 | |
16 import com.oracle.truffle.api.*; | |
17 import com.oracle.truffle.ruby.nodes.core.*; | |
18 import com.oracle.truffle.ruby.parser.*; | |
19 import com.oracle.truffle.ruby.runtime.*; | |
20 import com.oracle.truffle.ruby.runtime.configuration.*; | |
21 import com.oracle.truffle.ruby.runtime.core.array.*; | |
22 | |
23 /** | |
24 * The entry point class for RubyTruffle. Implements the MRI command line interface. | |
25 */ | |
26 public class Shell { | |
27 | |
28 /** | |
29 * Entry point method for Ruby both in batch and interactive mode. | |
30 */ | |
31 public static void main(String[] args) throws IOException { | |
32 // Parse the command line | |
33 | |
34 final CommandLineOptions options = CommandLineParser.parse(args); | |
35 | |
36 if (options == null) { | |
37 return; | |
38 } | |
39 | |
40 // Setup JLine | |
41 | |
42 ConsoleReader console = null; | |
43 | |
44 if (options.useJLine()) { | |
45 System.setProperty("jline.shutdownhook", "true"); | |
46 console = new ConsoleReader(); | |
47 console.setExpandEvents(false); | |
48 } | |
49 | |
50 // Override the home directory if RUBYHOME is set | |
51 | |
52 final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(options.getConfiguration()); | |
53 | |
54 if (System.getenv("RUBYHOME") != null) { | |
55 configurationBuilder.setStandardLibrary(System.getenv("RUBYHOME") + "/" + ConfigurationBuilder.JRUBY_STDLIB_JAR); | |
56 } | |
57 | |
58 // Use JLine for console input | |
59 | |
60 final ConsoleReader finalConsole = console; | |
61 | |
62 if (options.useJLine()) { | |
63 configurationBuilder.setInputReader(new InputReader() { | |
64 | |
65 @Override | |
66 public String readLine(String prompt) throws IOException { | |
67 return finalConsole.readLine(prompt); | |
68 } | |
69 | |
70 }); | |
71 } | |
72 | |
73 // Set up a context | |
74 | |
75 final RubyContext context = new RubyContext(new Configuration(configurationBuilder), new JRubyParser()); | |
76 | |
77 // Bring in core method nodes | |
78 | |
79 CoreMethodNodeManager.addMethods(context.getCoreLibrary().getObjectClass()); | |
80 | |
81 // Give the core library manager a chance to tweak some of those methods | |
82 | |
83 context.getCoreLibrary().initializeAfterMethodsAdded(); | |
84 | |
85 // Set program arguments | |
86 | |
87 for (String arg : options.getProgramArgs()) { | |
88 context.getCoreLibrary().getArgv().push(context.makeString(arg)); | |
89 } | |
90 | |
91 if (!options.getSwitchArgs().isEmpty()) { | |
92 context.implementationMessage("can't set -s switch arguments yet"); | |
93 } | |
94 | |
95 // Set the load path | |
96 | |
97 final RubyArray loadPath = (RubyArray) context.getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$:"); | |
98 | |
99 final String pathVar = System.getenv("PATH"); | |
100 | |
101 if (options.isImportFromPath() && pathVar != null) { | |
102 for (String path : pathVar.split(File.pathSeparator)) { | |
103 loadPath.push(context.makeString(path)); | |
104 } | |
105 } | |
106 | |
107 for (String path : options.getExtraLoadPath()) { | |
108 loadPath.push(context.makeString(path)); | |
109 } | |
110 | |
111 final String rubylibVar = System.getenv("RUBYLIB"); | |
112 | |
113 if (rubylibVar != null) { | |
114 for (String path : rubylibVar.split(File.pathSeparator)) { | |
115 loadPath.push(context.makeString(path)); | |
116 } | |
117 } | |
118 | |
119 if (context.getConfiguration().getStandardLibrary().endsWith(".jar")) { | |
13645
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
120 /* |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
121 * Use the 1.9 library, even though we're emulating 2.1, as there are some bugs running |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
122 * the 2.1 library at the moment. |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
123 */ |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
124 loadPath.push(context.makeString("jar:file:" + context.getConfiguration().getStandardLibrary() + "!/META-INF/jruby.home/lib/ruby/1.9")); |
13514 | 125 } else { |
126 loadPath.push(context.makeString(context.getConfiguration().getStandardLibrary())); | |
127 } | |
128 | |
129 // Pre-required modules | |
130 | |
131 for (String feature : options.getPreRequires()) { | |
132 context.getFeatureManager().require(feature); | |
133 } | |
134 | |
135 // Check for other options that are not implemented yet | |
136 | |
137 if (options.getRecordSeparator() != -1) { | |
138 context.implementationMessage("record separator not implemented"); | |
139 } | |
140 | |
141 if (options.isAutosplit()) { | |
142 context.implementationMessage("autosplit not implemented"); | |
143 } | |
144 | |
145 if (options.getPreChangeDirectory() != null) { | |
146 context.implementationMessage("not able to change directory"); | |
147 } | |
148 | |
149 if (options.isLineEndingProcessing()) { | |
150 context.implementationMessage("line end processing not implemented"); | |
151 } | |
152 | |
153 if (options.isInPlaceEdit()) { | |
154 context.implementationMessage("in place editing not implemented"); | |
155 } | |
156 | |
157 if (options.isImplicitLoop() || options.isImplicitSedLoop()) { | |
158 context.implementationMessage("implicit loops not implemented"); | |
159 } | |
160 | |
161 if (options.isStripMessage()) { | |
162 context.implementationMessage("strip message -x option not implemented"); | |
163 } | |
164 | |
165 // Run the scripts, program file, or run the temporary version of IRB | |
166 | |
167 try { | |
168 if (!options.getCommandLineScripts().isEmpty()) { | |
169 final StringBuilder combinedScript = new StringBuilder(); | |
170 | |
171 for (String script : options.getCommandLineScripts()) { | |
172 combinedScript.append(script); | |
173 combinedScript.append("\n"); | |
174 } | |
175 | |
176 try { | |
177 final Source source = context.getSourceManager().get("-e", combinedScript.toString()); | |
178 if (options.isCheckSyntaxOnly()) { | |
179 context.getParser().parse(context, source, RubyParser.ParserContext.TOP_LEVEL, null); | |
180 System.out.println("Syntax OK"); | |
181 } else { | |
182 context.execute(context, source, RubyParser.ParserContext.TOP_LEVEL, context.getCoreLibrary().getMainObject(), null); | |
183 } | |
184 } catch (Exception e) { | |
185 e.printStackTrace(); | |
186 } | |
187 } else if (options.getProgramFile() != null) { | |
188 try { | |
189 if (options.isCheckSyntaxOnly()) { | |
190 final Source source = context.getSourceManager().get(options.getProgramFile()); | |
191 context.getParser().parse(context, source, RubyParser.ParserContext.TOP_LEVEL, null); | |
192 System.out.println("Syntax OK"); | |
193 } else { | |
194 context.loadFile(options.getProgramFile()); | |
195 } | |
196 } catch (Exception e) { | |
197 e.printStackTrace(); | |
198 } | |
199 } else { | |
200 if (options.isCheckSyntaxOnly()) { | |
201 System.err.println("Can't check syntax in IRB mode"); | |
202 return; | |
203 } | |
204 | |
205 context.runShell(null, null); | |
206 } | |
207 } finally { | |
208 context.shutdown(); | |
209 } | |
210 } | |
211 | |
212 } |