Example #1
0
  /**
   * Translates a given expression in the format expected by the cas.
   *
   * @param ve the Expression to be translated
   * @param casStringType one of StringType.{MAXIMA, MPREDUCE, MATH_PIPER}
   * @return the translated String.
   */
  protected String translateToCAS(ValidExpression ve, StringType casStringType) {
    Kernel kernel = ve.getKernel();
    StringType oldPrintForm = kernel.getCASPrintForm();
    kernel.setCASPrintForm(casStringType);

    try {
      ValidExpression tmp = ve;
      if (!ve.isExpressionNode()) tmp = new ExpressionNode(kernel, ve);

      String body = ((ExpressionNode) tmp).getCASstring(casStringType, true);

      // handle assignments
      String label = ve.getLabel();
      if (label != null) { // is an assignment or a function declaration
        // make sure to escape labels to avoid problems with reserved
        // CAS labels
        label = kernel.printVariableName(casStringType, label);
        if (ve instanceof FunctionNVar) {
          FunctionNVar fun = (FunctionNVar) ve;
          return translateFunctionDeclaration(label, fun.getVarString(), body);
        }
        return translateAssignment(label, body);
      }
      return body;
    } finally {
      kernel.setCASPrintForm(oldPrintForm);
    }
  }