Skip to content

Commit

Permalink
New bitwise operators NAND, NOR, XOR #152, ":" as division #318, Inte…
Browse files Browse the repository at this point in the history
…ger division added #285
  • Loading branch information
mariuszgromada committed May 18, 2024
1 parent e267815 commit fc1af76
Show file tree
Hide file tree
Showing 44 changed files with 6,848 additions and 4,589 deletions.
76 changes: 60 additions & 16 deletions CURRENT/c-sharp/src/org/mariuszgromada/math/mxparser/Expression.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)Expression.cs 5.2.1 2023-02-05
* @(#)Expression.cs 6.0.0 2024-05-18
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2023-01-29
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -195,7 +195,7 @@

namespace org.mariuszgromada.math.mxparser {
/**
* Expression - base class for real expressions definition.
* Expression - base class for real expression definition.
*
* Examples:
* <ul>
Expand All @@ -219,7 +219,7 @@ namespace org.mariuszgromada.math.mxparser {
* <a href="https://play.google.com/store/apps/details?id=org.mathparser.scalar.pro" target="_blank">Scalar Pro</a><br>
* <a href="https://mathspace.pl" target="_blank">MathSpace.pl</a><br>
*
* @version 5.2.1
* @version 6.0.0
*
* @see Argument
* @see RecursiveArgument
Expand Down Expand Up @@ -2226,6 +2226,17 @@ private void DIVIDE(int pos) {
}
else opSetDecreaseRemove(pos, MathFunctions.div(a, b), true);
}
/**
* Integer division handling.
*
* @param pos the token position
*/
private void DIVIDE_QUOTIENT(int pos) {
if (!isFullyCompiled) registerCompiledElement(CompiledElement.ToCall.DIVIDE_QUOTIENT, pos);
double a = getTokenValue(pos-1);
double b = getTokenValue(pos+1);
opSetDecreaseRemove(pos, MathFunctions.divQuotient(a, b), true);
}
/**
* Multiplication handling.
*
Expand Down Expand Up @@ -2540,6 +2551,36 @@ private void BITWISE_XOR(int pos) {
long b = (long)getTokenValue(pos + 1);
opSetDecreaseRemove(pos, a ^ b);
}
/**
* Bitwise NAND
*
* @param pos the token position
*/
private void BITWISE_NAND(int pos) {
long a = (long)getTokenValue(pos-1);
long b = (long)getTokenValue(pos+1);
opSetDecreaseRemove(pos, ~(a & b));
}
/**
* Bitwise NOR
*
* @param pos the token position
*/
private void BITWISE_NOR(int pos) {
long a = (long)getTokenValue(pos-1);
long b = (long)getTokenValue(pos+1);
opSetDecreaseRemove(pos, ~(a | b));
}
/**
* Bitwise NOR
*
* @param pos the token position
*/
private void BITWISE_XNOR(int pos) {
long a = (long)getTokenValue(pos-1);
long b = (long)getTokenValue(pos+1);
opSetDecreaseRemove(pos, ~(a ^ b));
}
/**
* Bitwise LEFT SHIFT
*
Expand Down Expand Up @@ -5188,7 +5229,7 @@ private int calculateFirstAndFullyCompile(CalcStepsRegister calcStepsRegister, S
int calculusPos, ifPos, iffPos, variadicFunPos;
int depArgPos, recArgPos, f3ArgPos, f2ArgPos;
int f1ArgPos, userFunPos, plusPos, minusPos;
int multiplyPos, dividePos, powerPos, tetrationPos;
int multiplyPos, dividePos, divideQuotientPos, powerPos, tetrationPos;
int powerNum, factPos, modPos, percPos;
int negPos, rootOperGroupPos, andGroupPos, orGroupPos;
int implGroupPos, bolPos, eqPos, neqPos;
Expand All @@ -5200,6 +5241,7 @@ private int calculateFirstAndFullyCompile(CalcStepsRegister calcStepsRegister, S
bool depArgFound;
int lPos, rPos;
int tokenIndex, pos, p;
int firstPos;
Token token, tokenL, tokenR;
Argument argument;
List<int> commas = null;
Expand Down Expand Up @@ -5232,7 +5274,7 @@ private int calculateFirstAndFullyCompile(CalcStepsRegister calcStepsRegister, S
calculusPos = -1; ifPos = -1; iffPos = -1; variadicFunPos = -1;
recArgPos = -1; depArgPos = -1; f3ArgPos = -1; f2ArgPos = -1;
f1ArgPos = -1; userFunPos = -1; plusPos = -1; minusPos = -1;
multiplyPos = -1; dividePos = -1; powerPos = -1; tetrationPos = -1;
multiplyPos = -1; dividePos = -1; divideQuotientPos = -1; powerPos = -1; tetrationPos = -1;
factPos = -1; modPos = -1; percPos = -1; powerNum = 0;
negPos = -1; rootOperGroupPos = -1; andGroupPos = -1; orGroupPos = -1;
implGroupPos = -1; bolPos = -1; eqPos = -1; neqPos = -1;
Expand Down Expand Up @@ -5362,6 +5404,7 @@ private int calculateFirstAndFullyCompile(CalcStepsRegister calcStepsRegister, S
else if (token.tokenId == Operator.MINUS_ID && minusPos < 0 && rigthIsNumber) minusPos = pos;
else if (token.tokenId == Operator.MULTIPLY_ID && multiplyPos < 0 && leftIsNumber && rigthIsNumber) multiplyPos = pos;
else if (token.tokenId == Operator.DIVIDE_ID && dividePos < 0 && leftIsNumber && rigthIsNumber) dividePos = pos;
else if (token.tokenId == Operator.DIVIDE_QUOTIENT_ID && divideQuotientPos < 0 && leftIsNumber && rigthIsNumber) divideQuotientPos = pos;
} else if (token.tokenTypeId == BooleanOperator.TYPE_ID) {
if (token.tokenId == BooleanOperator.NEG_ID && negPos < 0 && rigthIsNumber) negPos = pos;
else if (leftIsNumber && rigthIsNumber) {
Expand Down Expand Up @@ -5423,19 +5466,16 @@ private int calculateFirstAndFullyCompile(CalcStepsRegister calcStepsRegister, S
else if (negPos >= 0) NEG(negPos);
else if (rootOperGroupPos >= 0) rootOperCalc(rootOperGroupPos);
else if (bitwiseComplPos >= 0) BITWISE_COMPL(bitwiseComplPos);
else if (multiplyPos >= 0 || dividePos >= 0) {
if (multiplyPos >= 0 && dividePos >= 0) {
if (multiplyPos <= dividePos) MULTIPLY(multiplyPos);
else DIVIDE(dividePos);
} else if (multiplyPos >= 0) MULTIPLY(multiplyPos);
else DIVIDE(dividePos);
else if (multiplyPos >= 0 || dividePos >= 0 || divideQuotientPos >= 0) {
firstPos = ExpressionUtils.findNonNegativeMinimum(multiplyPos, dividePos, divideQuotientPos);
if (multiplyPos == firstPos) MULTIPLY(multiplyPos);
else if (dividePos == firstPos) DIVIDE(dividePos);
else if (divideQuotientPos == firstPos) DIVIDE_QUOTIENT(divideQuotientPos);
} else
if (minusPos >= 0 || plusPos >= 0) {
if (minusPos >= 0 && plusPos >= 0) {
if (minusPos <= plusPos) MINUS(minusPos);
else PLUS(plusPos);
} else if (minusPos >= 0) MINUS(minusPos);
else PLUS(plusPos);
firstPos = ExpressionUtils.findNonNegativeMinimum(minusPos, plusPos);
if (minusPos == firstPos) MINUS(minusPos);
else if (plusPos == firstPos) PLUS(plusPos);
} else
if (neqPos >= 0) NEQ(neqPos);
else if (eqPos >= 0) EQ(eqPos);
Expand Down Expand Up @@ -5551,6 +5591,7 @@ private int applySequenceFromCompilation(CalcStepsRegister calcStepsRegister, St
case CompiledElement.ToCall.BITWISE_COMPL: BITWISE_COMPL(pos); break;
case CompiledElement.ToCall.MULTIPLY: MULTIPLY(pos); break;
case CompiledElement.ToCall.DIVIDE: DIVIDE(pos); break;
case CompiledElement.ToCall.DIVIDE_QUOTIENT: DIVIDE_QUOTIENT(pos); break;
case CompiledElement.ToCall.MINUS: MINUS(pos); break;
case CompiledElement.ToCall.PLUS: PLUS(pos); break;
case CompiledElement.ToCall.NEQ: NEQ(pos); break;
Expand Down Expand Up @@ -5963,6 +6004,9 @@ private void bitwiseCalc(int pos) {
case BitwiseOperator.AND_ID: BITWISE_AND(pos); break;
case BitwiseOperator.OR_ID: BITWISE_OR(pos); break;
case BitwiseOperator.XOR_ID: BITWISE_XOR(pos); break;
case BitwiseOperator.NAND_ID: BITWISE_NAND(pos); break;
case BitwiseOperator.NOR_ID: BITWISE_NOR(pos); break;
case BitwiseOperator.XNOR_ID: BITWISE_XNOR(pos); break;
case BitwiseOperator.LEFT_SHIFT_ID: BITWISE_LEFT_SHIFT(pos); break;
case BitwiseOperator.RIGHT_SHIFT_ID: BITWISE_RIGHT_SHIFT(pos); break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ private static void makeParserKeyWords(bool parserKeyWordsOnly, bool UDFExpressi
addKeyWordUnicode(Operator.MULTIPLY_STR_UNI_2, Operator.MULTIPLY_DESC, Operator.MULTIPLY_ID, Operator.MULTIPLY_SYN_UNI_2, Operator.MULTIPLY_SINCE_UNI_2, Operator.TYPE_ID, unicodeKeyWordsEnabled, keyWordsList);
addKeyWordUnicode(Operator.MULTIPLY_STR_UNI_3, Operator.MULTIPLY_DESC, Operator.MULTIPLY_ID, Operator.MULTIPLY_SYN_UNI_3, Operator.MULTIPLY_SINCE_UNI_3, Operator.TYPE_ID, unicodeKeyWordsEnabled, keyWordsList);
addKeyWord(Operator.DIVIDE_STR, Operator.DIVIDE_DESC, Operator.DIVIDE_ID, Operator.DIVIDE_SYN, Operator.DIVIDE_SINCE, Operator.TYPE_ID, keyWordsList);
addKeyWord(Operator.DIVIDE_EUROPE_STR, Operator.DIVIDE_DESC, Operator.DIVIDE_ID, Operator.DIVIDE_EUROPE_SYN, Operator.DIVIDE_SINCE_EUROPE, Operator.TYPE_ID, keyWordsList);
addKeyWord(Operator.DIVIDE_QUOTIENT_STR, Operator.DIVIDE_QUOTIENT_DESC, Operator.DIVIDE_QUOTIENT_ID, Operator.DIVIDE_QUOTIENT_SYN, Operator.DIVIDE_QUOTIENT_SINCE, Operator.TYPE_ID, keyWordsList);
addKeyWordUnicode(Operator.DIVIDE_STR_UNI_1, Operator.DIVIDE_DESC, Operator.DIVIDE_ID, Operator.DIVIDE_SYN_UNI_1, Operator.DIVIDE_SINCE_UNI_1, Operator.TYPE_ID, unicodeKeyWordsEnabled, keyWordsList);
addKeyWord(Operator.POWER_STR, Operator.POWER_DESC, Operator.POWER_ID, Operator.POWER_SYN, Operator.POWER_SINCE, Operator.TYPE_ID, keyWordsList);
addKeyWord(Operator.FACT_STR, Operator.FACT_DESC, Operator.FACT_ID, Operator.FACT_SYN, Operator.FACT_SINCE, Operator.TYPE_ID, keyWordsList);
Expand Down Expand Up @@ -744,6 +746,9 @@ private static void makeParserKeyWords(bool parserKeyWordsOnly, bool UDFExpressi
addKeyWord(BitwiseOperator.OR_STR, BitwiseOperator.OR_DESC, BitwiseOperator.OR_ID, BitwiseOperator.OR_SYN, BitwiseOperator.OR_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
addKeyWord(BitwiseOperator.LEFT_SHIFT_STR, BitwiseOperator.LEFT_SHIFT_DESC, BitwiseOperator.LEFT_SHIFT_ID, BitwiseOperator.LEFT_SHIFT_SYN, BitwiseOperator.LEFT_SHIFT_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
addKeyWord(BitwiseOperator.RIGHT_SHIFT_STR, BitwiseOperator.RIGHT_SHIFT_DESC, BitwiseOperator.RIGHT_SHIFT_ID, BitwiseOperator.RIGHT_SHIFT_SYN, BitwiseOperator.RIGHT_SHIFT_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
addKeyWord(BitwiseOperator.NAND_STR, BitwiseOperator.NAND_DESC, BitwiseOperator.NAND_ID, BitwiseOperator.NAND_SYN, BitwiseOperator.NAND_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
addKeyWord(BitwiseOperator.NOR_STR, BitwiseOperator.NOR_DESC, BitwiseOperator.NOR_ID, BitwiseOperator.NOR_SYN, BitwiseOperator.NOR_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
addKeyWord(BitwiseOperator.XNOR_STR, BitwiseOperator.XNOR_DESC, BitwiseOperator.XNOR_ID, BitwiseOperator.XNOR_SYN, BitwiseOperator.XNOR_SINCE, BitwiseOperator.TYPE_ID, keyWordsList);
/*
* Units
*/
Expand Down Expand Up @@ -1656,6 +1661,21 @@ internal static int getNumeralSystemBaseFromBaseInd(String baseInd) {
if (baseInd.Equals("b36")) return 36;
return 0;
}
internal static int findNonNegativeMinimum(int a, int b) {
int posMin = int.MaxValue;
if (a >= 0 && a < posMin) posMin = a;
if (b >= 0 && b < posMin) posMin = b;
if (posMin < int.MaxValue) return posMin;
return int.MinValue;
}
internal static int findNonNegativeMinimum(int a, int b, int c) {
int posMin = int.MaxValue;
if (a >= 0 && a < posMin) posMin = a;
if (b >= 0 && b < posMin) posMin = b;
if (c >= 0 && c < posMin) posMin = c;
if (posMin < int.MaxValue) return posMin;
return int.MinValue;
}
internal static List<Argument> cloneForThreadSafeArgumenstList(Expression relatedExpressionThatInitiatedClone, List<Argument> argumentsListToClone, CloneCache cloneCache) {
List<Argument> argumentListClone = new List<Argument>();
for (int i = 0; i < argumentsListToClone.Count; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)Miscellaneous.cs 5.2.1 2023-02-05
* @(#)Miscellaneous.cs 6.0.0 2024-05-18
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2023-01-29
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -469,6 +469,7 @@ internal enum ToCall {
,BITWISE_COMPL
,MULTIPLY
,DIVIDE
,DIVIDE_QUOTIENT
,MINUS
,PLUS
,NEQ
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)StringModel.cs 6.0.0 2024-05-11
* @(#)StringModel.cs 6.0.0 2024-05-18
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2023-01-29
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -284,6 +284,9 @@ internal static void setLanguageSpecificDescriptionsBitwiseOperator() {
BitwiseOperator.OR_DESC = STRING_RESOURCES.BITWISE_OPERATOR_OR + StringInvariant.SPACE + StringInvariant.OR_SYMBOL + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
BitwiseOperator.LEFT_SHIFT_DESC = STRING_RESOURCES.BITWISE_OPERATOR_LEFT_SHIFT + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
BitwiseOperator.RIGHT_SHIFT_DESC = STRING_RESOURCES.BITWISE_OPERATOR_RIGHT_SHIFT + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
BitwiseOperator.NAND_DESC = STRING_RESOURCES.BITWISE_OPERATOR_NAND + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
BitwiseOperator.NOR_DESC = STRING_RESOURCES.BITWISE_OPERATOR_NOR + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
BitwiseOperator.XNOR_DESC = STRING_RESOURCES.BITWISE_OPERATOR_XNOR + StringInvariant.SEPARATOR + STRING_RESOURCES.BITWISE_OPERATOR;
}
internal static void setLanguageSpecificDescriptionsBooleanOperator() {
BooleanOperator.TYPE_DESC = STRING_RESOURCES.BOOLEAN_OPERATOR;
Expand Down Expand Up @@ -566,6 +569,7 @@ internal static void setLanguageSpecificDescriptionsOperator() {
Operator.MINUS_DESC = STRING_RESOURCES.OPERATOR_MINUS + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.MULTIPLY_DESC = STRING_RESOURCES.OPERATOR_MULTIPLY + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.DIVIDE_DESC = STRING_RESOURCES.OPERATOR_DIVIDE + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.DIVIDE_QUOTIENT_DESC = STRING_RESOURCES.OPERATOR_DIVIDE_QUOTIENT + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.POWER_DESC = STRING_RESOURCES.OPERATOR_POWER + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.FACT_DESC = STRING_RESOURCES.OPERATOR_FACT + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Operator.MOD_DESC = STRING_RESOURCES.OPERATOR_MOD + StringInvariant.SEPARATOR + STRING_RESOURCES.OPERATOR;
Expand Down Expand Up @@ -830,6 +834,9 @@ public static void printDescriptions() {
StringUtils.consolePrintln("BitwiseOperator.OR_DESC = \"" + BitwiseOperator.OR_DESC + "\";");
StringUtils.consolePrintln("BitwiseOperator.LEFT_SHIFT_DESC = \"" + BitwiseOperator.LEFT_SHIFT_DESC + "\";");
StringUtils.consolePrintln("BitwiseOperator.RIGHT_SHIFT_DESC = \"" + BitwiseOperator.RIGHT_SHIFT_DESC + "\";");
StringUtils.consolePrintln("BitwiseOperator.NAND_DESC = \"" + BitwiseOperator.NAND_DESC + "\";");
StringUtils.consolePrintln("BitwiseOperator.NOR_DESC = \"" + BitwiseOperator.NOR_DESC + "\";");
StringUtils.consolePrintln("BitwiseOperator.XNOR_DESC = \"" + BitwiseOperator.XNOR_DESC + "\";");
StringUtils.consolePrintln("// -------------------------------------------------");
StringUtils.consolePrintln("BooleanOperator.TYPE_DESC = \"" + BooleanOperator.TYPE_DESC + "\";");
StringUtils.consolePrintln("BooleanOperator.AND_DESC = \"" + BooleanOperator.AND_DESC + "\";");
Expand Down Expand Up @@ -1102,6 +1109,7 @@ public static void printDescriptions() {
StringUtils.consolePrintln("Operator.MINUS_DESC = \"" + Operator.MINUS_DESC + "\";");
StringUtils.consolePrintln("Operator.MULTIPLY_DESC = \"" + Operator.MULTIPLY_DESC + "\";");
StringUtils.consolePrintln("Operator.DIVIDE_DESC = \"" + Operator.DIVIDE_DESC + "\";");
StringUtils.consolePrintln("Operator.DIVIDE_QUOTIENT_DESC = \"" + Operator.DIVIDE_QUOTIENT_DESC + "\";");
StringUtils.consolePrintln("Operator.POWER_DESC = \"" + Operator.POWER_DESC + "\";");
StringUtils.consolePrintln("Operator.FACT_DESC = \"" + Operator.FACT_DESC + "\";");
StringUtils.consolePrintln("Operator.MOD_DESC = \"" + Operator.MOD_DESC + "\";");
Expand Down
Loading

0 comments on commit fc1af76

Please sign in to comment.