Modify HyDict semantics to allow nesting expressions

HyDicts are now HyLists, that get compiled down to dicts
only by the compiler.
This commit is contained in:
Nicolas Dandrimont 2013-05-14 11:39:20 +02:00
parent 4d625fff20
commit 9e03e0e6ec
5 changed files with 14 additions and 28 deletions

View File

@ -456,7 +456,8 @@ class HyASTCompiler(object):
"arguments or one &key argument")
# As you can see, Python has a funny way of
# defining keyword arguments.
for k, v in expr.items():
it = iter(expr)
for k, v in zip(it, it):
args.append(k)
ret += self.compile(v)
defaults.append(ret.force_expr)
@ -543,7 +544,7 @@ class HyASTCompiler(object):
name = form.__class__.__name__
imports = set([name])
if isinstance(form, HyList):
if isinstance(form, (HyList, HyDict)):
if not form:
contents = HyList()
else:
@ -1685,7 +1686,7 @@ class HyASTCompiler(object):
@builds(HyDict)
def compile_dict(self, m):
keyvalues, ret = self._compile_collect(sum(m.items(), ()))
keyvalues, ret = self._compile_collect(m)
ret += ast.Dict(lineno=m.start_line,
col_offset=m.start_column,

View File

@ -228,9 +228,7 @@ class Dict(ListeyThing):
def exit(self):
self.commit()
it = iter(self.nodes)
result = dict(zip(it, it))
self.result = HyDict(result)
self.result = HyDict(self.nodes)
end_char = "}"

View File

@ -20,7 +20,6 @@
from hy.models.expression import HyExpression
from hy.models.string import HyString
from hy.models.dict import HyDict
from hy.models.list import HyList
_hy_macros = {}
@ -51,13 +50,8 @@ def process(tree):
ntree.replace(tree)
return ntree
if isinstance(tree, HyDict):
obj = HyDict(dict((process(x), process(tree[x])) for x in tree))
obj.replace(tree)
return obj
if isinstance(tree, HyList):
obj = HyList([process(x) for x in tree]) # NOQA
obj = tree.__class__([process(x) for x in tree]) # NOQA
# flake8 thinks we're redefining from 52.
obj.replace(tree)
return obj

View File

@ -18,17 +18,13 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from hy.models import HyObject
from hy.models.list import HyList
class HyDict(HyObject, dict):
class HyDict(HyList):
"""
HyDict (just a dict)
HyDict (just a representation of a dict)
"""
def replace(self, other):
for x in self:
self[x].replace(other)
x.replace(other)
HyObject.replace(self, other)
def __repr__(self):
return "{%s}" % (" ".join([repr(x) for x in self]))

View File

@ -185,15 +185,12 @@ def test_lex_line_counting_multi_inner():
def test_dicts():
""" Ensure that we can tokenize a dict. """
objs = tokenize("{foo bar bar baz}")
assert objs == [HyDict({
"foo": "bar",
"bar": "baz"
})]
assert objs == [HyDict(["foo", "bar", "bar", "baz"])]
objs = tokenize("(bar {foo bar bar baz})")
assert objs == [HyExpression([HySymbol("bar"),
HyDict({"foo": "bar",
"bar": "baz"})])]
HyDict(["foo", "bar",
"bar", "baz"])])]
def test_nospace():