-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
executable file
·63 lines (49 loc) · 1.78 KB
/
memory.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
#!/usr/bin/env python
class memory:
def __init__(self):
self.storage = {}
self.opMatrix = {'S<': 'append',
'S>': 'pop',
'L<': '__setitem__',
'L>': '__getitem__'}
def operation(self, action, elem = None):
for operations in action.split(","):
ptr, act = operations[:2], operations[2:]
print "Operation ", operations
print "Ptr: ", ptr, " Act:", act
if ptr != "S'":
try:
m = self.storage[ptr]
except KeyError:
self.storage[ptr] = []
m = self.storage[ptr]
operation = m.__getattribute__( self.opMatrix[ ptr[:1] + act[:1] ] )
print "operation: ", operation
if act[1:2] == 'v':
print "v", act[2:]
value = act[2:]
if act[1] == 'r':
cnt = len(elem['link'])
value = act[2:]
print "r", act[2:], cnt
for x in range(cnt):
operation(act[2:])
if act[:1] == '<':
if value != None:
operation(value)
else:
operation()
elif act[:1] == '>':
if value != None:
if value != operation():
raise RuntimeError('Not balanced brace')
else:
operation()
def debug(self):
print "Memory storage:"
for s in self.storage:
print "-----"
print s
print "-----"
for s1 in self.storage[s]:
print s1