Пример #1
0
 def out(self):
     out = ""
     if self.use_sentinel:
         out += sentinel_var + " = _coconut.object()\n"
     closes = 0
     for checks, defs in self.checkdefs:
         if checks:
             out += "if " + paren_join(checks, "and") + ":\n" + openindent
             closes += 1
         if defs:
             out += "\n".join(defs) + "\n"
     return out + (match_check_var + " = True\n" + closeindent * closes +
                   "".join(other.out() for other in self.others) +
                   ("if " + match_check_var + " and not (" +
                    paren_join(self.guards, "and") + "):\n" + openindent +
                    match_check_var + " = False\n" +
                    closeindent if self.guards else ""))
Пример #2
0
 def out(self):
     """Return pattern-matching code."""
     out = ""
     closes = 0
     for checks, defs in self.checkdefs:
         if checks:
             out += "if " + paren_join(checks, "and") + ":\n" + openindent
             closes += 1
         if defs:
             out += "\n".join(defs) + "\n"
     return out + (
         self.check_var + " = True\n"
         + closeindent * closes
         + "".join(other.out() for other in self.others)
         + (
             "if " + self.check_var + " and not ("
             + paren_join(self.guards, "and")
             + "):\n" + openindent
             + self.check_var + " = False\n" + closeindent
             if self.guards else ""
         )
     )
Пример #3
0
 def out(self):
     """Return pattern-matching code."""
     out = ""
     if self.use_sentinel:
         out += sentinel_var + " = _coconut.object()\n"
     closes = 0
     for checks, defs in self.checkdefs:
         if checks:
             out += "if " + paren_join(checks, "and") + ":\n" + openindent
             closes += 1
         if defs:
             out += "\n".join(defs) + "\n"
     return out + (
         self.check_var + " = True\n"
         + closeindent * closes
         + "".join(other.out() for other in self.others)
         + (
             "if " + self.check_var + " and not ("
             + paren_join(self.guards, "and")
             + "):\n" + openindent
             + self.check_var + " = False\n" + closeindent
             if self.guards else ""
         )
     )
Пример #4
0
    def out(self):
        """Return pattern-matching code assuming check_var starts False."""
        out = []

        # set match_set_name_vars to sentinels
        for name in self.names:
            out.append(self.get_set_name_var(name) + " = _coconut_sentinel\n")

        # match checkdefs setting check_var
        closes = 0
        for checks, defs in self.checkdefs:
            if checks:
                out.append("if " + paren_join(checks, "and") + ":\n" + openindent)
                closes += 1
            if defs:
                out.append("\n".join(defs) + "\n")
        out.append(self.check_var + " = True\n" + closeindent * closes)

        # handle children
        for children in self.child_groups:
            out.append(
                handle_indentation(
                    """
if {check_var}:
    {check_var} = False
    {children}
                """,
                    add_newline=True,
                ).format(
                    check_var=self.check_var,
                    children="".join(child.out() for child in children),
                ),
            )

        # commit variable definitions
        name_set_code = []
        for name, val in self.names.items():
            name_set_code.append(
                handle_indentation(
                    """
if {set_name_var} is not _coconut_sentinel:
    {name} = {val}
                """,
                    add_newline=True,
                ).format(
                    set_name_var=self.get_set_name_var(name),
                    name=name,
                    val=val,
                ),
            )
        if name_set_code:
            out.append(
                handle_indentation(
                    """
if {check_var}:
    {name_set_code}
                """,
                ).format(
                    check_var=self.check_var,
                    name_set_code="".join(name_set_code),
                ),
            )

        # handle guards
        if self.guards:
            out.append(
                handle_indentation(
                    """
if {check_var} and not ({guards}):
    {check_var} = False
                """,
                    add_newline=True,
                ).format(
                    check_var=self.check_var,
                    guards=paren_join(self.guards, "and"),
                ),
            )

        return "".join(out)