-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.java
More file actions
212 lines (191 loc) · 6.94 KB
/
Terminal.java
File metadata and controls
212 lines (191 loc) · 6.94 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package edu.kit.informatik;
import java.io.*;
public final class Terminal {
private static final BufferedReader IN = new BufferedReader(new InputStreamReader(System.in));
private static Terminal singleton;
public static final String MAIN_PREFIX="# ";
public static final String MESSAGE_PREFIX ="$ ";
public static final String COMMAND_PREFIX="> ";
public static final String COMMENT_PREFIX="//";
private BufferedReader FIN;
private int linenumber = 0;
private Line commandbuffer;
private Line argsbuffer;
private Thread actual;
public static Terminal getInstance() {
if (singleton == null)
singleton = new Terminal();
return singleton;
}
private Terminal() {
try {
FIN = new BufferedReader(new FileReader(
new File("C:\\Fill\\in\\the\\path\\to\\your\\test.file"))); //TODO Fill in the absolute path to your test-file
} catch (FileNotFoundException e) {
System.out.println("Please add File");
e.printStackTrace();
}
}
static class Runab implements Runnable{
String t;
Runab(String a){
this.t =a;
}
@Override
public void run() {
YourMain.main(t.split(" ")); //TODO Fill in the name of your Main-Class
}
}
public static void printLine(Object o) {
if (singleton == null) {
System.out.println(o);
return;
}
getInstance().printLineAndCompare(o);
}
public static String readLine() {
if (singleton == null)
try {
return IN.readLine();
} catch (IOException e) {
e.printStackTrace();
return "Terminal-Error";
}
return getInstance().readLineAndCompare();
}
public static void printError(final String message) {
Terminal.printLine("Error, " + message);
}
public static void printLine(final char[] charArray) {
printLine(new String(charArray));
}
public static void main(String[] args) {
do {
Line tmp;
boolean gg = true;
do {
if(getInstance().argsbuffer==null)
tmp = getInstance().getNext();
else {
tmp = getInstance().argsbuffer;
getInstance().argsbuffer = null;
}
if (tmp.getLine() == -1)
return;
if (tmp.getText().startsWith("> ")) {
System.out.println("Programm finished to early. Line " + tmp.getLine() + " could not be called");
gg = false;
} else if ((!tmp.getText().startsWith(MAIN_PREFIX)) & gg)
getInstance().issueError(tmp, "<nothing>");
} while (!tmp.getText().startsWith(MAIN_PREFIX));
getInstance().actual = new Thread(new Runab(tmp.getText().substring(2)));
getInstance().actual.start();
try {
getInstance().actual.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (singleton != null);
}
private String readLineAndCompare() {
if (commandbuffer != null) {
return commandbuffer.getText();
}
if (argsbuffer != null) {
System.out.println("Error in line " + linenumber + ": Program should already be terminated!");
actual.stop();
return "Game should be terminated";
}
Line tmp;
do {
tmp = getNext();
if (tmp.getLine() == -1) {
try {
return IN.readLine();
} catch (IOException e) {
e.printStackTrace();
return "Error!";
}
}
if (tmp.getText().startsWith("# ")) {
argsbuffer = tmp;
System.out.println("Error in line " + linenumber + ": Program should already be terminated!");
actual.stop();
return readLineAndCompare();
}
if (!tmp.getText().startsWith("> "))
issueError(new Line(linenumber,"<nothing>"),tmp.getText());
} while (!tmp.getText().startsWith("> "));
return tmp.getText().substring(2);
}
private Line getNext() {
String tmp;
do {
try {
linenumber++;
tmp = FIN.readLine();
} catch (IOException e) {
tmp = null;
}
if (tmp == null) {
singleton = null;
return new Line(-1, "");
}
if (tmp.startsWith(MESSAGE_PREFIX)) {
System.out.println(tmp.substring(2));
}
} while (tmp.equals("") | tmp.startsWith(COMMENT_PREFIX) | tmp.startsWith(MESSAGE_PREFIX));
return new Line(linenumber, tmp);
}
private void printLineAndCompare(Object o) {
if (o.toString().contains("\n")) {
for (String s : o.toString().split("\n"))
printLineAndCompare(s);
return;
}
if (commandbuffer != null) {
issueError(new Line(linenumber, "<nothing>"), o.toString());
return;
}
if (argsbuffer != null) {
issueError(new Line(linenumber, "<nothing>"), o.toString());
return;
}
Line tmp = getNext();
if (tmp.getLine() == -1) {
System.out.println(o.toString());
}
if (tmp.getText().startsWith(COMMAND_PREFIX)) {
commandbuffer = tmp;
issueError(new Line(linenumber, "<nothing>"), o.toString());
} else if (tmp.getText().startsWith("# ")) {
argsbuffer = tmp;
issueError(new Line(linenumber, "<nothing>"), o.toString());
} else {
try {
if (!o.toString().matches(tmp.getText()))
issueError(tmp, o.toString());
} catch (Exception e){
System.out.println("Invalid Regex-Expression in Line "+tmp.getLine()+": "+tmp.getText());
}
}
}
private void issueError(Line expected, String error) {
System.out.println("Error in line " + expected.getLine() + ": Expected: \"" + expected.getText() + "\""
+ "\nYour Output: \"" + error + "\"\n");
}
private class Line {
private int line;
private String text;
Line(int line, String text) {
this.line = line;
this.text = text;
}
public int getLine() {
return line;
}
public String getText() {
return text;
}
}
}