Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.ruby.shell/src/com/oracle/truffle/ruby/shell/Shell.java @ 14095:cf16ff2dba69
Ruby/Instrumentation: class rename
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 04 Feb 2014 11:17:09 -0800 |
parents | 3f27e57439ed |
children |
rev | line source |
---|---|
13514 | 1 /* |
14094
3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. This |
13514 | 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 | |
14094
3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
77 // Ruby should always have a debug context. |
14095
cf16ff2dba69
Ruby/Instrumentation: class rename
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
14094
diff
changeset
|
78 context.setDebugContext(new MinimalRubyDebugContext(context)); |
14094
3f27e57439ed
Truffle/Instrumentation: significant rearrangement (including moved class) and extension of the Truffle Instrumentation Framework. New interfaces include DebugContext (which can be attached to the ExecutionContext), through which access is provided to possibly language-specific (a) node instrumentation, (b) debug services manager, (c) notification when programs halt, (d) display of language values, and (e) display of variable identifiers.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
79 |
13514 | 80 // Bring in core method nodes |
81 | |
82 CoreMethodNodeManager.addMethods(context.getCoreLibrary().getObjectClass()); | |
83 | |
84 // Give the core library manager a chance to tweak some of those methods | |
85 | |
86 context.getCoreLibrary().initializeAfterMethodsAdded(); | |
87 | |
88 // Set program arguments | |
89 | |
90 for (String arg : options.getProgramArgs()) { | |
91 context.getCoreLibrary().getArgv().push(context.makeString(arg)); | |
92 } | |
93 | |
94 if (!options.getSwitchArgs().isEmpty()) { | |
95 context.implementationMessage("can't set -s switch arguments yet"); | |
96 } | |
97 | |
98 // Set the load path | |
99 | |
100 final RubyArray loadPath = (RubyArray) context.getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$:"); | |
101 | |
102 final String pathVar = System.getenv("PATH"); | |
103 | |
104 if (options.isImportFromPath() && pathVar != null) { | |
105 for (String path : pathVar.split(File.pathSeparator)) { | |
106 loadPath.push(context.makeString(path)); | |
107 } | |
108 } | |
109 | |
110 for (String path : options.getExtraLoadPath()) { | |
111 loadPath.push(context.makeString(path)); | |
112 } | |
113 | |
114 final String rubylibVar = System.getenv("RUBYLIB"); | |
115 | |
116 if (rubylibVar != null) { | |
117 for (String path : rubylibVar.split(File.pathSeparator)) { | |
118 loadPath.push(context.makeString(path)); | |
119 } | |
120 } | |
121 | |
122 if (context.getConfiguration().getStandardLibrary().endsWith(".jar")) { | |
13645
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 * 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
|
125 * the 2.1 library at the moment. |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
126 */ |
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
127 loadPath.push(context.makeString("jar:file:" + context.getConfiguration().getStandardLibrary() + "!/META-INF/jruby.home/lib/ruby/1.9")); |
13514 | 128 } else { |
129 loadPath.push(context.makeString(context.getConfiguration().getStandardLibrary())); | |
130 } | |
131 | |
132 // Pre-required modules | |
133 | |
134 for (String feature : options.getPreRequires()) { | |
135 context.getFeatureManager().require(feature); | |
136 } | |
137 | |
138 // Check for other options that are not implemented yet | |
139 | |
140 if (options.getRecordSeparator() != -1) { | |
141 context.implementationMessage("record separator not implemented"); | |
142 } | |
143 | |
144 if (options.isAutosplit()) { | |
145 context.implementationMessage("autosplit not implemented"); | |
146 } | |
147 | |
148 if (options.getPreChangeDirectory() != null) { | |
149 context.implementationMessage("not able to change directory"); | |
150 } | |
151 | |
152 if (options.isLineEndingProcessing()) { | |
153 context.implementationMessage("line end processing not implemented"); | |
154 } | |
155 | |
156 if (options.isInPlaceEdit()) { | |
157 context.implementationMessage("in place editing not implemented"); | |
158 } | |
159 | |
160 if (options.isImplicitLoop() || options.isImplicitSedLoop()) { | |
161 context.implementationMessage("implicit loops not implemented"); | |
162 } | |
163 | |
164 if (options.isStripMessage()) { | |
165 context.implementationMessage("strip message -x option not implemented"); | |
166 } | |
167 | |
168 // Run the scripts, program file, or run the temporary version of IRB | |
169 | |
170 try { | |
171 if (!options.getCommandLineScripts().isEmpty()) { | |
172 final StringBuilder combinedScript = new StringBuilder(); | |
173 | |
174 for (String script : options.getCommandLineScripts()) { | |
175 combinedScript.append(script); | |
176 combinedScript.append("\n"); | |
177 } | |
178 | |
179 try { | |
180 final Source source = context.getSourceManager().get("-e", combinedScript.toString()); | |
181 if (options.isCheckSyntaxOnly()) { | |
182 context.getParser().parse(context, source, RubyParser.ParserContext.TOP_LEVEL, null); | |
183 System.out.println("Syntax OK"); | |
184 } else { | |
185 context.execute(context, source, RubyParser.ParserContext.TOP_LEVEL, context.getCoreLibrary().getMainObject(), null); | |
186 } | |
187 } catch (Exception e) { | |
188 e.printStackTrace(); | |
189 } | |
190 } else if (options.getProgramFile() != null) { | |
191 try { | |
192 if (options.isCheckSyntaxOnly()) { | |
193 final Source source = context.getSourceManager().get(options.getProgramFile()); | |
194 context.getParser().parse(context, source, RubyParser.ParserContext.TOP_LEVEL, null); | |
195 System.out.println("Syntax OK"); | |
196 } else { | |
197 context.loadFile(options.getProgramFile()); | |
198 } | |
199 } catch (Exception e) { | |
200 e.printStackTrace(); | |
201 } | |
202 } else { | |
203 if (options.isCheckSyntaxOnly()) { | |
204 System.err.println("Can't check syntax in IRB mode"); | |
205 return; | |
206 } | |
207 | |
208 context.runShell(null, null); | |
209 } | |
210 } finally { | |
211 context.shutdown(); | |
212 } | |
213 } | |
214 | |
215 } |