Search in sources :

Example 6 with Context

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;
}
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 7 with Context

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;
}
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)

Example 8 with Context

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;
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath)

Example 9 with Context

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;
}
Also used : Context(org.matheclipse.core.expression.Context)

Example 10 with Context

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);
    }
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath)

Aggregations

Context (org.matheclipse.core.expression.Context)14 ContextPath (org.matheclipse.core.expression.ContextPath)7 IExpr (org.matheclipse.core.interfaces.IExpr)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 IOException (java.io.IOException)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2 IAST (org.matheclipse.core.interfaces.IAST)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)1