Update Interpreter.java

This commit is contained in:
nora 2021-05-28 15:33:02 +02:00 committed by GitHub
parent d8f2549b29
commit 1e0c9c84c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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());
} }
@ -230,6 +234,12 @@ public class Interpreter {
throw runException("[VM] - Error reading input"); 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 ///// control flow
@ -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);
} }
} }
@ -349,4 +363,4 @@ public class Interpreter {
this.name = name; this.name = name;
} }
} }
} }