|
64
|
1 {{{#!python
|
|
|
2 class Expr(object):
|
|
|
3 def __init__(self, a, op=None, b=None):
|
|
|
4 self.a, self.op, self.b = a, op, b
|
|
|
5 def __repr__(self):
|
|
|
6 if self.op:
|
|
|
7 if self.b:
|
|
|
8 return '(%r %s %r)' % (self.a, self.op, self.b)
|
|
|
9 else:
|
|
|
10 return '(%s%r)' % (self.op, self.a)
|
|
|
11 return self.a
|
|
|
12 def __add__(self, other):
|
|
|
13 return Expr(self, '+', other)
|
|
|
14 def __sub__(self, other):
|
|
|
15 return Expr(self, '-', other)
|
|
|
16 def __mul__(self, other):
|
|
|
17 return Expr(self, '*', other)
|
|
|
18 def __div__(self, other):
|
|
|
19 return Expr(self, '/', other)
|
|
|
20 def __neg__(self, other):
|
|
|
21 return Expr(self, '-')
|
|
|
22 def __pos__(self, other):
|
|
|
23 return Expr(self, '+')
|
|
|
24 }}} |