/
Ecomp.py
36 lines (30 loc) · 868 Bytes
/
Ecomp.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
from copy import copy
from ast import Compare, Num
from Visit import OpVisitor
class Ecomp:
def __init__(self, left, ops, rs, env, k):
"""
:param left: The evaluated left side of the comparison
:type left: Val
:type ops: List
:type rs: List
:type env: Scope
:type k: Continuation
"""
self.left = left
self.ops = ops
self.rs = rs
self.env = env
self.k = k
def apply(self, val, scope):
ops = copy(self.ops)
op = ops.pop()
visitor = OpVisitor()
operation = visitor.visit(op)
left = self.left
res = operation(left.n, val.n)
if len(ops) == 0:
expr = Num(n=res)
else:
expr = Compare(left=Num(n=res), ops=ops, comparators=self.rs)
return expr, self.env, self.k