Skip to content

Commit cb7067e

Browse files
committed
Add a REPL backed by the script interpreter
It's basic, but the language switching feature is pretty snazzy. And it's convenient to have for CLI usage. The main method provides a CLI-based REPL. But the API should enable more sophisticated UIs built around it, as well.
1 parent 7607c0f commit cb7067e

File tree

1 file changed

+351
-0
lines changed

1 file changed

+351
-0
lines changed
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import java.io.BufferedReader;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.io.InputStreamReader;
38+
import java.io.OutputStream;
39+
import java.io.PrintStream;
40+
import java.lang.reflect.Constructor;
41+
import java.util.ArrayList;
42+
import java.util.List;
43+
44+
import javax.script.Bindings;
45+
46+
import org.scijava.Context;
47+
import org.scijava.Gateway;
48+
import org.scijava.log.LogService;
49+
import org.scijava.plugin.Parameter;
50+
import org.scijava.plugin.PluginInfo;
51+
import org.scijava.plugin.PluginService;
52+
import org.scijava.service.Service;
53+
54+
/**
55+
* A REPL for SciJava script engines, which allows dynamic language switching.
56+
*
57+
* @author Curtis Rueden
58+
*/
59+
public class ScriptREPL {
60+
61+
private static final String NULL = "<null>";
62+
63+
@Parameter
64+
private Context context;
65+
66+
@Parameter
67+
private ScriptService scriptService;
68+
69+
@Parameter(required = false)
70+
private PluginService pluginService;
71+
72+
@Parameter(required = false)
73+
private LogService log;
74+
75+
private final PrintStream out;
76+
77+
private ScriptInterpreter interpreter;
78+
79+
public ScriptREPL(final Context context) {
80+
this(context, System.out);
81+
}
82+
83+
public ScriptREPL(final Context context, final OutputStream out) {
84+
context.inject(this);
85+
this.out = out instanceof PrintStream ?
86+
(PrintStream) out : new PrintStream(out);
87+
}
88+
89+
/** Gets the script interpreter for the currently active language. */
90+
public ScriptInterpreter getInterpreter() {
91+
return interpreter;
92+
}
93+
94+
/**
95+
* Starts a Read-Eval-Print-Loop from the standard input stream, returning
96+
* when the loop terminates.
97+
*/
98+
public void loop() throws IOException {
99+
loop(System.in);
100+
}
101+
102+
/**
103+
* Starts a Read-Eval-Print-Loop from the given input stream, returning when
104+
* the loop terminates.
105+
*
106+
* @param in Input stream from which commands are read.
107+
*/
108+
public void loop(final InputStream in) throws IOException {
109+
initialize();
110+
final BufferedReader bin = new BufferedReader(new InputStreamReader(in));
111+
while (true) {
112+
prompt();
113+
final String line = bin.readLine();
114+
if (line == null) break;
115+
if (!evaluate(line)) return;
116+
}
117+
}
118+
119+
/** Outputs a greeting, and sets up the initial language of the REPL. */
120+
public void initialize() {
121+
out.println("Welcome to the SciJava REPL!");
122+
out.println();
123+
help();
124+
out.println("Have fun!");
125+
out.println();
126+
lang(scriptService.getLanguages().get(0).getLanguageName());
127+
populateBindings(interpreter.getBindings());
128+
}
129+
130+
/** Outputs the prompt. */
131+
public void prompt() {
132+
out.print(interpreter.isReady() ? "> " : "\\ ");
133+
}
134+
135+
/**
136+
* Evaluates the line, including handling of special colon-prefixed REPL
137+
* commands.
138+
*
139+
* @param line The line to evaluate.
140+
* @return False iff the REPL should exit.
141+
*/
142+
public boolean evaluate(final String line) {
143+
final String tLine = line.trim();
144+
if (tLine.equals(":help")) help();
145+
else if (tLine.equals(":vars")) vars();
146+
else if (tLine.equals(":langs")) langs();
147+
else if (tLine.startsWith(":lang ")) lang(line.substring(6).trim());
148+
else if (line.trim().equals(":quit")) return false;
149+
else {
150+
// pass the input to the current interpreter for evaluation
151+
try {
152+
final Object result = interpreter.interpret(line);
153+
if (result != ScriptInterpreter.MORE_INPUT_PENDING) {
154+
out.println(s(result));
155+
}
156+
}
157+
catch (final Throwable exc) {
158+
exc.printStackTrace(out);
159+
}
160+
}
161+
return true;
162+
}
163+
164+
// -- Commands --
165+
166+
/** Prints a usage guide. */
167+
public void help() {
168+
out.println("Available built-in commands:");
169+
out.println();
170+
out.println(" :help | this handy list of commands");
171+
out.println(" :vars | dump a list of variables");
172+
out.println(" :lang <name> | switch the active language");
173+
out.println(" :langs | list available languages");
174+
out.println(" :quit | exit the REPL");
175+
out.println();
176+
out.println("Or type a statement to evaluate it with the active language.");
177+
out.println();
178+
}
179+
180+
/** Lists variables in the script context. */
181+
public void vars() {
182+
final List<String> keys = new ArrayList<String>();
183+
final List<Object> types = new ArrayList<Object>();
184+
final Bindings bindings = interpreter.getBindings();
185+
for (final String key : bindings.keySet()) {
186+
final Object value = bindings.get(key);
187+
keys.add(key);
188+
types.add(type(value));
189+
}
190+
printColumns(keys, types);
191+
}
192+
193+
/**
194+
* Creates a new {@link ScriptInterpreter} to interpret statements, preserving
195+
* existing variables from the previous interpreter.
196+
*
197+
* @param langName The script language of the new interpreter.
198+
* @throws IllegalArgumentException if the requested language is not
199+
* available.
200+
*/
201+
public void lang(final String langName) {
202+
// create the new interpreter
203+
final ScriptLanguage language = scriptService.getLanguageByName(langName);
204+
if (language == null) {
205+
throw new IllegalArgumentException("No such language: " + langName);
206+
}
207+
final ScriptInterpreter newInterpreter =
208+
new DefaultScriptInterpreter(language);
209+
210+
// preserve state of the previous interpreter
211+
copyBindings(interpreter, newInterpreter);
212+
out.println("language -> " +
213+
newInterpreter.getLanguage().getLanguageName());
214+
interpreter = newInterpreter;
215+
}
216+
217+
public void langs() {
218+
final List<String> names = new ArrayList<String>();
219+
final List<String> versions = new ArrayList<String>();
220+
final List<Object> aliases = new ArrayList<Object>();
221+
for (final ScriptLanguage lang : scriptService.getLanguages()) {
222+
names.add(lang.getLanguageName());
223+
versions.add(lang.getLanguageVersion());
224+
aliases.add(lang.getNames());
225+
}
226+
printColumns(names, versions, aliases);
227+
}
228+
229+
// -- Main method --
230+
231+
public static void main(final String... args) throws Exception {
232+
// make a SciJava application context
233+
final Context context = new Context();
234+
235+
// create the script interpreter
236+
final ScriptREPL scriptCLI = new ScriptREPL(context);
237+
238+
// start the REPL
239+
scriptCLI.loop();
240+
241+
// clean up
242+
context.dispose();
243+
System.exit(0);
244+
}
245+
246+
// -- Helper methods --
247+
248+
/** Populates the bindings with the context + services + gateways. */
249+
private void populateBindings(final Bindings bindings) {
250+
bindings.put("ctx", context);
251+
for (final Service service : context.getServiceIndex().getAll()) {
252+
final String name = serviceName(service);
253+
bindings.put(name, service);
254+
}
255+
for (final Gateway gateway : gateways()) {
256+
bindings.put(gateway.getShortName(), gateway);
257+
}
258+
}
259+
260+
/** Transfers variables from one interpreter's bindings to another. */
261+
private void copyBindings(final ScriptInterpreter src,
262+
final ScriptInterpreter dest)
263+
{
264+
if (src == null) return; // nothing to copy
265+
final Bindings srcBindings = src.getBindings();
266+
final Bindings destBindings = dest.getBindings();
267+
for (final String key : src.getBindings().keySet()) {
268+
final Object value = src.getLanguage().decode(srcBindings.get(key));
269+
destBindings.put(key, value);
270+
}
271+
}
272+
273+
private List<Gateway> gateways() {
274+
final ArrayList<Gateway> gateways = new ArrayList<Gateway>();
275+
if (pluginService == null) return gateways;
276+
// HACK: Instantiating a Gateway with the noargs constructor spins
277+
// up a second Context, which is not what we want. Perhaps SJC should
278+
// be changed to prefer a single-argument constructor that accepts a
279+
// Context, before trying the noargs constructor?
280+
// In the meantime, we do it manually here.
281+
final List<PluginInfo<Gateway>> infos =
282+
pluginService.getPluginsOfType(Gateway.class);
283+
for (final PluginInfo<Gateway> info : infos) {
284+
try {
285+
final Constructor<? extends Gateway> ctor =
286+
info.loadClass().getConstructor(Context.class);
287+
final Gateway gateway = ctor.newInstance(context);
288+
gateways.add(gateway);
289+
}
290+
catch (final Throwable t) {
291+
if (log != null) log.error(t);
292+
}
293+
}
294+
return gateways;
295+
}
296+
297+
private String serviceName(final Service service) {
298+
final String serviceName = service.getClass().getSimpleName();
299+
final String shortName = lowerCamelCase(
300+
serviceName.replaceAll("^(Default)?(.*)Service$", "$2"));
301+
return shortName;
302+
}
303+
304+
private String type(final Object value) {
305+
if (value == null) return NULL;
306+
final Object decoded = interpreter.getLanguage().decode(value);
307+
if (decoded == null) return NULL;
308+
return "[" + decoded.getClass().getName() + "]";
309+
}
310+
311+
private void printColumns(final List<?>... columns) {
312+
final int pad = 2;
313+
314+
// compute width of each column
315+
final int[] widths = new int[columns.length];
316+
for (int c = 0; c < columns.length; c++) {
317+
final List<?> list = columns[c];
318+
for (final Object o : list) {
319+
final String s = s(o);
320+
if (s.length() > widths[c]) widths[c] = s.length();
321+
}
322+
}
323+
324+
// output the columns
325+
for (int i = 0; i < columns[0].size(); i++) {
326+
for (int c = 0; c < columns.length; c++) {
327+
final String s = s(columns[c].get(i));
328+
out.print(s);
329+
for (int p = s.length(); p < widths[c] + pad; p++) {
330+
out.print(' ');
331+
}
332+
}
333+
out.println();
334+
}
335+
}
336+
337+
private static String lowerCamelCase(final String s) {
338+
final StringBuilder sb = new StringBuilder(s);
339+
for (int i=0; i<sb.length(); i++) {
340+
final char c = sb.charAt(i);
341+
if (c >= 'A' && c <= 'Z') sb.setCharAt(i, (char) (c - 'A' + 'a'));
342+
else break;
343+
}
344+
return sb.toString();
345+
}
346+
347+
private static String s(final Object o) {
348+
return o == null ? NULL : o.toString();
349+
}
350+
351+
}

0 commit comments

Comments
 (0)