Search in sources :

Example 11 with AST2Expr

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;
}
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 12 with AST2Expr

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;
}
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 13 with AST2Expr

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());
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) IExpr(org.matheclipse.core.interfaces.IExpr) AST2Expr(org.matheclipse.core.convert.AST2Expr) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 14 with AST2Expr

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);
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) EvalEngine(org.matheclipse.core.eval.EvalEngine) ArrayList(java.util.ArrayList) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 15 with AST2Expr

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");
    }
}
Also used : TimeConstrainedEvaluator(org.matheclipse.core.eval.TimeConstrainedEvaluator) StringWriter(java.io.StringWriter) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) Parser(org.matheclipse.parser.client.Parser) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Aggregations

AST2Expr (org.matheclipse.core.convert.AST2Expr)21 IExpr (org.matheclipse.core.interfaces.IExpr)18 ASTNode (org.matheclipse.parser.client.ast.ASTNode)17 EvalEngine (org.matheclipse.core.eval.EvalEngine)8 IAST (org.matheclipse.core.interfaces.IAST)8 Parser (org.matheclipse.parser.client.Parser)7 IOException (java.io.IOException)5 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)5 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)5 FileReader (java.io.FileReader)4 CSVFormat (org.apache.commons.csv.CSVFormat)4 CSVRecord (org.apache.commons.csv.CSVRecord)4 SyntaxError (org.matheclipse.parser.client.SyntaxError)4 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)3 IStringX (org.matheclipse.core.interfaces.IStringX)3 Extension (org.matheclipse.core.io.Extension)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)2 Context (org.matheclipse.core.expression.Context)2 ContextPath (org.matheclipse.core.expression.ContextPath)2