diff --git a/Behavioral/Interpreter/AbstractExp.php b/Behavioral/Interpreter/AbstractExp.php new file mode 100644 index 0000000..5e34736 --- /dev/null +++ b/Behavioral/Interpreter/AbstractExp.php @@ -0,0 +1,8 @@ +first->interpret($context) && $this->second->interpret($context); + } +} diff --git a/Behavioral/Interpreter/Context.php b/Behavioral/Interpreter/Context.php new file mode 100644 index 0000000..2a0b2c7 --- /dev/null +++ b/Behavioral/Interpreter/Context.php @@ -0,0 +1,24 @@ +poolVariable)) { + throw new Exception("no exist variable: $name"); + } + + return $this->poolVariable[$name]; + } + + public function assign(VariableExp $variable, bool $val) + { + $this->poolVariable[$variable->getName()] = $val; + } +} diff --git a/Behavioral/Interpreter/OrExp.php b/Behavioral/Interpreter/OrExp.php new file mode 100644 index 0000000..7ff749e --- /dev/null +++ b/Behavioral/Interpreter/OrExp.php @@ -0,0 +1,18 @@ +first->interpret($context) || $this->second->interpret($context); + } +} diff --git a/Behavioral/Interpreter/README.rst b/Behavioral/Interpreter/README.rst new file mode 100644 index 0000000..cdecafc --- /dev/null +++ b/Behavioral/Interpreter/README.rst @@ -0,0 +1,71 @@ +`Interpreter`__ +============ + +Purpose +------- + +For a given language, it defines the representation of its grammar as +"No Terminal Expression" and "Terminal Expression", +as well as an interpreter for the sentences of that language. + +Examples +-------- + +- An example of a binary logic interpreter, each definition is defined by its own class + +UML Diagram +----------- + +.. image:: uml/uml.png + :alt: Alt Interpreter UML Diagram + :align: center + +Code +---- + +You can also find this code on `GitHub`_ + +AbstractExp.php + +.. literalinclude:: AbstractExp.php + :language: php + :linenos: + +Context.php + +.. literalinclude:: Context.php + :language: php + :linenos: + + +VariableExp.php + +.. literalinclude:: VariableExp.php + :language: php + :linenos: + + +AndExp.php + +.. literalinclude:: AndExp.php + :language: php + :linenos: + + +OrExp.php + +.. literalinclude:: OrExp.php + :language: php + :linenos: + +Test +---- + +Tests/InterpreterTest.php + +.. literalinclude:: Tests/InterpreterTest.php + :language: php + :linenos: + +.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/main/Behavioral/Interpreter +.. __: https://en.wikipedia.org/wiki/Interpreter_pattern diff --git a/Behavioral/Interpreter/Tests/InterpreterTest.php b/Behavioral/Interpreter/Tests/InterpreterTest.php new file mode 100644 index 0000000..3b1c07a --- /dev/null +++ b/Behavioral/Interpreter/Tests/InterpreterTest.php @@ -0,0 +1,63 @@ +context = new Context(); + $this->a = new VariableExp('A'); + $this->b = new VariableExp('B'); + $this->c = new VariableExp('C'); + } + + public function testOr() + { + $this->context->assign($this->a, false); + $this->context->assign($this->b, false); + $this->context->assign($this->c, true); + + // A ∨ B + $exp1 = new OrExp($this->a, $this->b); + $result1 = $exp1->interpret($this->context); + + $this->assertFalse($result1, 'A ∨ B must false'); + + // $exp1 ∨ C + $exp2 = new OrExp($exp1, $this->c); + $result2 = $exp2->interpret($this->context); + + $this->assertTrue($result2, '(A ∨ B) ∨ C must true'); + } + + public function testAnd() + { + $this->context->assign($this->a, true); + $this->context->assign($this->b, true); + $this->context->assign($this->c, false); + + // A ∧ B + $exp1 = new AndExp($this->a, $this->b); + $result1 = $exp1->interpret($this->context); + + $this->assertTrue($result1, 'A ∧ B must true'); + + // $exp1 ∧ C + $exp2 = new AndExp($exp1, $this->c); + $result2 = $exp2->interpret($this->context); + + $this->assertFalse($result2, '(A ∧ B) ∧ C must false'); + } +} diff --git a/Behavioral/Interpreter/VariableExp.php b/Behavioral/Interpreter/VariableExp.php new file mode 100644 index 0000000..a0f834f --- /dev/null +++ b/Behavioral/Interpreter/VariableExp.php @@ -0,0 +1,23 @@ +lookUp($this->name); + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/Behavioral/Interpreter/uml/uml.png b/Behavioral/Interpreter/uml/uml.png new file mode 100644 index 0000000..3d604c6 Binary files /dev/null and b/Behavioral/Interpreter/uml/uml.png differ diff --git a/Behavioral/README.md b/Behavioral/README.md index b98202b..95801e5 100644 --- a/Behavioral/README.md +++ b/Behavioral/README.md @@ -7,6 +7,7 @@ communication. * [ChainOfResponsibilities](ChainOfResponsibilities) [:notebook:](http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern) * [Command](Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern) +* [Interpreter](Interpreter) [:notebook:](https://en.wikipedia.org/wiki/Interpreter_pattern) * [Iterator](Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern) * [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern) * [Memento](Memento) [:notebook:](http://en.wikipedia.org/wiki/Memento_pattern) diff --git a/Behavioral/README.rst b/Behavioral/README.rst index 5577f4c..7bde771 100644 --- a/Behavioral/README.rst +++ b/Behavioral/README.rst @@ -11,6 +11,7 @@ carrying out this communication. ChainOfResponsibilities/README Command/README + Interpreter/README Iterator/README Mediator/README Memento/README @@ -22,4 +23,4 @@ carrying out this communication. TemplateMethod/README Visitor/README -.. __: http://en.wikipedia.org/wiki/Behavioral_pattern \ No newline at end of file +.. __: http://en.wikipedia.org/wiki/Behavioral_pattern diff --git a/README.md b/README.md index f780724..6227ee5 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ The patterns can be structured in roughly three different categories. Please cli * [ChainOfResponsibilities](Behavioral/ChainOfResponsibilities) [:notebook:](http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern) * [Command](Behavioral/Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern) +* [Interpreter](Behavioral/Interpreter) [:notebook:](https://en.wikipedia.org/wiki/Interpreter_pattern) * [Iterator](Behavioral/Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern) * [Mediator](Behavioral/Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern) * [Memento](Behavioral/Memento) [:notebook:](http://en.wikipedia.org/wiki/Memento_pattern) @@ -113,4 +114,4 @@ The patterns can be structured in roughly three different categories. Please cli | es_MX | Spanish-Mexican | [Docs :notebook:](https://designpatternsphp.readthedocs.io/es_MX/latest/README.html) | | tr | Turkish | [Docs :notebook:](https://designpatternsphp.readthedocs.io/tr/latest/README.html) | | bg | Bulgarian | [Docs :notebook:](https://designpatternsphp.readthedocs.io/bg/latest/README.html) | -| fr | French | [Docs :notebook:](https://designpatternsphp.readthedocs.io/fr/latest/README.html) | \ No newline at end of file +| fr | French | [Docs :notebook:](https://designpatternsphp.readthedocs.io/fr/latest/README.html) |