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 = $first; + $this->second = $second; + } + + public function interpret(Context $context): bool + { + return (bool) $this->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..db3ba1b --- /dev/null +++ b/Behavioral/Interpreter/OrExp.php @@ -0,0 +1,20 @@ +first = $first; + $this->second = $second; + } + + public function interpret(Context $context): bool + { + return $this->first->interpret($context) || $this->second->interpret($context); + } +} 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..f7fc871 --- /dev/null +++ b/Behavioral/Interpreter/VariableExp.php @@ -0,0 +1,23 @@ +name = $name; + } + + public function interpret(Context $context): bool + { + return $context->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..09d372b Binary files /dev/null and b/Behavioral/Interpreter/uml/uml.png differ