Method: doEvaluate(String)

1: package org.overture.interpreter.debug;
2:
3: import java.io.BufferedReader;
4: import java.io.File;
5: import java.io.IOException;
6: import java.io.PrintWriter;
7: import java.io.Reader;
8: import java.io.StringReader;
9: import java.io.StringWriter;
10: import java.io.Writer;
11: import java.util.List;
12: import java.util.Vector;
13:
14: import org.overture.interpreter.commands.ClassCommandReader;
15: import org.overture.interpreter.commands.ModuleCommandReader;
16: import org.overture.interpreter.messages.rtlog.RTLogger;
17: import org.overture.interpreter.runtime.ClassInterpreter;
18: import org.overture.interpreter.runtime.DebuggerException;
19: import org.overture.interpreter.runtime.Interpreter;
20: import org.overture.interpreter.runtime.ModuleInterpreter;
21: import org.overture.interpreter.util.ExitStatus;
22: import org.overture.parser.messages.VDMErrorsException;
23: import org.overture.parser.syntax.ParserException;
24:
25: public class DBGPExecProcesser
26: {
27:         public static class DBGPExecResult
28:         {
29:                 public final String result;
30:                 public final boolean quit;
31:
32:                 public DBGPExecResult(boolean quit, String result)
33:                 {
34:                         this.quit = quit;
35:                         this.result = result;
36:                 }
37:         }
38:
39:         static boolean quitRequest = false;
40:
41:         public static DBGPExecResult process(DBGPReader reader,
42:                         Interpreter interpreter, String command)
43:         {
44:
45:                 Interpreter i = interpreter;
46:                 final DBGPReader d = reader;
47:                 List<File> fileList = new Vector<File>(i.getSourceFiles());
48:                 final Writer result = new StringWriter();
49:                 final Reader input = new StringReader(command);
50:                 // System.out.println("Command session started in " + new File(".").getAbsolutePath());
51:
52:                 if (i instanceof ClassInterpreter)
53:                 {
54:                         new ClassCommandReader((ClassInterpreter) i, "", true)
55:                         {
56:                                 private PrintWriter stdout = new PrintWriter(result, true);
57:                                 private BufferedReader stdin = new BufferedReader(input);
58:
59:                                 @Override
60:                                 protected PrintWriter getStdout()
61:                                 {
62:                                         return stdout;
63:                                 }
64:
65:                                 @Override
66:                                 protected BufferedReader getStdin()
67:                                 {
68:                                         return stdin;
69:                                 }
70:
71:                                 @Override
72:                                 protected boolean notAvailable(String line)
73:                                 {
74:                                         println("Command not available from here");
75:                                         return true;
76:                                 }
77:
78:                                 protected boolean doEvaluate(String line)
79:                                 {
80:                                         line = line.substring(line.indexOf(' ') + 1);
81:
82:                                         try
83:                                         {
84:                                                 // long before = System.currentTimeMillis();
85:                                                 println("= " + interpreter.execute(line, d));
86:                                                 // long after = System.currentTimeMillis();
87:                                                 // println("Executed in " + (double)(after-before)/1000 + " secs. ");
88:
89:•                                                if (RTLogger.getLogSize() > 0)
90:                                                 {
91:                                                         println("Dumped RT events");
92:                                                         RTLogger.dump(false);
93:                                                 }
94:                                         } catch (ParserException e)
95:                                         {
96:                                                 println("Syntax: " + e.getMessage());
97:                                         } catch (DebuggerException e)
98:                                         {
99:                                                 println("Debug: " + e.getMessage());
100:                                         } catch (RuntimeException e)
101:                                         {
102:                                                 println("Runtime: " + e);
103:                                         } catch (VDMErrorsException e)
104:                                         {
105:                                                 println(e.toString());
106:                                         } catch (Exception e)
107:                                         {
108:                                                 println("Error: " + e.getMessage());
109:                                         }
110:
111:                                         return true;
112:                                 }
113:
114:                                 @Override
115:                                 protected boolean doRuntrace(String line, boolean debug)
116:                                 {
117:                                         if (!debug)
118:                                         {
119:                                                 return super.doRuntrace(line, debug);
120:                                         }
121:
122:                                         return notAvailable(line);
123:                                 }
124:
125:                                 @Override
126:                                 protected boolean doReLoad(String line)
127:                                 {
128:                                         return notAvailable(line);
129:                                 }
130:
131:                                 @Override
132:                                 protected boolean doLoad(String line, List<File> filenames)
133:                                 {
134:                                         return notAvailable(line);
135:                                 }
136:
137:                                 @Override
138:                                 protected boolean doRemove(String line)
139:                                 {
140:                                         return notAvailable(line);
141:                                 }
142:
143:                                 @Override
144:                                 protected boolean doList(String line)
145:                                 {
146:                                         return notAvailable(line);
147:                                 }
148:
149:                                 @Override
150:                                 protected boolean doSource(String line)
151:                                 {
152:                                         return notAvailable(line);
153:                                 }
154:
155:                                 @Override
156:                                 protected boolean doBreak(String line) throws Exception
157:                                 {
158:                                         return notAvailable(line);
159:                                 }
160:
161:                                 @Override
162:                                 protected boolean doTrace(String line) throws Exception
163:                                 {
164:                                         return notAvailable(line);
165:                                 }
166:
167:                                 @Override
168:                                 protected boolean doStep(String line)
169:                                 {
170:                                         return notAvailable(line);
171:                                 }
172:
173:                                 @Override
174:                                 protected boolean doNext(String line)
175:                                 {
176:                                         return notAvailable(line);
177:                                 }
178:
179:                                 @Override
180:                                 protected boolean doOut(String line)
181:                                 {
182:                                         return notAvailable(line);
183:                                 }
184:
185:                                 @Override
186:                                 protected boolean doStack(String line)
187:                                 {
188:                                         return notAvailable(line);
189:                                 }
190:
191:                                 @Override
192:                                 protected boolean doUp(String line)
193:                                 {
194:                                         return notAvailable(line);
195:                                 }
196:
197:                                 @Override
198:                                 protected boolean doDown(String line)
199:                                 {
200:                                         return notAvailable(line);
201:                                 }
202:
203:                                 @Override
204:                                 protected boolean doContinue(String line)
205:                                 {
206:                                         return notAvailable(line);
207:                                 }
208:
209:                                 // @Override
210:                                 // protected boolean doThreads(String line)
211:                                 // {
212:                                 // return notAvailable(line);
213:                                 // }
214:
215:                                 @Override
216:                                 public ExitStatus run(List<File> filenames)
217:                                 {
218:                                         Interpreter.setTraceOutput(stdout);
219:                                         setDebugReader(d);
220:                                         return super.run(filenames);
221:                                 }
222:
223:                                 @Override
224:                                 protected boolean doQuit(String line)
225:                                 {
226:                                         boolean ok = super.doQuit(line);
227:                                         d.complete(ok ? DBGPReason.OK : DBGPReason.ERROR, null);
228:                                         quitRequest = true;
229:                                         return ok;
230:                                 }
231:
232:                                 @Override
233:                                 protected void doHelp(String line)
234:                                 {
235:                                         println("classes - list the loaded class names");
236:                                         println("default <class> - set the default class name");
237:                                         println("create <id> := <exp> - create a named variable");
238:                                         println("print <expression> - evaluate expression");
239:                                         println("runtrace <name> [test number] - run CT trace(s)");
240:                                         println("filter %age | <reduction type> - reduce CT trace(s)");
241:                                         println("assert <file> - run assertions from a file");
242:                                         println("init - re-initialize the global environment");
243:                                         println("env - list the global symbols in the default environment");
244:                                         println("pog [<function/operation>] - generate proof obligations");
245:                                         println("coverage clear|write <dir>|merge <dir>|<filenames> - handle line coverage");
246:                                         println("latex|latexdoc [<files>] - generate LaTeX line coverage files");
247:                                         println("word [<files>] - generate Word HTML line coverage files");
248:                                         println("files - list files in the current specification");
249:                                         println("set [<pre|post|inv|dtc|measures> <on|off>] - set runtime checks");
250:                                         println("quit - leave the interpreter");
251:                                 }
252:
253:                         }.run(fileList);
254:                 } else
255:                 {
256:                         new ModuleCommandReader((ModuleInterpreter) i, "", true)
257:                         {
258:                                 private PrintWriter stdout = new PrintWriter(result, true);
259:                                 private BufferedReader stdin = new BufferedReader(input);
260:
261:                                 @Override
262:                                 protected PrintWriter getStdout()
263:                                 {
264:                                         return stdout;
265:                                 }
266:
267:                                 @Override
268:                                 protected BufferedReader getStdin()
269:                                 {
270:                                         return stdin;
271:                                 }
272:
273:                                 @Override
274:                                 protected boolean notAvailable(String line)
275:                                 {
276:                                         println("Command not available from here");
277:                                         return true;
278:                                 }
279:
280:                                 protected boolean doEvaluate(String line)
281:                                 {
282:                                         line = line.substring(line.indexOf(' ') + 1);
283:
284:                                         try
285:                                         {
286:                                                 // long before = System.currentTimeMillis();
287:                                                 println("= " + interpreter.execute(line, d));
288:                                                 // long after = System.currentTimeMillis();
289:                                                 // println("Executed in " + (double)(after-before)/1000 + " secs. ");
290:
291:                                                 if (RTLogger.getLogSize() > 0)
292:                                                 {
293:                                                         println("Dumped RT events");
294:                                                         RTLogger.dump(false);
295:                                                 }
296:                                         } catch (ParserException e)
297:                                         {
298:                                                 println("Syntax: " + e.getMessage());
299:                                         } catch (DebuggerException e)
300:                                         {
301:                                                 println("Debug: " + e.getMessage());
302:                                         } catch (RuntimeException e)
303:                                         {
304:                                                 println("Runtime: " + e);
305:                                         } catch (VDMErrorsException e)
306:                                         {
307:                                                 println(e.toString());
308:                                         } catch (Exception e)
309:                                         {
310:                                                 println("Error: " + e.getMessage());
311:                                         }
312:
313:                                         return true;
314:                                 }
315:
316:                                 @Override
317:                                 protected boolean doRuntrace(String line, boolean debug)
318:                                 {
319:                                         if (!debug)
320:                                         {
321:                                                 return super.doRuntrace(line, debug);
322:                                         }
323:
324:                                         return notAvailable(line);
325:                                 }
326:
327:                                 @Override
328:                                 protected boolean doReLoad(String line)
329:                                 {
330:                                         return notAvailable(line);
331:                                 }
332:
333:                                 @Override
334:                                 protected boolean doLoad(String line, List<File> filenames)
335:                                 {
336:                                         return notAvailable(line);
337:                                 }
338:
339:                                 @Override
340:                                 protected boolean doRemove(String line)
341:                                 {
342:                                         return notAvailable(line);
343:                                 }
344:
345:                                 @Override
346:                                 protected boolean doList(String line)
347:                                 {
348:                                         return notAvailable(line);
349:                                 }
350:
351:                                 @Override
352:                                 protected boolean doSource(String line)
353:                                 {
354:                                         return notAvailable(line);
355:                                 }
356:
357:                                 @Override
358:                                 protected boolean doBreak(String line) throws Exception
359:                                 {
360:                                         return notAvailable(line);
361:                                 }
362:
363:                                 @Override
364:                                 protected boolean doTrace(String line) throws Exception
365:                                 {
366:                                         return notAvailable(line);
367:                                 }
368:
369:                                 @Override
370:                                 protected boolean doStep(String line)
371:                                 {
372:                                         return notAvailable(line);
373:                                 }
374:
375:                                 @Override
376:                                 protected boolean doNext(String line)
377:                                 {
378:                                         return notAvailable(line);
379:                                 }
380:
381:                                 @Override
382:                                 protected boolean doOut(String line)
383:                                 {
384:                                         return notAvailable(line);
385:                                 }
386:
387:                                 @Override
388:                                 protected boolean doStack(String line)
389:                                 {
390:                                         return notAvailable(line);
391:                                 }
392:
393:                                 @Override
394:                                 protected boolean doUp(String line)
395:                                 {
396:                                         return notAvailable(line);
397:                                 }
398:
399:                                 @Override
400:                                 protected boolean doDown(String line)
401:                                 {
402:                                         return notAvailable(line);
403:                                 }
404:
405:                                 @Override
406:                                 protected boolean doContinue(String line)
407:                                 {
408:                                         return notAvailable(line);
409:                                 }
410:
411:                                 @Override
412:                                 protected boolean doThreads(String line)
413:                                 {
414:                                         return notAvailable(line);
415:                                 }
416:
417:                                 @Override
418:                                 public ExitStatus run(List<File> filenames)
419:                                 {
420:                                         Interpreter.setTraceOutput(stdout);
421:                                         setDebugReader(d);
422:                                         return super.run(filenames);
423:                                 }
424:
425:                                 @Override
426:                                 protected boolean doQuit(String line)
427:                                 {
428:                                         boolean ok = super.doQuit(line);
429:                                         d.complete(ok ? DBGPReason.OK : DBGPReason.ERROR, null);
430:                                         quitRequest = true;
431:                                         return ok;
432:                                 }
433:
434:                                 @Override
435:                                 protected void doHelp(String line)
436:                                 {
437:                                         println("modules - list the loaded module names");
438:                                         println("default <module> - set the default module name");
439:                                         println("state - show the default module state");
440:                                         println("print <expression> - evaluate expression");
441:                                         println("runtrace <name> [test number] - run CT trace(s)");
442:                                         println("filter %age | <reduction type> - reduce CT trace(s)");
443:                                         println("assert <file> - run assertions from a file");
444:                                         println("init - re-initialize the global environment");
445:                                         println("env - list the global symbols in the default environment");
446:                                         println("pog [<function/operation>] - generate proof obligations");
447:                                         println("coverage clear|write <dir>|merge <dir>|<filenames> - handle line coverage");
448:                                         println("latex|latexdoc [<files>] - generate LaTeX line coverage files");
449:                                         println("word [<files>] - generate Word HTML line coverage files");
450:                                         println("files - list files in the current specification");
451:                                         println("set [<pre|post|inv|dtc|measures> <on|off>] - set runtime checks");
452:                                         println("quit - leave the interpreter");
453:                                 }
454:
455:                         }.run(fileList);
456:                 }
457:                 try
458:                 {
459:                         result.flush();
460:                         result.close();
461:                 } catch (IOException e)
462:                 {
463:                         // TODO Auto-generated catch block
464:                         e.printStackTrace();
465:                 }
466:
467:                 return new DBGPExecResult(quitRequest, result.toString());
468:         }
469: }