forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter_concept.py
83 lines (59 loc) · 2.16 KB
/
interpreter_concept.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# pylint: disable=too-few-public-methods
# pylint: disable=arguments-differ
"The Interpreter Pattern Concept"
class AbstractExpression():
"All Terminal and Non-Terminal expressions will implement an `interpret` method"
@staticmethod
def interpret():
"""
The `interpret` method gets called recursively for each
AbstractExpression
"""
class Number(AbstractExpression):
"Terminal Expression"
def __init__(self, value):
self.value = int(value)
def interpret(self):
return self.value
def __repr__(self):
return str(self.value)
class Add(AbstractExpression):
"Non-Terminal Expression."
def __init__(self, left, right):
self.left = left
self.right = right
def interpret(self):
return self.left.interpret() + self.right.interpret()
def __repr__(self):
return f"({self.left} Add {self.right})"
class Subtract(AbstractExpression):
"Non-Terminal Expression"
def __init__(self, left, right):
self.left = left
self.right = right
def interpret(self):
return self.left.interpret() - self.right.interpret()
def __repr__(self):
return f"({self.left} Subtract {self.right})"
# The Client
# The sentence complies with a simple grammar of
# Number -> Operator -> Number -> etc,
SENTENCE = "5 + 4 - 3 + 7 - 2"
print(SENTENCE)
# Split the sentence into individual expressions that will be added to
# an Abstract Syntax Tree (AST) as Terminal and Non-Terminal expressions
TOKENS = SENTENCE.split(" ")
print(TOKENS)
# Manually Creating an Abstract Syntax Tree from the tokens
AST: list[AbstractExpression] = [] # Python 3.9
# AST = [] # Python 3.8 or earlier
AST.append(Add(Number(TOKENS[0]), Number(TOKENS[2]))) # 5 + 4
AST.append(Subtract(AST[0], Number(TOKENS[4]))) # ^ - 3
AST.append(Add(AST[1], Number(TOKENS[6]))) # ^ + 7
AST.append(Subtract(AST[2], Number(TOKENS[8]))) # ^ - 2
# Use the final AST row as the root node.
AST_ROOT = AST.pop()
# Interpret recursively through the full AST starting from the root.
print(AST_ROOT.interpret())
# Print out a representation of the AST_ROOT
print(AST_ROOT)