mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Немного рефакторинга и исправлений
This commit is contained in:
parent
795d7660f9
commit
eb7430f507
@ -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;
|
||||||
|
|
||||||
|
@ -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,19 +51,19 @@ public final class Main {
|
|||||||
|
|
||||||
case "-b":
|
case "-b":
|
||||||
case "--beautify":
|
case "--beautify":
|
||||||
beautifyMode = true;
|
options.beautifyMode = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "-t":
|
case "-t":
|
||||||
case "--showtokens":
|
case "--showtokens":
|
||||||
options.showTokens = true;
|
options.showTokens = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "-m":
|
case "-m":
|
||||||
case "--showtime":
|
case "--showtime":
|
||||||
options.showMeasurements = true;
|
options.showMeasurements = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "-o":
|
case "-o":
|
||||||
case "--optimize":
|
case "--optimize":
|
||||||
if (i + 1 < args.length) {
|
if (i + 1 < args.length) {
|
||||||
@ -88,7 +87,7 @@ public final class Main {
|
|||||||
case "--lint":
|
case "--lint":
|
||||||
options.lintMode = true;
|
options.lintMode = true;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case "-f":
|
case "-f":
|
||||||
case "--file":
|
case "--file":
|
||||||
if (i + 1 < args.length) {
|
if (i + 1 < args.length) {
|
||||||
@ -104,7 +103,7 @@ public final class Main {
|
|||||||
System.arraycopy(ownlangArgs, 0, newArgs, 0, ownlangArgs.length);
|
System.arraycopy(ownlangArgs, 0, newArgs, 0, ownlangArgs.length);
|
||||||
Sandbox.main(newArgs);
|
Sandbox.main(newArgs);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
input = args[i];
|
input = args[i];
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user