-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
401 lines (321 loc) · 11 KB
/
components.py
File metadata and controls
401 lines (321 loc) · 11 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from dataclasses import dataclass
from typing import List, Dict, Any, ClassVar, Set, Tuple
from typing import List, Union
import re
class FunctionTranslationException(Exception):
pass
@dataclass(frozen=True, slots=True)
class Token:
pass
@dataclass(frozen=True, slots=True)
class OperatorToken(Token):
value: str
@dataclass(frozen=True, slots=True)
class OpeningRoundBracketToken(Token): # No need value as they are only used for parsing and translation, not for final output
pass
@dataclass(frozen=True, slots=True)
class ClosingRoundBracketToken(Token):
pass
@dataclass(frozen=True, slots=True)
class OpeningSquigglyBracketToken(Token):
pass
@dataclass(frozen=True, slots=True)
class ClosingSquigglyBracketToken(Token):
pass
@dataclass(frozen=True, slots=True)
class TermToken(Token):
value: str
@property
def is_set(self) -> bool:
return self.value in PatGlobal.sets
@dataclass(frozen=True, slots=True)
class CommaToken(Token):
pass
@dataclass(frozen=True, slots=True)
class TranslatableToken(Token):
value: str
def get_translation(self) -> str:
raise NotImplementedError("This method should be implemented by subclasses of TranslatableToken")
@dataclass(frozen=True, slots=True)
class TranslatedToken(TranslatableToken):
value: str
def get_translation(self) -> str:
return self.value
@dataclass(frozen=True, slots=True)
class PlainTextToken(TranslatableToken):
value: str
def get_translation(self) -> str:
return self.value
@dataclass(frozen=True, slots=True)
class SetToken(TranslatableToken):
value: List[Union[TermToken, "SetToken"]]
name: str = ""
def __post_init__(self):
PatGlobal.add_enum([term.value for term, _ in self.flatten_with_level()])
def flatten_with_level(self, current_level: int = 1) -> List[Tuple["TermToken", int]]:
result: List[Tuple["TermToken", int]] = []
for item in self.value:
if isinstance(item, TermToken):
result.append((item, current_level))
elif isinstance(item, SetToken):
result.extend(item.flatten_with_level(current_level + 1))
else:
raise TypeError(f"Invalid item in SetToken: {item}")
return result
def get_translation(self) -> str:
flattened = self.flatten_with_level()
seen = {}
for term, _ in flattened:
if term.value not in seen:
seen[term.value] = len(seen)
enum_names = list(seen.keys())
mask_expr = " | ".join(f"{name}_BIT" for name in enum_names)
return f"var {self.name} = {mask_expr};"
@dataclass(frozen=True, slots=True)
class FunctionTypeToken(Token):
parameters: str
return_type: str
@property
def value(self) -> str:
return f"({self.parameters}) -> {self.return_type}"
@dataclass(frozen=True, slots=True)
class FunctionDefinitionToken(TranslatableToken):
func_name: str
value: str
@dataclass(frozen=True, slots=True)
class FunctionCallToken(Token):
value: str
#got lazy
def to_pat_call(self) -> str:
"""
Converts 'func(param1, param2, ...)' into 'call(func, param1, param2, ...)'.
"""
match = re.fullmatch(r'\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*', self.value)
if not match:
raise ValueError(f"Invalid function call expression: {self.value}")
func_name, params = match.groups()
params = [p.strip() for p in params.split(',')] if params.strip() else []
return f"call({func_name}, {', '.join(params)})"
TokenT = Union[
OperatorToken,
OpeningRoundBracketToken,
ClosingRoundBracketToken,
OpeningSquigglyBracketToken,
ClosingSquigglyBracketToken,
TermToken,
SetToken,
CommaToken,
TranslatedToken,
FunctionTypeToken,
FunctionCallToken,
]
@dataclass(frozen=True, slots=True)
class EventBAxiom:
name: str
predicate: str
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBAxiom":
return EventBAxiom(
name=data.get("name", ""),
predicate=data.get("predicate", "")
)
@dataclass(frozen=True, slots=True)
class EventBInvariant:
name: str
predicate: str
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBInvariant":
return EventBInvariant(
name=data.get("name", ""),
predicate=data.get("predicate", "")
)
@dataclass(frozen=True, slots=True)
class EventBGuard:
name: str
predicate: str
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBGuard":
return EventBGuard(
name=data.get("name", ""),
predicate=data.get("predicate", "")
)
@dataclass(frozen=True, slots=True)
class EventBAction:
name: str
assignment: str
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBAction":
return EventBAction(
name=data.get("name", ""),
assignment=data.get("assignment", "")
)
@dataclass(frozen=True, slots=True)
class EventBContext:
name: str
sets: List[str]
constants: List[str]
axioms: List[EventBAxiom]
extends: List[str]
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBContext":
return EventBContext(
name=data.get("CONTEXT", ""),
sets=list(data.get("SETS", [])),
constants=list(data.get("CONSTANTS", [])),
axioms=[
EventBAxiom.from_dict(ax)
for ax in data.get("AXIOMS", [])
],
extends=list(data.get("EXTENDS", []))
)
def __str__(self):
return (
f"Context: {self.name}\n"
f" Sets: {self.sets}\n"
f" Constants: {self.constants}\n"
f" Axioms: {[ax.predicate for ax in self.axioms]}\n"
f" Extends: {self.extends}"
)
@dataclass(frozen=True, slots=True)
class EventBEvent:
name: str
refines: List[str]
any: List[str]
where: List[EventBGuard]
withs: List[str]
then: List[EventBAction]
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBEvent":
return EventBEvent(
name=data.get("event_name", ""),
refines=list(data.get("REFINES", [])),
any=list(data.get("ANY", [])),
where=[
EventBGuard.from_dict(g)
for g in data.get("WHERE", [])
],
withs=list(data.get("WITH", [])),
then=[
EventBAction.from_dict(a)
for a in data.get("THEN", [])
]
)
def is_initialisation(self) -> bool:
return self.name.lower() == "initialisation"
def __str__(self):
return (
f"Event: {self.name}\n"
f" ANY: {self.any}\n"
f" Guards: {[g.predicate for g in self.where]}\n"
f" Actions: {[a.assignment for a in self.then]}"
)
@dataclass(frozen=True, slots=True)
class EventBMachine:
name: str
refines: List[str]
sees: List[str]
variables: List[str]
invariants: List[EventBInvariant]
events: List[EventBEvent]
@staticmethod
def from_dict(data: Dict[str, Any]) -> "EventBMachine":
return EventBMachine(
name=data.get("MACHINE", ""),
refines=list(data.get("REFINES", [])),
sees=list(data.get("SEES", [])),
variables=list(data.get("VARIABLES", [])),
invariants=[
EventBInvariant.from_dict(inv)
for inv in data.get("INVARIANTS", [])
],
events=[
EventBEvent.from_dict(ev)
for ev in data.get("EVENTS", [])
]
)
def __str__(self):
return (
f"Machine: {self.name}\n"
f" Refines: {self.refines}\n"
f" Sees: {self.sees}\n"
f" Variables: {self.variables}\n"
f" Invariants: {[inv.predicate for inv in self.invariants]}\n"
f" Events:\n" +
"\n".join(str(ev) for ev in self.events)
)
@dataclass(frozen=True, slots=True)
class PatGlobal:
"""
This class serves as a global registry for enums, variables, defines, and custom functions that are encountered during the translation process.
Not sure if this will be the best way to handle this, but it allows us to keep track of these elements and ensure that they are defined in the generated code as needed.
"""
@dataclass
class FunctionInfo:
definition: List[str]
arity: int
assertCount: ClassVar[int] = 0
enums: ClassVar[Set[str]] = set() # A global enum is used as enum {blue, red}; enume {yellow, green}; red == green when it really shouldnt
sets: ClassVar[Set[str]] = set() # so we know which terms are sets and not just normal terms
functions: ClassVar[Dict[str, FunctionInfo]] = {}
variables: ClassVar[Set[str]] = set()
constants: ClassVar[Set[str]] = set()
is_ai_used: ClassVar[bool] = False
@classmethod
def increment_assert_count(cls) -> int:
cls.assertCount += 1
return cls.assertCount
@classmethod
def add_enum(cls, elements :List[str]) -> None:
for element in elements:
cls.enums.add(element)
@classmethod
def add_variable(cls, var_name: str) -> None:
cls.variables.add(var_name)
@classmethod
def add_constant(cls, const_name: str) -> None:
cls.constants.add(const_name)
@classmethod
def add_set(cls, set_name: str) -> None:
if len(set_name) == 0:
return
cls.sets.add(set_name)
@classmethod
def add_function_definition(
cls,
func_name: str,
definition: str
) -> None:
if func_name not in cls.functions:
arity = cls._extract_arity(definition)
cls.functions[func_name] = cls.FunctionInfo(definition=[definition], arity=arity)
info = cls.functions[func_name]
info.definition.append(definition)
@classmethod
def _extract_arity(cls, signature: str) -> int:
signature = signature.replace("->", "→")
match = re.search(r'∈\s*(.*?)\s*→', signature)
if not match:
return -1;
domain = match.group(1).strip()
parts = re.split(r'[×x]', domain)
return len([p for p in parts if p.strip()])
@classmethod
def print_globals(cls) -> None:
print(f"Enums: {cls.enums}")
print(f"Variables: {cls.variables}")
print(f"Sets: {cls.sets}")
print(f"Functions: {cls.functions}")
@classmethod
def functions_to_string(cls) -> str:
result = []
for func_name, info in cls.functions.items():
result.append(f"Function: {func_name} has an arity of {info.arity} where:\n")
for definition in info.definition:
result.append(f" {definition}\n")
return "".join(result)
@classmethod
def set_ai_used(cls) -> None:
cls.is_ai_used = True
@classmethod
def get_ai_used(cls) -> bool:
return cls.is_ai_used