Skip to content

Commit eb7430f

Browse files
committed
Немного рефакторинга и исправлений
1 parent 795d766 commit eb7430f

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/main/java/com/annimon/ownlang/Console.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
public class Console {
1010

11+
private Console() { }
12+
1113
private static final String FILE_PREFIX = "tmp/";
1214
private static boolean filePrefixEnabled;
1315

src/main/java/com/annimon/ownlang/Main.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public static void main(String[] args) throws IOException {
4040
return;
4141
}
4242

43-
final Options options = new Options();
44-
boolean beautifyMode = false;
43+
final RunOptions options = new RunOptions();
4544
String input = null;
4645
for (int i = 0; i < args.length; i++) {
4746
switch (args[i]) {
@@ -52,19 +51,19 @@ public static void main(String[] args) throws IOException {
5251

5352
case "-b":
5453
case "--beautify":
55-
beautifyMode = true;
54+
options.beautifyMode = true;
5655
break;
57-
56+
5857
case "-t":
5958
case "--showtokens":
6059
options.showTokens = true;
6160
break;
62-
61+
6362
case "-m":
6463
case "--showtime":
6564
options.showMeasurements = true;
6665
break;
67-
66+
6867
case "-o":
6968
case "--optimize":
7069
if (i + 1 < args.length) {
@@ -88,7 +87,7 @@ public static void main(String[] args) throws IOException {
8887
case "--lint":
8988
options.lintMode = true;
9089
return;
91-
90+
9291
case "-f":
9392
case "--file":
9493
if (i + 1 < args.length) {
@@ -104,7 +103,7 @@ public static void main(String[] args) throws IOException {
104103
System.arraycopy(ownlangArgs, 0, newArgs, 0, ownlangArgs.length);
105104
Sandbox.main(newArgs);
106105
return;
107-
106+
108107
default:
109108
if (input == null) {
110109
input = args[i];
@@ -116,20 +115,15 @@ public static void main(String[] args) throws IOException {
116115
if (input == null) {
117116
throw new IllegalArgumentException("Empty input");
118117
}
119-
if (beautifyMode) {
118+
if (options.beautifyMode) {
120119
System.out.println(Beautifier.beautify(input));
121120
return;
122121
}
123122
run(input, options);
124123
}
125124

126125
private static void runDefault() throws IOException {
127-
final Options options = new Options();
128-
options.showAst = false;
129-
options.showTokens = false;
130-
options.showMeasurements = false;
131-
options.lintMode = false;
132-
options.optimizationLevel = 0;
126+
final RunOptions options = new RunOptions();
133127
run(SourceLoader.readSource("program.own"), options);
134128
}
135129

@@ -153,7 +147,7 @@ private static void createOwnLangArgs(String[] javaArgs, int index) {
153147
System.arraycopy(javaArgs, index, ownlangArgs, 0, ownlangArgs.length);
154148
}
155149

156-
private static void run(String input, Options options) {
150+
private static void run(String input, RunOptions options) {
157151
options.validate();
158152
final TimeMeasurement measurement = new TimeMeasurement();
159153
measurement.start("Tokenize time");
@@ -209,16 +203,18 @@ private static void run(String input, Options options) {
209203
}
210204
}
211205

212-
private static class Options {
206+
private static class RunOptions {
213207
boolean showTokens, showAst, showMeasurements;
214208
boolean lintMode;
209+
boolean beautifyMode;
215210
int optimizationLevel;
216211

217-
Options() {
212+
RunOptions() {
218213
showTokens = false;
219214
showAst = false;
220215
showMeasurements = false;
221216
lintMode = false;
217+
beautifyMode = false;
222218
optimizationLevel = 0;
223219
}
224220

src/test/java/com/annimon/ownlang/parser/ParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ private static BlockStatement assertProgram(String input, BlockStatement actual)
5050
}
5151

5252
private static void assertStatements(BlockStatement expected, BlockStatement actual) {
53-
for (int i = 0; i < expected.statements.size(); i++) {
53+
final int size = expected.statements.size();
54+
for (int i = 0; i < size; i++) {
5455
assertEquals(expected.statements.get(i).getClass(), actual.statements.get(i).getClass());
5556
}
5657
}

src/test/java/com/annimon/ownlang/parser/ProgramsTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ public ProgramsTest(String programPath) {
3636
public static Collection<String> data() {
3737
final File resDir = new File(RES_DIR);
3838
return scanDirectory(resDir)
39-
.map(f -> f.getPath())
39+
.map(File::getPath)
4040
.collect(Collectors.toList());
4141
}
4242

4343
private static Stream<File> scanDirectory(File dir) {
44-
return Arrays.stream(dir.listFiles())
44+
final File[] files = dir.listFiles();
45+
if (files == null || files.length == 0) {
46+
return Stream.empty();
47+
}
48+
return Arrays.stream(files)
4549
.flatMap(file -> {
4650
if (file.isDirectory()) {
4751
return scanDirectory(file);

0 commit comments

Comments
 (0)