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;
}
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);
}
}
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;
}
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;
}
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!");
}
Aggregations