Exemplo n.º 1
0
 def cs_format(self, precision=None):
     statement = "else"
     body_fmt = Indented(self.body.cs_format(precision))
     if _is_simple_if_body(self.body):
         return (statement, body_fmt)
     else:
         return (statement, "{", body_fmt, "}")
Exemplo n.º 2
0
    def cs_format(self, precision=None):
        cases = []
        for case in self.cases:
            caseheader = "case " + case[0].ce_format(precision) + ":"
            casebody = case[1].cs_format(precision)
            if self.autoscope:
                casebody = ("{", Indented(casebody), "}")
            if self.autobreak:
                casebody = (casebody, "break;")
            cases.extend([caseheader, Indented(casebody)])

        if self.default is not None:
            caseheader = "default:"
            casebody = self.default.cs_format(precision)
            if self.autoscope:
                casebody = ("{", Indented(casebody), "}")
            cases.extend([caseheader, Indented(casebody)])

        return ("switch (" + self.arg.ce_format(precision) + ")", "{", cases, "}")
Exemplo n.º 3
0
    def cs_format(self, precision=None):
        if not all(self.sizes):
            raise RuntimeError(
                "Detected an array dimension of zero. This is not valid in C.")

        # Pad innermost array dimension
        sizes = pad_innermost_dim(self.sizes, self.padlen)

        # Add brackets
        brackets = ''.join("[%d]" % n for n in sizes)

        # Join declaration
        decl = self.typename + " " + self.symbol.name + brackets

        # NB! C++11 style alignas prefix syntax.
        # If trying other target languages, must use other syntax.
        if self.alignas:
            align = "alignas(%d)" % int(self.alignas)
            decl = align + " " + decl

        if self.values is None:
            # Undefined initial values
            return decl + ";"
        elif _is_zero_valued(self.values):
            # Zero initial values
            # (NB! C style zero initialization, not sure about other target languages)
            nb = len(sizes)
            return decl + " = {lbr} 0 {rbr};".format(lbr="{" * nb,
                                                     rbr="}" * nb)
        else:
            # Construct initializer lists for arbitrary multidimensional array values
            if self.values.dtype.kind == "f":
                formatter = format_float
            elif self.values.dtype.kind == "i":
                formatter = format_int
            else:
                formatter = format_value
            initializer_lists = build_initializer_lists(self.values,
                                                        self.sizes,
                                                        0,
                                                        formatter,
                                                        padlen=self.padlen,
                                                        precision=precision)
            if len(initializer_lists) == 1:
                return decl + " = " + initializer_lists[0] + ";"
            else:
                initializer_lists[-1] += ";"  # Close statement on final line
                return (decl + " =", Indented(initializer_lists))
Exemplo n.º 4
0
    def cs_format(self, precision=None):
        if not all(self.sizes):
            raise RuntimeError(f"Detected an array {self.symbol} dimension of zero. This is not valid in C.")

        # Pad innermost array dimension
        sizes = pad_innermost_dim(self.sizes, self.padlen)

        # Add brackets
        brackets = ''.join("[%d]" % n for n in sizes)

        # Join declaration
        decl = self.typename + " " + self.symbol.name + brackets

        if self.values is None:
            # Undefined initial values
            return decl + ";"
        elif _is_zero_valued(self.values):
            # Zero initial values
            # (NB! C style zero initialization, not sure about other target languages)
            nb = len(sizes)
            lbr = "{" * nb
            rbr = "}" * nb
            return f"{decl} = {lbr} 0 {rbr};"
        else:
            # Construct initializer lists for arbitrary multidimensional array values
            if self.values.dtype.kind == "f":
                formatter = format_float
            elif self.values.dtype.kind == "i":
                formatter = format_int
            elif self.values.dtype == numpy.bool_:
                def format_bool(x, precision=None):
                    return "true" if x is True else "false"
                formatter = format_bool
            else:
                formatter = format_value
            initializer_lists = build_initializer_lists(
                self.values, self.sizes, 0, formatter, padlen=self.padlen, precision=precision)
            if len(initializer_lists) == 1:
                return decl + " = " + initializer_lists[0] + ";"
            else:
                initializer_lists[-1] += ";"  # Close statement on final line
                return (decl + " =", Indented(initializer_lists))
Exemplo n.º 5
0
    def cs_format(self, precision=None):
        indextype = self.index_type
        index = self.index.ce_format(precision)
        begin = self.begin.ce_format(precision)
        end = self.end.ce_format(precision)

        init = indextype + " " + index + " = " + begin
        check = index + " < " + end
        update = "++" + index

        prelude = "for (" + init + "; " + check + "; " + update + ")"
        body = Indented(self.body.cs_format(precision))

        # Reduce size of code with lots of simple loops by dropping {} in obviously safe cases
        if is_simple_inner_loop(self.body):
            code = (prelude, body)
        else:
            code = (prelude, "{", body, "}")

        return code
Exemplo n.º 6
0
 def cs_format(self, precision=None):
     return ("{", Indented(self.body.cs_format(precision)), "}")