Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/parser/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import parser.methods.Method;

import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
Expand Down Expand Up @@ -223,7 +224,7 @@ public double calc(double... x) {
return Double.NaN;
}

public static boolean assignObject(String input, Class mathExpClass) {
public static <T extends MathExpression> boolean assignObject(String input, Class<T> clazz) {

/**
* Check if it is a function assignment operation...e.g:
Expand Down Expand Up @@ -273,7 +274,15 @@ public static boolean assignObject(String input, Class mathExpClass) {

success = true;
} else {
MathExpression expr = mathExpClass == BigMathExpression.class ? new BigMathExpression(rhs) : new MathExpression(rhs);
MathExpression expr;
try {
expr = clazz.getConstructor(String.class).newInstance(rhs);
} catch (InstantiationException |
IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e); // unreachable
}
String val = expr.solve();
String referenceName = expr.getReturnObjectName();
//System.out.println("rhs: "+rhs+", mathExpClass: "+mathExpClass+", expr.class: "+expr.getClass()+", val: "+val+", type: "+expr.getReturnType());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/util/MathExpressionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ public MathExpression createFunction( String expr ){
* it has to interpret and then evaluate it.
* It then stores the expression.
* @param expr The expression to evaluate.
* @param mathExpClazz The java.util.Class that the parent MathExpression belongs to.. may be MathExpression.class, BigMathExpression.class etc.
* @param clazz The java.util.Class that the parent MathExpression belongs to.. may be MathExpression.class, BigMathExpression.class etc.
* @return the result.
*/
public String solve( String expr , Class mathExpClazz) throws NullPointerException{
public <T extends MathExpression> String solve(String expr, Class<T> clazz) throws NullPointerException{
try {
CustomScanner cs = new CustomScanner( STRING.purifier(expr), false, VariableManager.endOfLine);

Expand All @@ -260,7 +260,7 @@ public String solve( String expr , Class mathExpClazz) throws NullPointerExcepti

for(String code : scanned) {
if(code.contains("=")){
boolean success = Function.assignObject(code+";", mathExpClazz);
boolean success = Function.assignObject(code+";", clazz);
if(!success) {
throw new Exception("Bad Variable or Function assignment!");
}
Expand Down