-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation.py
More file actions
27 lines (21 loc) · 910 Bytes
/
operation.py
File metadata and controls
27 lines (21 loc) · 910 Bytes
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
UNARY = 1
BINARY = 2
LEFT = 1
RIGHT = 2
class Operation:
def __init__(self, name, arity, associativity, priority, method):
self.name = name
self.arity = arity
self.associativity = associativity
self.priority = priority
self.method = method
def __repr__(self):
return ("UNARY" if self.arity == UNARY else "BINARY") + self.name
opDict = dict()
opDict['+', UNARY] = Operation('+', UNARY, RIGHT, 3, lambda a: +a)
opDict['-', UNARY] = Operation('-', UNARY, RIGHT, 3, lambda a: -a)
opDict['+', BINARY] = Operation('+', BINARY, LEFT, 1, lambda a, b: a + b)
opDict['-', BINARY] = Operation('-', BINARY, LEFT, 1, lambda a, b: a - b)
opDict['*', BINARY] = Operation('*', BINARY, LEFT, 2, lambda a, b: a * b)
opDict['/', BINARY] = Operation('/', BINARY, LEFT, 2, lambda a, b: a / b)
opDict['^', BINARY] = Operation('^', BINARY, RIGHT, 3, lambda a, b: a ** b)