1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-12 13:49:54 -08:00
Abacus/src/org/nwapw/abacus/plugin/StandardPlugin.java

91 lines
2.9 KiB
Java
Raw Normal View History

package org.nwapw.abacus.plugin;
2017-07-25 21:57:14 -07:00
import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface;
public class StandardPlugin extends Plugin {
public StandardPlugin(PluginManager manager) {
super(manager);
}
@Override
public void load() {
registerFunction("+", new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface sum = params[0];
for(int i = 1; i < params.length; i++){
sum = sum.add(params[i]);
}
return sum;
}
});
registerFunction("-", new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].subtract(params[1]);
}
});
registerFunction("*", new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface product = params[0];
for(int i = 1; i < params.length; i++){
product = product.multiply(params[i]);
}
return product;
}
});
registerFunction("/", new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].divide(params[1]);
}
});
2017-07-25 11:12:25 -07:00
registerFunction("!", new Function() {
2017-07-25 11:12:25 -07:00
@Override
protected boolean matchesParams(NumberInterface[] params) {
2017-07-25 11:12:25 -07:00
return params.length == 1;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface factorial = params[0];
NumberInterface multiplier = params[0];
2017-07-25 11:12:25 -07:00
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
2017-07-25 11:12:25 -07:00
factorial = factorial.multiply(multiplier);
}
return factorial;
}
});
}
}