worked on documentation and added an optional environment parameter
This commit is contained in:
@@ -5,7 +5,7 @@ from .tokenize import tokenize
|
|||||||
class ParseError(Exception):
|
class ParseError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def callIMP(chars):
|
def callIMP(chars, env=None):
|
||||||
"""
|
"""
|
||||||
The entry point for everyone who wants to call
|
The entry point for everyone who wants to call
|
||||||
IMP from within Python. The function either
|
IMP from within Python. The function either
|
||||||
@@ -13,6 +13,8 @@ def callIMP(chars):
|
|||||||
succeed.
|
succeed.
|
||||||
|
|
||||||
chars -- the string to evaluate
|
chars -- the string to evaluate
|
||||||
|
env -- a environment to bootstrap the imp
|
||||||
|
environment with
|
||||||
|
|
||||||
returns -- the IMP environment | None
|
returns -- the IMP environment | None
|
||||||
"""
|
"""
|
||||||
@@ -22,6 +24,6 @@ def callIMP(chars):
|
|||||||
parsed = parse(tokens)
|
parsed = parse(tokens)
|
||||||
if not parsed or not parsed.value: raise ParseError("tokens could not be parsed")
|
if not parsed or not parsed.value: raise ParseError("tokens could not be parsed")
|
||||||
ast = parsed.value
|
ast = parsed.value
|
||||||
env = {}
|
if not env: env = {}
|
||||||
ast.eval(env)
|
ast.eval(env)
|
||||||
return env
|
return env
|
||||||
|
@@ -213,7 +213,20 @@ def statements():
|
|||||||
return Exp(statement(), sep)
|
return Exp(statement(), sep)
|
||||||
|
|
||||||
def parser():
|
def parser():
|
||||||
|
"""
|
||||||
|
Builds a parser for Imp.
|
||||||
|
|
||||||
|
returns -- a parser for the Imp language
|
||||||
|
"""
|
||||||
return Phrase(statements())
|
return Phrase(statements())
|
||||||
|
|
||||||
def parse(tokens):
|
def parse(tokens):
|
||||||
|
"""
|
||||||
|
The parser entry point, takes a list of tokens and
|
||||||
|
transforms them into an AST.
|
||||||
|
|
||||||
|
tokens -- the token list
|
||||||
|
|
||||||
|
returns -- the AST
|
||||||
|
"""
|
||||||
return parser()(tokens, 0)
|
return parser()(tokens, 0)
|
||||||
|
Reference in New Issue
Block a user