Skip to content

Commit bb9a6b5

Browse files
committed
refact: move Exception to its own class
1 parent c479ebf commit bb9a6b5

4 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/ast/ast.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
import types
99
from collections.abc import Callable
10-
from typing import Any
10+
from typing import Any, Final
1111

1212
from .tree import Tree
1313

14+
__all__: Final[tuple[str, ...]] = "Ast", "NodeVisitor"
15+
1416

1517
# ----------------------------------------------------------------------
1618
# Abstract Syntax Tree class
@@ -34,12 +36,13 @@ def visit(self, node):
3436

3537
while stack:
3638
try:
37-
last = stack[-1]
38-
if isinstance(last, types.GeneratorType):
39-
stack.append(last.send(last_result))
39+
stack_top = stack[-1]
40+
if isinstance(stack_top, types.GeneratorType):
41+
stack.append(stack_top.send(last_result))
4042
last_result = None
41-
elif isinstance(last, self.node_type):
42-
stack.append(self._visit(stack.pop()))
43+
elif isinstance(stack_top, self.node_type):
44+
stack.pop()
45+
stack.append(self._visit(stack_top))
4346
else:
4447
last_result = stack.pop()
4548
except StopIteration:
@@ -55,7 +58,10 @@ def generic_visit(self, node: Ast):
5558
raise RuntimeError(f"No visit_{node.token}() method defined")
5659

5760
def filter_inorder(
58-
self, node, filter_func: Callable[[Any], bool], child_selector: Callable[[Ast], bool] = lambda x: True
61+
self,
62+
node,
63+
filter_func: Callable[[Any], bool],
64+
child_selector: Callable[[Ast], bool] = lambda x: True,
5965
):
6066
"""Visit the tree inorder, but only those that return true for filter_func and visiting children which
6167
return true for child_selector.

src/ast/exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Final
2+
3+
from src.api.exception import Error
4+
5+
__all__: Final[tuple[str, ...]] = ("NotAnAstError",)
6+
7+
8+
class NotAnAstError(Error):
9+
"""Thrown when the "pointer" is not
10+
an AST, but another thing.
11+
"""
12+
13+
def __init__(self, instance):
14+
self.instance = instance
15+
self.msg = "Object '%s' is not an Ast instance" % str(instance)
16+
17+
def __str__(self):
18+
return self.msg

src/ast/tree.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,7 @@
1111
from collections.abc import Iterable, Iterator
1212
from typing import Any
1313

14-
from src.api.exception import Error
15-
16-
__all__ = "ChildrenList", "NotAnAstError", "Tree"
17-
18-
19-
class NotAnAstError(Error):
20-
"""Thrown when the "pointer" is not
21-
an AST, but another thing.
22-
"""
23-
24-
def __init__(self, instance):
25-
self.instance = instance
26-
self.msg = "Object '%s' is not an Ast instance" % str(instance)
27-
28-
def __str__(self):
29-
return self.msg
14+
__all__ = "ChildrenList", "Tree"
3015

3116

3217
class Tree:

src/zxbasm/expr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from src.api.errmsg import error
99
from src.ast import Ast
10-
from src.ast.tree import NotAnAstError
10+
from src.ast.exceptions import NotAnAstError
1111
from src.zxbasm.label import Label
1212

1313

0 commit comments

Comments
 (0)