From c3393ca2828b9f4eeef9f5b60ca6b45c1cd73c3e Mon Sep 17 00:00:00 2001 From: David Benn Date: Sun, 21 Jul 2024 20:29:59 +0930 Subject: [PATCH] #357: added a pop-with-error-message method to interpreter and used it in a refactoring of IS/BIND assignment cases --- .../tools/vstar/vela/VeLaInterpreter.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/org/aavso/tools/vstar/vela/VeLaInterpreter.java b/src/org/aavso/tools/vstar/vela/VeLaInterpreter.java index 348b520b..06a2f11d 100644 --- a/src/org/aavso/tools/vstar/vela/VeLaInterpreter.java +++ b/src/org/aavso/tools/vstar/vela/VeLaInterpreter.java @@ -202,6 +202,21 @@ public Stack getStack() { return stack; } + /** + * Pop and return an operand from the stack if not empty. + * + * @param msgOnError The exception message to use if the stack is empty. + * @return The operand on top of the stack. + * @throws VeLaEvalError Thrown when the stack is empty. + */ + public Operand pop(String msgOnError) throws VeLaEvalError { + if (!stack.isEmpty()) { + return stack.pop(); + } else { + throw new VeLaEvalError(msgOnError); + } + } + /** * VeLa program interpreter entry point. * @@ -580,23 +595,12 @@ private void specialForm(AST ast) { } break; - case BIND: - eval(ast.right()); - - String varName = ast.left().getToken(); - - if (!stack.isEmpty()) { - bind(varName, stack.pop(), false); - } else { - String msg = "No value to assign to \"" + varName + "\""; - throw new VeLaEvalError(msg); - } - - break; - + case BIND: case IS: eval(ast.right()); - bind(ast.left().getToken(), stack.pop(), true); + String varName = ast.left().getToken(); + String msg = "No value to bind to \"" + varName + "\""; + bind(varName, pop(msg), ast.getOp() == Operation.IS); break; case FUNDEF: