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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# -*- coding: utf-8 -*-
"""
pint.pint_eval
~~~~~~~~~~~~~~
An expression evaluator to be used as a safe replacement for builtin eval.
:copyright: 2016 by Pint Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import operator
import token as tokenlib
from .errors import DefinitionSyntaxError
# For controlling order of operations
_OP_PRIORITY = {
"**": 3,
"^": 3,
"unary": 2,
"*": 1,
"": 1, # operator for implicit ops
"/": 1,
"+": 0,
"-": 0,
}
_BINARY_OPERATOR_MAP = {
"**": operator.pow,
"*": operator.mul,
"": operator.mul, # operator for implicit ops
"/": operator.truediv,
"+": operator.add,
"-": operator.sub,
}
_UNARY_OPERATOR_MAP = {"+": lambda x: x, "-": lambda x: x * -1}
class EvalTreeNode:
def __init__(self, left, operator=None, right=None):
"""
left + operator + right --> binary op
left + operator --> unary op
left + right --> implicit op
left --> single value
"""
self.left = left
self.operator = operator
self.right = right
def to_string(self):
# For debugging purposes
if self.right:
comps = [self.left.to_string()]
if self.operator:
comps.append(self.operator[1])
comps.append(self.right.to_string())
elif self.operator:
comps = [self.operator[1], self.left.to_string()]
else:
return self.left[1]
return "(%s)" % " ".join(comps)
def evaluate(
self, define_op, bin_op=_BINARY_OPERATOR_MAP, un_op=_UNARY_OPERATOR_MAP
):
"""
define_op is a callable that translates tokens into objects
bin_op and un_op provide functions for performing binary and unary operations
"""
if self.right:
# binary or implicit operator
op_text = self.operator[1] if self.operator else ""
if op_text not in bin_op:
raise DefinitionSyntaxError('missing binary operator "%s"' % op_text)
left = self.left.evaluate(define_op, bin_op, un_op)
return bin_op[op_text](left, self.right.evaluate(define_op, bin_op, un_op))
elif self.operator:
# unary operator
op_text = self.operator[1]
if op_text not in un_op:
raise DefinitionSyntaxError('missing unary operator "%s"' % op_text)
return un_op[op_text](self.left.evaluate(define_op, bin_op, un_op))
else:
# single value
return define_op(self.left)
def build_eval_tree(tokens, op_priority=_OP_PRIORITY, index=0, depth=0, prev_op=None):
"""
Params:
Index, depth, and prev_op used recursively, so don't touch.
Tokens is an iterable of tokens from an expression to be evaluated.
Transform the tokens from an expression into a recursive parse tree, following order
of operations. Operations can include binary ops (3 + 4), implicit ops (3 kg), or
unary ops (-1).
General Strategy:
1) Get left side of operator
2) If no tokens left, return final result
3) Get operator
4) Use recursion to create tree starting at token on right side of operator (start at step #1)
4.1) If recursive call encounters an operator with lower or equal priority to step #2, exit recursion
5) Combine left side, operator, and right side into a new left side
6) Go back to step #2
"""
if depth == 0 and prev_op is None:
# ensure tokens is list so we can access by index
tokens = list(tokens)
result = None
while True:
current_token = tokens[index]
token_type = current_token[0]
token_text = current_token[1]
if token_type == tokenlib.OP:
if token_text == ")":
if prev_op is None:
raise DefinitionSyntaxError(
"unopened parentheses in tokens: %s" % current_token
)
elif prev_op == "(":
# close parenthetical group
return result, index
else:
# parenthetical group ending, but we need to close sub-operations within group
return result, index - 1
elif token_text == "(":
# gather parenthetical group
right, index = build_eval_tree(
tokens, op_priority, index + 1, 0, token_text
)
if not tokens[index][1] == ")":
raise DefinitionSyntaxError("weird exit from parentheses")
if result:
# implicit op with a parenthetical group, i.e. "3 (kg ** 2)"
result = EvalTreeNode(left=result, right=right)
else:
# get first token
result = right
elif token_text in op_priority:
if result:
# equal-priority operators are grouped in a left-to-right order,
# unless they're exponentiation, in which case they're grouped
# right-to-left this allows us to get the expected behavior for
# multiple exponents
# (2^3^4) --> (2^(3^4))
# (2 * 3 / 4) --> ((2 * 3) / 4)
if op_priority[token_text] <= op_priority.get(
prev_op, -1
) and token_text not in ["**", "^"]:
# previous operator is higher priority, so end previous binary op
return result, index - 1
# get right side of binary op
right, index = build_eval_tree(
tokens, op_priority, index + 1, depth + 1, token_text
)
result = EvalTreeNode(
left=result, operator=current_token, right=right
)
else:
# unary operator
right, index = build_eval_tree(
tokens, op_priority, index + 1, depth + 1, "unary"
)
result = EvalTreeNode(left=right, operator=current_token)
elif token_type == tokenlib.NUMBER or token_type == tokenlib.NAME:
if result:
# tokens with an implicit operation i.e. "1 kg"
if op_priority[""] <= op_priority.get(prev_op, -1):
# previous operator is higher priority than implicit, so end
# previous binary op
return result, index - 1
right, index = build_eval_tree(
tokens, op_priority, index, depth + 1, ""
)
result = EvalTreeNode(left=result, right=right)
else:
# get first token
result = EvalTreeNode(left=current_token)
if tokens[index][0] == tokenlib.ENDMARKER:
if prev_op == "(":
raise DefinitionSyntaxError("unclosed parentheses in tokens")
if depth > 0 or prev_op:
# have to close recursion
return result, index
else:
# recursion all closed, so just return the final result
return result
if index + 1 >= len(tokens):
# should hit ENDMARKER before this ever happens
raise DefinitionSyntaxError("unexpected end to tokens")
index += 1
|