-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyeval_operator.py
59 lines (49 loc) · 1.57 KB
/
pyeval_operator.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
"""
operator.py - implements an operator class for use by the evaluator
AUTHOR: Jon Fincher
"""
# Dictionary lookup for precedence
# Currently four levels of precedence
PRECEDENCE = {
'+' : 2, # Addition
'-' : 2, # Subtraction
'*' : 3, # Multiplication
'/' : 3, # Division
'%' : 3, # Modulo
'^' : 4 # Power
}
# Dictionary lookup for number of operands to use
OPERAND_COUNT = {
'+' : 2, # Addition
'-' : 2, # Subtraction
'*' : 2, # Multiplication
'/' : 2, # Division
'%' : 2, # Modulo
'^' : 2 # Power
}
class Operator:
"""
Common operator class used by the evaluator.
"""
_op_string = "" # String to hold the operator
_op_prec = 0 # Integer to hold the precedence
_op_cnt = 0 # Integer to hold the number of operands to use
def __init__(self, operator_string):
""" Create a new operator object. """
self._op_string = operator_string
self._op_prec = PRECEDENCE[self._op_string]
self._op_cnt = OPERAND_COUNT[self._op_string]
@property
def op_string(self):
return self._op_string
@op_string.setter
def op_string(self, new_op_string):
self._op_string = new_op_string
self._op_prec = PRECEDENCE[self._op_string]
self._op_cnt = OPERAND_COUNT[self._op_string]
@property
def count(self):
return self._op_cnt
@property
def precedence(self):
return self._op_prec