Initial commit - fix problems & refactor code
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
from core.parser.parse_expression import parseExpression
|
||||
from core.evaluator.naively_evaluate import naivelyEvaluate
|
||||
from core.exceptions import error
|
||||
|
||||
from core.environment import ENV
|
||||
|
||||
from traceback import print_exception
|
||||
|
||||
def parseEvaluate(source: str, *, load: bool = False) -> str:
|
||||
results: list[str] = []
|
||||
s = source
|
||||
while s:
|
||||
try:
|
||||
expr, s = parseExpression(s)
|
||||
value = naivelyEvaluate(expr, ENV, True)
|
||||
results.append(repr(value))
|
||||
except error as e:
|
||||
# Unwrap nested causes so that the original error message is shown
|
||||
cause = e
|
||||
while cause.__cause__ and isinstance(cause.__cause__, error):
|
||||
cause = cause.__cause__
|
||||
print(cause)
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
return "interrupted\n"
|
||||
except Exception as exc:
|
||||
print_exception(exc)
|
||||
break
|
||||
return ' '.join(results)
|
||||
Reference in New Issue
Block a user