-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpbench_classes.py
54 lines (47 loc) · 1.75 KB
/
fpbench_classes.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
class expr:
def __repr__(self):
return type(self).__name__ + '(' + repr(self.val) + ')'
def __str__(self):
return str(self.val)
def infix(self):
return str(self.val)
class var(expr):
def __init__(self, val):
self.val = val
class constant(expr):
def __init__(self, val):
self.val = val
class number(expr):
def __init__(self, val):
self.val = val
class op(expr):
def __init__(self, op, args):
self.op = op
self.args = args
def __repr__(self):
return '{}({},{})'.format(type(self).__name__,self.op, ",".join(list(map(repr,self.args))))
def __str__(self):
return '({} {})'.format(self.op, " ".join(list(map(str, self.args))))
def infix(self):
if len(self.args) == 1:
return "{}({})".format(self.op, self.args[0].infix())
elif len(self.args) == 2:
return "({}{}{})".format(self.args[0].infix(),
self.op,
self.args[1].infix())
else:
return "{}({})".format(self.op, ','.join(list(map(lambda x: x.infix(),
self.args))))
class let(expr):
def __init__(self, bindings, expr):
self.bindings = bindings
self.expr = expr
def __str__(self):
# Make this better
return '(let {} in {})'.format(str(self.bindings), str(self.expr))
def __repr__(self):
# Make this better
return str(self)
def infix(self):
return '{}{}'.format("".join(list(map(lambda x: "{}={};\n".format(x[0],x[1].infix()), self.bindings))),
self.expr.infix())