blob: 7408a51964f0c0f219a200e31a09e3b6a7c0b0ac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package org.archphantom.shenanigans;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import org.archphantom.shenanigans.elements.Program;
import org.archphantom.shenanigans.parser.Parser;
import org.archphantom.shenanigans.parser.Tokenizer;
public class Shenanigans {
private static final String HELP_STRING = "############################### Shenanigans Help ###############################\r\n"
+ "# #"
+ "# <no arguments>: parse from standard input as Shenanigans code #"
+ "# -h or --HELP: display this help message #"
+ "# -f or --FILE <file>: parse this file as Shenanigans code #"
+ "# -p or --PARSED <file>: load raw parser output into the interpreter #"
+ "# #"
+ "################################################################################";
public static void main (String[] args) {
try {
boolean useparsed = false;
boolean usefiles = false;
ArrayList<InputStream> streams = new ArrayList<InputStream>();
ArrayList<InputStream> pstreams = new ArrayList<InputStream>();
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-h") || args[i].equalsIgnoreCase("--help")) {
System.out.println(HELP_STRING);
return;
}
if ((args[i].equals("-p") || args[i].equalsIgnoreCase("--parsed")) && (i+1) < args.length) {
pstreams.add(new FileInputStream(args[i+1]));
useparsed = true;
}
if ((args[i].equals("-f") || args[i].equalsIgnoreCase("--file")) && (i+1) < args.length) {
streams.add(new FileInputStream(args[i+1]));
usefiles = true;
}
if ((args[i].equals("-f") || args[i].equalsIgnoreCase("--file")) && (i+1) < args.length) {
streams.add(new FileInputStream(args[i+1]));
usefiles = true;
}
}
if (useparsed) {
for (int i = 0; i < pstreams.size(); i++) {
try {
ObjectInputStream oistream = new ObjectInputStream(pstreams.get(i));
Object oread = oistream.readObject();
if (oread instanceof Program) {
Program p = (Program) oread;
p.run();
} else {
System.err.println("Object is not valid parser output.");
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
if (usefiles) {
parse(streams.toArray(new InputStream[] {}));
}
if (!useparsed && !usefiles) {
parse(System.in);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void parse (InputStream[] streams) throws IOException {
for (InputStream stream : streams) {
parse(stream);
}
}
public static void parse (InputStream stream) throws IOException {
Parser parser = new Parser(new Tokenizer(stream));
Program program = parser.program();
program.run();
}
}
|