From 3933ce4736a1ff85a5f195f8d946668faccc1653 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 23 May 2024 14:39:57 +0200 Subject: [PATCH] feat: improve error handling in interpreter Introduced better error handling in the Deuthon interpreter loop by wrapping the code execution in a try-except block to catch and print exceptions using `traceback`. This change also updates the interpreter to only print output when `eval` returns a non-None result, enhancing usability and feedback for the user. The addition of `traceback` provides users with more insightful error messages, helping to debug incorrect German code inputs more effectively. --- src/deuthon/interpreter.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/deuthon/interpreter.py b/src/deuthon/interpreter.py index 4b285ec..873b3c2 100644 --- a/src/deuthon/interpreter.py +++ b/src/deuthon/interpreter.py @@ -1,11 +1,19 @@ from .transformer import parse_german_code +import traceback + def interpreter(): while True: try: - german_code = input('>>> ') + german_code = input(">>> ") except EOFError: break - python_code = parse_german_code(german_code) - exec(python_code) \ No newline at end of file + try: + python_code = parse_german_code(german_code) + result = eval(python_code, globals()) + + if result is not None: + print(result) + except Exception: + print(traceback.format_exc())