Skip to content

Commit

Permalink
Refactor/clean up attribute "get" methods
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Sep 21, 2024
1 parent 2180117 commit e4ef946
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public IExpr evaluateSet(final IExpr leftHandSide, IExpr rightHandSide,
// `1` is not a variable with a value, so its value cannot be changed.
return Errors.printMessage(builtinSymbol, "rvalue", F.list(symbol), engine);
} else {
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
return Errors.printMessage(builtinSymbol, "wrsym", F.list(symbol), EvalEngine.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private IExpr clearAttributes(final IExpr expr, IExpr attributes, IAST ast, Eval
throw new RuleCreationError(sym);
}
}
if (sym.isProtected()) {
if (sym.hasProtectedAttribute()) {
// Tag `1` in `2` is Protected.
Errors.printMessage(S.ClearAttributes, "write", F.list(sym, expr), EvalEngine.get());
throw new FailedException();
Expand All @@ -216,8 +216,7 @@ private IExpr clearAttributes(final IExpr expr, IExpr attributes, IAST ast, Eval
ISymbol attribute = (ISymbol) lst.get(i);
if (!clearAttributes(sym, attribute)) {
// `1` is not a known attribute.
return Errors.printMessage(S.ClearAttributes, "attnf", F.List(attribute),
engine);
return Errors.printMessage(S.ClearAttributes, "attnf", F.List(attribute), engine);
}
}
return S.Null;
Expand Down Expand Up @@ -301,13 +300,17 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
mutable.set(i, x);
}
final IASTAppendable result = F.ListAlloc(mutable.size());
mutable.forEach(x -> appendProtected(result, x));
mutable.forEach(x -> {
if (x.isSymbol()) {
appendProtected(result, (ISymbol) x);
}
});
return result;
}

private static void appendProtected(final IASTAppendable result, IExpr x) {
ISymbol symbol = (ISymbol) x;
if (!symbol.isProtected()) {
private static void appendProtected(final IASTAppendable result, ISymbol x) {
ISymbol symbol = x;
if (!symbol.hasProtectedAttribute()) {
symbol.addAttributes(ISymbol.PROTECTED);
result.append(x);
}
Expand Down Expand Up @@ -344,7 +347,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {

private static void appendUnprotected(final IASTAppendable result, IExpr x) {
ISymbol symbol = (ISymbol) x;
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
symbol.clearAttributes(ISymbol.PROTECTED);
result.append(x);
}
Expand Down Expand Up @@ -411,7 +414,7 @@ private static IExpr setSymbolsAttributes(IAST listOfSymbols, IExpr attributes,
for (int i = 1; i < listOfSymbols.size(); i++) {
final IExpr arg = listOfSymbols.get(i);
if (arg.isSymbol()) {
if (((ISymbol) arg).isProtected()) {
if (((ISymbol) arg).hasProtectedAttribute()) {
Errors.printMessage(S.ClearAttributes, "write", F.list(arg), EvalEngine.get());
throw new FailedException();
}
Expand Down Expand Up @@ -473,7 +476,7 @@ private static IExpr addAttributes(final IExpr expr, IExpr attributes, IAST ast,
* @param attribute
*/
private static boolean addAttributes(final ISymbol sym, ISymbol attribute) {
if (sym.isProtected()) {
if (sym.hasProtectedAttribute()) {
Errors.printMessage(S.SetAttributes, "write", F.list(sym), EvalEngine.get());
throw new FailedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public Formula expr2LogicNGFormula(final IExpr logicExpr, boolean substituteExpr
if (symbol.isTrue()) {
return factory.verum();
}
if (!symbol.isVariable() || symbol.isProtected()) {
if (!symbol.isVariable() || symbol.hasProtectedAttribute()) {
// `1` is not a valid variable.
String message = Errors.getMessage("ivar", F.list(symbol), EvalEngine.get());
throw new ArgumentTypeException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (x.isNIL()) {
return F.NIL;
}
if (((ISymbol) x).isProtected()) {
if (((ISymbol) x).hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(ast.topHead(), "wrsym", F.list(x), engine);
return S.Null;
Expand Down Expand Up @@ -271,7 +271,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (x.isNIL()) {
return F.NIL;
}
if (((ISymbol) x).isProtected()) {
if (((ISymbol) x).hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(ast.topHead(), "wrsym", F.list(x), engine);
return S.Null;
Expand Down Expand Up @@ -435,7 +435,7 @@ public IExpr evaluateSet(final IExpr leftHandSide, IExpr rightHandSide,
return rightHandSide;
}
ISymbol symbol = (ISymbol) leftHandSide.first();
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
Errors.printMessage(S.Default, "write", F.list(symbol, leftHandSide), EvalEngine.get());
throw new FailedException();
}
Expand Down Expand Up @@ -1131,7 +1131,7 @@ public IExpr evaluateSet(final IExpr leftHandSide, IExpr rightHandSide,
IBuiltInSymbol builtinSymbol, EvalEngine engine) {
if (leftHandSide.isAST(S.MessageName, 3, 4) && leftHandSide.first().isSymbol()) {
ISymbol symbol = (ISymbol) leftHandSide.first();
if (!symbol.isProtected()) {
if (!symbol.hasProtectedAttribute()) {
String messageName = leftHandSide.second().toString();
rightHandSide = engine.evaluate(rightHandSide);
IStringX message;
Expand Down Expand Up @@ -1275,7 +1275,7 @@ public IExpr evaluateSet(final IExpr leftHandSide, IExpr rightHandSide,
IBuiltInSymbol builtinSymbol, EvalEngine engine) {
if (leftHandSide.isAST(S.Options, 2) && leftHandSide.first().isSymbol()) {
ISymbol symbol = (ISymbol) leftHandSide.first();
if (!symbol.isProtected()) {
if (!symbol.hasProtectedAttribute()) {
try {
if (!builtinSymbol.equals(S.SetDelayed)) {
rightHandSide = engine.evaluate(rightHandSide);
Expand Down Expand Up @@ -2212,7 +2212,7 @@ private static IExpr setDownRule(IExpr leftHandSide, int flags, IExpr rightHandS
// final Object[] result = new Object[] { null, rightHandSide };
if (leftHandSide.isAST()) {
final ISymbol lhsSymbol = determineRuleTag(leftHandSide);
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(S.Set, "wrsym", F.list(lhsSymbol), EvalEngine.get());
return rightHandSide;
Expand All @@ -2223,7 +2223,7 @@ private static IExpr setDownRule(IExpr leftHandSide, int flags, IExpr rightHandS
}
if (leftHandSide.isSymbol()) {
final ISymbol lhsSymbol = (ISymbol) leftHandSide;
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(S.Set, "wrsym", F.list(lhsSymbol), EvalEngine.get());
return rightHandSide;
Expand Down Expand Up @@ -2282,7 +2282,7 @@ private static void setDelayedDownRule(IExpr leftHandSide, int flags, IExpr righ
lhsSymbol = determineRuleTag(leftHandSide);
}
if (lhsSymbol != null) {
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
Expand All @@ -2298,7 +2298,7 @@ private static void setDelayedDownRule(IExpr leftHandSide, int flags, IExpr righ
}
if (leftHandSide.isSymbol()) {
lhsSymbol = (ISymbol) leftHandSide;
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
Expand Down Expand Up @@ -2340,7 +2340,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
} catch (final ReturnException e) {
rightHandSide = e.getValue();
}
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
// Tag `1` in `2` is Protected.
Errors.printMessage(S.TagSet, "write", F.list(symbol, leftHandSide), EvalEngine.get());
throw new FailedException();
Expand Down Expand Up @@ -2425,7 +2425,7 @@ protected static Object[] createPatternMatcher(ISymbol tagSetSymbol, IExpr leftH
}
if (putDownRule) {
boolean pMode = packageMode;
if (!packageMode && tagSetSymbol.isProtected()) {
if (!packageMode && tagSetSymbol.hasProtectedAttribute()) {
// Tag `1` in `2` is Protected.
Errors.printMessage(tagSymbol.topHead(), "write", F.list(tagSetSymbol, leftHandSide),
EvalEngine.get());
Expand Down Expand Up @@ -2477,7 +2477,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
ISymbol symbol = (ISymbol) arg1;
final IExpr leftHandSide = ast.arg2();
final IExpr rightHandSide = ast.arg3();
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
// Tag `1` in `2` is Protected.
Errors.printMessage(ast.topHead(), "write", F.list(symbol, leftHandSide),
EvalEngine.get());
Expand Down Expand Up @@ -2652,15 +2652,15 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {

if (leftHandSide.isAST()) {
final ISymbol lhsSymbol = determineRuleTag(leftHandSide);
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(ast.topHead(), "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
}
}
if (leftHandSide.isSymbol()) {
final ISymbol lhsSymbol = (ISymbol) leftHandSide;
if (lhsSymbol.isProtected()) {
if (lhsSymbol.hasProtectedAttribute()) {
// Symbol `1` is Protected.
Errors.printMessage(ast.topHead(), "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,7 @@ public IExpr evaluateSet(final IExpr leftHandSide, IExpr rightHandSide,
// `1` is not a variable with a value, so its value cannot be changed.
return Errors.printMessage(builtinSymbol, "rvalue", F.list(symbol), engine);
} else {
if (symbol.isProtected()) {
if (symbol.hasProtectedAttribute()) {
return Errors.printMessage(builtinSymbol, "write", F.list(symbol), EvalEngine.get());
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.function.Function;
import javax.annotation.Nullable;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.basic.OperationSystem;
import org.matheclipse.core.basic.ToggleFeature;
import org.matheclipse.core.convert.JASConvert;
import org.matheclipse.core.convert.JASIExpr;
Expand Down Expand Up @@ -357,7 +356,7 @@ private static IExpr evalLimit(final IExpr expr, LimitData data, EvalEngine engi
return powerLimit(ast, data, engine);
} else if (ast.isAST(S.Piecewise, 3)) {
return piecewiseLimit(ast, data, engine);
} else if (ast.argSize() > 0 && ast.topHead().isNumericFunctionAttribute()) {
} else if (ast.argSize() > 0 && ast.isNumericFunctionAST()) {
IASTMutable copy = ast.copy();
IExpr temp = F.NIL;
boolean indeterminate = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public boolean visit(IAST list) {
fCollection.add(list);
}
} else {
if (!list.isBuiltInFunction()
|| ((ISymbol) list.head()).isNumericFunctionAttribute()) {
if (!list.isBuiltInFunction() || list.isNumericFunctionAST()) {
if (!list.isNumericFunction(true)) {
fCollection.add(list);
}
Expand All @@ -76,6 +75,7 @@ public boolean visit(ISymbol symbol) {
}
}


/**
* Collect the variables which satisfy the <code>IExpr#isVariable()</code> predicate and which are
* used in logical functions like Not, And, OR, Xor,...
Expand Down Expand Up @@ -112,6 +112,7 @@ public boolean visit(ISymbol symbol) {
}
}


static class BooleanVariablesPureFunctionVisitor extends AbstractVisitorBoolean {
int highestSlotNumber;

Expand Down Expand Up @@ -151,6 +152,7 @@ public boolean visit(ISymbol symbol) throws NoEvalException {

}


/**
* Return <code>true</code>, if the expression contains one of the variable store in the internal
* <code>java.util.Set</code>.
Expand All @@ -176,6 +178,7 @@ public boolean visit(ISymbol symbol) {
}
}


/**
* Collect the variables with the <code>IExpr#isVariable()</code> method.
*
Expand Down Expand Up @@ -218,6 +221,7 @@ public boolean visit(ISymbol symbol) {
}
return false;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.IOException;
import java.math.BigInteger;

import org.matheclipse.core.basic.OperationSystem;
import org.matheclipse.core.eval.Errors;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.expression.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.matheclipse.core.expression.F.Less;
import static org.matheclipse.core.expression.F.Subtract;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.basic.OperationSystem;
import org.matheclipse.core.builtin.QuantityFunctions;
import org.matheclipse.core.eval.Errors;
import org.matheclipse.core.eval.EvalEngine;
Expand Down Expand Up @@ -381,7 +380,7 @@ public void tearDown() {
}
}

private static class DoubleIterator implements IIterator<IExpr> {
public static class DoubleIterator implements IIterator<IExpr> {
double count;

double lowerLimit;
Expand Down Expand Up @@ -1208,7 +1207,7 @@ public static IIterator<IExpr> create(final IAST list, int position, final EvalE

if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
if (!sym.isVariable() || sym.hasProtectedAttribute()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(
Errors.getMessage("setraw", F.list(sym), EvalEngine.get()));
Expand Down Expand Up @@ -1253,7 +1252,7 @@ public static IIterator<IExpr> create(final IAST list, int position, final EvalE

if (list.arg1().isSymbol()) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
if (!sym.isVariable() || sym.hasProtectedAttribute()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(
Errors.getMessage("setraw", F.list(sym), EvalEngine.get()));
Expand Down Expand Up @@ -1299,7 +1298,7 @@ public static IIterator<IExpr> create(final IAST list, int position, final EvalE
step = evalEngine.evalWithoutNumericReset(list.arg4());
if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
if (!sym.isVariable() || sym.hasProtectedAttribute()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(
Errors.getMessage("setraw", F.list(sym), EvalEngine.get()));
Expand Down Expand Up @@ -1384,7 +1383,7 @@ public static IIterator<IExpr> create(final IAST list, final ISymbol symbol,
ISymbol variable;
boolean fNumericMode;

if (symbol != null && (!symbol.isVariable() || symbol.isProtected())) {
if (symbol != null && (!symbol.isVariable() || symbol.hasProtectedAttribute())) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(
Errors.getMessage("setraw", F.list(symbol), EvalEngine.get()));
Expand Down Expand Up @@ -1432,7 +1431,7 @@ public static IIterator<IExpr> create(final IAST list, final ISymbol symbol,
variable = symbol;
if (upperLimit.isList()) {
if (variable != null) {
if (!variable.isVariable() || variable.isProtected()) {
if (!variable.isVariable() || variable.hasProtectedAttribute()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(
Errors.getMessage("setraw", F.list(variable), EvalEngine.get()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public boolean isPositive() {

/** {@inheritDoc} */
@Override
public boolean isProtected() {
public boolean hasProtectedAttribute() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final Context getContext() {

/** {@inheritDoc} */
@Override
public boolean isProtected() {
public boolean hasProtectedAttribute() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ private boolean matchASTSequence(IAST lhsPatternAST, IAST lhsEvalAST, final int
try {
IExpr head = lhsPatternAST.head();
boolean flat = lhsPatternAST.isFlatAST();
boolean oneIdentity = head.isSymbol() ? ((ISymbol) head).isOneIdentityAttribute() : false;
boolean oneIdentity = head.isSymbol() ? ((ISymbol) head).hasOneIdentityAttribute() : false;
if (lhsPatternAST.size() == lhsEvalAST.size()) {
IAST[] removed = remove(lhsPatternAST, lhsEvalAST, engine, stackMatcher);
if (removed == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private static IExpr evaluateDIfPossible(IAST head, IAST headDerivative, IAST fu
IExpr temp = newFunction;
if (headDerivative.arg1().isBuiltInSymbol()) {
IBuiltInSymbol builtin = (IBuiltInSymbol) headDerivative.arg1();
if (builtin.isNumericFunctionAttribute()) {
if (builtin.hasNumericFunctionAttribute()) {
if (head.isAST1()) {
int n = head.first().toIntDefault();
if (n > 0) {
Expand Down
Loading

0 comments on commit e4ef946

Please sign in to comment.