Menu

[r15]: / trunk / src / bex / Interpreter.java  Maximize  Restore  History

Download this file

265 lines (237 with data), 8.4 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
/*
* $Id: Interpreter.java,v 1.10 2006/06/18 19:10:38 juhal Exp $
*/
package bex;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
public class Interpreter implements Runnable {
public static final String VERSION = "1.1";
private EvalContext context;
private boolean quiet;
public Interpreter() {
try {
init(null, null, null, null, null);
} catch (Exception ex) {
// this should not happen with the defaults
throw new AssertionException(
"interpreter initialization failed", ex);
}
}
public Interpreter(
JavaBridge bridge,
Environment root,
Reader stdin,
PrintWriter stdout,
PrintWriter stderr)
throws EvalException, InternalEvalException {
init(bridge, root, stdin, stdout, stderr);
}
private void init(
JavaBridge bridge,
Environment root,
Reader stdin,
PrintWriter stdout,
PrintWriter stderr)
throws EvalException, InternalEvalException {
if (bridge == null)
bridge = new ReflectJavaBridge();
if (root == null)
root = getDefaultEnv(bridge);
this.context = new EvalContext(bridge, root, stdin, stdout, stderr);
}
public EvalContext getContext() {
return context;
}
public boolean isQuiet() {
return quiet;
}
public void setQuiet(boolean quiet) {
this.quiet = quiet;
}
public Object eval(String script)
throws ParseException, EvalException {
return eval(new StringReader(script));
}
public Object eval(File file)
throws ParseException, EvalException, IOException {
return eval(context, file);
}
public Object eval(Reader reader)
throws ParseException, EvalException {
return eval(context, reader);
}
public static Object eval(EvalContext ctx, String script)
throws ParseException, EvalException {
return eval(ctx, new StringReader(script));
}
public static Object eval(EvalContext ctx, File file)
throws ParseException, EvalException, IOException {
Reader reader = new BufferedReader(new FileReader(file));
try {
return eval(ctx, reader, file.getName());
} finally {
reader.close();
}
}
public static Object eval(EvalContext ctx, Reader reader)
throws ParseException, EvalException {
return eval(ctx, reader, null);
}
public static Object eval(EvalContext ctx, Reader reader, String sourceFile)
throws ParseException, EvalException {
Parser parser = new Parser(reader);
Object value = null;
while(true) {
Expression exp = parser.read();
if (exp == null) break;
if (exp == Expression.VOID) continue;
Node node = (Node)exp;
node.setSourceFile(sourceFile);
value = eval(ctx, exp);
if (value instanceof ReturnControl) break;
}
return value;
}
public static Object eval(EvalContext ctx, Expression exp)
throws EvalException {
try {
return exp.eval(ctx);
} catch (EvalException ee) {
ee.addScriptStackTraceElement((Node)exp);
throw ee;
}
}
public void put(String name, Object value)
throws EvalException, InternalEvalException {
context.root().setVariable(context, name, value);
}
public Object get(String name)
throws EvalException, InternalEvalException {
return context.root().getVariable(context, name);
}
public static Environment getDefaultEnv(JavaBridge bridge) {
Environment defenv = new Environment();
try {
EvalContext ctx = new EvalContext(bridge, defenv);
InputStream in = Interpreter.class.getResourceAsStream("default.bex");
if (in == null) throw new AssertionException("default.bex not found");
try {
eval(ctx, new InputStreamReader(in), "default.bex");
return defenv;
} finally {
in.close();
}
} catch (Exception e) {
System.err.println("failed to load default environment");
e.printStackTrace(System.err);
}
return new Environment();
}
private String getString(EvalContext ctx, String name, String defval) {
try {
Object val = ctx.root().getVariable(ctx, name);
if (val instanceof String) {
return (String) val;
} else if (val instanceof Callable) {
return String
.valueOf(((Callable) val).call(ctx, new Object[0]));
}
return (val != Primitive.VOID) ? String.valueOf(val) : defval;
} catch (Exception e) {
return defval;
}
}
private String getPrompt() {
return getString(context, "bex_prompt", "bex> ");
}
private String getMotd() {
return getString(context, "bex_motd",
"Bex script " + VERSION + " (c) Juha Lindström 2005-2006");
}
public void run() {
Expression exp = null;
Parser parser = new Parser(context.getStdin());
PrintWriter stdout = context.getStdout();
PrintWriter stderr = context.getStderr();
if (!isQuiet()) stdout.println(getMotd());
while(true) {
try {
if (!isQuiet()) {
stdout.print(getPrompt());
stdout.flush();
}
exp = parser.read();
if (exp == null) break;
if (exp == Expression.VOID) continue;
Object value = eval(context, exp);
if (!isQuiet()) stdout.println(value);
put("$_", value);
} catch (TokenMgrError e) {
stderr.println(e);
stderr.flush();
parser.ReInit(context.getStdin());
} catch (ParseException e) {
stderr.println(e);
stderr.flush();
parser.ReInit(context.getStdin());
} catch (TargetException te) {
te.printStackTrace(stderr);
stderr.flush();
} catch (EvalException ee) {
stderr.println(ee);
stderr.flush();
} catch (Throwable e) {
e.printStackTrace(stderr);
stderr.flush();
}
}
}
private static void evalBexrc(Interpreter interpreter) {
File f = new File(System.getenv("HOME"), ".bexrc");
if (f.exists()) {
try {
interpreter.eval(f);
} catch (Exception e) {
System.err.println("failed to evaluate .bexrc");
e.printStackTrace(System.err);
}
}
}
public static void main(String args[])
throws Exception {
int i = 0;
Interpreter interpreter = new Interpreter();
if (args.length > i && "-q".equals(args[i])) {
i++;
interpreter.setQuiet(true);
}
evalBexrc(interpreter);
boolean interactive = args.length == i;
if (args.length > i && "-i".equals(args[i])) {
i++;
interactive = true;
}
try {
if (args.length > i) {
String[] sargs = new String[args.length - (i + 1)];
if (sargs.length > 0)
System.arraycopy(args, i + 1, sargs, 0, sargs.length);
interpreter.put("$args", sargs);
interpreter.eval(new File(args[i]));
}
} catch (TargetException te) {
te.printStackTrace(System.err);
} catch (EvalException e) {
System.err.println(e);
}
if (interactive)
interpreter.run();
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.