Search in sources :

Example 1 with ContextPath

use of org.matheclipse.core.expression.ContextPath 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;
}
Also used : Context(org.matheclipse.core.expression.Context) ISymbol(org.matheclipse.core.interfaces.ISymbol) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) AST2Expr(org.matheclipse.core.convert.AST2Expr) ContextPath(org.matheclipse.core.expression.ContextPath) BufferedReader(java.io.BufferedReader) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 2 with ContextPath

use of org.matheclipse.core.expression.ContextPath 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;
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Aggregations

AST2Expr (org.matheclipse.core.convert.AST2Expr)2 Context (org.matheclipse.core.expression.Context)2 ContextPath (org.matheclipse.core.expression.ContextPath)2 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1