Search in sources :

Example 6 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Get method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    IStringX arg1 = (IStringX) ast.arg1();
    FileReader reader;
    try {
        reader = new FileReader(arg1.toString());
        return loadPackage(engine, reader);
    } catch (FileNotFoundException e) {
        engine.printMessage("Get: file " + arg1.toString() + " not found!");
    }
    return F.Null;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IStringX(org.matheclipse.core.interfaces.IStringX) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments)

Example 7 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Package method evalPackage.

public static void evalPackage(IAST publicSymbols, IAST list, EvalEngine engine) {
    HashMap<String, ISymbol> convertedSymbolMap = new HashMap<String, ISymbol>();
    HashSet<ISymbol> publicSymbolSet = new HashSet<ISymbol>();
    ISymbol toSymbol;
    for (int i = 1; i < publicSymbols.size(); i++) {
        IExpr expr = publicSymbols.get(i);
        if (expr.isSymbol()) {
            publicSymbolSet.add((ISymbol) expr);
            toSymbol = F.predefinedSymbol(((ISymbol) expr).toString());
            convertedSymbolMap.put(expr.toString(), toSymbol);
        } else if (expr instanceof IStringX) {
            String symbolStr = ((IStringX) expr).toString();
            toSymbol = F.predefinedSymbol(symbolStr);
            publicSymbolSet.add(toSymbol);
            if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
                symbolStr = symbolStr.toLowerCase(Locale.ENGLISH);
            }
            convertedSymbolMap.put(symbolStr, toSymbol);
        }
    }
    // determine "private package rule headers" in convertedSymbolMap
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i).isAST()) {
            determineRuleHead((IAST) list.get(i), publicSymbolSet, convertedSymbolMap);
        }
    }
    // convert the rules into a new list:
    IAST resultList = F.List();
    for (int i = 1; i < list.size(); i++) {
        resultList.append(convertSymbolsInExpr(list.get(i), convertedSymbolMap));
    }
    try {
        engine.setPackageMode(true);
        // evaluate the new converted rules
        for (int i = 1; i < resultList.size(); i++) {
            engine.evaluate(resultList.get(i));
        }
    } finally {
        engine.setPackageMode(false);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) HashSet(java.util.HashSet)

Example 8 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Package method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    if (!(ast.arg1() instanceof IStringX) || !ast.arg2().isList() || !ast.arg3().isList()) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    if (Config.SERVER_MODE) {
        throw new RuleCreationError(null);
    }
    IAST symbols = (IAST) ast.arg2();
    IAST list = (IAST) ast.arg3();
    evalPackage(symbols, list, engine);
    return F.Null;
}
Also used : IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) RuleCreationError(org.matheclipse.core.eval.exception.RuleCreationError)

Example 9 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Put method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    int len = ast.size() - 1;
    IStringX fileName = Validate.checkStringType(ast, len);
    FileWriter writer;
    try {
        writer = new FileWriter(fileName.toString());
        final StringBuilder buf = new StringBuilder();
        for (int i = 1; i < len; i++) {
            IExpr temp = engine.evaluate(ast.get(i));
            OutputFormFactory.get().convert(buf, temp);
            buf.append('\n');
            if (i < len - 1) {
                buf.append('\n');
            }
        }
        writer.write(buf.toString());
        writer.close();
    } catch (IOException e) {
        engine.printMessage("Put: file " + fileName.toString() + " I/O exception !");
    }
    return F.Null;
}
Also used : FileWriter(java.io.FileWriter) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) IOException(java.io.IOException)

Example 10 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Validate method checkContextName.

/**
	 * Check if the argument at the given position is a <code>IStringX</code> string object.
	 * 
	 * @param position
	 *            the position which has to be a string.
	 * @throws WrongArgumentType
	 *             if it's not a symbol.
	 */
public static String checkContextName(IAST ast, int position) {
    if (ast.get(position) instanceof IStringX) {
        IStringX strX = (IStringX) ast.get(position);
        String contextName = strX.toString();
        if (contextName.charAt(contextName.length() - 1) != '`') {
            throw new WrongArgumentType(ast, ast.get(position), position, "Contextname must be prepended by a '`' character!");
        }
        return contextName;
    }
    throw new WrongArgumentType(ast, ast.get(position), position, "String expected!");
}
Also used : IStringX(org.matheclipse.core.interfaces.IStringX)

Aggregations

IStringX (org.matheclipse.core.interfaces.IStringX)12 IExpr (org.matheclipse.core.interfaces.IExpr)9 IAST (org.matheclipse.core.interfaces.IAST)7 IOException (java.io.IOException)5 WrongNumberOfArguments (org.matheclipse.core.eval.exception.WrongNumberOfArguments)5 TermOrder (edu.jas.poly.TermOrder)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)2 Options (org.matheclipse.core.eval.util.Options)2 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)2 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1