8
8
import com .annimon .ownlang .parser .SourceLoader ;
9
9
import com .annimon .ownlang .parser .Token ;
10
10
import com .annimon .ownlang .parser .ast .Statement ;
11
- import com .annimon .ownlang .parser .visitors .AssignValidator ;
12
11
import com .annimon .ownlang .parser .visitors .FunctionAdder ;
13
12
import java .io .IOException ;
14
13
import java .util .List ;
@@ -31,28 +30,33 @@ public static String[] getOwnlangArgs() {
31
30
public static void main (String [] args ) throws IOException {
32
31
if (args .length == 0 ) {
33
32
try {
34
- run (SourceLoader .readSource ("program.own" ), true , true , true );
33
+ final Options options = new Options ();
34
+ options .showAst = true ;
35
+ options .showTokens = true ;
36
+ options .showMeasurements = true ;
37
+ run (SourceLoader .readSource ("program.own" ), options );
35
38
} catch (IOException ioe ) {
36
39
System .out .println ("OwnLang version " + VERSION + "\n \n " +
37
40
"Usage: ownlang [options]\n " +
38
41
" options:\n " +
39
42
" -f, --file [input] Run program file. Required.\n " +
40
43
" -r, --repl Enter to a REPL mode\n " +
44
+ " -b, --beautify Beautify source code\n " +
41
45
" -a, --showast Show AST of program\n " +
42
46
" -t, --showtokens Show lexical tokens\n " +
43
47
" -m, --showtime Show elapsed time of parsing and execution" );
44
48
}
45
49
return ;
46
50
}
47
-
48
- boolean showTokens = false , showAst = false , showMeasurements = false ;
51
+
52
+ final Options options = new Options () ;
49
53
boolean beautifyMode = false ;
50
54
String input = null ;
51
55
for (int i = 0 ; i < args .length ; i ++) {
52
56
switch (args [i ]) {
53
57
case "-a" :
54
58
case "--showast" :
55
- showAst = true ;
59
+ options . showAst = true ;
56
60
break ;
57
61
58
62
case "-b" :
@@ -62,12 +66,12 @@ public static void main(String[] args) throws IOException {
62
66
63
67
case "-t" :
64
68
case "--showtokens" :
65
- showTokens = true ;
69
+ options . showTokens = true ;
66
70
break ;
67
71
68
72
case "-m" :
69
73
case "--showtime" :
70
- showMeasurements = true ;
74
+ options . showMeasurements = true ;
71
75
break ;
72
76
73
77
case "-r" :
@@ -99,7 +103,7 @@ public static void main(String[] args) throws IOException {
99
103
System .out .println (Beautifier .beautify (input ));
100
104
return ;
101
105
}
102
- run (input , showTokens , showAst , showMeasurements );
106
+ run (input , options );
103
107
}
104
108
105
109
private static void createOwnLangArgs (String [] javaArgs , int index ) {
@@ -108,12 +112,12 @@ private static void createOwnLangArgs(String[] javaArgs, int index) {
108
112
System .arraycopy (javaArgs , index , ownlangArgs , 0 , ownlangArgs .length );
109
113
}
110
114
111
- private static void run (String input , boolean showTokens , boolean showAst , boolean showMeasurements ) {
115
+ private static void run (String input , Options options ) {
112
116
final TimeMeasurement measurement = new TimeMeasurement ();
113
117
measurement .start ("Tokenize time" );
114
118
final List <Token > tokens = Lexer .tokenize (input );
115
119
measurement .stop ("Tokenize time" );
116
- if (showTokens ) {
120
+ if (options . showTokens ) {
117
121
for (int i = 0 ; i < tokens .size (); i ++) {
118
122
System .out .println (i + " " + tokens .get (i ));
119
123
}
@@ -123,22 +127,21 @@ private static void run(String input, boolean showTokens, boolean showAst, boole
123
127
final Parser parser = new Parser (tokens );
124
128
final Statement program = parser .parse ();
125
129
measurement .stop ("Parse time" );
126
- if (showAst ) {
130
+ if (options . showAst ) {
127
131
System .out .println (program .toString ());
128
132
}
129
133
if (parser .getParseErrors ().hasErrors ()) {
130
134
System .out .println (parser .getParseErrors ());
131
135
return ;
132
136
}
133
137
program .accept (new FunctionAdder ());
134
- program .accept (new AssignValidator ());
135
138
try {
136
139
measurement .start ("Execution time" );
137
140
program .execute ();
138
141
} catch (Exception ex ) {
139
142
Console .handleException (Thread .currentThread (), ex );
140
143
} finally {
141
- if (showMeasurements ) {
144
+ if (options . showMeasurements ) {
142
145
measurement .stop ("Execution time" );
143
146
System .out .println ("======================" );
144
147
System .out .println (measurement .summary (TimeUnit .MILLISECONDS , true ));
@@ -183,4 +186,14 @@ private static void repl() {
183
186
}
184
187
scanner .close ();
185
188
}
189
+
190
+ private static class Options {
191
+ boolean showTokens , showAst , showMeasurements ;
192
+
193
+ public Options () {
194
+ showTokens = false ;
195
+ showAst = false ;
196
+ showMeasurements = false ;
197
+ }
198
+ }
186
199
}
0 commit comments