From 7a1077e3715e03a107a610347350c469ce06196c Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 21 Oct 2022 19:24:47 -0700 Subject: [PATCH] Added error handling --- main.py | 6 ++++++ tokens.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index e42d202..f5f4f05 100755 --- a/main.py +++ b/main.py @@ -81,6 +81,12 @@ while True: ("#FFFFFF", "\n") ])) continue + except tokens.ReductionError as e: + printf(FormattedText([ + ("#FF0000", f"{e.msg}"), + ("#FFFFFF", "\n") + ])) + continue # If this line defined a macro, print nothing. if isinstance(x, runner.MacroStatus): diff --git a/tokens.py b/tokens.py index a30d48f..267884b 100755 --- a/tokens.py +++ b/tokens.py @@ -1,6 +1,16 @@ from ast import Lambda import enum + +class ReductionError(Exception): + """ + Raised when we encounter an error while reducing. + + These should be caught and elegantly presented to the user. + """ + def __init__(self, msg: str): + self.msg = msg + class ReductionType(enum.Enum): MACRO_EXPAND = enum.auto() MACRO_TO_FREE = enum.auto() @@ -137,7 +147,7 @@ class macro(LambdaToken): ) elif not auto_free_vars: - raise NameError(f"Name {self.name} is not defined!") + raise ReductionError(f"Macro {self.name} is not defined") else: return ReductionStatus( @@ -273,7 +283,7 @@ class lambda_func(LambdaToken): if not ((placeholder is None) and (val is None)): if not binding_self and isinstance(self.input, macro): if self.input == placeholder: - raise NameError("Bound variable name conflict.") + raise ReductionError(f"Variable name conflict: \"{self.input.name}\"") # If this function's variables haven't been bound yet, # bind them BEFORE binding the outer function's.