Package symbols

Source Code of symbols.SymbolsPlugin

package symbols;

import java.util.ArrayList;

import lipstone.joshua.parser.exceptions.ParserException;
import lipstone.joshua.parser.exceptions.PluginConflictException;
import lipstone.joshua.parser.exceptions.UndefinedResultException;
import lipstone.joshua.parser.plugin.ParserPlugin;
import lipstone.joshua.parser.plugin.helpdata.Operation;
import lipstone.joshua.parser.plugin.helpdata.Parameter;
import lipstone.joshua.parser.plugin.types.InputFilterPlugin;
import lipstone.joshua.parser.plugin.types.OperationPlugin;
import lipstone.joshua.parser.types.BigDec;
import lipstone.joshua.parser.util.ConsCell;
import lipstone.joshua.parser.util.ConsType;

public class SymbolsPlugin extends ParserPlugin implements OperationPlugin, InputFilterPlugin {
 
  @Override
  public void loadInputFilter() {/* Nothing to do here */}
 
  @Override
  public ConsCell preProcess(ConsCell input) throws ParserException {
    /*do {
      if (current.getCarType() == ConsType.IDENTIFIER && ((String) current.getCar()).equals("|")) {
        ConsCell inner = current.singular(), head = inner;
        while (!(current = current.remove()).isNull() && !(current.getCarType() == ConsType.IDENTIFIER && ((String) current.getCar()).equals("|")))
          head = head.append(current.singular());
        inner = preProcess(inner);
        current.replaceCar(new ConsCell("abs", ConsType.IDENTIFIER));
        current.insert(new ConsCell(inner, ConsType.CONS_CELL));
      }
    } while (!(current = current.getNextConsCell()).isNull());*/
    input = parser.replaceSymbolPair(input, "|", "|", "abs(", ")"); //Absolute value
//    input = parser.replaceSymbolPair(input, "|{", "}|", "det({", "})"); //Determinants of matrices
    return input;
  }
 
  @Override
  public void unloadInputFilter() {/* Nothing to do here */}
 
  @Override
  public void loadOperations() throws PluginConflictException {
    ArrayList<Parameter> params = new ArrayList<Parameter>();
    params.add(new Parameter("x", "Any real number"));
    addOperation(new Operation("abs", params, "the absolute value of the number x", "", this));
    params.remove(0);
    params.add(new Parameter("M", "some square matrix"));
    addOperation(new Operation("det", params, "the determinant of the square matrix, M.  Returns null if M is not square", "", this));
  }
 
  @Override
  public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
    if (operation.equals("abs")) {
      ConsCell temp = parser.run(input);
      if (temp.getCarType() != ConsType.NUMBER || temp.length() != 1)
        throw new UndefinedResultException("The single argument to the absolute value operation must evaluate to a number.", this);
      return new ConsCell(new BigDec(((BigDec) temp.getCar()).abs()), ConsType.NUMBER);
    }
    return null;
  }
 
  @Override
  public void unloadOperations() {/* Nothing to do here */}
TOP

Related Classes of symbols.SymbolsPlugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.