diff --git a/src/api/optimize.py b/src/api/optimize.py index 0330e80f3..df5ccc5a4 100644 --- a/src/api/optimize.py +++ b/src/api/optimize.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import symtable -from typing import NamedTuple, Optional, Set +from typing import Any, Generator, NamedTuple, Optional, Set import src.api.check as chk import src.api.global_ as gl @@ -13,7 +13,7 @@ from src.api.constants import CLASS, CONVENTION, SCOPE, TYPE from src.api.debug import __DEBUG__ from src.api.errmsg import warning_not_used -from src.ast import NodeVisitor +from src.ast import Ast, NodeVisitor from src.symbols import sym as symbols from src.symbols.id_ import ref @@ -63,7 +63,7 @@ def _visit(self, node: ToVisit): meth = getattr(self, f"visit_{node.obj.token}", self.generic_visit) return meth(node.obj) - def generic_visit(self, node: symbols.SYMBOL): + def generic_visit(self, node: Ast) -> Generator[Ast | None, Any, None]: for i, child in enumerate(node.children): node.children[i] = yield self.visit(child) diff --git a/src/ast/ast.py b/src/ast/ast.py index bde657efd..d3bdd3613 100644 --- a/src/ast/ast.py +++ b/src/ast/ast.py @@ -54,8 +54,7 @@ def _visit(self, node): meth = getattr(self, f"visit_{node.token}", self.generic_visit) return meth(node) - @staticmethod - def generic_visit(node: Ast): + def generic_visit(self, node: Ast): raise RuntimeError(f"No visit_{node.token}() method defined") def filter_inorder( diff --git a/src/ast/tree.py b/src/ast/tree.py index f8a0f4d54..00bb7db74 100644 --- a/src/ast/tree.py +++ b/src/ast/tree.py @@ -1,8 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +from __future__ import annotations import collections.abc -from typing import Iterable, Optional, Union +from typing import Iterable, Optional, Union, Any, Iterator from src.api.exception import Error @@ -27,9 +28,9 @@ class Tree: __slots__ = "_children", "parent" - def __init__(self): + def __init__(self) -> None: self._children = ChildrenList(self) - self.parent: Optional["Tree"] = None + self.parent: Optional[Tree] = None @property def children(self): @@ -66,11 +67,11 @@ def postorder(self): yield self - def append_child(self, node: "Tree"): + def append_child(self, node: Tree) -> None: """Appends the given node to the current children list""" self.children.append(node) - def prepend_child(self, node: "Tree"): + def prepend_child(self, node: Tree) -> None: """Inserts the given node at the beginning of the children list""" self.children.insert(0, node) @@ -78,49 +79,57 @@ def prepend_child(self, node: "Tree"): class ChildrenList: owner: Tree - def __init__(self, node: Tree): + def __init__(self, node: Tree) -> None: assert isinstance(node, Tree) self.owner = node # Node having this children self._children: list[Tree | None] = [] - def __getitem__(self, key: Union[int, slice]): + def __getitem__(self, key: int | slice) -> Tree | ChildrenList | None: if isinstance(key, int): return self._children[key] result = ChildrenList(self.owner) for x in self._children[key]: result.append(x) + return result - def __setitem__(self, key, value: Optional[Tree]): + def __setitem__(self, key: int, value: Tree | None) -> None: assert value is None or isinstance(value, Tree) if value is not None: value.parent = self.owner self._children[key] = value - def __delitem__(self, key): - self._children[key].parent = None + def __delitem__(self, key: int) -> None: + child = self._children[key] + if child is not None: + child.parent = None del self._children[key] - def append(self, value: Tree): - assert isinstance(value, Tree) - value.parent = self.owner + def __iter__(self) -> Iterator[Tree | None]: + return iter(self._children) + + def append(self, value: Tree | None) -> None: + if value is not None: + value.parent = self.owner self._children.append(value) - def insert(self, pos: int, value: Tree): + def insert(self, pos: int, value: Tree) -> None: assert isinstance(value, Tree) value.parent = self.owner self._children.insert(pos, value) - def pop(self, pos: int = -1): + def pop(self, pos: int = -1) -> Tree | None: result = self._children.pop(pos) - result.parent = None + if result is not None: + result.parent = None + return result - def __len__(self): + def __len__(self) -> int: return len(self._children) - def __add__(self, other): + def __add__(self, other: Any) -> ChildrenList: if not isinstance(other, ChildrenList): assert isinstance(other, collections.abc.Container) @@ -129,7 +138,8 @@ def __add__(self, other): result.append(x) for x in other: result.append(x) + return result - def __repr__(self): + def __repr__(self) -> str: return f"{repr(self.owner)}:{str([repr(x) for x in self._children])}" diff --git a/src/zxbc/args_config.py b/src/zxbc/args_config.py index da07d1b24..028655591 100644 --- a/src/zxbc/args_config.py +++ b/src/zxbc/args_config.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 from __future__ import annotations + import os from typing import TYPE_CHECKING