1
1
package com .annimon .ownlang ;
2
2
3
- import com .annimon .ownlang .utils .TimeMeasurement ;
4
3
import com .annimon .ownlang .exceptions .LexerException ;
5
4
import com .annimon .ownlang .parser .Beautifier ;
6
5
import com .annimon .ownlang .parser .Lexer ;
7
6
import com .annimon .ownlang .parser .Linter ;
7
+ import com .annimon .ownlang .parser .Optimizer ;
8
8
import com .annimon .ownlang .parser .Parser ;
9
9
import com .annimon .ownlang .parser .SourceLoader ;
10
10
import com .annimon .ownlang .parser .Token ;
11
11
import com .annimon .ownlang .parser .ast .Statement ;
12
12
import com .annimon .ownlang .parser .visitors .FunctionAdder ;
13
+ import com .annimon .ownlang .utils .TimeMeasurement ;
13
14
import java .io .IOException ;
14
15
import java .util .List ;
15
16
import java .util .Scanner ;
@@ -35,7 +36,8 @@ public static void main(String[] args) throws IOException {
35
36
options .showAst = true ;
36
37
options .showTokens = true ;
37
38
options .showMeasurements = true ;
38
- options .lintMode = true ;
39
+ options .lintMode = false ;
40
+ options .optimizationLevel = 2 ;
39
41
run (SourceLoader .readSource ("program.own" ), options );
40
42
} catch (IOException ioe ) {
41
43
System .out .println ("OwnLang version " + VERSION + "\n \n " +
@@ -44,6 +46,7 @@ public static void main(String[] args) throws IOException {
44
46
" -f, --file [input] Run program file. Required.\n " +
45
47
" -r, --repl Enter to a REPL mode\n " +
46
48
" -l, --lint Find bugs in code\n " +
49
+ " -o N, --optimize N Perform optimization with N passes\n " +
47
50
" -b, --beautify Beautify source code\n " +
48
51
" -a, --showast Show AST of program\n " +
49
52
" -t, --showtokens Show lexical tokens\n " +
@@ -77,6 +80,20 @@ public static void main(String[] args) throws IOException {
77
80
options .showMeasurements = true ;
78
81
break ;
79
82
83
+ case "-o" :
84
+ case "--optimize" :
85
+ if (i + 1 < args .length ) {
86
+ try {
87
+ options .optimizationLevel = Integer .parseInt (args [i + 1 ]);
88
+ } catch (NumberFormatException nfe ) {
89
+ options .optimizationLevel = 2 ;
90
+ }
91
+ i ++;
92
+ } else {
93
+ options .optimizationLevel = 2 ;
94
+ }
95
+ return ;
96
+
80
97
case "-r" :
81
98
case "--repl" :
82
99
repl ();
@@ -134,19 +151,30 @@ private static void run(String input, Options options) {
134
151
135
152
measurement .start ("Parse time" );
136
153
final Parser parser = new Parser (tokens );
137
- final Statement program = parser .parse ();
154
+ final Statement parsedProgram = parser .parse ();
138
155
measurement .stop ("Parse time" );
139
156
if (options .showAst ) {
140
- System .out .println (program .toString ());
157
+ System .out .println (parsedProgram .toString ());
141
158
}
142
159
if (parser .getParseErrors ().hasErrors ()) {
143
160
System .out .println (parser .getParseErrors ());
144
161
return ;
145
162
}
146
163
if (options .lintMode ) {
147
- Linter .lint (program );
164
+ Linter .lint (parsedProgram );
148
165
return ;
149
166
}
167
+ final Statement program ;
168
+ if (options .optimizationLevel > 0 ) {
169
+ measurement .start ("Optimization time" );
170
+ program = Optimizer .optimize (parsedProgram , options .optimizationLevel );
171
+ measurement .stop ("Optimization time" );
172
+ if (options .showAst ) {
173
+ System .out .println (program .toString ());
174
+ }
175
+ } else {
176
+ program = parsedProgram ;
177
+ }
150
178
program .accept (new FunctionAdder ());
151
179
try {
152
180
measurement .start ("Execution time" );
@@ -203,19 +231,22 @@ private static void repl() {
203
231
private static class Options {
204
232
boolean showTokens , showAst , showMeasurements ;
205
233
boolean lintMode ;
234
+ int optimizationLevel ;
206
235
207
236
public Options () {
208
237
showTokens = false ;
209
238
showAst = false ;
210
239
showMeasurements = false ;
211
240
lintMode = false ;
241
+ optimizationLevel = 1 ;
212
242
}
213
243
214
244
public void validate () {
215
245
if (lintMode == true ) {
216
246
showTokens = false ;
217
247
showAst = false ;
218
248
showMeasurements = false ;
249
+ optimizationLevel = 0 ;
219
250
}
220
251
}
221
252
}
0 commit comments