use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class Console method main.
public static void main(final String[] args) {
// console.getDefaultSystemRulesFilename(),
F.initSymbols(null, null, true);
// null, false);
Console console;
try {
console = new Console();
} catch (final SyntaxError e1) {
e1.printStackTrace();
return;
}
String inputExpression = null;
String trimmedInput = null;
console.setArgs(args);
final File file = console.getFile();
if (file != null) {
try {
final BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
final StringBuffer buff = new StringBuffer(1024);
String line;
while ((line = f.readLine()) != null) {
buff.append(line);
buff.append('\n');
}
f.close();
inputExpression = buff.toString();
System.out.println("In [" + COUNTER + "]: " + inputExpression);
console.resultPrinter(inputExpression);
COUNTER++;
} catch (final IOException ioe) {
final String msg = "Cannot read from the specified file. " + "Make sure the path exists and you have read permission.";
System.out.println(msg);
return;
}
}
while (true) {
try {
inputExpression = console.readString(System.out, ">> ");
if (inputExpression != null) {
trimmedInput = inputExpression.trim();
if ((inputExpression.length() >= 4) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 4).equals("exit")) {
System.out.println("Closing Symja console... bye.");
System.exit(0);
} else if ((inputExpression.length() >= 10) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 10).equals("timeoutoff")) {
System.out.println("Disabling timeout for evaluation");
console.fSeconds = -1;
continue;
} else if ((inputExpression.length() >= 9) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 9).equals("timeouton")) {
System.out.println("Enabling timeout for evaluation to 60 seconds.");
console.fSeconds = 60;
continue;
} else if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
String name = trimmedInput.substring(1);
IAST list = Names.getNamesByPrefix(name);
for (int i = 1; i < list.size(); i++) {
System.out.print(list.get(i).toString());
if (i != list.size() - 1) {
System.out.print(", ");
}
}
System.out.println();
if (list.size() == 2) {
printDocumentation(list.get(1).toString());
} else if (list.size() == 1 && (name.equals("D") || name.equals("E") || name.equals("I") || name.equals("N"))) {
printDocumentation(name);
}
continue;
}
String postfix = Scanner.balanceCode(inputExpression);
if (postfix != null && postfix.length() > 0) {
System.err.println("Automatically closing brackets: " + postfix);
inputExpression = inputExpression + postfix;
}
System.out.println("In [" + COUNTER + "]: " + inputExpression);
if (console.fPrettyPrinter) {
console.prettyPrinter(inputExpression);
} else {
console.resultPrinter(inputExpression);
}
COUNTER++;
}
// } catch (final MathRuntimeException mre) {
// Throwable me = mre.getCause();
// System.out.println(me.getMessage());
} catch (final Exception e) {
System.out.println(e.getMessage());
}
}
}
use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class SymjaInterpreter method interpreter.
/**
* Evaluate the expression assigned to this interpreter.
*
* @param function
* <code>null</code> if you like to evaluate in symbolic mode;
* "N" if you like to evaluate in numeric mode
* @return
*/
public String interpreter(String function) {
String evalStr = codeString;
IExpr expr;
EvalEngine engine = EvalEngine.get();
try {
ExprParser p = new ExprParser(engine, true);
// throws SyntaxError exception, if syntax isn't valid
if (function != null) {
evalStr = function + "(" + codeString + ")";
}
expr = p.parse(evalStr);
} catch (SyntaxError e1) {
try {
ExprParser p = new ExprParser(engine);
// throws SyntaxError exception, if syntax isn't valid
if (function != null) {
evalStr = function + "[" + codeString + "]";
}
expr = p.parse(evalStr);
} catch (Exception e2) {
outStream.println(e2.getMessage());
return "";
}
}
IExpr result;
final StringBuilder buf = new StringBuilder();
try {
result = evaluate(expr);
if (result.isPresent()) {
if (result.equals(F.Null)) {
return buf.toString();
}
OutputFormFactory.get(true).convert(buf, result);
}
return buf.toString();
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
} catch (final Exception ex) {
Validate.printException(buf, ex);
} catch (final StackOverflowError soe) {
Validate.printException(buf, soe);
} catch (final OutOfMemoryError oome) {
Validate.printException(buf, oome);
}
return buf.toString();
}
use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class MMAConsole method interpreter.
/**
* Evaluates the given string-expression and returns the result in
* <code>OutputForm</code>
*
* @param inputExpression
* @return
*/
public String interpreter(final String inputExpression) {
IExpr result;
final StringWriter buf = new StringWriter();
try {
if (fSeconds <= 0) {
result = fEvaluator.evaluate(inputExpression);
} else {
result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
}
if (result != null) {
if (result.equals(F.Null)) {
return "";
}
return result.toString();
}
} catch (final SyntaxError se) {
String msg = se.getMessage();
System.err.println(msg);
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
} catch (final Exception e) {
Validate.printException(buf, e);
} catch (final OutOfMemoryError e) {
Validate.printException(buf, e);
} catch (final StackOverflowError e) {
Validate.printException(buf, e);
}
return buf.toString();
}
use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class Console method interpreter.
/**
* Evaluates the given string-expression and returns the result in <code>OutputForm</code>
*
* @param inputExpression
* @return
*/
public String interpreter(final String inputExpression) {
IExpr result;
final StringWriter buf = new StringWriter();
try {
if (fSeconds <= 0) {
result = fEvaluator.evaluate(inputExpression);
} else {
result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
}
if (result != null) {
if (result.equals(F.Null)) {
return "";
}
StringBuilder strBuffer = new StringBuilder();
fOutputFactory.convert(strBuffer, result);
return strBuffer.toString();
}
} catch (final SyntaxError se) {
String msg = se.getMessage();
System.err.println();
System.err.println(msg);
return "";
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
return "";
} catch (final Exception e) {
Validate.printException(buf, e);
return "";
} catch (final OutOfMemoryError e) {
Validate.printException(buf, e);
return "";
} catch (final StackOverflowError e) {
Validate.printException(buf, e);
return "";
}
return buf.toString();
}
use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class PolynomialExample method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr expr = util.evaluate("x^2+y+a*x+b*y+c");
System.out.println(expr.toString());
final IAST variables = F.List(F.x, F.y);
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.size() - 1, ExprTermOrderByName.Lexicographic, false);
ExprPolynomial poly = ring.create(expr);
System.out.println(poly.toString());
// x degree
System.out.println(poly.degree(0));
// y degree
System.out.println(poly.degree(1));
// show internal structure:
System.out.println(poly.coefficientRules());
System.out.println();
for (ExprMonomial monomial : poly) {
System.out.println(monomial.toString());
}
} catch (SyntaxError e) {
// catch Symja parser errors here
System.out.println(e.getMessage());
} catch (MathException me) {
// catch Symja math errors here
System.out.println(me.getMessage());
} catch (Exception e) {
e.printStackTrace();
} catch (final StackOverflowError soe) {
System.out.println(soe.getMessage());
} catch (final OutOfMemoryError oome) {
System.out.println(oome.getMessage());
}
}
Aggregations