use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class Get method loadPackage.
/**
* Load a package from the given reader
*
* @param engine
* @param is
* @return the last evaluated expression result
*/
public static IExpr loadPackage(final EvalEngine engine, final Reader is) {
final BufferedReader r = new BufferedReader(is);
Context packageContext = null;
try {
final List<ASTNode> node = parseReader(r, engine);
IExpr temp;
int i = 0;
AST2Expr ast2Expr = AST2Expr.CONST;
if (engine.isRelaxedSyntax()) {
ast2Expr = AST2Expr.CONST_LC;
}
IExpr result = F.Null;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++), engine);
if (temp.isAST()) {
IAST ast = (IAST) temp;
IExpr head = temp.head();
if (head.equals(F.BeginPackage) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
packageContext = new Context(contextName);
ISymbol endSymbol = F.EndPackage;
for (int j = 2; j < ast.size(); j++) {
FileReader reader = new FileReader(ast.get(j).toString());
Get.loadPackage(engine, reader);
reader.close();
}
i = addContextToPath(new ContextPath(packageContext), node, i, engine, endSymbol);
continue;
} else if (head.equals(F.Begin) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
ISymbol endSymbol = F.End;
i = addContextToPath(new ContextPath(contextName), node, i, engine, endSymbol);
continue;
}
}
result = engine.evaluate(temp);
}
return result;
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (packageContext != null) {
engine.getContextPath().add(packageContext);
}
try {
r.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return F.Null;
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class Get method addContextToPath.
private static int addContextToPath(ContextPath contextPath, final List<ASTNode> node, int i, final EvalEngine engine, ISymbol endSymbol) {
ContextPath path = engine.getContextPath();
try {
engine.setContextPath(contextPath);
AST2Expr ast2Expr = AST2Expr.CONST;
if (engine.isRelaxedSyntax()) {
ast2Expr = AST2Expr.CONST_LC;
}
while (i < node.size()) {
IExpr temp = ast2Expr.convert(node.get(i++), engine);
if (temp.isAST()) {
IExpr head = temp.head();
IAST ast = (IAST) temp;
if (head.equals(endSymbol) && ast.isAST0()) {
continue;
} else if (head.equals(F.Begin) && ast.size() >= 2) {
try {
contextPath.add(new Context(ast.arg1().toString()));
i = addContextToPath(contextPath, node, i, engine, F.End);
} finally {
contextPath.remove(contextPath.size() - 1);
}
continue;
}
}
engine.evaluate(temp);
}
// TODO add error message
} finally {
engine.setContextPath(path);
}
return i;
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class CoreCallbackFunction method evaluate.
@Override
public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) {
ASTNode node = functionNode.getNode(0);
if (node instanceof SymbolNode) {
AST2Expr ast2Expr = new AST2Expr();
IExpr head = ast2Expr.convert(node);
IASTAppendable fun = F.ast(head, args.length);
fun.appendArgs(0, args.length, i -> F.num(args[i]));
// for (int i = 0; i < args.length; i++) {
// fun.append(args[i]);
// }
final IExpr result = F.evaln(fun);
if (result.isReal()) {
return ((ISignedNumber) result).doubleValue();
}
} else if (node instanceof FunctionNode) {
AST2Expr ast2Expr = new AST2Expr();
IExpr head = ast2Expr.convert(node);
IASTAppendable fun = F.ast(head);
for (int i = 0; i < args.length; i++) {
fun.append(args[i]);
}
final IExpr result = F.evaln(fun);
if (result.isReal()) {
return ((ISignedNumber) result).doubleValue();
}
}
throw new SymjaMathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString());
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class PatternMatchingTestCase method checkPattern.
public void checkPattern(String patternString, String evalString, String resultString) {
try {
ASTNode node = fParser.parse(patternString);
EvalEngine engine = EvalEngine.get();
IExpr pat = new AST2Expr(false, engine).convert(node);
node = fParser.parse(evalString);
IExpr eval = new AST2Expr(false, engine).convert(node);
PatternMatcher matcher = new PatternMatcher(pat);
if (matcher.test(eval)) {
ArrayList<IExpr> resultList = new ArrayList<IExpr>();
matcher.getPatterns(resultList, pat);
assertEquals(resultList.toString(), resultString);
return;
}
assertEquals("", resultString);
} catch (Exception e) {
e.printStackTrace();
assertEquals("", resultString);
}
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class PatternMatchingTestCase method check.
public void check(EvalEngine engine, boolean configMode, String strEval, String strResult, boolean relaxedSyntax) {
try {
if (strEval.length() == 0 && strResult.length() == 0) {
return;
}
IExpr result;
StringWriter buf = new StringWriter();
// configMode;
Config.SERVER_MODE = configMode;
if (Config.SERVER_MODE) {
Parser parser = new Parser(relaxedSyntax);
ASTNode node = parser.parse(strEval);
IExpr inExpr = new AST2Expr(false, engine).convert(node);
TimeConstrainedEvaluator utility = new TimeConstrainedEvaluator(engine, false, Config.FOREVER, relaxedSyntax);
result = utility.constrainedEval(buf, inExpr);
} else {
Parser parser = new Parser(relaxedSyntax);
ASTNode node = parser.parse(strEval);
IExpr inExpr = new AST2Expr(false, engine).convert(node);
result = util.evaluate(inExpr);
if ((result != null) && !result.equals(F.Null)) {
OutputFormFactory off = OutputFormFactory.get(relaxedSyntax);
off.setIgnoreNewLine(true);
off.convert(buf, result);
}
}
assertEquals(buf.toString(), strResult);
} catch (Exception e) {
e.printStackTrace();
assertEquals("", "1");
}
}
Aggregations