use of org.matheclipse.core.expression.Context 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.expression.Context 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.expression.Context in project symja_android_library by axkr.
the class EvalEngine method end.
public Context end() {
if (fContextPathStack.size() > 0) {
ContextPath p = fContextPath;
Context c = fContextPath.currentContext();
fContextPath = fContextPathStack.pop();
fContextPath.synchronize(p);
return c;
}
return null;
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class EvalEngine method begin.
public Context begin(String contextName, Context parentContext) {
fContextPathStack.push(fContextPath);
fContextPath = fContextPath.copy();
Context packageContext = fContextPath.getContext(contextName, parentContext);
setContext(packageContext);
return packageContext;
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class EvalEngine method endPackage.
public void endPackage() {
if (fContextPathStack.size() > 0) {
ContextPath p = fContextPath;
Context c = fContextPath.currentContext();
fContextPath = fContextPathStack.pop();
fContextPath.synchronize(p);
fContextPath.add(0, c);
}
}
Aggregations