list flattener
This commit is contained in:
parent
3cd35e3ca6
commit
b2406a9d72
@ -27,6 +27,8 @@ from hy.models.symbol import HySymbol
|
|||||||
from hy.models.list import HyList
|
from hy.models.list import HyList
|
||||||
from hy.models.dict import HyDict
|
from hy.models.dict import HyDict
|
||||||
|
|
||||||
|
from hy.util import flatten_literal_list
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -63,23 +65,18 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
def _mangle_branch(self, tree):
|
def _mangle_branch(self, tree):
|
||||||
ret = []
|
ret = []
|
||||||
|
tree = list(flatten_literal_list(tree))
|
||||||
tree.reverse()
|
tree.reverse()
|
||||||
|
|
||||||
if self.returnable and len(tree) > 0:
|
if self.returnable and len(tree) > 0:
|
||||||
el = tree[0]
|
el = tree[0]
|
||||||
if not isinstance(el, (ast.stmt, list)):
|
if not isinstance(el, ast.stmt):
|
||||||
el = tree.pop(0)
|
el = tree.pop(0)
|
||||||
ret.append(ast.Return(value=el,
|
ret.append(ast.Return(value=el,
|
||||||
lineno=el.lineno,
|
lineno=el.lineno,
|
||||||
col_offset=el.col_offset))
|
col_offset=el.col_offset))
|
||||||
|
|
||||||
for el in tree:
|
for el in tree:
|
||||||
if type(el) == list:
|
|
||||||
blob = self._mangle_branch(el)
|
|
||||||
blob.reverse()
|
|
||||||
ret += blob
|
|
||||||
continue
|
|
||||||
|
|
||||||
if isinstance(el, ast.stmt):
|
if isinstance(el, ast.stmt):
|
||||||
ret.append(el)
|
ret.append(el)
|
||||||
continue
|
continue
|
||||||
|
28
hy/util.py
Normal file
28
hy/util.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
# copy of this software and associated documentation files (the "Software"),
|
||||||
|
# to deal in the Software without restriction, including without limitation
|
||||||
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
# and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
# DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
def flatten_literal_list(entry):
|
||||||
|
for e in entry:
|
||||||
|
if type(e) == list:
|
||||||
|
for x in flatten_literal_list(e):
|
||||||
|
yield x # needs more yield-from
|
||||||
|
else:
|
||||||
|
yield e
|
@ -223,15 +223,17 @@
|
|||||||
(with-as (open "README.md" "r") fd
|
(with-as (open "README.md" "r") fd
|
||||||
(pass)))
|
(pass)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-for-doodle []
|
(defn test-for-doodle []
|
||||||
"NATIVE: test for-do"
|
"NATIVE: test for-do"
|
||||||
(setf (, x y) (, 0 0))
|
(do (do (do (do (do (do (do (do (do (setf (, x y) (, 0 0)))))))))))
|
||||||
(foreach [- [1 2]]
|
(foreach [- [1 2]]
|
||||||
(do
|
(do
|
||||||
(setf x (+ x 1))
|
(setf x (+ x 1))
|
||||||
(setf y (+ y 1))))
|
(setf y (+ y 1))))
|
||||||
(assert (= y x 2)))
|
(assert (= y x 2)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-comprehensions []
|
(defn test-comprehensions []
|
||||||
"NATIVE: test list comprehensions"
|
"NATIVE: test list comprehensions"
|
||||||
(assert (= (list-comp (* x 2) (x (range 2))) [0 2]))
|
(assert (= (list-comp (* x 2) (x (range 2))) [0 2]))
|
||||||
@ -241,6 +243,7 @@
|
|||||||
(assert (= (list-comp (, x y) (x (range 2) y (range 2)))
|
(assert (= (list-comp (, x y) (x (range 2) y (range 2)))
|
||||||
[(, 0 0) (, 0 1) (, 1 0) (, 1 1)])))
|
[(, 0 0) (, 0 1) (, 1 0) (, 1 1)])))
|
||||||
|
|
||||||
|
|
||||||
(defn test-defn-order []
|
(defn test-defn-order []
|
||||||
"NATIVE: test defn evaluation order"
|
"NATIVE: test defn evaluation order"
|
||||||
(setv acc [])
|
(setv acc [])
|
||||||
@ -251,12 +254,14 @@
|
|||||||
(my-fun)
|
(my-fun)
|
||||||
(assert (= acc ["Foo" "Bar" "Baz"])))
|
(assert (= acc ["Foo" "Bar" "Baz"])))
|
||||||
|
|
||||||
|
|
||||||
(defn test-defn-return []
|
(defn test-defn-return []
|
||||||
"NATIVE: test defn return"
|
"NATIVE: test defn return"
|
||||||
(defn my-fun [x]
|
(defn my-fun [x]
|
||||||
(+ x 1))
|
(+ x 1))
|
||||||
(assert (= 43 (my-fun 42))))
|
(assert (= 43 (my-fun 42))))
|
||||||
|
|
||||||
|
|
||||||
(defn test-defn-do []
|
(defn test-defn-do []
|
||||||
"NATIVE: test defn evaluation order with do"
|
"NATIVE: test defn evaluation order with do"
|
||||||
(setv acc [])
|
(setv acc [])
|
||||||
@ -268,6 +273,7 @@
|
|||||||
(my-fun)
|
(my-fun)
|
||||||
(assert (= acc ["Foo" "Bar" "Baz"])))
|
(assert (= acc ["Foo" "Bar" "Baz"])))
|
||||||
|
|
||||||
|
|
||||||
(defn test-defn-do-return []
|
(defn test-defn-do-return []
|
||||||
"NATIVE: test defn return with do"
|
"NATIVE: test defn return with do"
|
||||||
(defn my-fun [x]
|
(defn my-fun [x]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user