-
Notifications
You must be signed in to change notification settings - Fork 3
/
state.py
348 lines (285 loc) · 12.1 KB
/
state.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import sys
from dataclasses import dataclass
from typing import List, Tuple, Dict, Set, Optional, Any, Generator
from enum import Enum, auto
from functools import reduce
from parsing.op import Op
class InternalAssertionError(Exception):
"""Error used in case there is an assertion needed in cont"""
def cont_assert(condition: bool, message: str):
"""Raise InternalAssertionError with the provided `message` if the `condition` is true"""
if not condition:
raise InternalAssertionError(message)
class BlockType(Enum):
"""The type of a `Block`"""
IF = auto()
ELSE = auto()
WHILE = auto()
FOR = auto()
PROC = auto()
BIND = auto()
@dataclass
class Block:
"""A block in code, which should be used for all the blocks in the `BlockType` enum."""
type: BlockType
start: int
end: int = -1
stack_effect: Optional[Tuple[int, int]] = None
binded: int = 0
@dataclass
class Memory:
"""A memory defined with `memo` or `memory` keyword"""
name: str
offset: int
global_offset = 0 # NOTE: The field doesn't have an annotation, so it's a static one
@staticmethod
def new_memory(name: str, size: int) -> "Memory":
"""
Defines a new local or global memory depending on the `State`.
Computes the offset of the memory and modifies `Memory.lobal_offset` if needed
"""
if State.current_proc is None:
mem = Memory(name, Memory.global_offset)
Memory.global_offset += size + (8 - size % 8 if size % 8 != 0 else 0)
State.memories[name] = mem
else:
mem = Memory(name, State.current_proc.memory_size)
State.current_proc.memory_size += size + (
8 - size % 8 if size % 8 != 0 else 0
)
State.current_proc.memories[name] = mem
return mem
class Proc:
"""
A procedure either defined in cont code or imported from an external source.
Whether it was imported is denoted by is_imported, which might make some fields None if it's True.
"""
def __init__(
self, name: str, ip: int, in_stack: List["Type"],
out_stack: List["Type"], block: Block, is_named: bool,
is_self_named: bool, owner=None
):
cont_assert(not (is_named and is_self_named), "Procedure cannot be both self-named and named.")
self.name: str = name
self.ip: int = ip
self.owner: "Struct" = None if owner is None else owner.typ
self.in_stack: List[object] = in_stack + ([owner] if owner is not None else [])
self.out_stack: List[object] = out_stack
self.block: Block = block
self.is_named: bool = is_named
self.is_self_named: bool = is_self_named
self.is_imported: bool = False
self.is_exported: bool = False
self.memories: Dict[str, Memory] = {}
self.memory_size: int = 0
self.variables: Dict[str, "Type"] = {} # type: ignore
self.used_procs: Set[Proc] = set()
if owner is not None:
owner.typ.add_method(self)
@classmethod
def create_imported(cls, name: str, in_stack: List["Type"], out_stack: List["Type"]) -> "Proc":
"""Creates and returns a new Proc, which was imported from an external source."""
self = cls.__new__(cls)
self.name = name
self.ip = State.get_new_ip(None)
self.in_stack = in_stack
self.out_stack = out_stack
self.block: Block = None
self.is_exported: bool = False
self.is_named: bool = False
self.is_self_named: bool = False
self.is_imported: bool = True
self.is_exported: bool = False
self.memories: Dict[str, Memory] = {}
self.memory_size: int = 0
self.variables: Dict[str, "Type"] = {} # type: ignore
self.used_procs: Set[Proc] = set()
return self
def __str__(self) -> str:
return f"Proc({self.name}, {None if self.owner is None else self.owner.name})"
def __hash__(self) -> int:
return id(self)
class StateSaver:
"""
A class used for storing certain state from the `State` class, while parsing included modules.
The constructor will load in the fields automatically.
"""
def __init__(self):
self.block_stack = State.block_stack
self.tokens = State.tokens
self.tokens_queue = State.tokens_queue
self.loc = State.loc
def load(self):
"""Loads the stored fields back into the `State` class"""
State.block_stack = self.block_stack
State.tokens = self.tokens
State.tokens_queue = self.tokens_queue
State.loc = self.loc
class State:
@classmethod
def initialize(cls):
"""Sets all the static fields to their default values"""
cls.config: Any = None
cls.block_stack: List[Block] = []
cls.route_stack: List[Tuple[str, List["Type"]]] = [] # type: ignore
cls.bind_stack: list = []
cls.do_stack: List[List[Op]] = []
cls.bind_stack_size: int = 0
cls.compile_ifs_opened: int = 0
cls.false_compile_ifs: int = 0
# This is used for the let keyowrd to unbind in the end, becuase main can be called multiple times on wat64
cls.global_binded: int = 0
cls.memories: Dict[str, Memory] = {}
cls.variables: Dict[str, "Type"] = {} # type: ignore
cls.procs: Dict[str, Proc] = {}
cls.imported_procs: List[Tuple[str, str]] = []
cls.referenced_procs: Set[Proc] = set() # The procedures, which were used for proc pointers
cls.structures: Dict[str, "Struct"] = {} # type: ignore
cls.constants: Dict[str, int] = {}
cls.enums: Dict[str, List[str]] = {}
cls.var_type_scopes: List[Dict[str, "VarType"]] = [] # type: ignore
cls.used_procs: Set[Proc] = set()
cls.included_files: List[str] = []
cls.runtimed_types_set: Set["Type"] = set() # type: ignore
cls.runtimed_types_list: List["Type"] = [] # type: ignore
cls.curr_type_id: int = 3
cls.string_data: List[bytes] = []
cls.locs_to_include: List[str] = []
cls.tokens: Generator = (i for i in ()) # type: ignore
cls.tokens_queue: List[Tuple[str, str]] = []
cls.ops_by_ips: List[Op] = []
cls.is_unpack = False
cls.is_init = False
cls.is_static = False
cls.is_named = False
cls.owner: Optional["Struct"] = None # type: ignore
cls.loc: str = ""
cls.filename: str = ""
cls.abs_path: str = ""
cls.current_ip: int = -1
cls.current_proc: Optional[Proc] = None
cls.dir: str = ""
@classmethod
def full_reset(cls):
"""Resets the state to the default values including the static state of other classes."""
cls.initialize()
Memory.global_offset = 0
UNAVAILABLE_NAMES: List[str] = [
"if", "else", "end", "while", "proc", "bind",
*["syscall" + str(i) for i in range(7)],
"+", "-", "*", "div", "dup", "drop", "swap", "rot",
"<", ">", "<=", ">=", "==", "!=", "!", "!8", "@",
"@8"
]
ONE_RETURN_DUNDER_METHODS: List[str] = [
"__add__", "__sub__", "__mul__", "__gt__", "__lt__", "__ge__", "__le__", "__eq__", "__ne__"
]
NOT_SAME_TYPE_DUNDER_METHODS: List[str] = ["__index__", "__index_ptr__"]
DUNDER_NEGATION_MAP: Dict[str, str] = {
"__eq__" : "__ne__",
"__gt__" : "__le__",
"__lt__" : "__ge__",
}
for from_, to in DUNDER_NEGATION_MAP.copy().items():
DUNDER_NEGATION_MAP[to] = from_
TYPE_STRUCTS: List[str] = ["Type", "PtrType", "ArrayType", "AddrType", "Struct"]
TYPE_IDS: Dict[str, int] = {
"int" : 0,
"ptr" : 1,
"array" : 2,
"addr" : 3,
}
def var_types() -> Dict[str, "VarType"]: # type: ignore
"""Returns the union of all dictionaries in `State.var_type_scopes`"""
if State.var_type_scopes:
return reduce(lambda a, b: {**a, **b}, State.var_type_scopes)
return {}
@staticmethod
def get_new_ip(op: Op):
"""
Gets the next available instruction position for the op and
performs all the state changes required.
"""
State.current_ip += 1
State.ops_by_ips.append(op)
return State.current_ip
@staticmethod
def check_name(token: Tuple[str, str], error="procedure"):
"""
Checks whether the provided token has a value, that is an available and valid name.
Throws an error if it isn't. The `error` parameter indicates what was the type of the thing,
that was supposed be named with the provided name e. g. a procedure.
"""
if token[0] in [*State.procs, *State.memories,
*State.constants, *State.structures, *State.enums]:
State.loc = token[1]
State.throw_error(f'name for {error} "{token[0]}" is already taken')
if token[0] in State.UNAVAILABLE_NAMES:
State.loc = token[1]
State.throw_error(f'name for {error} "{token[0]}" is unavailable')
@staticmethod
def get_proc_by_block(block: Block):
"""
Gets the `Proc` object for the procedure, that uses the block `Block`.
If such a procedure is not found an exception will be raised.
"""
proc_op = State.ops_by_ips[block.start]
return proc_op.operand
@staticmethod
def throw_error(error: str, do_exit: bool = True):
"""
Throws a cont error with all the formatting.
The `State.loc` will be used for the location of the error.
If `do_exit` is false the message will be printed to stderr, but the script won't exit.
"""
sys.stderr.write(f"\033[1;31mError {State.loc}:\033[0m {error}\n")
if do_exit:
exit(1)
@staticmethod
def add_proc_use(proc):
"""Modifies the directed graph of procedures usage according to the state"""
if State.current_proc is None:
State.used_procs.add(proc)
else:
State.current_proc.used_procs.add(proc)
@staticmethod
def compute_used_procs():
"""Modifies `State.used_procs` to include every single procedure, that should be compiled."""
orig = State.used_procs.copy()
State.used_procs = set()
for i in orig:
State.used_procs.add(i)
State._compute_used_procs(i)
@staticmethod
def _compute_used_procs(proc: Proc):
"""A helper method for `State.compute_used_procs`"""
for i in proc.used_procs:
if i in State.used_procs:
continue
State.used_procs.add(i)
State._compute_used_procs(i)
@staticmethod
def is_hex(token: str) -> bool:
"""
A utils method, which determines whether a given string is a valid hex number.
The method does not need any prefixes.
The token of "05aF" will result in a True, meanwhile "0xa0" will result in a False.
"""
return all(i.lower() in "abcdef1234567890" for i in token)
@staticmethod
def is_bin(token: str) -> bool:
"""
A utils method, which determines whether a given string is a valid binary number.
The method does not need any prefixes.
The token of "0001010" will result in a True, meanwhile "0b110" will result in a False.
"""
return all(i.lower() in "01" for i in token)
@staticmethod
def is_oct(token: str) -> bool:
"""
A utils method, which determines whether a given string is a valid octal number.
The method does not need any prefixes.
The token of "163" will result in a True, meanwhile "0o120" will result in a False.
"""
return all(i.lower() in "01234567" for i in token)
State.initialize()