Skip to content

Commit cdb1a79

Browse files
committed
WIP #972 Definition, FullDefiniton, Save for symbols In, Out
1 parent 3603c85 commit cdb1a79

File tree

10 files changed

+326
-154
lines changed

10 files changed

+326
-154
lines changed

symja_android_library/doc/functions/FullDefinition.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ArcSinh(ComplexInfinity)=ComplexInfinity
3232

3333
```
3434
>> a(x_):=b(x,y);b[u_,v_]:={{u,v},a}
35+
36+
>> FullDefinition(a)
3537
a(x_):=b(x,y)
3638
3739
b(u_,v_):={{u,v},a}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/AttributeFunctions.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ private IExpr clearAttributes(final IExpr expr, IExpr attributes, IAST ast, Eval
194194
}
195195
}
196196
if (sym.isProtected()) {
197-
Errors.printMessage(S.ClearAttributes, "write", F.list(sym), EvalEngine.get());
197+
// Tag `1` in `2` is Protected.
198+
Errors.printMessage(S.ClearAttributes, "write", F.list(sym, expr), EvalEngine.get());
198199
throw new FailedException();
199200
}
200201
if (attributes.isSymbol()) {

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/FileFunctions.java

+74-5
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ protected static IExpr loadPackage(final EvalEngine engine, final String is) {
600600
try {
601601
final List<ASTNode> node = parseReader(is, engine);
602602
return evaluatePackage(node, engine);
603-
} catch (final Exception e) {
604-
LOGGER.error("Get.loadPackage() failed", e);
603+
} catch (final RuntimeException rex) {
604+
Errors.printMessage(S.Get, rex, engine);
605605
}
606606
return S.Null;
607607
}
@@ -1188,6 +1188,70 @@ public int[] expectedArgSize(IAST ast) {
11881188
}
11891189
}
11901190

1191+
/**
1192+
* <pre>
1193+
* <code>Save(&quot;path-to-filename&quot;, expression)
1194+
* </code>
1195+
* </pre>
1196+
*
1197+
* <p>
1198+
* if the file system is enabled, export the <code>FullDefinition</code> of the
1199+
* <code>expression</code> to the &quot;path-to-filename&quot; file. The saved file can be
1200+
* imported with <code>Get</code>.
1201+
* </p>
1202+
*
1203+
* <pre>
1204+
* <code>Save(&quot;path-to-filename&quot;, &quot;Global`*&quot;)
1205+
* </code>
1206+
* </pre>
1207+
*
1208+
* <p>
1209+
* if the file system is enabled, export the <code>FullDefinition</code> of all symbols in the
1210+
* <code>&quot;Global</code>*&quot;` context to the &quot;path-to-filename&quot; file.
1211+
* </p>
1212+
*
1213+
* <h3>Examples</h3>
1214+
* <p>
1215+
* Save a definition with dependent symbol definitions into temporary file
1216+
* </p>
1217+
*
1218+
* <pre>
1219+
* <code>&gt;&gt; g(x_) := x^3;
1220+
*
1221+
* &gt;&gt; g(x_,y_) := f(x,y);
1222+
*
1223+
* &gt;&gt; SetAttributes(f, Listable);
1224+
*
1225+
* &gt;&gt; f(x_) := g(x^2);
1226+
*
1227+
* &gt;&gt; temp = FileNameJoin({$TemporaryDirectory, \&quot;savedlist.txt\&quot;});Print(temp);
1228+
*
1229+
* &gt;&gt; Save(temp, {f,g})
1230+
*
1231+
* &gt;&gt; ClearAll(f,g)
1232+
*
1233+
* &gt;&gt; &quot;Attributes(f)
1234+
*
1235+
* &gt;&gt; {f(2),g(7)}
1236+
*
1237+
* &gt;&gt; Get(temp)
1238+
*
1239+
* &gt;&gt; {f(2),g(7)}
1240+
* {64,343}
1241+
*
1242+
* &gt;&gt; Attributes(f)
1243+
* {Listable}
1244+
* </code>
1245+
* </pre>
1246+
*
1247+
* <h3>Related terms</h3>
1248+
* <p>
1249+
* <a href="BinaryDeserialize.md">BinaryDeserialize</a>,
1250+
* <a href="BinarySerialize.md">BinarySerialize</a>, <a href="ByteArray.md">ByteArray</a>,
1251+
* <a href="ByteArrayQ.md">ByteArrayQ</a>, <a href="Export.md">Export</a>,
1252+
* <a href="Import.md">Import</a>
1253+
* </p>
1254+
*/
11911255
private static final class Save extends AbstractFunctionEvaluator {
11921256

11931257
@Override
@@ -1210,7 +1274,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
12101274
boolean ignoreCase = false;
12111275
symbolsList = IOFunctions.getSymbolsByPattern(arg2, ignoreCase, ast, engine);
12121276
}
1213-
if (symbolsList.isPresent()) {
1277+
if (symbolsList.isPresent()) {
12141278
String str = ISymbol.fullDefinitionListToString(symbolsList);
12151279
try (FileWriter writer = new FileWriter(fileName.toString())) {
12161280
writer.write(str);
@@ -1377,8 +1441,13 @@ private static IExpr evaluatePackageRecursive(final List<ASTNode> node, int i,
13771441
result = evaluatePackageRecursive(((FunctionNode) astNode), 1, compoundExpression, ast2Expr,
13781442
engine);
13791443
} else {
1380-
temp = ast2Expr.convert(astNode);
1381-
result = engine.evaluate(temp);
1444+
try {
1445+
temp = ast2Expr.convert(astNode);
1446+
result = engine.evaluate(temp);
1447+
} catch (final RuntimeException rex) {
1448+
result = S.Null;
1449+
Errors.printMessage(S.Get, rex, engine);
1450+
}
13821451
}
13831452
i++;
13841453
}

0 commit comments

Comments
 (0)