The variable table. A variable table stores the all variables when parsing TVMScript. Parameters ---------- frames : List[VarTableFrame] The list or stack of variable table frame. name2value : Dict[str, List[Any]] The dictionary for variable table name-based que
| 211 | |
| 212 | |
| 213 | class VarTable: |
| 214 | """The variable table. |
| 215 | A variable table stores the all variables when parsing TVMScript. |
| 216 | |
| 217 | Parameters |
| 218 | ---------- |
| 219 | frames : List[VarTableFrame] |
| 220 | The list or stack of variable table frame. |
| 221 | |
| 222 | name2value : Dict[str, List[Any]] |
| 223 | The dictionary for variable table name-based query. |
| 224 | """ |
| 225 | |
| 226 | frames: list[VarTableFrame] |
| 227 | name2value: dict[str, list[Any]] |
| 228 | |
| 229 | def __init__(self): |
| 230 | self.frames = [] |
| 231 | self.name2value = defaultdict(list) |
| 232 | |
| 233 | def with_frame(self): |
| 234 | """Create a new variable table frame as with statement. |
| 235 | |
| 236 | Returns |
| 237 | ------- |
| 238 | res : Any |
| 239 | The context with new variable table frame. |
| 240 | """ |
| 241 | |
| 242 | def pop_frame(): |
| 243 | frame = self.frames.pop() |
| 244 | frame.pop_all(lambda name: self.name2value[name].pop()) |
| 245 | |
| 246 | self.frames.append(VarTableFrame()) |
| 247 | return _deferred(pop_frame) |
| 248 | |
| 249 | def add(self, var: str, value: Any, allow_shadowing: bool = False): |
| 250 | """Add a new variable to variable table. |
| 251 | |
| 252 | Parameters |
| 253 | ---------- |
| 254 | var : str |
| 255 | The name of variable. |
| 256 | |
| 257 | value : Any |
| 258 | The value of variable. |
| 259 | |
| 260 | allow_shadowing : bool |
| 261 | The options of whether variable shadowing allowed for this variable. |
| 262 | """ |
| 263 | # Skip if the key and value are equal to those in the var_table |
| 264 | if self.name2value[var] and isinstance(self.name2value[var][-1], type(value)): |
| 265 | if isinstance(value, np.ndarray) and (self.name2value[var][-1] == value).all(): |
| 266 | return |
| 267 | elif self.name2value[var][-1] == value: |
| 268 | return |
| 269 | if allow_shadowing and var in self.frames[-1].vars: |
| 270 | # Shadowing |