1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-09-24 20:32:35 +00:00

Merge branch 'master' into thread-safety

This commit is contained in:
2017-09-15 22:58:33 -07:00
committed by GitHub
25 changed files with 80 additions and 72 deletions

View File

@@ -0,0 +1,9 @@
package org.nwapw.abacus.exception;
public class AbacusException extends RuntimeException {
public AbacusException(String baseMessage, String description){
super(baseMessage + ((description.equals("")) ? "." : (": " + description)));
}
}

View File

@@ -1,16 +1,16 @@
package org.nwapw.abacus.number;
package org.nwapw.abacus.exception;
/**
* Exception thrown when the computation is interrupted by
* the user.
*/
public class ComputationInterruptedException extends RuntimeException {
public class ComputationInterruptedException extends AbacusException {
/**
* Creates a new exception of this type.
*/
public ComputationInterruptedException() {
super("Computation interrupted by user.");
super("Computation interrupted", "");
}
}

View File

@@ -1,24 +1,24 @@
package org.nwapw.abacus.function;
package org.nwapw.abacus.exception;
/**
* Exception thrown if the function parameters do not match
* requirements.
*/
public class DomainException extends RuntimeException {
public class DomainException extends AbacusException {
/**
* Creates a new DomainException.
* @param reason the reason for which the exception is thrown.
*/
public DomainException(String reason) {
super(reason);
super("Domain error", reason);
}
/**
* Creates a new DomainException with a default message.
*/
public DomainException(){
this("Domain Error");
this("");
}
}

View File

@@ -1,17 +1,17 @@
package org.nwapw.abacus.function;
package org.nwapw.abacus.exception;
/**
* An exception thrown primarily from Tree Value operators and functions,
* which have to deal with the result of a Reducer as well as the results
* of Applicable.
*/
public class EvaluationException extends RuntimeException {
public class EvaluationException extends AbacusException {
/**
* Creates a new EvaluationException with the default string.
*/
public EvaluationException() {
super("Error evaluating expression.");
this("");
}
/**
@@ -19,7 +19,7 @@ public class EvaluationException extends RuntimeException {
* @param message the message string.
*/
public EvaluationException(String message) {
super(message);
super("Evaluation error", message);
}
}

View File

@@ -1,5 +1,7 @@
package org.nwapw.abacus.number;
import org.nwapw.abacus.exception.ComputationInterruptedException;
/**
* An interface used to represent a number.
*/

View File

@@ -154,19 +154,20 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
return new VariableNode(match.getContent());
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
String functionName = match.getContent();
CallNode node;
if (matchType == TokenType.FUNCTION) {
node = new FunctionNode(functionName);
} else {
node = new TreeValueFunctionNode(functionName);
}
List<TreeNode> children = new ArrayList<>();
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
TreeNode argument = constructRecursive(matches);
if (argument == null) return null;
node.getChildren().add(0, argument);
children.add(0, argument);
}
if (matches.isEmpty()) return null;
matches.remove(0);
CallNode node;
if (matchType == TokenType.FUNCTION) {
node = new FunctionNode(functionName, children);
} else {
node = new TreeValueFunctionNode(functionName, children);
}
return node;
}
return null;

View File

@@ -29,8 +29,7 @@ public class StandardPlugin extends Plugin {
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
NumberInterface value = params[1].reduce(context.getReducer());
if(value == null) throw new EvaluationException();
NumberInterface value = params[1].reduce(context.getInheritedReducer());
context.setVariable(assignTo, value);
return value;
}
@@ -48,9 +47,7 @@ public class StandardPlugin extends Plugin {
public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
context.setDefinition(assignTo, params[1]);
NumberInterface value = params[1].reduce(context.getReducer());
if(value == null) throw new EvaluationException();
return value;
return params[1].reduce(context.getInheritedReducer());
}
};
/**