use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.
the class Import method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
if (!(ast.arg1() instanceof IStringX)) {
throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
}
if (!(ast.arg2() instanceof IStringX)) {
throw new WrongNumberOfArguments(ast, 2, ast.size() - 1);
}
IStringX arg1 = (IStringX) ast.arg1();
IStringX arg2 = (IStringX) ast.arg2();
FileReader reader = null;
try {
reader = new FileReader(arg1.toString());
if (arg2.contentEquals("Table")) {
AST2Expr ast2Expr = AST2Expr.CONST;
if (engine.isRelaxedSyntax()) {
ast2Expr = AST2Expr.CONST_LC;
}
final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
Iterable<CSVRecord> records = csvFormat.parse(reader);
IAST rowList = F.List();
for (CSVRecord record : records) {
IAST columnList = F.List();
for (String string : record) {
final ASTNode node = parser.parse(string);
IExpr temp = ast2Expr.convert(node, engine);
columnList.append(temp);
}
rowList.append(columnList);
}
return rowList;
}
} catch (IOException ioe) {
engine.printMessage("Import: file " + arg1.toString() + " not found!");
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.
the class Package method loadPackage.
/**
* Load a package from the given reader
*
* @param is
*/
public static void loadPackage(final EvalEngine engine, final Reader is) {
String record = null;
// new
final BufferedReader r = new BufferedReader(is);
try {
StringBuilder builder = new StringBuilder(2048);
while ((record = r.readLine()) != null) {
builder.append(record);
builder.append('\n');
}
IExpr parsedExpression = engine.parse(builder.toString());
if (parsedExpression != null && parsedExpression.isAST()) {
IAST ast = (IAST) parsedExpression;
if (ast.size() != 4 || !(ast.arg1() instanceof IStringX) || !ast.arg2().isList() || !ast.arg3().isList()) {
throw new WrongNumberOfArguments(ast, 3, ast.size() - 1);
}
IAST symbols = (IAST) ast.arg2();
IAST list = (IAST) ast.arg3();
evalPackage(symbols, list, engine);
}
} catch (final Exception e) {
e.printStackTrace();
} finally {
try {
r.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.
the class Print method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
PrintStream stream = engine.getOutPrintStream();
if (stream == null) {
stream = System.out;
}
try {
final StringBuilder buf = new StringBuilder();
IExpr temp;
for (int i = 1; i < ast.size(); i++) {
temp = engine.evaluate(ast.get(i));
if (temp instanceof IStringX) {
buf.append(temp.toString());
} else {
OutputFormFactory.get().convert(buf, temp);
}
}
stream.println(buf.toString());
} catch (IOException e) {
stream.println(e.getMessage());
if (Config.DEBUG) {
e.printStackTrace();
}
}
return F.Null;
}
use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.
the class MessageName method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
if (!ast.arg1().isSymbol()) {
throw new WrongArgumentType(ast, ast.arg1(), 1, "");
}
IExpr arg2 = engine.evaluate(ast.arg2());
if (arg2 instanceof IStringX || arg2.isSymbol()) {
return F.NIL;
}
if (!ast.arg2().isAST(F.Set, 3)) {
// !ast.arg2().isSymbol()) {
throw new WrongArgumentType(ast, ast.arg2(), 2, "");
}
// symbol.putDownRule(RuleType.SET, true, symbol, ast.arg2(), true);
return F.Null;
}
use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.
the class Export method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3, 4);
if (!(ast.arg1() instanceof IStringX)) {
throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
}
String format = "Table";
if (ast.size() == 4) {
if (!(ast.arg3() instanceof IStringX)) {
throw new WrongNumberOfArguments(ast, 3, ast.size() - 1);
}
format = ((IStringX) ast.arg3()).toString();
}
IStringX arg1 = (IStringX) ast.arg1();
IExpr arg2 = ast.arg2();
FileWriter writer = null;
try {
writer = new FileWriter(arg1.toString());
if (format.equals("Table")) {
int[] dims = arg2.isMatrix();
if (dims != null) {
for (int j = 0; j < dims[0]; j++) {
IAST rowList = (IAST) arg2.getAt(j + 1);
for (int i = 1; i <= dims[1]; i++) {
if (rowList.get(i).isSignedNumber()) {
writer.append(rowList.get(i).toString());
} else {
writer.append("\"");
writer.append(rowList.get(i).toString());
writer.append("\"");
}
if (i < dims[1]) {
writer.append(" ");
}
}
writer.append("\n");
}
return arg1;
} else {
if (arg2.isList()) {
}
}
}
} catch (IOException ioe) {
engine.printMessage("Export: file " + arg1.toString() + " not found!");
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
}
}
}
return F.NIL;
}
Aggregations