-
Notifications
You must be signed in to change notification settings - Fork 1
/
datatypes.py
109 lines (73 loc) · 2.33 KB
/
datatypes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import command
from collections import deque
from dataclasses import dataclass
class ASQ:
pass
class ValueCopy:
def copy(self):
return type(self)(self.value)
@dataclass(frozen=True)
class Statement(ASQ):
pass
@dataclass(frozen=True)
class Boolean(Statement, ValueCopy):
value: bool
def execute(self, env):
env.qframe.append(self)
def __str__(self):
return str(self.value)
@dataclass(frozen=True)
class Number(Statement, ValueCopy):
value: int
def execute(self, env):
env.qframe.append(self)
def __str__(self):
return str(self.value)
@dataclass(frozen=True)
class String(Statement, ValueCopy):
value: str
def execute(self, env):
env.qframe.append(self)
def __str__(self):
return self.value
class Block(ASQ):
def __init__(self, statements):
self.statements = deque(statements)
def execute(self, env):
env.qframe.append(Queue(self.statements))
def __eq__(self, other):
return type(self) == type(other) and self.statements == other.statements
def __repr__(self):
return f"{type(self).__name__}(statements={repr(self.statements)})"
def __str__(self):
return repr(self)
def copy(self):
return Block([s.copy() for s in self.statements])
class Queue(ASQ):
def __init__(self, statements):
self.statements = deque(statements)
def execute(self, env):
while self.statements:
inst = self.statements.popleft()
term = inst.execute(env)
if term != command.NO_TERMINATE:
return term
def execute_loop(self, env):
while self.statements:
inst = self.statements.popleft()
self.statements.append(inst.copy())
term = inst.execute(env)
if term != command.NO_TERMINATE:
return term
def pop(self):
return self.statements.popleft()
def push(self, value):
self.statements.append(value)
def __eq__(self, other):
return type(self) == type(other) and self.statements == other.statements
def __repr__(self):
return f"{type(self).__name__}(statements={repr(self.statements)})"
def __str__(self):
return repr(self)
def copy(self):
return Queue([s.copy() for s in self.statements])