|
79 | 79 | Binding,
|
80 | 80 | NodeEnv,
|
81 | 81 | Catch,
|
| 82 | + PyList, |
| 83 | + PySet, |
| 84 | + PyTuple, |
| 85 | + PyDict, |
82 | 86 | )
|
83 | 87 | from basilisp.lang.runtime import Var
|
84 | 88 | from basilisp.lang.typing import LispForm
|
@@ -1752,6 +1756,51 @@ def _vec_to_py_ast(
|
1752 | 1756 | )
|
1753 | 1757 |
|
1754 | 1758 |
|
| 1759 | +##################### |
| 1760 | +# Python Collections |
| 1761 | +##################### |
| 1762 | + |
| 1763 | + |
| 1764 | +@_with_ast_loc |
| 1765 | +def _py_dict_to_py_ast(ctx: GeneratorContext, node: PyDict) -> GeneratedPyAST: |
| 1766 | + assert node.op == NodeOp.PY_DICT |
| 1767 | + |
| 1768 | + key_deps, keys = _chain_py_ast(*map(partial(gen_py_ast, ctx), node.keys)) |
| 1769 | + val_deps, vals = _chain_py_ast(*map(partial(gen_py_ast, ctx), node.vals)) |
| 1770 | + return GeneratedPyAST( |
| 1771 | + node=ast.Dict(keys=list(keys), values=list(vals)), |
| 1772 | + dependencies=list(chain(key_deps, val_deps)), |
| 1773 | + ) |
| 1774 | + |
| 1775 | + |
| 1776 | +@_with_ast_loc |
| 1777 | +def _py_list_to_py_ast(ctx: GeneratorContext, node: PyList) -> GeneratedPyAST: |
| 1778 | + assert node.op == NodeOp.PY_LIST |
| 1779 | + |
| 1780 | + elem_deps, elems = _chain_py_ast(*map(partial(gen_py_ast, ctx), node.items)) |
| 1781 | + return GeneratedPyAST( |
| 1782 | + node=ast.List(elts=list(elems), ctx=ast.Load()), dependencies=list(elem_deps) |
| 1783 | + ) |
| 1784 | + |
| 1785 | + |
| 1786 | +@_with_ast_loc |
| 1787 | +def _py_set_to_py_ast(ctx: GeneratorContext, node: PySet) -> GeneratedPyAST: |
| 1788 | + assert node.op == NodeOp.PY_SET |
| 1789 | + |
| 1790 | + elem_deps, elems = _chain_py_ast(*map(partial(gen_py_ast, ctx), node.items)) |
| 1791 | + return GeneratedPyAST(node=ast.Set(elts=list(elems)), dependencies=list(elem_deps)) |
| 1792 | + |
| 1793 | + |
| 1794 | +@_with_ast_loc |
| 1795 | +def _py_tuple_to_py_ast(ctx: GeneratorContext, node: PyTuple) -> GeneratedPyAST: |
| 1796 | + assert node.op == NodeOp.PY_TUPLE |
| 1797 | + |
| 1798 | + elem_deps, elems = _chain_py_ast(*map(partial(gen_py_ast, ctx), node.items)) |
| 1799 | + return GeneratedPyAST( |
| 1800 | + node=ast.Tuple(elts=list(elems), ctx=ast.Load()), dependencies=list(elem_deps) |
| 1801 | + ) |
| 1802 | + |
| 1803 | + |
1755 | 1804 | ############
|
1756 | 1805 | # With Meta
|
1757 | 1806 | ############
|
@@ -1871,6 +1920,34 @@ def _uuid_to_py_ast(_: GeneratorContext, form: uuid.UUID) -> ast.AST:
|
1871 | 1920 | return ast.Call(func=_NEW_UUID_FN_NAME, args=[ast.Str(str(form))], keywords=[])
|
1872 | 1921 |
|
1873 | 1922 |
|
| 1923 | +def _const_py_dict_to_py_ast(ctx: GeneratorContext, node: dict) -> GeneratedPyAST: |
| 1924 | + key_deps, keys = _chain_py_ast(*_collection_literal_to_py_ast(ctx, node.keys())) |
| 1925 | + val_deps, vals = _chain_py_ast(*_collection_literal_to_py_ast(ctx, node.values())) |
| 1926 | + return GeneratedPyAST( |
| 1927 | + node=ast.Dict(keys=list(keys), values=list(vals)), |
| 1928 | + dependencies=list(chain(key_deps, val_deps)), |
| 1929 | + ) |
| 1930 | + |
| 1931 | + |
| 1932 | +def _const_py_list_to_py_ast(ctx: GeneratorContext, node: list) -> GeneratedPyAST: |
| 1933 | + elem_deps, elems = _chain_py_ast(*_collection_literal_to_py_ast(ctx, node)) |
| 1934 | + return GeneratedPyAST( |
| 1935 | + node=ast.List(elts=list(elems), ctx=ast.Load()), dependencies=list(elem_deps) |
| 1936 | + ) |
| 1937 | + |
| 1938 | + |
| 1939 | +def _const_py_set_to_py_ast(ctx: GeneratorContext, node: set) -> GeneratedPyAST: |
| 1940 | + elem_deps, elems = _chain_py_ast(*_collection_literal_to_py_ast(ctx, node)) |
| 1941 | + return GeneratedPyAST(node=ast.Set(elts=list(elems)), dependencies=list(elem_deps)) |
| 1942 | + |
| 1943 | + |
| 1944 | +def _const_py_tuple_to_py_ast(ctx: GeneratorContext, node: tuple) -> GeneratedPyAST: |
| 1945 | + elem_deps, elems = _chain_py_ast(*_collection_literal_to_py_ast(ctx, node)) |
| 1946 | + return GeneratedPyAST( |
| 1947 | + node=ast.Tuple(elts=list(elems), ctx=ast.Load()), dependencies=list(elem_deps) |
| 1948 | + ) |
| 1949 | + |
| 1950 | + |
1874 | 1951 | def _const_map_to_py_ast(ctx: GeneratorContext, form: lmap.Map) -> GeneratedPyAST:
|
1875 | 1952 | key_deps, keys = _chain_py_ast(*_collection_literal_to_py_ast(ctx, form.keys()))
|
1876 | 1953 | val_deps, vals = _chain_py_ast(*_collection_literal_to_py_ast(ctx, form.values()))
|
@@ -1951,17 +2028,21 @@ def _const_vec_to_py_ast(ctx: GeneratorContext, form: vec.Vector) -> GeneratedPy
|
1951 | 2028 | complex: _num_to_py_ast,
|
1952 | 2029 | datetime: _inst_to_py_ast,
|
1953 | 2030 | Decimal: _decimal_to_py_ast,
|
| 2031 | + dict: _const_py_dict_to_py_ast, |
1954 | 2032 | float: _num_to_py_ast,
|
1955 | 2033 | Fraction: _fraction_to_py_ast,
|
1956 | 2034 | int: _num_to_py_ast,
|
1957 | 2035 | kw.Keyword: _kw_to_py_ast,
|
| 2036 | + list: _const_py_list_to_py_ast, |
1958 | 2037 | llist.List: _const_seq_to_py_ast,
|
1959 | 2038 | lmap.Map: _const_map_to_py_ast,
|
1960 | 2039 | lset.Set: _const_set_to_py_ast,
|
1961 | 2040 | lseq.Seq: _const_seq_to_py_ast,
|
1962 | 2041 | type(re.compile("")): _regex_to_py_ast,
|
| 2042 | + set: _const_py_set_to_py_ast, |
1963 | 2043 | sym.Symbol: _const_sym_to_py_ast,
|
1964 | 2044 | str: _str_to_py_ast,
|
| 2045 | + tuple: _const_py_tuple_to_py_ast, |
1965 | 2046 | type(None): _name_const_to_py_ast,
|
1966 | 2047 | uuid.UUID: _uuid_to_py_ast,
|
1967 | 2048 | vec.Vector: _const_vec_to_py_ast,
|
@@ -2008,6 +2089,10 @@ def _collection_literal_to_py_ast(
|
2008 | 2089 | ConstType.STRING: _str_to_py_ast,
|
2009 | 2090 | ConstType.NIL: _name_const_to_py_ast,
|
2010 | 2091 | ConstType.UUID: _uuid_to_py_ast,
|
| 2092 | + ConstType.PY_DICT: _const_py_dict_to_py_ast, |
| 2093 | + ConstType.PY_LIST: _const_py_list_to_py_ast, |
| 2094 | + ConstType.PY_SET: _const_py_set_to_py_ast, |
| 2095 | + ConstType.PY_TUPLE: _const_py_tuple_to_py_ast, |
2011 | 2096 | ConstType.VECTOR: _const_vec_to_py_ast,
|
2012 | 2097 | }
|
2013 | 2098 |
|
@@ -2044,6 +2129,10 @@ def _const_node_to_py_ast(ctx: GeneratorContext, lisp_ast: Const) -> GeneratedPy
|
2044 | 2129 | NodeOp.MAP: _map_to_py_ast,
|
2045 | 2130 | NodeOp.MAYBE_CLASS: _maybe_class_to_py_ast,
|
2046 | 2131 | NodeOp.MAYBE_HOST_FORM: _maybe_host_form_to_py_ast,
|
| 2132 | + NodeOp.PY_DICT: _py_dict_to_py_ast, |
| 2133 | + NodeOp.PY_LIST: _py_list_to_py_ast, |
| 2134 | + NodeOp.PY_SET: _py_set_to_py_ast, |
| 2135 | + NodeOp.PY_TUPLE: _py_tuple_to_py_ast, |
2047 | 2136 | NodeOp.QUOTE: _quote_to_py_ast,
|
2048 | 2137 | NodeOp.RECUR: _recur_to_py_ast,
|
2049 | 2138 | NodeOp.SET: _set_to_py_ast,
|
|
0 commit comments