-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathShellCommand.java
More file actions
343 lines (273 loc) · 11.6 KB
/
ShellCommand.java
File metadata and controls
343 lines (273 loc) · 11.6 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
/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Mark Hills - Mark.Hills@cwi.nl (CWI)
* * Arnold Lankamp - Arnold.Lankamp@cwi.nl
*******************************************************************************/
package org.rascalmpl.semantics.dynamic;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Map.Entry;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jline.utils.InfoCmp.Capability;
import org.rascalmpl.ast.OptionalTerminator;
import org.rascalmpl.ast.QualifiedName;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.interpreter.Configuration;
import org.rascalmpl.interpreter.IEvaluator;
import org.rascalmpl.interpreter.control_exceptions.QuitException;
import org.rascalmpl.interpreter.env.ModuleEnvironment;
import org.rascalmpl.interpreter.env.Pair;
import org.rascalmpl.interpreter.result.AbstractFunction;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.utils.Names;
import org.rascalmpl.values.parsetrees.TreeAdapter;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValue;
public abstract class ShellCommand extends org.rascalmpl.ast.ShellCommand {
static public class Edit extends org.rascalmpl.ast.ShellCommand.Edit {
public Edit(ISourceLocation __param1, IConstructor tree, QualifiedName __param2, OptionalTerminator term) {
super(__param1, tree, __param2, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
IRascalMonitor monitor = __eval.getMonitor();
if (monitor instanceof IDEServices) {
IDEServices services = (IDEServices) monitor;
String name = Names.fullName(getName());
ISourceLocation uri = __eval.getRascalResolver().resolveModule(name);
if (uri == null) {
__eval.getErrorPrinter().println("module " + name + " can not be found in the search path.");
}
else {
services.edit(uri);
}
}
else {
__eval.getErrorPrinter().println("The current Rascal execution environment does not know how to start an editor.");
}
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class Clear extends org.rascalmpl.ast.ShellCommand.Clear {
public Clear(ISourceLocation __param1, IConstructor tree, OptionalTerminator term) {
super(__param1, tree, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
IRascalMonitor monitor = __eval.getMonitor();
// clear the screen, if we have that capability
if (monitor instanceof IDEServices) {
var services = (IDEServices) monitor;
var term = services.activeTerminal();
if (term != null) {
term.puts(Capability.clear_screen);
}
else {
__eval.getErrorPrinter().println(":clear does not work for this context.");
}
}
else {
__eval.getErrorPrinter().println(":clear is not implemented in this context.");
}
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class Help extends org.rascalmpl.ast.ShellCommand.Help {
public Help(ISourceLocation __param1, IConstructor tree, OptionalTerminator term) {
super(__param1, tree, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
__eval.setCurrentAST(this);
__eval.printHelpMessage(__eval.getOutPrinter());
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class History extends org.rascalmpl.ast.ShellCommand.History {
public History(ISourceLocation __param1, IConstructor tree, OptionalTerminator term) {
super(__param1, tree, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> eval) {
eval.getErrorPrinter().println(":history command is not implemented here yet.");
return nothing();
}
}
static public class ListDeclarations extends org.rascalmpl.ast.ShellCommand.ListDeclarations {
public ListDeclarations(ISourceLocation __param1, IConstructor tree, OptionalTerminator term) {
super(__param1, tree, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
ModuleEnvironment env = __eval.getCurrentModuleEnvironment();
var pr = __eval.getOutPrinter();
if (!env.getVariables().isEmpty()) {
pr.println("Variables:");
var vars = env.getVariables();
for (Entry<String, Result<IValue>> var : vars.entrySet()) {
pr.println(var.getValue().getStaticType() + " " + var.getKey());
}
}
if (!env.getFunctions().isEmpty()) {
var functions = env.getFunctions();
pr.println("Functions:");
for (Pair<String, LinkedHashSet<AbstractFunction>> func : functions) {
pr.println(func.getFirst() + ":");
for (AbstractFunction alt : func.getSecond()) {
pr.println(" - " + alt.getHeader().replaceAll("\n", " "));
}
}
}
if (!env.getAbstractDatatypes().isEmpty()) {
var data = env.getAbstractDatatypes();
pr.println("Data:");
for (io.usethesource.vallang.type.Type t : data) {
pr.println(t + ":");
env.getStore().getConstructors().stream().filter(c -> c.getAbstractDataType() == t).forEach(cons -> {
pr.println(" - " + cons);
});
}
}
if (!env.getProductions().isEmpty()) {
var syntax = env.getProductions();
pr.println("Syntax definitions:");
for (IValue def : syntax) {
pr.println(TreeAdapter.yield((IConstructor) def));
}
}
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class Quit extends org.rascalmpl.ast.ShellCommand.Quit {
public Quit(ISourceLocation __param1, IConstructor tree, OptionalTerminator term) {
super(__param1, tree, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
throw new QuitException();
}
}
static public class SetOptionTrue extends org.rascalmpl.ast.ShellCommand.SetOptionTrue {
public SetOptionTrue(ISourceLocation src, IConstructor node , org.rascalmpl.ast.QualifiedName name, org.rascalmpl.ast.OptionalTerminator terminator) {
super(src, node, name, terminator);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
String name = "rascal." + ((org.rascalmpl.semantics.dynamic.QualifiedName.Default) this.getName()).fullName();
setOption(__eval, name, "true");
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class UnsetOptionTrue extends org.rascalmpl.ast.ShellCommand.UnsetOption {
public UnsetOptionTrue(ISourceLocation src, IConstructor node , org.rascalmpl.ast.QualifiedName name, org.rascalmpl.ast.OptionalTerminator terminator) {
super(src, node, name, terminator);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
String name = "rascal." + ((org.rascalmpl.semantics.dynamic.QualifiedName.Default) this.getName()).fullName();
setOption(__eval, name, "false");
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
private static void setOption(IEvaluator<Result<IValue>> __eval, String name, String value) {
switch (name) {
case Configuration.GENERATOR_PROFILING_PROPERTY:
__eval.getConfiguration().setGeneratorProfiling(Boolean.parseBoolean(value));
break;
case Configuration.PROFILING_PROPERTY:
__eval.getConfiguration().setProfiling(Boolean.parseBoolean(value));
break;
case Configuration.ERRORS_PROPERTY:
__eval.getConfiguration().setErrors(Boolean.parseBoolean(value));
break;
case Configuration.TRACING_PROPERTY:
__eval.getConfiguration().setTracing(Boolean.parseBoolean(value));
break;
case Configuration.DEBUGGING_PROPERTY:
__eval.getConfiguration().setDebugging(Boolean.parseBoolean(value));
}
__eval.updateProperties();
}
static public class SetOption extends
org.rascalmpl.ast.ShellCommand.SetOption {
public SetOption(ISourceLocation src, IConstructor node , org.rascalmpl.ast.QualifiedName name, org.rascalmpl.ast.OptionalEqualSign sign, org.rascalmpl.ast.Expression expression, org.rascalmpl.ast.OptionalTerminator terminator) {
super(src, node, name, sign, expression, terminator);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
String name = "rascal." + ((org.rascalmpl.semantics.dynamic.QualifiedName.Default) this.getName()).fullName();
String value = this.getExpression().interpret(__eval).getValue().toString();
setOption(__eval, name, value);
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class Test extends org.rascalmpl.ast.ShellCommand.Test {
public Test(ISourceLocation __param1, IConstructor tree, QualifiedName mut, OptionalTerminator term) {
super(__param1, tree, mut, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
Optional<String> name = getOptName() != null ? Optional.of(Names.fullName(getOptName())) : Optional.empty();
return org.rascalmpl.interpreter.result.ResultFactory.makeResult(TF.boolType(), VF.bool(__eval.runTests(__eval.getMonitor(), name)), __eval);
}
}
static public class Undeclare extends org.rascalmpl.ast.ShellCommand.Undeclare {
public Undeclare(ISourceLocation src, IConstructor node, @Nullable QualifiedName optName, OptionalTerminator terminator) {
super(src, node, optName, terminator);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> eval) {
if (getOptName() == null) {
// then we clear everything!
eval.getCurrentModuleEnvironment().reset();
}
else {
var n = getOptName();
if (Names.isQualified(n)) {
throw new IllegalArgumentException("name " + Names.fullName(n) + " should not be qualified for :undeclare");
}
var simpleName = Names.name(Names.lastName(n));
var env = eval.getCurrentModuleEnvironment();
env.unsetSimpleVariable(simpleName);
env.unsetAllFunctions(simpleName);
// TODO: remove ADT from store when vallang TypeStore gets that capability.
env.unsetConcreteSyntaxType(simpleName);
}
return nothing();
}
}
static public class Unimport extends
org.rascalmpl.ast.ShellCommand.Unimport {
public Unimport(ISourceLocation __param1, IConstructor tree, QualifiedName __param2, OptionalTerminator term) {
super(__param1, tree, __param2, term);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
((ModuleEnvironment) __eval.getCurrentEnvt().getRoot()).unImport(Names.fullName(this.getName()));
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
static public class Unextend extends org.rascalmpl.ast.ShellCommand.Unextend {
public Unextend(ISourceLocation src, IConstructor node, QualifiedName name, OptionalTerminator terminator) {
super(src, node, name, terminator);
}
@Override
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
// ((ModuleEnvironment) __eval.getCurrentEnvt().getRoot()).unExtend(Names.fullName(this.getName()));
__eval.getOutPrinter().println(":unextend is not implemented.");
return org.rascalmpl.interpreter.result.ResultFactory.nothing();
}
}
public ShellCommand(ISourceLocation __param1, IConstructor tree) {
super(__param1, tree);
}
}