Files
pylisp/core/parse_evaluate.py

29 lines
956 B
Python

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)