Skip to content

Commit 945baa3

Browse files
committed
Add pattern Interpreter/RealWorld
1 parent d72f780 commit 945baa3

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
boolean expression A ∧ (B ∨ C) = true
1+
boolean expression A ∧ (B ∨ C) = true, with variables A=true, B=true, C=false
2+
boolean expression B ∨ (A ∧ (B ∨ C)) = false, with variables A=false, B=false, C=true

src/RefactoringGuru/Interpreter/RealWorld/index.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function interpret(Context $context): bool
101101
$b = new VariableExp('B');
102102
$c = new VariableExp('C');
103103

104+
// example 1:
104105
// A ∧ (B ∨ C)
105106
$exp = new AndExp(
106107
$a,
@@ -113,4 +114,23 @@ public function interpret(Context $context): bool
113114

114115
$result = $exp->interpret($context) ? 'true' : 'false';
115116

116-
echo 'boolean expression A ∧ (B ∨ C) = ' . $result;
117+
echo 'boolean expression A ∧ (B ∨ C) = ' . $result . ', with variables A=true, B=true, C=false' . PHP_EOL;
118+
119+
120+
// example 2:
121+
// B ∨ (A ∧ (B ∨ C))
122+
$exp = new OrExp(
123+
$b,
124+
new AndExp(
125+
$a,
126+
new OrExp($b, $c)
127+
)
128+
);
129+
130+
$context->assign($a, false);
131+
$context->assign($b, false);
132+
$context->assign($c, true);
133+
134+
$result2 = $exp->interpret($context) ? 'true' : 'false';
135+
136+
echo 'boolean expression B ∨ (A ∧ (B ∨ C)) = ' . $result2 . ', with variables A=false, B=false, C=true';

0 commit comments

Comments
 (0)