Skip to content

Commit

Permalink
#357: added a pop-with-error-message method to interpreter and used i…
Browse files Browse the repository at this point in the history
…t in a refactoring of IS/BIND assignment cases
  • Loading branch information
dbenn committed Jul 21, 2024
1 parent 6686a91 commit c3393ca
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/org/aavso/tools/vstar/vela/VeLaInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ public Stack<Operand> 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.
*
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c3393ca

Please sign in to comment.