Skip to content

Commit da2beda

Browse files
committed
Запуск из командной строки
1 parent 5f25d6b commit da2beda

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

src/com/annimon/ownlang/Main.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,59 @@
1818
public final class Main {
1919

2020
public static void main(String[] args) throws IOException {
21-
final String file = "program.own";
22-
final String input = new String( Files.readAllBytes(Paths.get(file)), "UTF-8");
21+
if (args.length == 0) {
22+
run(readFile("program.own"), true, true);
23+
return;
24+
}
25+
26+
boolean showTokens = false, showAst = false;
27+
String input = null;
28+
for (int i = 0; i < args.length; i++) {
29+
switch (args[i]) {
30+
case "-a":
31+
case "--showast":
32+
showAst = true;
33+
break;
34+
35+
case "-t":
36+
case "--showtokens":
37+
showTokens = true;
38+
break;
39+
40+
case "-f":
41+
case "--file":
42+
if (i + 1 < args.length) {
43+
input = readFile(args[i + 1]);
44+
i++;
45+
}
46+
break;
47+
48+
default:
49+
input = args[i];
50+
}
51+
}
52+
if (input == null) {
53+
throw new IllegalArgumentException("Empty input");
54+
}
55+
run(input, showTokens, showAst);
56+
}
57+
58+
private static String readFile(String file) throws IOException {
59+
return new String( Files.readAllBytes(Paths.get(file)), "UTF-8");
60+
}
61+
62+
private static void run(String input, boolean showTokens, boolean showAst) {
2363
final List<Token> tokens = new Lexer(input).tokenize();
24-
for (int i = 0; i < tokens.size(); i++) {
25-
System.out.println(i + " " + tokens.get(i));
64+
if (showTokens) {
65+
for (int i = 0; i < tokens.size(); i++) {
66+
System.out.println(i + " " + tokens.get(i));
67+
}
2668
}
27-
// for (Token token : tokens) {
28-
// System.out.println(token);
29-
// }
3069

3170
final Statement program = new Parser(tokens).parse();
32-
System.out.println(program.toString());
71+
if (showAst) {
72+
System.out.println(program.toString());
73+
}
3374
program.accept(new FunctionAdder());
3475
// program.accept(new VariablePrinter());
3576
program.accept(new AssignValidator());

0 commit comments

Comments
 (0)