-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.py
More file actions
40 lines (33 loc) · 1.24 KB
/
expression.py
File metadata and controls
40 lines (33 loc) · 1.24 KB
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
import operation
def process(s, d):
opList = []
stList = []
def processLastOp():
op = opList.pop()
if op.arity == operation.UNARY:
stList.append(op.method(stList.pop()))
elif op.arity == operation.BINARY:
b, a = stList.pop(), stList.pop()
stList.append(op.method(a, b))
curOpArity = operation.UNARY
for t in s:
if t == '(':
opList.append('(')
curOpArity = operation.UNARY
elif t == ')':
while opList[-1] != '(': processLastOp()
opList.pop()
curOpArity = operation.BINARY
elif (t, curOpArity) in operation.opDict:
curOp = operation.opDict[t, curOpArity]
while len(opList) > 0 and opList[-1] != '(' and (
opList[-1].associativity == operation.LEFT and opList[-1].priority >= curOp.priority
or opList[-1].associativity == operation.RIGHT and opList[-1].priority > curOp.priority):
processLastOp()
opList.append(curOp)
curOpArity = operation.UNARY
else:
stList.append(d[t])
curOpArity = operation.BINARY
while len(opList) > 0: processLastOp()
return stList[-1]