2
2
3
3
from typing import Dict , List , AbstractSet , Tuple , Optional , Hashable
4
4
5
- from ..objects .cfg_objects import Variable , CFGObjectConvertible
5
+ from ..objects .formal_object import FormalObject
6
+ from ..objects .cfg_objects import Variable
6
7
7
8
8
9
class CFGVariableConverter :
9
10
"""A CFG Variable Converter"""
10
11
11
12
def __init__ (self ,
12
- states : AbstractSet [CFGObjectConvertible ],
13
- stack_symbols : AbstractSet [CFGObjectConvertible ]) -> None :
13
+ states : AbstractSet [FormalObject ],
14
+ stack_symbols : AbstractSet [FormalObject ]) -> None :
14
15
self ._counter = 0
15
- self ._inverse_states_d : Dict [CFGObjectConvertible , int ] = {}
16
+ self ._inverse_states_d : Dict [FormalObject , int ] = {}
16
17
self ._counter_state = 0
17
18
for self ._counter_state , state in enumerate (states ):
18
19
self ._inverse_states_d [state ] = self ._counter_state
19
- state .index_cfg_converter = self ._counter_state
20
+ state .index = self ._counter_state
20
21
self ._counter_state += 1
21
- self ._inverse_stack_symbol_d : Dict [CFGObjectConvertible , int ] = {}
22
+ self ._inverse_stack_symbol_d : Dict [FormalObject , int ] = {}
22
23
self ._counter_symbol = 0
23
24
for self ._counter_symbol , symbol in enumerate (stack_symbols ):
24
25
self ._inverse_stack_symbol_d [symbol ] = self ._counter_symbol
25
- symbol .index_cfg_converter = self ._counter_symbol
26
+ symbol .index = self ._counter_symbol
26
27
self ._counter_symbol += 1
27
28
self ._conversions : List [List [List [Tuple [bool , Optional [Variable ]]]]] \
28
29
= [[[(False , None ) for _ in range (len (states ))]
29
30
for _ in range (len (stack_symbols ))] for _ in
30
31
range (len (states ))]
31
32
32
- def _get_state_index (self , state : CFGObjectConvertible ) -> int :
33
+ def _get_state_index (self , state : FormalObject ) -> int :
33
34
"""Get the state index"""
34
- if state .index_cfg_converter is None :
35
+ if state .index is None :
35
36
if state not in self ._inverse_states_d :
36
37
self ._inverse_states_d [state ] = self ._counter_state
37
38
self ._counter_state += 1
38
- state .index_cfg_converter = self ._inverse_states_d [state ]
39
- return state .index_cfg_converter
39
+ state .index = self ._inverse_states_d [state ]
40
+ return state .index
40
41
41
- def _get_symbol_index (self , symbol : CFGObjectConvertible ) -> int :
42
+ def _get_symbol_index (self , symbol : FormalObject ) -> int :
42
43
"""Get the symbol index"""
43
- if symbol .index_cfg_converter is None :
44
+ if symbol .index is None :
44
45
if symbol not in self ._inverse_stack_symbol_d :
45
46
self ._inverse_stack_symbol_d [symbol ] = self ._counter_symbol
46
47
self ._counter_symbol += 1
47
- symbol .index_cfg_converter = self ._inverse_stack_symbol_d [symbol ]
48
- return symbol .index_cfg_converter
48
+ symbol .index = self ._inverse_stack_symbol_d [symbol ]
49
+ return symbol .index
49
50
50
51
def to_cfg_combined_variable (self ,
51
- state0 : CFGObjectConvertible ,
52
- stack_symbol : CFGObjectConvertible ,
53
- state1 : CFGObjectConvertible ) -> Variable :
52
+ state0 : FormalObject ,
53
+ stack_symbol : FormalObject ,
54
+ state1 : FormalObject ) -> Variable :
54
55
""" Conversion used in the to_pda method """
55
56
i_stack_symbol , i_state0 , i_state1 = self ._get_indexes (
56
57
stack_symbol , state0 , state1 )
@@ -74,19 +75,19 @@ def _create_new_variable(self,
74
75
return temp
75
76
76
77
def set_valid (self ,
77
- state0 : CFGObjectConvertible ,
78
- stack_symbol : CFGObjectConvertible ,
79
- state1 : CFGObjectConvertible ) -> None :
78
+ state0 : FormalObject ,
79
+ stack_symbol : FormalObject ,
80
+ state1 : FormalObject ) -> None :
80
81
"""Set valid"""
81
82
i_stack_symbol , i_state0 , i_state1 = self ._get_indexes (
82
83
stack_symbol , state0 , state1 )
83
84
prev = self ._conversions [i_state0 ][i_stack_symbol ][i_state1 ]
84
85
self ._conversions [i_state0 ][i_stack_symbol ][i_state1 ] = (True , prev [1 ])
85
86
86
87
def is_valid_and_get (self ,
87
- state0 : CFGObjectConvertible ,
88
- stack_symbol : CFGObjectConvertible ,
89
- state1 : CFGObjectConvertible ) -> Optional [Variable ]:
88
+ state0 : FormalObject ,
89
+ stack_symbol : FormalObject ,
90
+ state1 : FormalObject ) -> Optional [Variable ]:
90
91
"""Check if valid and get"""
91
92
i_state0 = self ._get_state_index (state0 )
92
93
i_stack_symbol = self ._get_symbol_index (stack_symbol )
@@ -102,9 +103,9 @@ def is_valid_and_get(self,
102
103
return current [1 ]
103
104
104
105
def _get_indexes (self ,
105
- stack_symbol : CFGObjectConvertible ,
106
- state0 : CFGObjectConvertible ,
107
- state1 : CFGObjectConvertible ) \
106
+ stack_symbol : FormalObject ,
107
+ state0 : FormalObject ,
108
+ state1 : FormalObject ) \
108
109
-> Tuple [int , int , int ]:
109
110
i_state0 = self ._get_state_index (state0 )
110
111
i_stack_symbol = self ._get_symbol_index (stack_symbol )
0 commit comments