Commit e65a0e3f authored by Tom Niget's avatar Tom Niget

Enhance error display

parent b761813b
......@@ -4,6 +4,7 @@ import builtins
import inspect
from transpiler.consts import MAPPINGS
from transpiler.exceptions import CompileError
from transpiler.phases.desugar_with import DesugarWith
#from transpiler.phases import initial_pytype
from transpiler.phases.emit_cpp.file import FileVisitor
......@@ -51,9 +52,11 @@ def exception_hook(exc_type, exc_value, tb):
if last_node is not None:
print(f"In file \"{Fore.RESET}{last_file}{Fore.RED}\", line {last_node.lineno}")
print("\t" + highlight(ast.unparse(last_node)))
print()
print(f"{Fore.RED}Error:{Fore.RESET} {exc_value}")
print(inspect.cleandoc(exc_value.detail(last_node)))
if isinstance(exc_value, CompileError):
print(inspect.cleandoc(exc_value.detail(last_node)))
print()
sys.excepthook = exception_hook
......
......@@ -46,7 +46,8 @@ class ModuleVisitor(BlockVisitor):
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}')}"
yield f"auto {alias}("
yield "struct {"
yield f"auto operator()("
for i, argty in enumerate(fty.parameters):
if i != 0:
......@@ -66,6 +67,7 @@ class ModuleVisitor(BlockVisitor):
yield from self.visit(fty.return_type)
yield ">();"
yield "}"
yield f"}} {alias};"
def visit_ImportFrom(self, node: ast.ImportFrom) -> Iterable[str]:
if node.module_obj.is_python:
......@@ -74,7 +76,6 @@ class ModuleVisitor(BlockVisitor):
assert isinstance(fty, FunctionType)
yield from self.emit_python_func(node.module, alias.name, alias.asname or alias.name, fty)
yield "// python"
elif node.module in {"typon", "typing", "__future__"}:
yield ""
else:
......
......@@ -23,7 +23,7 @@ class UnresolvedTypeVariableError(CompileError):
As such, you need to give enough information to the compiler to infer the type of the function.
For example:
vvv this tells the compiler that {highlight('math.factorial')} returns an {highlight('int')}
↓↓↓ this tells the compiler that {highlight('math.factorial')} returns an {highlight('int')}
{highlight('res: int = math.factorial(5)')}"""
return """
This generally indicates the compiler was unable to infer the type of a variable or expression.
......
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