sync from Atlas
This commit is contained in:
@@ -2,16 +2,12 @@
|
||||
Doctests for `parse`
|
||||
--------------------
|
||||
|
||||
# tag::PARSE_ATOM[]
|
||||
# tag::PARSE[]
|
||||
>>> from lis import parse
|
||||
>>> parse('1.5')
|
||||
1.5
|
||||
>>> parse('ni!')
|
||||
'ni!'
|
||||
|
||||
# end::PARSE_ATOM[]
|
||||
|
||||
# tag::PARSE_LIST[]
|
||||
>>> parse('(gcd 18 45)')
|
||||
['gcd', 18, 45]
|
||||
>>> parse('''
|
||||
@@ -21,15 +17,15 @@ Doctests for `parse`
|
||||
... ''')
|
||||
['define', 'double', ['lambda', ['n'], ['*', 'n', 2]]]
|
||||
|
||||
# end::PARSE_LIST[]
|
||||
# end::PARSE[]
|
||||
|
||||
Doctest for `Environment`
|
||||
-------------------------
|
||||
|
||||
# tag::ENVIRONMENT[]
|
||||
>>> from lis import Environment
|
||||
>>> outer_env = {'a': 0, 'b': 1}
|
||||
>>> inner_env = {'a': 2}
|
||||
>>> outer_env = {'a': 0, 'b': 1}
|
||||
>>> env = Environment(inner_env, outer_env)
|
||||
>>> env['a'] = 111 # <1>
|
||||
>>> env['c'] = 222
|
||||
@@ -64,11 +60,11 @@ KeyError: 'ni!'
|
||||
|
||||
|
||||
# tag::EVAL_QUOTE[]
|
||||
>>> evaluate(parse('(quote no-such-name)'), {})
|
||||
>>> evaluate(parse('(quote no-such-name)'), standard_env())
|
||||
'no-such-name'
|
||||
>>> evaluate(parse('(quote (99 bottles of beer))'), {})
|
||||
>>> evaluate(parse('(quote (99 bottles of beer))'), standard_env())
|
||||
[99, 'bottles', 'of', 'beer']
|
||||
>>> evaluate(parse('(quote (/ 10 0))'), {})
|
||||
>>> evaluate(parse('(quote (/ 10 0))'), standard_env())
|
||||
['/', 10, 0]
|
||||
|
||||
# end::EVAL_QUOTE[]
|
||||
@@ -156,11 +152,12 @@ gcd_src = """
|
||||
(if (= n 0)
|
||||
m
|
||||
(gcd n (mod m n))))
|
||||
(gcd 18 45)
|
||||
(display (gcd 18 45))
|
||||
"""
|
||||
def test_gcd():
|
||||
got = run(gcd_src)
|
||||
assert got == 9
|
||||
def test_gcd(capsys):
|
||||
run(gcd_src)
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == '9\n'
|
||||
|
||||
|
||||
quicksort_src = """
|
||||
@@ -216,7 +213,7 @@ closure_src = """
|
||||
(define inc (make-adder 1))
|
||||
(inc 99)
|
||||
"""
|
||||
def test_newton():
|
||||
def test_closure():
|
||||
got = run(closure_src)
|
||||
assert got == 100
|
||||
|
||||
@@ -228,13 +225,15 @@ closure_with_change_src = """
|
||||
n)
|
||||
)
|
||||
(define counter (make-counter))
|
||||
(counter)
|
||||
(counter)
|
||||
(counter)
|
||||
(display (counter))
|
||||
(display (counter))
|
||||
(display (counter))
|
||||
"""
|
||||
def test_closure_with_change():
|
||||
got = run(closure_with_change_src)
|
||||
assert got == 3
|
||||
def test_closure_with_change(capsys):
|
||||
run(closure_with_change_src)
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == '1\n2\n3\n'
|
||||
|
||||
|
||||
|
||||
# tag::RUN_AVERAGER[]
|
||||
@@ -256,4 +255,4 @@ closure_averager_src = """
|
||||
def test_closure_averager():
|
||||
got = run(closure_averager_src)
|
||||
assert got == 12.0
|
||||
# end::RUN_AVERAGER[]
|
||||
# end::RUN_AVERAGER[]
|
||||
|
||||
@@ -58,10 +58,11 @@ def parse_atom(token: str) -> Atom:
|
||||
except ValueError:
|
||||
return Symbol(token)
|
||||
|
||||
|
||||
################ Global Environment
|
||||
|
||||
# tag::ENV_CLASS[]
|
||||
class Environment(ChainMap):
|
||||
class Environment(ChainMap[Symbol, Any]):
|
||||
"A ChainMap that allows changing an item in-place."
|
||||
|
||||
def change(self, key: Symbol, value: object) -> None:
|
||||
@@ -73,7 +74,6 @@ class Environment(ChainMap):
|
||||
raise KeyError(key)
|
||||
# end::ENV_CLASS[]
|
||||
|
||||
|
||||
def standard_env() -> Environment:
|
||||
"An environment with some Scheme standard procedures."
|
||||
env = Environment()
|
||||
@@ -119,11 +119,11 @@ def standard_env() -> Environment:
|
||||
################ Interaction: A REPL
|
||||
|
||||
# tag::REPL[]
|
||||
def repl() -> NoReturn:
|
||||
def repl(prompt: str = 'lis.py> ') -> NoReturn:
|
||||
"A prompt-read-eval-print loop."
|
||||
global_env = standard_env()
|
||||
while True:
|
||||
ast = parse(input('lis.py> '))
|
||||
ast = parse(input(prompt))
|
||||
val = evaluate(ast, global_env)
|
||||
if val is not None:
|
||||
print(lispstr(val))
|
||||
@@ -149,8 +149,6 @@ def evaluate(exp: Expression, env: Environment) -> Any:
|
||||
return x
|
||||
case Symbol(var):
|
||||
return env[var]
|
||||
case []:
|
||||
return []
|
||||
case ['quote', x]:
|
||||
return x
|
||||
case ['if', test, consequence, alternative]:
|
||||
@@ -166,8 +164,8 @@ def evaluate(exp: Expression, env: Environment) -> Any:
|
||||
env[name] = Procedure(parms, body, env)
|
||||
case ['set!', Symbol(var), value_exp]:
|
||||
env.change(var, evaluate(value_exp, env))
|
||||
case [op, *args] if op not in KEYWORDS:
|
||||
proc = evaluate(op, env)
|
||||
case [func_exp, *args] if func_exp not in KEYWORDS:
|
||||
proc = evaluate(func_exp, env)
|
||||
values = [evaluate(arg, env) for arg in args]
|
||||
return proc(*values)
|
||||
case _:
|
||||
|
||||
@@ -73,10 +73,10 @@ def test_evaluate(source: str, expected: Optional[Expression]) -> None:
|
||||
def std_env() -> Environment:
|
||||
return standard_env()
|
||||
|
||||
# tests for each of the cases in evaluate
|
||||
# tests for cases in evaluate
|
||||
|
||||
def test_evaluate_variable() -> None:
|
||||
env: Environment = dict(x=10)
|
||||
env = Environment({'x': 10})
|
||||
source = 'x'
|
||||
expected = 10
|
||||
got = evaluate(parse(source), env)
|
||||
@@ -168,8 +168,6 @@ def test_invocation_user_procedure(std_env: Environment) -> None:
|
||||
assert got == 22
|
||||
|
||||
|
||||
###################################### for py3.10/lis.py only
|
||||
|
||||
def test_define_function(std_env: Environment) -> None:
|
||||
source = '(define (max a b) (if (>= a b) a b))'
|
||||
got = evaluate(parse(source), std_env)
|
||||
|
||||
Reference in New Issue
Block a user