From 1e0c9c84c91e21effb38f3e51f37de79f54ce2ea Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Fri, 28 May 2021 15:33:02 +0200 Subject: [PATCH] Update Interpreter.java --- .../nilstrieb/grsbpl/language/Interpreter.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 +}