30 lines
840 B
Python
30 lines
840 B
Python
"""The interpreter"""
|
|
from .parser import parse
|
|
from .tokenize import tokenize
|
|
|
|
class ParseError(Exception):
|
|
pass
|
|
|
|
def callIMP(chars, env=None):
|
|
"""
|
|
The entry point for everyone who wants to call
|
|
IMP from within Python. The function either
|
|
returns the value or None, if the call did not
|
|
succeed.
|
|
|
|
chars -- the string to evaluate
|
|
env -- a environment to bootstrap the imp
|
|
environment with
|
|
|
|
returns -- the IMP environment | None
|
|
"""
|
|
if not chars: return None
|
|
tokens = tokenize(chars)
|
|
if not tokens: raise ParseError("tokens could not be generated")
|
|
parsed = parse(tokens)
|
|
if not parsed or not parsed.value: raise ParseError("tokens could not be parsed")
|
|
ast = parsed.value
|
|
if not env: env = {}
|
|
ast.eval(env)
|
|
return env
|