Skip to content

Commit 3ecfb21

Browse files
committed
updated from Atlas
1 parent 6527037 commit 3ecfb21

File tree

6 files changed

+438
-114
lines changed

6 files changed

+438
-114
lines changed

18-with-match/lispy/py3.10/examples_test.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
Doctests for `parse`
33
--------------------
44
5-
# tag::PARSE_ATOM[]
5+
# tag::PARSE[]
66
>>> from lis import parse
77
>>> parse('1.5')
88
1.5
99
>>> parse('ni!')
1010
'ni!'
11-
12-
# end::PARSE_ATOM[]
13-
14-
# tag::PARSE_LIST[]
1511
>>> parse('(gcd 18 45)')
1612
['gcd', 18, 45]
1713
>>> parse('''
@@ -21,15 +17,15 @@
2117
... ''')
2218
['define', 'double', ['lambda', ['n'], ['*', 'n', 2]]]
2319
24-
# end::PARSE_LIST[]
20+
# end::PARSE[]
2521
2622
Doctest for `Environment`
2723
-------------------------
2824
2925
# tag::ENVIRONMENT[]
3026
>>> from lis import Environment
31-
>>> outer_env = {'a': 0, 'b': 1}
3227
>>> inner_env = {'a': 2}
28+
>>> outer_env = {'a': 0, 'b': 1}
3329
>>> env = Environment(inner_env, outer_env)
3430
>>> env['a'] = 111 # <1>
3531
>>> env['c'] = 222
@@ -64,11 +60,11 @@
6460
6561
6662
# tag::EVAL_QUOTE[]
67-
>>> evaluate(parse('(quote no-such-name)'), {})
63+
>>> evaluate(parse('(quote no-such-name)'), standard_env())
6864
'no-such-name'
69-
>>> evaluate(parse('(quote (99 bottles of beer))'), {})
65+
>>> evaluate(parse('(quote (99 bottles of beer))'), standard_env())
7066
[99, 'bottles', 'of', 'beer']
71-
>>> evaluate(parse('(quote (/ 10 0))'), {})
67+
>>> evaluate(parse('(quote (/ 10 0))'), standard_env())
7268
['/', 10, 0]
7369
7470
# end::EVAL_QUOTE[]
@@ -156,11 +152,12 @@ def test_factorial():
156152
(if (= n 0)
157153
m
158154
(gcd n (mod m n))))
159-
(gcd 18 45)
155+
(display (gcd 18 45))
160156
"""
161-
def test_gcd():
162-
got = run(gcd_src)
163-
assert got == 9
157+
def test_gcd(capsys):
158+
run(gcd_src)
159+
captured = capsys.readouterr()
160+
assert captured.out == '9\n'
164161

165162

166163
quicksort_src = """
@@ -216,7 +213,7 @@ def test_newton():
216213
(define inc (make-adder 1))
217214
(inc 99)
218215
"""
219-
def test_newton():
216+
def test_closure():
220217
got = run(closure_src)
221218
assert got == 100
222219

@@ -228,13 +225,15 @@ def test_newton():
228225
n)
229226
)
230227
(define counter (make-counter))
231-
(counter)
232-
(counter)
233-
(counter)
228+
(display (counter))
229+
(display (counter))
230+
(display (counter))
234231
"""
235-
def test_closure_with_change():
236-
got = run(closure_with_change_src)
237-
assert got == 3
232+
def test_closure_with_change(capsys):
233+
run(closure_with_change_src)
234+
captured = capsys.readouterr()
235+
assert captured.out == '1\n2\n3\n'
236+
238237

239238

240239
# tag::RUN_AVERAGER[]
@@ -256,4 +255,4 @@ def test_closure_with_change():
256255
def test_closure_averager():
257256
got = run(closure_averager_src)
258257
assert got == 12.0
259-
# end::RUN_AVERAGER[]
258+
# end::RUN_AVERAGER[]

18-with-match/lispy/py3.10/lis.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ def parse_atom(token: str) -> Atom:
5858
except ValueError:
5959
return Symbol(token)
6060

61+
6162
################ Global Environment
6263

6364
# tag::ENV_CLASS[]
64-
class Environment(ChainMap):
65+
class Environment(ChainMap[Symbol, Any]):
6566
"A ChainMap that allows changing an item in-place."
6667

6768
def change(self, key: Symbol, value: object) -> None:
@@ -119,11 +120,11 @@ def standard_env() -> Environment:
119120
################ Interaction: A REPL
120121

121122
# tag::REPL[]
122-
def repl() -> NoReturn:
123+
def repl(prompt: str = 'lis.py> ') -> NoReturn:
123124
"A prompt-read-eval-print loop."
124125
global_env = standard_env()
125126
while True:
126-
ast = parse(input('lis.py> '))
127+
ast = parse(input(prompt))
127128
val = evaluate(ast, global_env)
128129
if val is not None:
129130
print(lispstr(val))
@@ -140,7 +141,10 @@ def lispstr(exp: object) -> str:
140141
################ Evaluator
141142

142143
# tag::EVALUATE[]
143-
KEYWORDS = ['quote', 'if', 'lambda', 'define', 'set!']
144+
KEYWORDS = {'quote', 'if', 'lambda', 'define', 'set!'}
145+
146+
def is_keyword(s: Any) -> bool:
147+
return isinstance(s, Symbol) and s in KEYWORDS
144148

145149
def evaluate(exp: Expression, env: Environment) -> Any:
146150
"Evaluate an expression in an environment."
@@ -149,8 +153,6 @@ def evaluate(exp: Expression, env: Environment) -> Any:
149153
return x
150154
case Symbol(var):
151155
return env[var]
152-
case []:
153-
return []
154156
case ['quote', x]:
155157
return x
156158
case ['if', test, consequence, alternative]:
@@ -166,8 +168,8 @@ def evaluate(exp: Expression, env: Environment) -> Any:
166168
env[name] = Procedure(parms, body, env)
167169
case ['set!', Symbol(var), value_exp]:
168170
env.change(var, evaluate(value_exp, env))
169-
case [op, *args] if op not in KEYWORDS:
170-
proc = evaluate(op, env)
171+
case [func_exp, *args] if not is_keyword(func_exp):
172+
proc = evaluate(func_exp, env)
171173
values = [evaluate(arg, env) for arg in args]
172174
return proc(*values)
173175
case _:

18-with-match/lispy/py3.10/lis_test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ def test_evaluate(source: str, expected: Optional[Expression]) -> None:
7373
def std_env() -> Environment:
7474
return standard_env()
7575

76-
# tests for each of the cases in evaluate
76+
# tests for cases in evaluate
7777

7878
def test_evaluate_variable() -> None:
79-
env: Environment = dict(x=10)
79+
env = Environment({'x': 10})
8080
source = 'x'
8181
expected = 10
8282
got = evaluate(parse(source), env)
@@ -168,8 +168,6 @@ def test_invocation_user_procedure(std_env: Environment) -> None:
168168
assert got == 22
169169

170170

171-
###################################### for py3.10/lis.py only
172-
173171
def test_define_function(std_env: Environment) -> None:
174172
source = '(define (max a b) (if (>= a b) a b))'
175173
got = evaluate(parse(source), std_env)

0 commit comments

Comments
 (0)