mirror of
https://github.com/Noratrieb/GRSBPL.git
synced 2026-01-14 19:55:03 +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 NOUT -> nout();
|
||||
case IN -> in();
|
||||
case STRING -> string();
|
||||
// control flow
|
||||
case COLUMN -> ignoreLabel();
|
||||
case GOTO -> condGoto();
|
||||
|
|
@ -214,6 +215,9 @@ public class Interpreter {
|
|||
|
||||
private void out() {
|
||||
consume();
|
||||
if (stack().isEmpty()) {
|
||||
throw runException("Cannot pop empty stack");
|
||||
}
|
||||
System.out.print((char) stack().pop());
|
||||
}
|
||||
|
||||
|
|
@ -230,6 +234,12 @@ public class Interpreter {
|
|||
throw runException("[VM] - Error reading input");
|
||||
}
|
||||
}
|
||||
|
||||
private void string() {
|
||||
String s = advance().getStringValue();
|
||||
expect(OUT, "String can only be used together with out");
|
||||
System.out.print(s);
|
||||
}
|
||||
|
||||
///// control flow
|
||||
|
||||
|
|
@ -293,10 +303,14 @@ public class Interpreter {
|
|||
///// parsing helper methods
|
||||
|
||||
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) {
|
||||
return advance();
|
||||
} else {
|
||||
throw runException("Excepted token '" + type + "' but found '" + peek().getType() + "'");
|
||||
throw runException(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -349,4 +363,4 @@ public class Interpreter {
|
|||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue