MCPcopy
hub / github.com/hylang/hy / compile_comprehension

Function compile_comprehension

hy/core/result_macros.py:824–1099  ·  view source on GitHub ↗
(compiler, expr, root, parts, final)

Source from the content-addressed store, hash-verified

822# Here `finished` is a hack replacement for FORM + FORM:
823# https://github.com/vlasovskikh/funcparserlib/issues/75
824def compile_comprehension(compiler, expr, root, parts, final):
825 node_class = {
826 "for": asty.For,
827 "lfor": asty.ListComp,
828 "dfor": asty.DictComp,
829 "sfor": asty.SetComp,
830 "gfor": asty.GeneratorExp,
831 }[root]
832 is_for = root == "for"
833
834 ctx = nullcontext() if is_for else compiler.scope.create(ScopeGen)
835 mac_con = nullcontext() if is_for else compiler.local_state()
836 with mac_con, ctx as scope:
837
838 # Compile the parts.
839 dict_unpack = False
840 if is_for:
841 parts = parts[0]
842 if node_class is asty.DictComp:
843 dict_unpack = bool(final)
844 if not (dict_unpack or (parts and parts[-1].tag == "for")):
845 compiler._syntax_error(
846 parts[-1] if parts else parts,
847 "`dfor` must end with key and value forms, or `#** FORM`",
848 )
849 final = final or parts.pop().value
850 if not parts:
851 return Result(
852 expr=asty.parse(
853 expr,
854 {
855 asty.For: "None",
856 asty.ListComp: "[]",
857 asty.DictComp: "{}",
858 asty.SetComp: "{1}.__class__()",
859 asty.GeneratorExp: "(_ for _ in [])",
860 }[node_class],
861 )
862 .body[0]
863 .value
864 )
865 new_parts = []
866 for p in parts:
867 if p.tag in ("if", "do"):
868 tag_value = compiler.compile(p.value)
869 else:
870 tag_value = [
871 compiler._storeize(p.value[0], compiler.compile(p.value[0])),
872 compiler.compile(p.value[1]),
873 ]
874 if not is_for:
875 scope.iterator(tag_value[0])
876 new_parts.append(Tag(p.tag, tag_value))
877 parts = new_parts
878
879 orel = []
880 if is_for:
881 # Get the `else`.

Callers

nothing calls this directly

Calls 14

ResultClass · 0.90
is_inside_function_scopeFunction · 0.90
createMethod · 0.80
local_stateMethod · 0.80
_syntax_errorMethod · 0.80
compileMethod · 0.80
_storeizeMethod · 0.80
iteratorMethod · 0.80
_compile_branchMethod · 0.80
expr_as_stmtMethod · 0.80
get_anon_varMethod · 0.80
finalizeMethod · 0.80

Tested by

no test coverage detected