-
Notifications
You must be signed in to change notification settings - Fork 41
Boolean operators #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khoatran
wants to merge
10
commits into
mossadal:master
Choose a base branch
from
khoatran:boolean-operators
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a9b1c3
Add =, &&, || boolean operators
khoatran f81c3e5
Add ! operator for boolean
khoatran f2f4b5e
Update more case
khoatran a9c0b73
Update more case for ! operator
khoatran b1764f6
Add greater operator
khoatran dca693e
Fix the bug of > operator with variable and expression
khoatran 87c1686
Fix issue of expression with variables
khoatran 93b6876
Add GreaterOrEqual operator
khoatran 7e68326
Add Smaller operator
khoatran a4f6b80
Add Smaller or equal operator
khoatran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/MathParser/Parsing/Nodes/Factories/BoolAndNodeFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
| /* | ||
| * @package Parsing | ||
| * @author Frank Wikström <frank@mossadal.se> | ||
| * @copyright 2015 Frank Wikström | ||
| * @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
| * | ||
| */ | ||
|
|
||
| namespace MathParser\Parsing\Nodes\Factories; | ||
|
|
||
| use MathParser\Parsing\Nodes\Interfaces\ExpressionNodeFactory; | ||
|
|
||
| use MathParser\Parsing\Nodes\Node; | ||
| use MathParser\Parsing\Nodes\NumberNode; | ||
| use MathParser\Parsing\Nodes\IntegerNode; | ||
| use MathParser\Parsing\Nodes\RationalNode; | ||
|
|
||
| use MathParser\Parsing\Nodes\ExpressionNode; | ||
| use MathParser\Parsing\Nodes\Traits\Sanitize; | ||
| use MathParser\Parsing\Nodes\Traits\Numeric; | ||
|
|
||
| /** | ||
| * Factory for creating an ExpressionNode representing '&&'. | ||
| * | ||
| * Some basic simplification is applied to the resulting Node. | ||
| * | ||
| */ | ||
| class BoolAndNodeFactory implements ExpressionNodeFactory | ||
| { | ||
| use Sanitize; | ||
| use Numeric; | ||
|
|
||
| public function makeNode($leftOperand, $rightOperand) | ||
| { | ||
| $leftOperand = $this->sanitize($leftOperand); | ||
| $rightOperand = $this->sanitize($rightOperand); | ||
|
|
||
| $node = $this->numericTerms($leftOperand, $rightOperand); | ||
| if ($node) return $node; | ||
|
|
||
| return new ExpressionNode($leftOperand, '&&', $rightOperand); | ||
| } | ||
|
|
||
| protected function numericTerms($leftOperand, $rightOperand) | ||
| { | ||
|
|
||
| if (!$this->isNumeric($leftOperand) || !$this->isNumeric($rightOperand)) { | ||
| return null; | ||
| } | ||
| $type = $this->resultingType($leftOperand, $rightOperand); | ||
| switch($type) { | ||
| case Node::NumericFloat: | ||
| $result = ( intval(ceil($leftOperand->getValue())) && intval(ceil($rightOperand->getValue()))); | ||
| return new NumberNode($result); | ||
|
|
||
| case Node::NumericRational: | ||
| $leftValue = ($leftOperand->getNumerator() / $leftOperand->getDenominator()); | ||
| $leftValue = intval(ceil($leftValue)); | ||
| $rightValue = ($rightOperand->getDenominator() / $rightOperand->getNumerator()); | ||
| $rightValue = intval(ceil($rightValue)); | ||
| $result = ($leftValue && $rightValue); | ||
| return new IntegerNode($result); | ||
|
|
||
| case Node::NumericInteger: | ||
| $result = ($leftOperand->getValue() && $rightOperand->getValue()); | ||
| return new IntegerNode($result); | ||
| } | ||
|
|
||
|
|
||
| return null; | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/MathParser/Parsing/Nodes/Factories/BoolEqualNodeFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
| /* | ||
| * @package Parsing | ||
| * @author Frank Wikström <frank@mossadal.se> | ||
| * @copyright 2015 Frank Wikström | ||
| * @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
| * | ||
| */ | ||
|
|
||
| namespace MathParser\Parsing\Nodes\Factories; | ||
|
|
||
| use MathParser\Parsing\Nodes\Interfaces\ExpressionNodeFactory; | ||
|
|
||
| use MathParser\Parsing\Nodes\Node; | ||
| use MathParser\Parsing\Nodes\NumberNode; | ||
| use MathParser\Parsing\Nodes\IntegerNode; | ||
| use MathParser\Parsing\Nodes\RationalNode; | ||
|
|
||
| use MathParser\Parsing\Nodes\ExpressionNode; | ||
| use MathParser\Parsing\Nodes\Traits\Sanitize; | ||
| use MathParser\Parsing\Nodes\Traits\Numeric; | ||
|
|
||
| /** | ||
| * Factory for creating an ExpressionNode representing '='. | ||
| * | ||
| * Some basic simplification is applied to the resulting Node. | ||
| * | ||
| */ | ||
| class BoolEqualNodeFactory implements ExpressionNodeFactory | ||
| { | ||
| use Sanitize; | ||
| use Numeric; | ||
|
|
||
| public function makeNode($leftOperand, $rightOperand) | ||
| { | ||
| $leftOperand = $this->sanitize($leftOperand); | ||
| $rightOperand = $this->sanitize($rightOperand); | ||
|
|
||
| $node = $this->numericTerms($leftOperand, $rightOperand); | ||
| if ($node) return $node; | ||
|
|
||
| return new ExpressionNode($leftOperand, '=', $rightOperand); | ||
| } | ||
|
|
||
| protected function numericTerms($leftOperand, $rightOperand) | ||
| { | ||
| if (!$this->isNumeric($leftOperand) || !$this->isNumeric($rightOperand)) { | ||
| return null; | ||
| } | ||
| $type = $this->resultingType($leftOperand, $rightOperand); | ||
|
|
||
| switch($type) { | ||
| case Node::NumericFloat: | ||
| $result = ($leftOperand->getValue() === $rightOperand->getValue()); | ||
| return new NumberNode($result); | ||
|
|
||
| case Node::NumericRational: | ||
| $leftValue = ($leftOperand->getNumerator() / $leftOperand->getDenominator()); | ||
| $rightValue = ($rightOperand->getDenominator() / $rightOperand->getNumerator()); | ||
| $result = ($leftValue === $rightValue); | ||
| return new IntegerNode($result); | ||
|
|
||
| case Node::NumericInteger: | ||
| $result = ($leftOperand->getValue() === $rightOperand->getValue()); | ||
| return new IntegerNode($result); | ||
| } | ||
|
|
||
|
|
||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO,
&&should have higher precedence than||and||higher than=. Then=,>,<and other have same precedence.So,
A = B && CmeansA = (B && C)