mirror of
https://github.com/Noratrieb/GRSBPL.git
synced 2026-01-16 12:45:04 +01:00
Update Interpreter.java
This commit is contained in:
parent
d8f2549b29
commit
1e0c9c84c9
1 changed files with 16 additions and 2 deletions
|
|
@ -95,6 +95,7 @@ public class Interpreter {
|
||||||
case OUT -> out();
|
case OUT -> out();
|
||||||
case NOUT -> nout();
|
case NOUT -> nout();
|
||||||
case IN -> in();
|
case IN -> in();
|
||||||
|
case STRING -> string();
|
||||||
// control flow
|
// control flow
|
||||||
case COLUMN -> ignoreLabel();
|
case COLUMN -> ignoreLabel();
|
||||||
case GOTO -> condGoto();
|
case GOTO -> condGoto();
|
||||||
|
|
@ -214,6 +215,9 @@ public class Interpreter {
|
||||||
|
|
||||||
private void out() {
|
private void out() {
|
||||||
consume();
|
consume();
|
||||||
|
if (stack().isEmpty()) {
|
||||||
|
throw runException("Cannot pop empty stack");
|
||||||
|
}
|
||||||
System.out.print((char) stack().pop());
|
System.out.print((char) stack().pop());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,6 +235,12 @@ public class Interpreter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void string() {
|
||||||
|
String s = advance().getStringValue();
|
||||||
|
expect(OUT, "String can only be used together with out");
|
||||||
|
System.out.print(s);
|
||||||
|
}
|
||||||
|
|
||||||
///// control flow
|
///// control flow
|
||||||
|
|
||||||
private void ignoreLabel() {
|
private void ignoreLabel() {
|
||||||
|
|
@ -293,10 +303,14 @@ public class Interpreter {
|
||||||
///// parsing helper methods
|
///// parsing helper methods
|
||||||
|
|
||||||
private Token expect(TokenType type) {
|
private Token expect(TokenType type) {
|
||||||
|
return expect(type, "Excepted token '" + type + "' but found '" + peek().getType() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Token expect(TokenType type, String message) {
|
||||||
if (peek().getType() == type) {
|
if (peek().getType() == type) {
|
||||||
return advance();
|
return advance();
|
||||||
} else {
|
} else {
|
||||||
throw runException("Excepted token '" + type + "' but found '" + peek().getType() + "'");
|
throw runException(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue