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:
parent
4d625fff20
commit
9e03e0e6ec
@ -456,7 +456,8 @@ class HyASTCompiler(object):
|
|||||||
"arguments or one &key argument")
|
"arguments or one &key argument")
|
||||||
# As you can see, Python has a funny way of
|
# As you can see, Python has a funny way of
|
||||||
# defining keyword arguments.
|
# defining keyword arguments.
|
||||||
for k, v in expr.items():
|
it = iter(expr)
|
||||||
|
for k, v in zip(it, it):
|
||||||
args.append(k)
|
args.append(k)
|
||||||
ret += self.compile(v)
|
ret += self.compile(v)
|
||||||
defaults.append(ret.force_expr)
|
defaults.append(ret.force_expr)
|
||||||
@ -543,7 +544,7 @@ class HyASTCompiler(object):
|
|||||||
name = form.__class__.__name__
|
name = form.__class__.__name__
|
||||||
imports = set([name])
|
imports = set([name])
|
||||||
|
|
||||||
if isinstance(form, HyList):
|
if isinstance(form, (HyList, HyDict)):
|
||||||
if not form:
|
if not form:
|
||||||
contents = HyList()
|
contents = HyList()
|
||||||
else:
|
else:
|
||||||
@ -1685,7 +1686,7 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
@builds(HyDict)
|
@builds(HyDict)
|
||||||
def compile_dict(self, m):
|
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,
|
ret += ast.Dict(lineno=m.start_line,
|
||||||
col_offset=m.start_column,
|
col_offset=m.start_column,
|
||||||
|
@ -228,9 +228,7 @@ class Dict(ListeyThing):
|
|||||||
|
|
||||||
def exit(self):
|
def exit(self):
|
||||||
self.commit()
|
self.commit()
|
||||||
it = iter(self.nodes)
|
self.result = HyDict(self.nodes)
|
||||||
result = dict(zip(it, it))
|
|
||||||
self.result = HyDict(result)
|
|
||||||
|
|
||||||
end_char = "}"
|
end_char = "}"
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
from hy.models.expression import HyExpression
|
from hy.models.expression import HyExpression
|
||||||
from hy.models.string import HyString
|
from hy.models.string import HyString
|
||||||
from hy.models.dict import HyDict
|
|
||||||
from hy.models.list import HyList
|
from hy.models.list import HyList
|
||||||
|
|
||||||
_hy_macros = {}
|
_hy_macros = {}
|
||||||
@ -51,13 +50,8 @@ def process(tree):
|
|||||||
ntree.replace(tree)
|
ntree.replace(tree)
|
||||||
return ntree
|
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):
|
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.
|
# flake8 thinks we're redefining from 52.
|
||||||
obj.replace(tree)
|
obj.replace(tree)
|
||||||
return obj
|
return obj
|
||||||
|
@ -18,17 +18,13 @@
|
|||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
# DEALINGS IN THE SOFTWARE.
|
# 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):
|
def __repr__(self):
|
||||||
for x in self:
|
return "{%s}" % (" ".join([repr(x) for x in self]))
|
||||||
self[x].replace(other)
|
|
||||||
x.replace(other)
|
|
||||||
|
|
||||||
HyObject.replace(self, other)
|
|
||||||
|
@ -185,15 +185,12 @@ def test_lex_line_counting_multi_inner():
|
|||||||
def test_dicts():
|
def test_dicts():
|
||||||
""" Ensure that we can tokenize a dict. """
|
""" Ensure that we can tokenize a dict. """
|
||||||
objs = tokenize("{foo bar bar baz}")
|
objs = tokenize("{foo bar bar baz}")
|
||||||
assert objs == [HyDict({
|
assert objs == [HyDict(["foo", "bar", "bar", "baz"])]
|
||||||
"foo": "bar",
|
|
||||||
"bar": "baz"
|
|
||||||
})]
|
|
||||||
|
|
||||||
objs = tokenize("(bar {foo bar bar baz})")
|
objs = tokenize("(bar {foo bar bar baz})")
|
||||||
assert objs == [HyExpression([HySymbol("bar"),
|
assert objs == [HyExpression([HySymbol("bar"),
|
||||||
HyDict({"foo": "bar",
|
HyDict(["foo", "bar",
|
||||||
"bar": "baz"})])]
|
"bar", "baz"])])]
|
||||||
|
|
||||||
|
|
||||||
def test_nospace():
|
def test_nospace():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user