mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-14 19:05:19 +00:00
Move the source files into a new default directory.
This commit is contained in:
49
src/main/java/org/nwapw/abacus/function/Function.java
Executable file
49
src/main/java/org/nwapw/abacus/function/Function.java
Executable file
@@ -0,0 +1,49 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.number.PreciseNumber;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* A function that operates on one or more
|
||||
* inputs and returns a single number.
|
||||
*/
|
||||
public abstract class Function {
|
||||
|
||||
/**
|
||||
* A map to correctly promote different number implementations to each other.
|
||||
*/
|
||||
private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
|
||||
new HashMap<Class<? extends NumberInterface>, Integer>() {{
|
||||
put(NaiveNumber.class, 0);
|
||||
put(PreciseNumber.class, 1);
|
||||
}};
|
||||
|
||||
/**
|
||||
* Checks whether the given params will work for the given function.
|
||||
* @param params the given params
|
||||
* @return true if the params can be used with this function.
|
||||
*/
|
||||
protected abstract boolean matchesParams(NumberInterface[] params);
|
||||
|
||||
/**
|
||||
* Internal apply implementation, which already receives appropriately promoted
|
||||
* parameters that have bee run through matchesParams
|
||||
* @param params the promoted parameters.
|
||||
* @return the return value of the function.
|
||||
*/
|
||||
protected abstract NumberInterface applyInternal(NumberInterface[] params);
|
||||
|
||||
/**
|
||||
* Function to check, promote arguments and run the function.
|
||||
* @param params the raw input parameters.
|
||||
* @return the return value of the function, or null if an error occurred.
|
||||
*/
|
||||
public NumberInterface apply(NumberInterface...params) {
|
||||
if(!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
}
|
||||
|
||||
}
|
||||
70
src/main/java/org/nwapw/abacus/function/Operator.java
Normal file
70
src/main/java/org/nwapw/abacus/function/Operator.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
/**
|
||||
* A class that represents a single infix operator.
|
||||
*/
|
||||
public class Operator {
|
||||
|
||||
/**
|
||||
* The associativity of the operator.
|
||||
*/
|
||||
private OperatorAssociativity associativity;
|
||||
/**
|
||||
* The type of this operator.
|
||||
*/
|
||||
private OperatorType type;
|
||||
/**
|
||||
* The precedence of the operator.
|
||||
*/
|
||||
private int precedence;
|
||||
/**
|
||||
* The function that is called by this operator.
|
||||
*/
|
||||
private Function function;
|
||||
|
||||
/**
|
||||
* Creates a new operator with the given parameters.
|
||||
* @param associativity the associativity of the operator.
|
||||
* @param precedence the precedence of the operator.
|
||||
* @param function the function that the operator calls.
|
||||
*/
|
||||
public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function){
|
||||
this.associativity = associativity;
|
||||
this.type = operatorType;
|
||||
this.precedence = precedence;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operator's associativity.
|
||||
* @return the associativity.
|
||||
*/
|
||||
public OperatorAssociativity getAssociativity() {
|
||||
return associativity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operator's type.
|
||||
* @return the type.
|
||||
*/
|
||||
public OperatorType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operator's precedence.
|
||||
* @return the precedence.
|
||||
*/
|
||||
public int getPrecedence() {
|
||||
return precedence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operator's function.
|
||||
* @return the function.
|
||||
*/
|
||||
public Function getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
/**
|
||||
* Enum to represent the associativity of an operator.
|
||||
*/
|
||||
public enum OperatorAssociativity {
|
||||
LEFT, RIGHT
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
/**
|
||||
* The type of an operator, describing how it should behave.
|
||||
*/
|
||||
public enum OperatorType {
|
||||
BINARY_INFIX, UNARY_POSTFIX
|
||||
}
|
||||
Reference in New Issue
Block a user