-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
65 lines (40 loc) · 1.66 KB
/
core.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
55
56
57
58
59
60
61
62
63
64
65
from Aspyct import Aspect
from operator import add, div, mul, sub #, pow
operations = {"(": {"priority": 0, "f": None},
")": {"priority": 1, "f": None},
"+": {"priority": 2, "f": add},
"-": {"priority": 2, "f": sub},
"*": {"priority": 3, "f": mul},
"/": {"priority": 3, "f": div}}
# ,"**": {"priority": 4, "f": None} 4}
class AbstractTreeAddContract (Aspect):
def atCall (self, cd):
self.old_length = len (cd.self.tree)
def atReturn (self, cd):
assert len (cd.self.tree) == self.old_length + 1, "Not working method 'add' in 'AbstractTree'"
class AbstractTreePopContract (Aspect):
def atCall (self, cd):
self.old_length = len (cd.self.tree)
def atReturn (self, cd):
assert len (cd.self.tree) == self.old_length - 1, "Not working method 'pop' in 'AbstractTree'"
class AbstractTree (object):
def __init__ (self):
self.tree = []
@AbstractTreeAddContract()
def add (self, value): self.tree.append (value)
@AbstractTreePopContract()
def pop (self): return self.tree.pop ()
def __len__ (self): return len (self.tree)
class Number (object):
def __init__ (self, num):
self.num = float (num)
value = property (lambda self: self.num)
__repr__ = lambda self: str (self.value)
class Operation (object):
def __init__ (self, op):
self.op = op
self.priority = operations[op]["priority"]
self.f = operations[op]["f"]
value = property (lambda self: self.op)
__repr__ = lambda self: str (self.value)
eval = lambda self, *args: self.f (*[x.value for x in args])