11package com .annimon .ownlang ;
22
3- import com .annimon .ownlang .utils .TimeMeasurement ;
43import com .annimon .ownlang .exceptions .LexerException ;
54import com .annimon .ownlang .parser .Beautifier ;
65import com .annimon .ownlang .parser .Lexer ;
76import com .annimon .ownlang .parser .Linter ;
7+ import com .annimon .ownlang .parser .Optimizer ;
88import com .annimon .ownlang .parser .Parser ;
99import com .annimon .ownlang .parser .SourceLoader ;
1010import com .annimon .ownlang .parser .Token ;
1111import com .annimon .ownlang .parser .ast .Statement ;
1212import com .annimon .ownlang .parser .visitors .FunctionAdder ;
13+ import com .annimon .ownlang .utils .TimeMeasurement ;
1314import java .io .IOException ;
1415import java .util .List ;
1516import java .util .Scanner ;
@@ -35,7 +36,8 @@ public static void main(String[] args) throws IOException {
3536 options .showAst = true ;
3637 options .showTokens = true ;
3738 options .showMeasurements = true ;
38- options .lintMode = true ;
39+ options .lintMode = false ;
40+ options .optimizationLevel = 2 ;
3941 run (SourceLoader .readSource ("program.own" ), options );
4042 } catch (IOException ioe ) {
4143 System .out .println ("OwnLang version " + VERSION + "\n \n " +
@@ -44,6 +46,7 @@ public static void main(String[] args) throws IOException {
4446 " -f, --file [input] Run program file. Required.\n " +
4547 " -r, --repl Enter to a REPL mode\n " +
4648 " -l, --lint Find bugs in code\n " +
49+ " -o N, --optimize N Perform optimization with N passes\n " +
4750 " -b, --beautify Beautify source code\n " +
4851 " -a, --showast Show AST of program\n " +
4952 " -t, --showtokens Show lexical tokens\n " +
@@ -77,6 +80,20 @@ public static void main(String[] args) throws IOException {
7780 options .showMeasurements = true ;
7881 break ;
7982
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+
8097 case "-r" :
8198 case "--repl" :
8299 repl ();
@@ -134,19 +151,30 @@ private static void run(String input, Options options) {
134151
135152 measurement .start ("Parse time" );
136153 final Parser parser = new Parser (tokens );
137- final Statement program = parser .parse ();
154+ final Statement parsedProgram = parser .parse ();
138155 measurement .stop ("Parse time" );
139156 if (options .showAst ) {
140- System .out .println (program .toString ());
157+ System .out .println (parsedProgram .toString ());
141158 }
142159 if (parser .getParseErrors ().hasErrors ()) {
143160 System .out .println (parser .getParseErrors ());
144161 return ;
145162 }
146163 if (options .lintMode ) {
147- Linter .lint (program );
164+ Linter .lint (parsedProgram );
148165 return ;
149166 }
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+ }
150178 program .accept (new FunctionAdder ());
151179 try {
152180 measurement .start ("Execution time" );
@@ -203,19 +231,22 @@ private static void repl() {
203231 private static class Options {
204232 boolean showTokens , showAst , showMeasurements ;
205233 boolean lintMode ;
234+ int optimizationLevel ;
206235
207236 public Options () {
208237 showTokens = false ;
209238 showAst = false ;
210239 showMeasurements = false ;
211240 lintMode = false ;
241+ optimizationLevel = 1 ;
212242 }
213243
214244 public void validate () {
215245 if (lintMode == true ) {
216246 showTokens = false ;
217247 showAst = false ;
218248 showMeasurements = false ;
249+ optimizationLevel = 0 ;
219250 }
220251 }
221252 }
0 commit comments