Немного рефакторинга и исправлений

This commit is contained in:
Victor 2018-12-07 22:19:23 +02:00
parent 795d7660f9
commit eb7430f507
4 changed files with 24 additions and 21 deletions

View File

@ -8,6 +8,8 @@ import java.io.UnsupportedEncodingException;
public class Console { public class Console {
private Console() { }
private static final String FILE_PREFIX = "tmp/"; private static final String FILE_PREFIX = "tmp/";
private static boolean filePrefixEnabled; private static boolean filePrefixEnabled;

View File

@ -40,8 +40,7 @@ public final class Main {
return; return;
} }
final Options options = new Options(); final RunOptions options = new RunOptions();
boolean beautifyMode = false;
String input = null; String input = null;
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
switch (args[i]) { switch (args[i]) {
@ -52,7 +51,7 @@ public final class Main {
case "-b": case "-b":
case "--beautify": case "--beautify":
beautifyMode = true; options.beautifyMode = true;
break; break;
case "-t": case "-t":
@ -116,7 +115,7 @@ public final class Main {
if (input == null) { if (input == null) {
throw new IllegalArgumentException("Empty input"); throw new IllegalArgumentException("Empty input");
} }
if (beautifyMode) { if (options.beautifyMode) {
System.out.println(Beautifier.beautify(input)); System.out.println(Beautifier.beautify(input));
return; return;
} }
@ -124,12 +123,7 @@ public final class Main {
} }
private static void runDefault() throws IOException { private static void runDefault() throws IOException {
final Options options = new Options(); final RunOptions options = new RunOptions();
options.showAst = false;
options.showTokens = false;
options.showMeasurements = false;
options.lintMode = false;
options.optimizationLevel = 0;
run(SourceLoader.readSource("program.own"), options); run(SourceLoader.readSource("program.own"), options);
} }
@ -153,7 +147,7 @@ public final class Main {
System.arraycopy(javaArgs, index, ownlangArgs, 0, ownlangArgs.length); System.arraycopy(javaArgs, index, ownlangArgs, 0, ownlangArgs.length);
} }
private static void run(String input, Options options) { private static void run(String input, RunOptions options) {
options.validate(); options.validate();
final TimeMeasurement measurement = new TimeMeasurement(); final TimeMeasurement measurement = new TimeMeasurement();
measurement.start("Tokenize time"); measurement.start("Tokenize time");
@ -209,16 +203,18 @@ public final class Main {
} }
} }
private static class Options { private static class RunOptions {
boolean showTokens, showAst, showMeasurements; boolean showTokens, showAst, showMeasurements;
boolean lintMode; boolean lintMode;
boolean beautifyMode;
int optimizationLevel; int optimizationLevel;
Options() { RunOptions() {
showTokens = false; showTokens = false;
showAst = false; showAst = false;
showMeasurements = false; showMeasurements = false;
lintMode = false; lintMode = false;
beautifyMode = false;
optimizationLevel = 0; optimizationLevel = 0;
} }

View File

@ -50,7 +50,8 @@ public class ParserTest {
} }
private static void assertStatements(BlockStatement expected, BlockStatement actual) { private static void assertStatements(BlockStatement expected, BlockStatement actual) {
for (int i = 0; i < expected.statements.size(); i++) { final int size = expected.statements.size();
for (int i = 0; i < size; i++) {
assertEquals(expected.statements.get(i).getClass(), actual.statements.get(i).getClass()); assertEquals(expected.statements.get(i).getClass(), actual.statements.get(i).getClass());
} }
} }

View File

@ -36,12 +36,16 @@ public class ProgramsTest {
public static Collection<String> data() { public static Collection<String> data() {
final File resDir = new File(RES_DIR); final File resDir = new File(RES_DIR);
return scanDirectory(resDir) return scanDirectory(resDir)
.map(f -> f.getPath()) .map(File::getPath)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static Stream<File> scanDirectory(File dir) { private static Stream<File> scanDirectory(File dir) {
return Arrays.stream(dir.listFiles()) final File[] files = dir.listFiles();
if (files == null || files.length == 0) {
return Stream.empty();
}
return Arrays.stream(files)
.flatMap(file -> { .flatMap(file -> {
if (file.isDirectory()) { if (file.isDirectory()) {
return scanDirectory(file); return scanDirectory(file);