diff --git a/src/main/java/com/github/nilstrieb/grsbpl/language/Interpreter.java b/src/main/java/com/github/nilstrieb/grsbpl/language/Interpreter.java index db37d21..674f959 100644 --- a/src/main/java/com/github/nilstrieb/grsbpl/language/Interpreter.java +++ b/src/main/java/com/github/nilstrieb/grsbpl/language/Interpreter.java @@ -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; } } -} \ No newline at end of file +}