-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathLoopShell.java
More file actions
414 lines (354 loc) · 13.5 KB
/
Copy pathLoopShell.java
File metadata and controls
414 lines (354 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package loop;
import jline.console.ConsoleReader;
import jline.console.completer.Completer;
import jline.console.completer.FileNameCompleter;
import loop.ast.Assignment;
import loop.ast.Node;
import loop.ast.Variable;
import loop.ast.script.FunctionDecl;
import loop.ast.script.ModuleDecl;
import loop.ast.script.ModuleLoader;
import loop.ast.script.RequireDecl;
import loop.ast.script.Unit;
import loop.lang.LoopObject;
import loop.runtime.Closure;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author [email protected] (Dhanji R. Prasanna)
*/
public class LoopShell {
private static Map<String, Object> shellContext;
public static Object shellObtain(String var) {
return shellContext.get(var);
}
public static void shell() throws Exception {
System.out.println("loOp (http://looplang.org)");
System.out.println(" by Dhanji R. Prasanna\n");
ConsoleReader reader = new ConsoleReader();
try {
reader.setExpandEvents(false);
reader.addCompleter(new MetaCommandCompleter());
Unit shellScope = new Unit(null, ModuleDecl.SHELL);
FunctionDecl main = new FunctionDecl("main", null);
shellScope.declare(main);
shellContext = new HashMap<String, Object>();
boolean inFunction = false;
// Used to build up multiline statement blocks (like functions)
StringBuilder block = null;
//noinspection InfiniteLoopStatement
do {
String prompt = (block != null) ? "| " : ">> ";
String rawLine = reader.readLine(prompt);
if (inFunction) {
if (rawLine == null || rawLine.trim().isEmpty()) {
inFunction = false;
// Eval the function to verify it.
printResult(Loop.evalClassOrFunction(block.toString(), shellScope));
block = null;
continue;
}
block.append(rawLine).append('\n');
continue;
}
if (rawLine == null) {
quit(reader);
}
//noinspection ConstantConditions
String line = rawLine.trim();
if (line.isEmpty())
continue;
// Add a require import.
if (line.startsWith("require ")) {
shellScope.declare(new LexprParser(new Tokenizer(line + '\n').tokenize()).require());
shellScope.loadDeps("<shell>");
continue;
}
if (line.startsWith(":q") || line.startsWith(":quit")) {
quit(reader);
}
if (line.startsWith(":h") || line.startsWith(":help")) {
printHelp();
}
if (line.startsWith(":run")) {
String[] split = line.split("[ ]+", 2);
if (split.length < 2 || !split[1].endsWith(".loop"))
System.out.println("You must specify a .loop file to run.");
Loop.run(split[1]);
continue;
}
if (line.startsWith(":r") || line.startsWith(":reset")) {
System.out.println("Context reset.");
shellScope = new Unit(null, ModuleDecl.SHELL);
main = new FunctionDecl("main", null);
shellScope.declare(main);
shellContext = new HashMap<String, Object>();
continue;
}
if (line.startsWith(":i") || line.startsWith(":imports")) {
for (RequireDecl requireDecl : shellScope.imports()) {
System.out.println(requireDecl.toSymbol());
}
System.out.println();
continue;
}
if (line.startsWith(":f") || line.startsWith(":functions")) {
for (FunctionDecl functionDecl : shellScope.functions()) {
StringBuilder args = new StringBuilder();
List<Node> children = functionDecl.arguments().children();
for (int i = 0, childrenSize = children.size(); i < childrenSize; i++) {
Node arg = children.get(i);
args.append(arg.toSymbol());
if (i < childrenSize - 1)
args.append(", ");
}
System.out.println(functionDecl.name()
+ ": (" + args.toString() + ")"
+ (functionDecl.patternMatching ? " #pattern-matching" : "")
);
}
System.out.println();
continue;
}
if (line.startsWith(":t") || line.startsWith(":type")) {
String[] split = line.split("[ ]+", 2);
if (split.length <= 1) {
System.out.println("Give me an expression to determine the type for.\n");
continue;
}
Object result = evalInFunction(split[1], main, shellScope, false);
printTypeOf(result);
continue;
}
if (line.startsWith(":javatype")) {
String[] split = line.split("[ ]+", 2);
if (split.length <= 1) {
System.out.println("Give me an expression to determine the type for.\n");
continue;
}
Object result = evalInFunction(split[1], main, shellScope, false);
if (result instanceof LoopError)
System.out.println(result.toString());
else
System.out.println(result == null ? "null" : result.getClass().getName());
continue;
}
// Function definitions can be multiline.
if (line.endsWith("->") || line.endsWith("=>")) {
inFunction = true;
block = new StringBuilder(line).append('\n');
continue;
} else if (isDangling(line)) {
if (block == null)
block = new StringBuilder();
block.append(line).append('\n');
continue;
}
if (block != null) {
rawLine = block.append(line).toString();
block = null;
}
// First determine what kind of expression this is.
main.children().clear();
// OK execute expression.
try {
printResult(evalInFunction(rawLine, main, shellScope, true));
} catch (ClassCastException e) {
StackTraceSanitizer.cleanForShell(e);
System.out.println("#error: " + e.getMessage());
System.out.println();
} catch (RuntimeException e) {
StackTraceSanitizer.cleanForShell(e);
e.printStackTrace();
System.out.println();
}
} while (true);
} catch (IOException e) {
System.err.println("Something went wrong =(");
reader.getTerminal().reset();
System.exit(1);
}
}
private static void printHelp() {
System.out.println("loOp Shell v1.0");
System.out.println(" :run <file.loop> - executes the specified loop file");
System.out.println(" :reset - discards current shell context (variables, funcs, etc.)");
System.out.println(" :imports - lists all currently imported loop modules and Java types");
System.out.println(" :functions - lists all currently defined functions by signature");
System.out.println(" :type <expr> - prints the type of the given expression");
System.out.println(" :javatype <expr> - prints the underlying java type (for examining loop internals)");
System.out.println(" :quit (or Ctrl-D) - exits the loop shell");
System.out.println(" :help - prints this help card");
System.out.println();
System.out.println(" Hint :h is short for :help, etc.");
}
private static void printTypeOf(Object result) {
if (result instanceof LoopError)
System.out.println(result.toString());
else if (result instanceof LoopObject)
System.out.println(((LoopObject)result).getType());
else if (result instanceof Closure)
System.out.println("#function: " + ((Closure)result).name);
else
System.out.println(result == null ? "Nothing" : "#java: " + result.getClass().getName());
}
private static Object evalInFunction(String rawLine,
FunctionDecl func,
Unit shellScope,
boolean addToWhereBlock) {
rawLine = rawLine.trim() + '\n';
Executable executable = new Executable(new StringReader(rawLine));
Node parsedLine;
try {
Parser parser = new LexprParser(new Tokenizer(rawLine).tokenize(), shellScope);
parsedLine = parser.line();
if (parsedLine == null || !parser.getErrors().isEmpty()) {
executable.printErrors(parser.getErrors());
return "";
}
// If this is an assignment, just check the rhs portion of it.
// This is a bit hacky but prevents verification from balking about new
// vars declared in the lhs.
if (parsedLine instanceof Assignment) {
new Reducer(parsedLine).reduce();
Assignment assignment = (Assignment) parsedLine;
// Strip the lhs of the assignment if this is a simple variable setter
// as that will happen after the fact in a where-block.
// However we still do have Assignment nodes that assign "in-place", i.e.
// mutate the state of existing variables (example: a.b = c), and these need
// to continue untouched.
if (assignment.lhs() instanceof Variable)
func.children().add(assignment.rhs());
else
func.children().add(parsedLine);
} else
func.children().add(parsedLine);
// Compress nodes and eliminate redundancies.
new Reducer(func).reduce();
shellScope.loadDeps("<shell>");
executable.runMain(true);
executable.compileExpression(shellScope);
if (executable.hasErrors()) {
executable.printStaticErrorsIfNecessary();
return "";
}
} catch (Exception e) {
e.printStackTrace();
return new LoopError("malformed expression " + rawLine);
}
try {
Object result = Loop.safeEval(executable, null);
if (addToWhereBlock && parsedLine instanceof Assignment) {
Assignment assignment = (Assignment) parsedLine;
boolean shouldReplace = false, shouldAddToWhere = true;
if (assignment.lhs() instanceof Variable) {
String name = ((Variable) assignment.lhs()).name;
shellContext.put(name, result);
// Look up the value of the RHS of the variable from the shell context,
// if this is the second reference to the same variable.
assignment.setRhs(new LexprParser(new Tokenizer(
"`loop.LoopShell`.shellObtain('" + name + "')").tokenize()).parse());
shouldReplace = true;
} else
shouldAddToWhere = false; // Do not add state-mutating assignments to where block.
// If this assignment is already present in the current scope, we should replace it.
if (shouldReplace)
for (Iterator<Node> iterator = func.whereBlock().iterator(); iterator.hasNext(); ) {
Node node = iterator.next();
if (node instanceof Assignment && ((Assignment) node).lhs() instanceof Variable) {
iterator.remove();
}
}
if (shouldAddToWhere)
func.declareLocally(parsedLine);
}
return result;
} finally {
ModuleLoader.reset(); // Cleans up the loaded classes.
}
}
private static void printResult(Object result) {
if (result instanceof Closure) {
Closure fun = (Closure) result;
System.out.println("#function: " + fun.name);
} else if (result instanceof Set) {
String r = result.toString();
System.out.println('{' + r.substring(1, r.length() - 1) + '}');
}
else
System.out.println(result == null ? "#nothing" : result);
}
private static boolean isLoadCommand(String line) {
return line.startsWith(":run");
}
private static void quit(ConsoleReader reader) throws Exception {
reader.getTerminal().restore();
System.out.println("Bye.");
System.exit(0);
}
// For tracking multiline expressions.
private static int braces = 0, brackets = 0, parens = 0;
private static boolean isDangling(String line) {
for (Token token : new Tokenizer(line).tokenize()) {
switch (token.kind) {
case LBRACE:
braces++;
break;
case LBRACKET:
brackets++;
break;
case LPAREN:
parens++;
break;
case RBRACE:
braces--;
break;
case RBRACKET:
brackets--;
break;
case RPAREN:
parens--;
break;
}
}
return braces > 0 || brackets > 0 || parens > 0;
}
private static class MetaCommandCompleter implements Completer {
private final List<String> commands = Arrays.asList(
":help",
":run",
":quit",
":reset",
":type",
":imports",
":javatype",
":functions"
);
private final FileNameCompleter fileNameCompleter = new FileNameCompleter();
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) {
if (buffer == null) {
buffer = "";
} else
buffer = buffer.trim();
// See if we should chain to the filename completer first.
if (isLoadCommand(buffer)) {
String[] split = buffer.split("[ ]+");
// Always complete the first argument.
if (split.length > 1)
return fileNameCompleter.complete(split[split.length - 1], cursor, candidates);
}
for (String command : commands) {
if (command.startsWith(buffer)) {
candidates.add(command.substring(buffer.length()) + ' ');
}
}
return cursor;
}
}
}