Added error handling

master
Mark 2022-10-21 19:24:47 -07:00
parent ef19fc42a6
commit 7a1077e371
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 18 additions and 2 deletions

View File

@ -81,6 +81,12 @@ while True:
("#FFFFFF", "\n") ("#FFFFFF", "\n")
])) ]))
continue continue
except tokens.ReductionError as e:
printf(FormattedText([
("#FF0000", f"{e.msg}"),
("#FFFFFF", "\n")
]))
continue
# If this line defined a macro, print nothing. # If this line defined a macro, print nothing.
if isinstance(x, runner.MacroStatus): if isinstance(x, runner.MacroStatus):

View File

@ -1,6 +1,16 @@
from ast import Lambda from ast import Lambda
import enum 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): class ReductionType(enum.Enum):
MACRO_EXPAND = enum.auto() MACRO_EXPAND = enum.auto()
MACRO_TO_FREE = enum.auto() MACRO_TO_FREE = enum.auto()
@ -137,7 +147,7 @@ class macro(LambdaToken):
) )
elif not auto_free_vars: elif not auto_free_vars:
raise NameError(f"Name {self.name} is not defined!") raise ReductionError(f"Macro {self.name} is not defined")
else: else:
return ReductionStatus( return ReductionStatus(
@ -273,7 +283,7 @@ class lambda_func(LambdaToken):
if not ((placeholder is None) and (val is None)): if not ((placeholder is None) and (val is None)):
if not binding_self and isinstance(self.input, macro): if not binding_self and isinstance(self.input, macro):
if self.input == placeholder: 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, # If this function's variables haven't been bound yet,
# bind them BEFORE binding the outer function's. # bind them BEFORE binding the outer function's.