Запуск из командной строки

This commit is contained in:
Victor 2016-01-19 18:25:19 +02:00
parent 5f25d6be07
commit da2bedad52

View File

@ -18,18 +18,59 @@ import java.util.List;
public final class Main {
public static void main(String[] args) throws IOException {
final String file = "program.own";
final String input = new String( Files.readAllBytes(Paths.get(file)), "UTF-8");
final List<Token> tokens = new Lexer(input).tokenize();
for (int i = 0; i < tokens.size(); i++) {
System.out.println(i + " " + tokens.get(i));
if (args.length == 0) {
run(readFile("program.own"), true, true);
return;
}
boolean showTokens = false, showAst = false;
String input = null;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-a":
case "--showast":
showAst = true;
break;
case "-t":
case "--showtokens":
showTokens = true;
break;
case "-f":
case "--file":
if (i + 1 < args.length) {
input = readFile(args[i + 1]);
i++;
}
break;
default:
input = args[i];
}
}
if (input == null) {
throw new IllegalArgumentException("Empty input");
}
run(input, showTokens, showAst);
}
private static String readFile(String file) throws IOException {
return new String( Files.readAllBytes(Paths.get(file)), "UTF-8");
}
private static void run(String input, boolean showTokens, boolean showAst) {
final List<Token> tokens = new Lexer(input).tokenize();
if (showTokens) {
for (int i = 0; i < tokens.size(); i++) {
System.out.println(i + " " + tokens.get(i));
}
}
// for (Token token : tokens) {
// System.out.println(token);
// }
final Statement program = new Parser(tokens).parse();
System.out.println(program.toString());
if (showAst) {
System.out.println(program.toString());
}
program.accept(new FunctionAdder());
// program.accept(new VariablePrinter());
program.accept(new AssignValidator());