-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First real progress on #4, after extensive prep/refactoring.
- Loading branch information
1 parent
d3bf45f
commit 8663847
Showing
5 changed files
with
90 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Env(object): | ||
""" | ||
Provide a nested series of environments to look things up in. If | ||
it's not found in the current environment, look in the parent, or | ||
its parent(s). The idea is explained well in SICP, Chapter 4. | ||
""" | ||
def __init__(self, parent=None): | ||
self.__values = {} | ||
self.__parent = parent | ||
|
||
def __setitem__(self, k, v): | ||
self.__values[k] = v | ||
|
||
def __getitem__(self, k): | ||
if k in self.__values: | ||
return self.__values[k] | ||
elif self.__parent is not None: | ||
return self.__parent[k] | ||
|
||
def copy(self): | ||
ret = Env(self.__parent) | ||
for k, v in self.__values.items(): | ||
ret[k] = v | ||
return ret | ||
|
||
def __contains__(self, k): | ||
if k in self.__values: | ||
return True | ||
elif self.__parent: | ||
return k in self.__parent | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from smallscheme.dtypes import * | ||
from smallscheme.env import Env | ||
|
||
def test_env(): | ||
outer = Env() | ||
outer['a'] = int_(0) | ||
assert 'a' in outer | ||
assert 'b' not in outer | ||
assert outer['a'] == int_(0) | ||
|
||
inner = Env(outer) | ||
assert 'a' in inner | ||
assert 'b' not in inner | ||
assert inner['a'] == int_(0) | ||
|
||
inner['c'] = int_(1) | ||
assert 'c' in inner | ||
assert 'c' not in outer | ||
assert inner['c'] == int_(1) | ||
|
||
outer['d'] = int_(2) | ||
assert 'd' in outer | ||
assert 'd' in inner | ||
assert inner['d'] == int_(2) | ||
|
||
# Overriding higher-level (global) env with | ||
# inner scope: | ||
inner['a'] = int_(3) | ||
assert 'a' in outer | ||
assert 'a' in inner | ||
assert inner['a'] == int_(3) | ||
assert outer['a'] == int_(0) |