Skip to content

Commit 840f049

Browse files
committed
Рефакторинг: параметры запуска в отдельном объекте
1 parent 8f0f379 commit 840f049

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/com/annimon/ownlang/Main.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.annimon.ownlang.parser.SourceLoader;
99
import com.annimon.ownlang.parser.Token;
1010
import com.annimon.ownlang.parser.ast.Statement;
11-
import com.annimon.ownlang.parser.visitors.AssignValidator;
1211
import com.annimon.ownlang.parser.visitors.FunctionAdder;
1312
import java.io.IOException;
1413
import java.util.List;
@@ -31,28 +30,33 @@ public static String[] getOwnlangArgs() {
3130
public static void main(String[] args) throws IOException {
3231
if (args.length == 0) {
3332
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);
3538
} catch (IOException ioe) {
3639
System.out.println("OwnLang version " + VERSION + "\n\n" +
3740
"Usage: ownlang [options]\n" +
3841
" options:\n" +
3942
" -f, --file [input] Run program file. Required.\n" +
4043
" -r, --repl Enter to a REPL mode\n" +
44+
" -b, --beautify Beautify source code\n" +
4145
" -a, --showast Show AST of program\n" +
4246
" -t, --showtokens Show lexical tokens\n" +
4347
" -m, --showtime Show elapsed time of parsing and execution");
4448
}
4549
return;
4650
}
47-
48-
boolean showTokens = false, showAst = false, showMeasurements = false;
51+
52+
final Options options = new Options();
4953
boolean beautifyMode = false;
5054
String input = null;
5155
for (int i = 0; i < args.length; i++) {
5256
switch (args[i]) {
5357
case "-a":
5458
case "--showast":
55-
showAst = true;
59+
options.showAst = true;
5660
break;
5761

5862
case "-b":
@@ -62,12 +66,12 @@ public static void main(String[] args) throws IOException {
6266

6367
case "-t":
6468
case "--showtokens":
65-
showTokens = true;
69+
options.showTokens = true;
6670
break;
6771

6872
case "-m":
6973
case "--showtime":
70-
showMeasurements = true;
74+
options.showMeasurements = true;
7175
break;
7276

7377
case "-r":
@@ -99,7 +103,7 @@ public static void main(String[] args) throws IOException {
99103
System.out.println(Beautifier.beautify(input));
100104
return;
101105
}
102-
run(input, showTokens, showAst, showMeasurements);
106+
run(input, options);
103107
}
104108

105109
private static void createOwnLangArgs(String[] javaArgs, int index) {
@@ -108,12 +112,12 @@ private static void createOwnLangArgs(String[] javaArgs, int index) {
108112
System.arraycopy(javaArgs, index, ownlangArgs, 0, ownlangArgs.length);
109113
}
110114

111-
private static void run(String input, boolean showTokens, boolean showAst, boolean showMeasurements) {
115+
private static void run(String input, Options options) {
112116
final TimeMeasurement measurement = new TimeMeasurement();
113117
measurement.start("Tokenize time");
114118
final List<Token> tokens = Lexer.tokenize(input);
115119
measurement.stop("Tokenize time");
116-
if (showTokens) {
120+
if (options.showTokens) {
117121
for (int i = 0; i < tokens.size(); i++) {
118122
System.out.println(i + " " + tokens.get(i));
119123
}
@@ -123,22 +127,21 @@ private static void run(String input, boolean showTokens, boolean showAst, boole
123127
final Parser parser = new Parser(tokens);
124128
final Statement program = parser.parse();
125129
measurement.stop("Parse time");
126-
if (showAst) {
130+
if (options.showAst) {
127131
System.out.println(program.toString());
128132
}
129133
if (parser.getParseErrors().hasErrors()) {
130134
System.out.println(parser.getParseErrors());
131135
return;
132136
}
133137
program.accept(new FunctionAdder());
134-
program.accept(new AssignValidator());
135138
try {
136139
measurement.start("Execution time");
137140
program.execute();
138141
} catch (Exception ex) {
139142
Console.handleException(Thread.currentThread(), ex);
140143
} finally {
141-
if (showMeasurements) {
144+
if (options.showMeasurements) {
142145
measurement.stop("Execution time");
143146
System.out.println("======================");
144147
System.out.println(measurement.summary(TimeUnit.MILLISECONDS, true));
@@ -183,4 +186,14 @@ private static void repl() {
183186
}
184187
scanner.close();
185188
}
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+
}
186199
}

0 commit comments

Comments
 (0)