Commit d0c84863 authored by Tom Niget's avatar Tom Niget

Extract method for better traceback

parent fc4a6b28
...@@ -97,7 +97,9 @@ def exception_hook(exc_type, exc_value, tb): ...@@ -97,7 +97,9 @@ def exception_hook(exc_type, exc_value, tb):
for i, line in enumerate(hg[start:last_node.end_lineno + CONTEXT_SIZE]): for i, line in enumerate(hg[start:last_node.end_lineno + CONTEXT_SIZE]):
erroneous = last_node.lineno <= offset + i <= last_node.end_lineno erroneous = last_node.lineno <= offset + i <= last_node.end_lineno
indicator = cf.white(" →") if erroneous else " " indicator = cf.white(" →") if erroneous else " "
disp = f"\x1b[24m{indicator}{cf.white}{(offset + i):>4}{cf.red if erroneous else cf.reset}{cf.reset} {line}\x1b[24m" bar = " ▎"
# bar = "│" if erroneous else "┊"
disp = f"\x1b[24m{indicator}{cf.white}{(offset + i):>4}{cf.red if erroneous else cf.reset}{bar}{cf.reset} {line}\x1b[24m"
print(disp) print(disp)
# print(repr(disp)) # print(repr(disp))
print() print()
......
...@@ -28,7 +28,6 @@ class ScoperVisitor(NodeVisitorSeq): ...@@ -28,7 +28,6 @@ class ScoperVisitor(NodeVisitorSeq):
if not block: if not block:
return 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])}"
from transpiler.phases.typing.block import ScoperBlockVisitor
self.fdecls = [] self.fdecls = []
for b in block: for b in block:
self.visit(b) self.visit(b)
...@@ -40,13 +39,7 @@ class ScoperVisitor(NodeVisitorSeq): ...@@ -40,13 +39,7 @@ class ScoperVisitor(NodeVisitorSeq):
for node, rtype in old_list: for node, rtype in old_list:
from transpiler.exceptions import CompileError from transpiler.exceptions import CompileError
try: try:
for b in node.body: self.visit_function_definition(node, rtype)
decls = {}
visitor = ScoperBlockVisitor(node.inner_scope, decls)
visitor.visit(b)
b.decls = decls
if not node.inner_scope.has_return:
rtype.unify(TY_NONE) # todo: properly indicate missing return
except CompileError as e: except CompileError as e:
new_list.append((node, rtype)) new_list.append((node, rtype))
if not exc: if not exc:
...@@ -58,3 +51,15 @@ class ScoperVisitor(NodeVisitorSeq): ...@@ -58,3 +51,15 @@ class ScoperVisitor(NodeVisitorSeq):
old_list = new_list old_list = new_list
exc = None exc = None
def visit_function_definition(self, node, rtype):
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 = {}
visitor = ScoperBlockVisitor(node.inner_scope, decls)
visitor.visit(b)
b.decls = decls
if not node.inner_scope.has_return:
rtype.unify(TY_NONE) # todo: properly indicate missing return
...@@ -174,8 +174,9 @@ class TypeOperator(BaseType, ABC): ...@@ -174,8 +174,9 @@ class TypeOperator(BaseType, ABC):
def unify_internal(self, other: BaseType): def unify_internal(self, other: BaseType):
from transpiler.phases.typing.exceptions import TypeMismatchError, TypeMismatchKind from transpiler.phases.typing.exceptions import TypeMismatchError, TypeMismatchKind
if from_node := next(filter(None, (getattr(x, "from_node", None) for x in (other, self))), None): # TODO(zdimension): this is really broken... but it would be nice
TB_NODE = from_node # if from_node := next(filter(None, (getattr(x, "from_node", None) for x in (other, self))), None):
# TB_NODE = from_node
if not isinstance(other, TypeOperator): if not isinstance(other, TypeOperator):
raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE) raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE)
if other.is_protocol and not self.is_protocol: if other.is_protocol and not self.is_protocol:
......
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