Commit 9bd19dba authored by Tom Niget's avatar Tom Niget

Use __ for traceback info

parent 775f5b0b
......@@ -50,10 +50,10 @@ def exception_hook(exc_type, exc_value, tb):
if name == "visit" and (node := local_vars["node"]) and isinstance(node, ast.AST):
last_node = node
if node := local_vars.get("TB_NODE", None):
if node := local_vars.get("__TB_NODE__", None):
last_node = node
if local_vars.get("TB_SKIP", None) and tb.tb_next:
if local_vars.get("__TB_SKIP__", None) and tb.tb_next:
tb = tb.tb_next
continue
......@@ -61,7 +61,7 @@ def exception_hook(exc_type, exc_value, tb):
line_no = tb.tb_lineno
print(cf.red(f"File {filename}:{line_no}, in {cf.green(name)}"), end="")
if info := local_vars.get("TB", None):
if info := local_vars.get("__TB__", None):
print(f": {cf.magenta(info)}\x1b[24m")
else:
print()
......@@ -177,7 +177,7 @@ else:
def transpile(source, name: str, path=None):
TB = f"transpiling module {cf.white(name)}"
__TB__ = f"transpiling module {cf.white(name)}"
res = ast.parse(source, type_comments=True)
IfMainVisitor().visit(res)
......
......@@ -16,8 +16,8 @@ from transpiler.utils import UnsupportedNodeError, highlight
class UniversalVisitor:
def visit(self, node):
"""Visit a node."""
TB = f"emitting C++ code for {highlight(node)}"
# TB_SKIP = True
__TB__ = f"emitting C++ code for {highlight(node)}"
# __TB_SKIP__ = True
if type(node) == list:
for n in node:
......
......@@ -13,7 +13,7 @@ class FileVisitor(BlockVisitor):
module_name: str
def visit_Module(self, node: ast.Module) -> Iterable[str]:
TB = "emitting C++ code for Python module"
__TB__ = "emitting C++ code for Python module"
stmt: ast.AST
yield "#include <python/builtins.hpp>"
......
......@@ -19,7 +19,7 @@ IGNORED_IMPORTS = {"typon", "typing", "__future__", "dataclasses", "enum"}
class ModuleVisitor(BlockVisitor):
includes: list[str] = field(default_factory=list)
def visit_Import(self, node: ast.Import) -> Iterable[str]:
TB = f"emitting C++ code for {highlight(node)}"
__TB__ = f"emitting C++ code for {highlight(node)}"
for alias in node.names:
concrete = self.fix_name(alias.asname or alias.name)
if alias.module_obj.is_python:
......@@ -46,7 +46,7 @@ class ModuleVisitor(BlockVisitor):
yield ""
def emit_python_func(self, mod: str, name: str, alias: str, fty: FunctionType) -> Iterable[str]:
TB = f"emitting C++ code for Python function {highlight(f'{mod}.{name}')}"
__TB__ = f"emitting C++ code for Python function {highlight(f'{mod}.{name}')}"
yield "struct {"
yield f"auto operator()("
......
......@@ -98,17 +98,17 @@ class ScoperBlockVisitor(ScoperVisitor):
node.is_declare = decl
if node.value is not None:
ty_val = self.get_type(node.value)
TB = f"unifying annotation {highlight(node.annotation)} with value {highlight(node.value)} of type {highlight(ty_val)}"
__TB__ = f"unifying annotation {highlight(node.annotation)} with value {highlight(node.value)} of type {highlight(ty_val)}"
ty.unify(ty_val)
def visit_assign_target(self, target, decl_val: BaseType) -> bool:
TB = f"analyzing assignment target {highlight(target)} with value {highlight(decl_val)}"
__TB__ = f"analyzing assignment target {highlight(target)} with value {highlight(decl_val)}"
if isinstance(target, ast.Name):
if target.id == "_":
return False
target.type = decl_val
if vdecl := self.scope.get(target.id, {VarKind.LOCAL, VarKind.GLOBAL, VarKind.NONLOCAL}, restrict_function=True):
TB = f"unifying existing variable {highlight(target.id)} of type {highlight(vdecl.type)} with assigned value {highlight(decl_val)}"
__TB__ = f"unifying existing variable {highlight(target.id)} of type {highlight(vdecl.type)} with assigned value {highlight(decl_val)}"
vdecl.type.unify(decl_val)
return False
else:
......@@ -333,7 +333,7 @@ class ScoperBlockVisitor(ScoperVisitor):
def visit(self, node: ast.AST):
if isinstance(node, ast.AST):
TB_SKIP = True
__TB_SKIP__ = True
super().visit(node)
node.scope = self.scope
else:
......
......@@ -57,7 +57,7 @@ class ScoperVisitor(NodeVisitorSeq):
def visit_block(self, block: list[ast.AST]):
if not block:
return
TB = f"running type analysis on block starting with {highlight(block[0])}"
__TB__ = f"running type analysis on block starting with {highlight(block[0])}"
self.fdecls = []
for b in block:
self.visit(b)
......@@ -82,8 +82,8 @@ class ScoperVisitor(NodeVisitorSeq):
exc = None
def visit_function_definition(self, node, rtype):
TB = f"running type analysis on the body of {highlight(node)}"
TB_NODE = node
__TB__ = f"running type analysis on the body of {highlight(node)}"
__TB_NODE__ = node
from transpiler.phases.typing.block import ScoperBlockVisitor
for b in node.body:
decls = {}
......
......@@ -38,10 +38,10 @@ class ScoperExprVisitor(ScoperVisitor):
def visit(self, node) -> BaseType:
if existing := getattr(node, "type", None):
return existing.resolve()
TB_SKIP = True
__TB_SKIP__ = True
res = super().visit(node)
if not res:
TB_SKIP = False
__TB_SKIP__ = False
raise NotImplementedError(f"`{ast.unparse(node)}` {type(node)}")
res = res.resolve()
if True or not hasattr(res, "from_node"):
......
......@@ -84,7 +84,7 @@ class BaseType(ABC):
def unify(self, other: "BaseType", mode = UnifyMode.NORMAL):
a, b = self.resolve(), other.resolve()
TB = f"unifying {highlight(a)} and {highlight(b)}"
__TB__ = f"unifying {highlight(a)} and {highlight(b)}"
if isinstance(b, TypeVariable):
a, b = b, a
a.unify_internal(b, mode)
......@@ -261,7 +261,7 @@ class TypeOperator(BaseType, ABC):
from transpiler.phases.typing.exceptions import TypeMismatchError, TypeMismatchKind
# TODO(zdimension): this is really broken... but it would be nice
# if from_node := next(filter(None, (getattr(x, "from_node", None) for x in (other, self))), None):
# TB_NODE = from_node
# __TB_NODE__ = from_node
if not isinstance(other, TypeOperator):
raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE)
if mode.match_protocol:
......
......@@ -7,7 +7,7 @@ from transpiler.utils import UnsupportedNodeError, highlight
class NodeVisitorSeq:
def visit(self, node):
TB = f"running type analysis on {highlight(node)}"
__TB__ = f"running type analysis on {highlight(node)}"
"""Visit a node."""
if type(node) == list:
for n in node:
......
......@@ -61,7 +61,7 @@ def highlight(code, full=False):
"""
Syntax highlights code as Python using colorama
"""
TB = f"syntax highlighting {code}"
__TB__ = f"syntax highlighting {code}"
if code is None:
return cf.yellow("<None>")
if type(code) == list:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment