package core;
import java.util.ArrayList;
import lipstone.joshua.parser.exceptions.ParserException;
import lipstone.joshua.parser.exceptions.PluginConflictException;
import lipstone.joshua.parser.plugin.ParserPlugin;
import lipstone.joshua.parser.plugin.helpdata.Command;
import lipstone.joshua.parser.plugin.helpdata.Keyword;
import lipstone.joshua.parser.plugin.helpdata.Operation;
import lipstone.joshua.parser.plugin.helpdata.Parameter;
import lipstone.joshua.parser.plugin.types.CommandPlugin;
import lipstone.joshua.parser.plugin.types.KeywordPlugin;
import lipstone.joshua.parser.plugin.types.OperationPlugin;
import lipstone.joshua.parser.plugin.types.OutputFilterPlugin;
import lipstone.joshua.parser.types.BigDec;
import lipstone.joshua.parser.util.ConsCell;
import lipstone.joshua.parser.util.ConsType;
public final class BasicPlugin extends ParserPlugin implements OperationPlugin, KeywordPlugin, OutputFilterPlugin, CommandPlugin {
@Override
public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
if (operation.equals("factor")) {
ConsCell number = parser.run(input);
if (number.getCarType() != ConsType.NUMBER || number.length() != 1)
return parser.getCAS().factoring(input);
ArrayList<BigDec> temp = parser.getCAS().findFactors(((BigDec) parser.run(input).getCar()));
ConsCell output = new ConsCell();
ConsCell head = output;
for (BigDec num : temp)
head = head.append(new ConsCell(num, ConsType.NUMBER)).append(new ConsCell(",", ConsType.SEPARATOR));
head.remove();
return output;
}
if (operation.equals("primeFactor"))
return parser.getCAS().primeFactor(parser.run(input));
return null;
}
@Override
public void loadOperations() throws PluginConflictException {
ArrayList<Parameter> params = new ArrayList<Parameter>();
params.add(new Parameter("x", "Any integer or algebraic expression", false));
addOperation(new Operation("factor", params, "The factorized form of the number or algebraic expression",
"This uses prime numbers to find the full factorization of the number or attempts to factor the algebraic expression.", this, true));
params.remove(0);
params.add(new Parameter("x", "Any integer", false));
addOperation(new Operation("primeFactor", params, "The prime factorization of the number", "This uses prime numbers to find the prime factorization of this number.", this, true));
}
@Override
public String getKeywordData(String keyword) {
if (keyword.equals("e"))
return new Double(Math.E).toString();
if (keyword.equals("pi"))
return new Double(Math.PI).toString();
if (keyword.equals("ans"))
return getData("lastAnswer").toString();
if (keyword.equals("mem"))
return getData("memory").toString();
return null;
}
@Override
public void loadKeywords() throws PluginConflictException {
addKeyword(new Keyword("e", "the constant e", this));
addKeyword(new Keyword("pi", "the constant pi", this));
addKeyword(new Keyword("ans", "the last answer", this));
addKeyword(new Keyword("mem", "the value stored in memory", this));
}
@Override
public void unloadKeywords() {/* Don't need to do anything here */}
@Override
public void unloadOperations() {/* Don't need to do anything here */}
@Override
public ConsCell postProcess(ConsCell input) {
try {
saveData("lastAnswer", parser.preProcess(input));
}
catch (ParserException e) {}
return input;
}
@Override
public void loadOutputFilter() {/* Don't need to do anything here */}
@Override
public void unloadOutputFilter() {/* Don't need to do anything here */}
@Override
public ConsCell runCommand(String command, ArrayList<ConsCell> arguments) throws ParserException {
if (command.equalsIgnoreCase("setMemory"))
return saveData("memory", getData("lastAnswer"));
else if (command.equalsIgnoreCase("addToMemory"))
return saveData("memory", parser.run(getData("memory") + "+" + getData("lastAnswer")));
else if (command.equalsIgnoreCase("subtractFromMemory"))
return saveData("memory", parser.run(getData("memory") + "-" + getData("lastAnswer")));
else if (command.equalsIgnoreCase("clearMemory"))
return saveData("memory", new ConsCell(BigDec.ZERO, ConsType.NUMBER));
else if (command.equalsIgnoreCase("getMemory"))
return getData("memory");
return null;
}
@Override
public void loadCommands() throws PluginConflictException {
addCommand(new Command("set memory", "set memory to the last answer", this));
addCommand(new Command("add to memory", "add the last answer to memory", this));
addCommand(new Command("subtract from memory", "subtract the last answer from the memory", this));
addCommand(new Command("clear memory", "clear the memory", this));
addCommand(new Command("get memory", "get the value stored in memory", this));
}
@Override
public void unloadCommands() {/* Nothing to do here */}
}