1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript;
23
24 import org.millscript.commons.alert.Alert;
25 import org.millscript.commons.alert.EscapeException;
26 import org.millscript.commons.alert.reporters.StandardMishapAlertReporter;
27 import org.millscript.commons.alert.reporters.StandardWarningAlertReporter;
28 import org.millscript.millscript.alert.Alerts;
29 import org.millscript.millscript.syntax.Parser;
30 import org.millscript.millscript.syntax.ParserImpl;
31
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.Reader;
35
36 import jline.ConsoleReader;
37 import jline.ConsoleReaderInputStream;
38
39 /**
40 * This class is used to start an interactive MillScript session.
41 */
42 public class Interactive extends Session {
43
44 /**
45 * Constructs a new interactive session object.
46 */
47 public Interactive() {
48 config.getLogger().println( "MillScript " + Resources.getMessage( "version" ) );
49 }
50
51 /**
52 * @see org.millscript.millscript.Session#compile(java.lang.String, java.io.Reader)
53 */
54 @Override
55 public void compile( final String origin, final Reader r ) {
56 Parser parser = new ParserImpl( origin, r, true, config );
57
58
59 for (;;) {
60 try {
61
62 this.state.compileAllowResults( parser, true );
63
64
65 return;
66 } catch ( Exception e ) {
67
68 this.config.reportException( e );
69 } finally {
70
71
72
73
74
75
76 System.out.flush();
77 System.err.flush();
78 engine.getConfig().getLogger().println( "Resetting input and resuming execution" );
79 try {
80
81 while ( r.ready() ) {
82 r.skip( 1 );
83 }
84 parser = new ParserImpl( origin, r, true, config );
85 } catch ( IOException ex ) {
86
87 engine.getConfig().getLogger().println( "!!! Input cannot be reset, resuming anyway" );
88 }
89 engine.getConfig().getLogger().println( "" );
90 System.out.flush();
91 System.err.flush();
92 engine.getMachine().reset();
93 }
94 }
95 }
96
97 /**
98 * Runs an interactive session first compiling and executing the specified
99 * MillScript files.
100 *
101 * @param args array of String arguments, treated as files to compile and
102 * execute before starting an interactive session
103 */
104 public static void main( final String[] args ) {
105 try {
106 final Interactive interactive = new Interactive();
107 long startTime = System.currentTimeMillis();
108 interactive.compile( args );
109 final ConsoleReaderInputStream cris = new ConsoleReaderInputStream(
110 new ConsoleReader()
111 );
112 cris.setPrompt( interactive.config.getInteractivePrompt() );
113 interactive.compile(
114 "origin",
115 new InputStreamReader(
116 cris,
117 "UTF-8"
118 )
119 );
120 interactive.printStatistics( startTime );
121 interactive.summarizeResults();
122 } catch ( Alert ex ) {
123 if ( ex.isMishap() ) {
124 StandardMishapAlertReporter.MISHAP_REPORTER.report( ex );
125 } else {
126 StandardWarningAlertReporter.WARNING_REPORTER.report( ex );
127 }
128 } catch ( EscapeException ex ) {
129 if ( ex.getAlert().isMishap() ) {
130 StandardMishapAlertReporter.MISHAP_REPORTER.report( ex.getAlert() );
131 } else {
132 StandardWarningAlertReporter.WARNING_REPORTER.report( ex.getAlert() );
133 }
134 } catch ( IOException ex ) {
135 throw(
136 Alerts.compile(
137 "Problem opening console for interactive session",
138 null
139 ).
140 culprit( "reason", ex.getMessage() ).
141 mishap()
142 );
143 }
144 }
145
146 }