Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


  }
 
  @Override
  public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
    if (operation.equals("integrate"))
      return new ConsCell(integrate(input), ConsType.NUMBER);
    ArrayList<ConsCell> parts = input.splitOnSeparator();
    if (operation.equals("derivative") && parts.size() == 2)
      return new ConsCell(derivative(input), ConsType.NUMBER);
    if (operation.equals("derivative") && parts.size() >= 1)
      return symbolicDerivative(input);
    return null;
  }
View Full Code Here


  }
 
  @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;
  }
View Full Code Here

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;
View Full Code Here

    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;
  }
View Full Code Here

   *            the new ending symbol
   * @return the input with the symbols replaced
   * @throws ParserException
   */
  public ConsCell replaceSymbolPair(ConsCell input, String startSymbol, String endSymbol, String newStart, String newEnd) throws ParserException {
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(replaceSymbolPair((ConsCell) current.getCar(), startSymbol, endSymbol, newStart, newEnd));
      if (current.getCarType() == ConsType.IDENTIFIER && ((String) current.getCar()).equals(startSymbol)) {
        ConsCell inner = new ConsCell(), head = inner;
        int count = 1;
        boolean reset = current == input;
        while (!(current = current.remove()).isNull()) {
          if (current.getCarType() == ConsType.IDENTIFIER) {
            if (((String) current.getCar()).equals(endSymbol))
              count--;
            else if (((String) current.getCar()).equals(startSymbol))
              count++;
          }
          if (count == 0)
            break;
          head = head.append(current.singular());
        }
        if (newStart.endsWith("(") && newEnd.startsWith(")")) { //So this is basically an operator replacement
          current.replaceCar(new ConsCell(newStart.substring(0, newStart.length() - 1), ConsType.IDENTIFIER));
          if (newEnd.length() > 1)
            inner.getLastConsCell().append(new ConsCell(newEnd.substring(1), ConsType.IDENTIFIER));
          current.insert(new ConsCell(inner, ConsType.CONS_CELL));
        }
        else {
          inner = new ConsCell(newStart, ConsType.IDENTIFIER, inner, ConsType.CONS_CELL);
          inner.getLastConsCell().append(new ConsCell(newEnd, ConsType.IDENTIFIER));
        }
        if (reset)
          input = current;
      }
    } while (!(current = current.getNextConsCell()).isNull());
View Full Code Here

   *            the user's query converted into ConsCell format
   * @return the query with the relevant input filters applied
   * @throws ParserException
   */
  public ConsCell preProcess(ConsCell input) throws ParserException {
    ConsCell current = input;
    if (current.length() == 0 || (current.length() == 1 && current.getCarType() == ConsType.EMPTY))
      return current;
   
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(preProcess((ConsCell) current.getCar()));
      if (current.getCarType() == ConsType.OBJECT && ((String) current.getCar()).startsWith("{[")) {
        currentEqn.matrices.add(new Matrix((String) current.getCar()));
        current.replaceCar(new ConsCell("{M" + (currentEqn.matrices.size() - 1) + "}", ConsType.OBJECT));
      }
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    current = input;
   
    for (InputFilterPlugin plugin : preProcessFilters)
      current = plugin.preProcess(current).getFirstConsCell();
    input = current;
   
    do {
      if (current.getCarType() == ConsType.IDENTIFIER && abbreviations.containsKey(current.getCar()))
        current.replaceCar(new ConsCell(abbreviations.get(current.getCar()), ConsType.IDENTIFIER));
      if (!current.getNextConsCell().isNull() && (current.getCarType() == ConsType.NUMBER || current.getCarType() == ConsType.CONS_CELL ||
          current.getCarType() == ConsType.OBJECT || current.getCarType() == ConsType.STRING)) {
        ConsCell cdr = current.getNextConsCell();
        if (cdr.getCarType() == ConsType.NUMBER || cdr.getCarType() == ConsType.CONS_CELL || cdr.getCarType() == ConsType.OBJECT ||
            (cdr.getCarType() == ConsType.IDENTIFIER && !cdr.getCar().equals("="))) //If there is an implied multiplication sign
          current.insert(new ConsCell('*', ConsType.OPERATOR));
       
        if (cdr.getCarType() == ConsType.STRING) //String concatenation
          current.insert(new ConsCell('+', ConsType.OPERATOR));
      }
    } while (!(current = current.getNextConsCell()).isNull()); //This steps current forward while checking for nulls
    return input; //Returns the first ConsCell in the list because we don't care about which one was last processed, just the whole thing.
  }
View Full Code Here

   
    return output;
  }
 
  private void processCommandQueue() throws ParserException {
    ConsCell command = new ConsCell();
    ConsCell end = new ConsCell("END_OF_STACK", ConsType.IDENTIFIER);
    while (!(command = commandQueue.pop()).equals(end))
      runCommands(command);
  }
View Full Code Here

  private ConsCell tokenizedEvaluationEntry(ConsCell input) throws ParserException {
    return tokenizedEvaluation(input);
  }
 
  private ConsCell tokenizedEvaluation(ConsCell input) throws ParserException {
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.IDENTIFIER) {
        if (keywords.containsKey(current.getCar()))
          current.replaceCar(Tokenizer.tokenizeString(((KeywordPlugin) this.keywords.get(current.getCar()).getPlugin()).getKeywordData((String) current.getCar())));
        else if (isOperation((String) current.getCar()))
          current.replaceCar(runOperation(current));
      }
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(tokenizedEvaluation((ConsCell) current.getCar()));
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
   
    return processEquation(input);
  }
View Full Code Here

      throw new InvalidOperationException(lastPlugin, (String) input.getCar(), new ArrayList<ConsCell>());
    if (!operations.containsKey(input.getCar()))
      throw new InvalidOperationException(((String) input.getCar()) + " is not a valid operation.", lastPlugin, (String) input.getCar(),
          (input.getNextConsCell().getCarType() == ConsType.CONS_CELL ? (ConsCell) input.getNextConsCell().singular().getCar() : input.getNextConsCell().singular()).splitOnSeparator());
    lastPlugin = operations.get(input.getCar()).getPlugin();
    ConsCell current = input.getNextConsCell(), number = new ConsCell();
    boolean skipCheck = true;
    do {
      number = number.append(current.singular());
      skipCheck = (current.getCarType() == ConsType.IDENTIFIER && operations.containsKey(current.getCar())) || (skipCheck && current.getCarType() == ConsType.OPERATOR);
    } while (!(current = current.remove()).isNull() && (skipCheck || (current.getCarType() == ConsType.IDENTIFIER && operations.containsKey(current.getCar()))));
    number = number.getFirstConsCell();
   
    if (!operations.get(input.getCar()).isFinal())
      number = fullEval ? tokenizedEvaluation(number) : seekOperations(number);
    lastPlugin = operations.get(input.getCar()).getPlugin();
    ConsCell result = ((OperationPlugin) operations.get(input.getCar()).getPlugin()).runOperation((String) input.getCar(), number.getCarType() == ConsType.CONS_CELL ? (ConsCell) number.getCar() : number);
    if (operations.get(input.getCar()).isFinal()) {
      finalSubstitutions.add(result);
      return new ConsCell("{FIN" + (finalSubstitutions.size() - 1) + "}", ConsType.OBJECT);
    }
    return result;
  }
View Full Code Here

    }
    return result;
  }
 
  public ConsCell seekOperations(ConsCell input) throws ParserException {
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(seekOperations((ConsCell) current.getCar()));
      if (current.getCarType() == ConsType.IDENTIFIER && operations.containsKey(current.getCar()))
        current.replaceCar(runOperation(current, false));
    } while (!((current = current.getNextConsCell()).isNull())); //This steps current forward while checking for nulls
    return input;
  }
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.util.ConsCell

Copyright © 2018 www.massapicom. 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.