Skip to content

Commit d259467

Browse files
committed
Initial Commit
1 parent f49969a commit d259467

18 files changed

+1102
-1
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/MiniJavaCompiler.iml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# MiniJavaCompiler
2-
Compiler for MiniJava
2+
At the moment a lot of Java token are detected but not properly parsed.
3+
I'll cut down on it later.
4+
5+
Also at the moment the Parser is a mess, I'll have to unify the methods still.

resources/input.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
block1 : {
2+
int in, op, res;
3+
4+
res = readInt();
5+
op = readInt();
6+
7+
while (0 < op) {
8+
block2: {
9+
if (op == 1) {
10+
in = readInt();
11+
op = res;
12+
while (0 < in) {
13+
res = res * op;
14+
in = in - 1;
15+
}
16+
}
17+
}
18+
block3 : {
19+
if (op == 2) {
20+
in = res;
21+
res = 0;
22+
while (0 < in) {
23+
res = res + in;
24+
in = in - 1;
25+
}
26+
}
27+
}
28+
block4 : {
29+
if (op == 3) {
30+
in = readInt();
31+
if (0 <= in) {
32+
while (0 < in) {
33+
res = res * 10;
34+
in = in - 1;
35+
}
36+
} else {
37+
block5 : {
38+
in = -in;
39+
while (0 < in) {
40+
res = res / 10;
41+
in = in - 1;
42+
}
43+
}
44+
}
45+
}
46+
}
47+
block6 : {
48+
op = readInt();
49+
}
50+
}
51+
52+
write(res);
53+
}

resources/prim.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int number, divisor;
2+
boolean prim;
3+
4+
number = readInt();
5+
divisor = 2;
6+
prim = true;
7+
8+
while(divisor * divisor <= number && prim) {
9+
if(number % divisor == 0) {
10+
prim = false;
11+
}
12+
divisor = divisor + 1;
13+
}
14+
if(number <= 1) {
15+
prim = false;
16+
}
17+
writeInt(number);

src/pgdp/PinguLib.java

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package pgdp;
2+
3+
import java.io.*;
4+
import java.util.Random;
5+
6+
/**
7+
* This class provides convenience methods to make the barrier of entry into
8+
* programming easier.
9+
* <p>
10+
* <i>Diese Klasse stellt simple Methoden zur Verfügung um den Einstieg in die
11+
* Programmierung zu vereinfachen.</i>
12+
*/
13+
public class PinguLib {
14+
15+
private static InputStream is = System.in;
16+
private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
17+
18+
/**
19+
* Prints the {@link String} to the console and breaks the line.
20+
* <p>
21+
* <i>Gibt den {@link String} auf der Konsole aus und beginnt eine neue
22+
* Zeile.</i>
23+
*/
24+
public static void write(String output) {
25+
System.out.println(output);
26+
}
27+
28+
/**
29+
* Prints the <code>int</code> to the console and breaks the line.
30+
* <p>
31+
* <i>Gibt den <code>int</code>-Wert auf der Konsole aus und beginnt eine neue
32+
* Zeile.</i>
33+
*/
34+
public static void write(int output) {
35+
write(String.valueOf(output));
36+
}
37+
38+
/**
39+
* Prints the <code>double</code> to the console and breaks the line.
40+
* <p>
41+
* <i>Gibt den <code>double</code>-Wert auf der Konsole aus und beginnt eine
42+
* neue Zeile.</i>
43+
*/
44+
public static void write(double output) {
45+
write(String.valueOf(output));
46+
}
47+
48+
/**
49+
* Identical to {@link #write(String)}.
50+
* <p>
51+
* <i>Identisch zu {@link #write(String)}</i>
52+
*/
53+
public static void writeLineConsole(String output) {
54+
System.out.println(output);
55+
}
56+
57+
/**
58+
* Identical to {@link #write(int)}.
59+
* <p>
60+
* <i>Identisch zu {@link #write(int)}</i>
61+
*/
62+
public static void writeLineConsole(int output) {
63+
writeLineConsole(String.valueOf(output));
64+
}
65+
66+
/**
67+
* Identical to {@link #write(double)}.
68+
* <p>
69+
* <i>Identisch zu {@link #write(double)}</i>
70+
*/
71+
public static void writeLineConsole(double output) {
72+
writeLineConsole(String.valueOf(output));
73+
}
74+
75+
/**
76+
* Prints a line separator in the console.
77+
* <p>
78+
* <i>Gibt einen Zeilenumbruch auf der Konsole aus.</i>
79+
*/
80+
public static void writeLineConsole() {
81+
writeLineConsole("");
82+
}
83+
84+
/**
85+
* Prints the {@link String} to the console. Does not end with a line break.
86+
* <p>
87+
* <i>Gibt den {@link String} auf der Konsole aus. Endet nicht mit einem
88+
* Zeilenumbruch.</i>
89+
*/
90+
public static void writeConsole(String output) {
91+
System.out.print(output);
92+
}
93+
94+
/**
95+
* Prints the <code>int</code> to the console. Does not end with a line break.
96+
* <p>
97+
* <i>Gibt den <code>int</code>-Wert auf der Konsole aus. Endet nicht mit einem
98+
* Zeilenumbruch.</i>
99+
*/
100+
public static void writeConsole(int output) {
101+
writeConsole(String.valueOf(output));
102+
}
103+
104+
/**
105+
* Prints the <code>double</code> to the console. Does not end with a line
106+
* break.
107+
* <p>
108+
* <i>Gibt den <code>double</code>-Wert auf der Konsole aus. Endet nicht mit
109+
* einem Zeilenumbruch.</i>
110+
*/
111+
public static void writeConsole(double output) {
112+
writeConsole(String.valueOf(output));
113+
}
114+
115+
/**
116+
* Reads a {@link String} from the console, and prompts the user by printing the
117+
* given <code>text</code> with a line break to the console.
118+
* <p>
119+
*
120+
* <i>Liest einen {@link String} von der Konsole ein, und fordert den oder die
121+
* Benutzerin durch die Ausgabe des übergebenen <code>text</code>es zur Eingabe
122+
* auf (mit Zeilenumbruch).</i>
123+
*
124+
* @param text the text to display on the console before reading an input.
125+
* @return the input string or <code>null</code>, if no input is available
126+
* (should normally not happen)
127+
*/
128+
public static String readString(String text) {
129+
// Exchange the reader in case System.in has changed.
130+
// This is necessary for testing, as for every test input, System.in is
131+
// changed.
132+
if (System.in != is) {
133+
is = System.in;
134+
bufferedReader = new BufferedReader(new InputStreamReader(is));
135+
}
136+
try {
137+
System.out.println(text);
138+
return bufferedReader.readLine();
139+
} catch (IOException e) {
140+
// We "hide" the exception in the method signature by rethrowing an
141+
// unchecked
142+
// exception
143+
throw new UncheckedIOException("Konnte Eingabe nicht lesen.", e);
144+
}
145+
}
146+
147+
/**
148+
* Reads a {@link String} from the console, and prompts the user by printing the
149+
* line
150+
*
151+
* <pre>
152+
* Eingabe:
153+
* </pre>
154+
*
155+
* <i>Liest einen {@link String} von der Konsole ein, und fordert den oder die
156+
* Benutzerin dazu auf durch die Ausgabe von
157+
*
158+
* <pre>
159+
* Eingabe:
160+
* </pre>
161+
*
162+
* </i>
163+
*/
164+
public static String readString() {
165+
return readString("Eingabe:");
166+
}
167+
168+
/**
169+
* Tries to read an <code>int</code> from the console, and retires if the input
170+
* was not a valid integer. It prompts the user by printing the given
171+
* <code>text</code> with a line break to the console.
172+
* <p>
173+
* <i>Versucht einen <code>int</code>-Wert von der Konsole einzulesen, und
174+
* wiederholt die Anfrage solange es nicht nicht um eine ganze Zahl handelt. Der
175+
* oder die Benutzerin wird durch die Ausgabe des übergebenen
176+
* <code>text</code>es zur Eingabe aufgefordert (mit Zeilenumbruch).</i>
177+
*
178+
* @see Integer#parseInt(String)
179+
*/
180+
public static int readInt(String text) {
181+
Integer x = null;
182+
do {
183+
String s = readString(text);
184+
if (s == null) {
185+
throw new IllegalStateException("Es ist keine Eingabe (mehr) verfügbar.");
186+
}
187+
try {
188+
x = Integer.parseInt(s.trim());
189+
} catch (@SuppressWarnings("unused") NumberFormatException e) {
190+
// try again
191+
}
192+
} while (x == null);
193+
return x;
194+
}
195+
196+
/**
197+
* Works like {@link #readInt(String)}, but uses the following preset text to
198+
* prompt the user:
199+
*
200+
* <pre>
201+
* Geben Sie eine ganze Zahl ein:
202+
* </pre>
203+
*
204+
* <i>Funktioniert wie {@link #readInt(String)}, nutzt jedoch folgenden
205+
* vorgegebenen Text:
206+
*
207+
* <pre>
208+
* Geben Sie eine ganze Zahl ein:
209+
* </pre>
210+
*
211+
* </i>
212+
*/
213+
public static int readInt() {
214+
return readInt("Geben Sie eine ganze Zahl ein:");
215+
}
216+
}

0 commit comments

Comments
 (0)