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
5f25d6be07
commit
da2bedad52
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user