Skip to content

Commit 73e5194

Browse files
authored
Merge pull request #70 from ilopX/add-intepreter-pattern
Add intepreter pattern
2 parents e4d92e4 + 05af9b7 commit 73e5194

File tree

12 files changed

+167
-2
lines changed

12 files changed

+167
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.32.0
2+
- Add conceptual interpreter pattern.
3+
14
## 0.31.0
25
- Add conceptual proxy pattern.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
1212
- [ ] **Behavioral**
1313
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
1414
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
15-
- [ ] Interpreter
15+
- [x] **Interpreter** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/interpreter/conceptual)]
1616
- [ ] **Iterator**
1717
- [x] **Mediator** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual)]
1818
- [x] **Memento** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/conceptual)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Memento Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/memento_editor)]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Interpreter Pattern
2+
In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate
3+
sentences in a language.
4+
5+
## Diagram:
6+
![Interpreter Diagram](https://user-images.githubusercontent.com/8049534/176169636-4c8eb3ba-d5e8-4ecb-81a8-96f1a30f6339.png)
7+
8+
## Client code:
9+
```dart
10+
void main() {
11+
final context = Context();
12+
final variable1 = BoolVariable('var1');
13+
final variable2 = BoolVariable('var2');
14+
final variable3 = BoolVariable('var3');
15+
final variable4 = BoolVariable('var4');
16+
17+
context.assign(variable1, true);
18+
context.assign(variable2, false);
19+
context.assign(variable3, true);
20+
context.assign(variable4, false);
21+
22+
final expression = And(
23+
variable1, // true
24+
Xor(
25+
variable2, // false
26+
Or(
27+
variable3, // true
28+
variable4, // false
29+
),
30+
),
31+
);
32+
33+
print(expression.evaluate(context));
34+
print(expression.toDebugString(context));
35+
}
36+
```
37+
38+
### Output:
39+
```
40+
var4(false) Or var3(true) Xor var2(false) And var1(true)
41+
result: true
42+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'operations/and.dart';
2+
import 'operations/or.dart';
3+
import 'operations/xor.dart';
4+
import 'pattern/context.dart';
5+
import 'variable/bool_variable.dart';
6+
7+
void main() {
8+
final context = Context();
9+
final variable1 = BoolVariable('var1');
10+
final variable2 = BoolVariable('var2');
11+
final variable3 = BoolVariable('var3');
12+
final variable4 = BoolVariable('var4');
13+
14+
context.assign(variable1, true);
15+
context.assign(variable2, false);
16+
context.assign(variable3, true);
17+
context.assign(variable4, false);
18+
19+
final expression = And(
20+
variable1, // true
21+
Xor(
22+
variable2, // false
23+
Or(
24+
variable3, // true
25+
variable4, // false
26+
),
27+
),
28+
);
29+
30+
print(expression.toDebugString(context));
31+
print('result: ${expression.evaluate(context)}');
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'operation.dart';
2+
3+
class And extends Operation {
4+
And(super.expression1, super.expression2);
5+
6+
@override
7+
bool operation(bool a, bool b) => a && b;
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import '../pattern/context.dart';
2+
import '../pattern/expression.dart';
3+
4+
abstract class Operation implements Expression {
5+
final Expression expression1;
6+
final Expression expression2;
7+
8+
Operation(this.expression1, this.expression2);
9+
10+
bool operation(bool a, bool b);
11+
12+
@override
13+
bool evaluate(Context context) {
14+
final a = expression1.evaluate(context);
15+
final b = expression2.evaluate(context);
16+
return operation(a, b);
17+
}
18+
19+
@override
20+
String toDebugString(Context context) {
21+
final a = expression1.toDebugString(context);
22+
final b = expression2.toDebugString(context);
23+
return '$b $runtimeType $a';
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'operation.dart';
2+
3+
class Or extends Operation {
4+
Or(super.expression1, super.expression2);
5+
6+
@override
7+
bool operation(bool a, bool b) => a || b;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'operation.dart';
2+
3+
class Xor extends Operation {
4+
Xor(super.expression1, super.expression2);
5+
6+
@override
7+
bool operation(bool a, bool b) => a ^ b;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import '../variable/bool_variable.dart';
2+
3+
class Context {
4+
void assign(BoolVariable variable, bool value) {
5+
_values.putIfAbsent(variable.name, () => value);
6+
}
7+
8+
bool lookup(String name) {
9+
return _values[name]!;
10+
}
11+
12+
final _values = <String, bool>{};
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'context.dart';
2+
3+
abstract class Expression {
4+
bool evaluate(Context context);
5+
6+
String toDebugString(Context context);
7+
}

0 commit comments

Comments
 (0)