Skip to content

Commit

Permalink
Version 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sominemo committed Mar 20, 2022
1 parent 6731acb commit 9cadf9c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 1.5.0

## Breaking changes

- Now the `'` symbol is allowed for variable and function names so you can have variables like `y'`.

## Equations

- Implemented `>=` and `<=`.

# 1.4.0

## Custom Functions
Expand All @@ -12,7 +22,7 @@
- Detect custom functions im math tree using
`MathExpression.getUsedFreeformFunctions()`.

## Breaking changes
## Breaking Changes

- `MissingFunctionArgumentListException` renamed to
`OutOfRangeFunctionArgumentListException`
Expand Down
76 changes: 74 additions & 2 deletions lib/src/math_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class MathExpression {
/// [MathNode.calc] result. For [MathComparisonEquation], a value of subnodes is
/// being returned but only if they equal, else null will be returned.
/// For [MathComparisonGreater] and [MathComparisonLess] returns a greater or a
/// smaller value accordingly even if the expression isn't true.
/// smaller value accordingly only if the expression is true.
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions,
Expand Down Expand Up @@ -846,7 +846,43 @@ class MathComparisonGreater extends MathComparison {
: super(left, right);
}

/// If Greater Comparison
/// If Greater or Equals Comparison
///
/// Looks for a bigger MathExpression
class MathComparisonGreaterOrEquals extends MathComparison {
@override
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult >= rightResult) return leftResult;
return rightResult;
}

@override
String toString() {
return '[$left >= $right]';
}

/// Creates a greater or equals comparison
const MathComparisonGreaterOrEquals(MathExpression left, MathExpression right)
: super(left, right);
}

/// If Less Comparison
///
/// Looks for a bigger MathExpression
class MathComparisonLess extends MathComparison {
Expand Down Expand Up @@ -882,6 +918,42 @@ class MathComparisonLess extends MathComparison {
: super(left, right);
}

/// If Less or Equals Comparison
///
/// Looks for a bigger MathExpression
class MathComparisonLessOrEquals extends MathComparison {
@override
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult <= rightResult) return leftResult;
return rightResult;
}

@override
String toString() {
return '[$left <= $right]';
}

/// Creates a less comparison
const MathComparisonLessOrEquals(MathExpression left, MathExpression right)
: super(left, right);
}

/// Create a new freeform function
///
/// This class is intended to be extended when developer needs to
Expand Down
15 changes: 10 additions & 5 deletions lib/src/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension MathNodeExpression on MathExpression {
final nodes = <_MathExpressionPart>[];

int start = 0;
for (final match in RegExp('[=<>]').allMatches(expression, 0)) {
for (final match in RegExp('(<=|>=|=|<|>)').allMatches(expression, 0)) {
var r = expression.substring(start, match.start);
if (r.isNotEmpty) nodes.add(_MathExpressionPartString(r));
r = match[0]!;
Expand All @@ -146,7 +146,7 @@ extension MathNodeExpression on MathExpression {
for (var i = 0; i < nodes.length; i++) {
final token = nodes[i];
if (token is! _MathExpressionPartString ||
!RegExp(r'^[=<>]$').hasMatch(token.str)) continue;
!RegExp(r'^(<=|>=|=|<|>)$').hasMatch(token.str)) continue;

if (i == 0 || i == nodes.length - 1) {
throw MissingOperatorOperandException(token.str);
Expand Down Expand Up @@ -196,7 +196,12 @@ extension MathNodeExpression on MathExpression {

late final MathExpression result;

if (token.str == '=') {
if (token.str == '>=') {
result =
MathComparisonGreaterOrEquals(leftParsed.node, rightParsed.node);
} else if (token.str == '<=') {
result = MathComparisonLessOrEquals(leftParsed.node, rightParsed.node);
} else if (token.str == '=') {
result = MathComparisonEquation(leftParsed.node, rightParsed.node);
} else if (token.str == '>') {
result = MathComparisonGreater(leftParsed.node, rightParsed.node);
Expand Down Expand Up @@ -326,7 +331,7 @@ const _bracketFuncs = {
};

final _variableNameBaseRegExp =
'[a-zA-Zα-ωΑ-Ω_]([a-zA-Zα-ωΑ-Ω0-9_.]+(?<=[a-zA-Zα-ωΑ-Ω0-9_]))?';
"[a-zA-Zα-ωΑ-Ω_]([a-zA-Zα-ωΑ-Ω0-9_.']+(?<=[a-zA-Zα-ωΑ-Ω0-9_']))?";

bool _validateVariableName(String name, {bool isFunction = false}) {
return (RegExp('^$_variableNameBaseRegExp\$').hasMatch(name)) &&
Expand Down Expand Up @@ -555,7 +560,7 @@ MathNode _parseMathString(
}

if (doesBuiltInContain) {
if (op == null || op is! _MathNodePartParsedList) {
if (op == null) {
throw OutOfRangeFunctionArgumentListException(cf.toString());
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: math_parser
description: Process math expressions, like formulas or parts of equations, convert them to machine-readable
form, and calculate them.
version: 1.4.0
version: 1.5.0
homepage: https://github.com/Sominemo/Math-Parser-Dart

environment:
Expand Down

0 comments on commit 9cadf9c

Please sign in to comment.