예제 #1
0
    def _render_pattern(self, pattern: Pattern):
        lines = []
        for var in pattern.all_vars():
            lines.append(self._generate_declaration(var))

        representative_var = pattern.all_vars()[0]
        if isinstance(pattern, SingularPattern):
            lines.append(self._input_code_for_var(representative_var))
        elif isinstance(pattern, ParallelPattern):
            lines.append(_loop_header(representative_var, False))
            for var in pattern.all_vars():
                lines.append("{indent}{line}".format(indent=self._indent(1),
                                                     line=self._input_code_for_var(var)))
            lines.append("}")
        elif isinstance(pattern, TwoDimensionalPattern):
            lines.append(_loop_header(representative_var, False))
            lines.append(
                "{indent}{line}".format(indent=self._indent(1), line=_loop_header(representative_var, True)))
            for var in pattern.all_vars():
                lines.append("{indent}{line}".format(indent=self._indent(2),
                                                     line=self._input_code_for_var(var)))
            lines.append("{indent}}}".format(indent=self._indent(1)))
            lines.append("}")
        else:
            raise NotImplementedError

        return lines
예제 #2
0
    def _input_code_for_single_pattern(self, pattern: Pattern) -> str:
        assert len(pattern.all_vars()) == 1
        var = pattern.all_vars()[0]

        if isinstance(pattern, SingularPattern):
            input_ = self._input_code_for_token(var.type)

        elif isinstance(pattern, ParallelPattern):
            input_ = "[ {input_} for _ in range({length}) ]".format(
                input_=self._input_code_for_token(var.type),
                length=var.first_index.get_length())

        elif isinstance(pattern, TwoDimensionalPattern):
            input_ = "[ [ {input_} for _ in range({second_length}) ] for _ in range({first_length}) ]".format(
                input_=self._input_code_for_token(var.type),
                first_length=var.first_index.get_length(),
                second_length=var.second_index.get_length())

        else:
            raise NotImplementedError

        return "{name} = {input_}  # type: {type_}".format(
            name=var.name,
            input_=input_,
            type_=self._get_declaration_type(var))
예제 #3
0
    def _render_pattern(self, pattern: Pattern):
        lines = []
        for var in pattern.all_vars():
            lines.append(self._generate_declaration(var))

        representative_var = pattern.all_vars()[0]
        if isinstance(pattern, SingularPattern):
            lines.append(self._input_code_for_var(representative_var))
        elif isinstance(pattern, ParallelPattern):
            lines.append(_loop_header(representative_var, False))
            for var in pattern.all_vars():
                lines.append("{indent}{line}".format(
                    indent=self._indent(1),
                    line=self._input_code_for_var(var)))
            lines.append("}")
        elif isinstance(pattern, TwoDimensionalPattern):
            lines.append(_loop_header(representative_var, False))
            lines.append("{indent}{line}".format(indent=self._indent(1),
                                                 line=_loop_header(
                                                     representative_var,
                                                     True)))
            for var in pattern.all_vars():
                lines.append("{indent}{line}".format(
                    indent=self._indent(2),
                    line=self._input_code_for_var(var)))
            lines.append("{indent}}}".format(indent=self._indent(1)))
            lines.append("}")
        else:
            raise NotImplementedError

        return lines
예제 #4
0
    def _input_code_for_non_single_pattern(self,
                                           pattern: Pattern) -> List[str]:
        lines = []
        for var in pattern.all_vars():
            lines.append(self._generate_declaration(var))
        representative_var = pattern.all_vars()[0]

        if isinstance(pattern, SingularPattern):
            assert False

        elif isinstance(pattern, ParallelPattern):
            lines.append(_loop_header(representative_var, False))
            for var in pattern.all_vars():
                lines.append("{indent}{name} = {input_}".format(
                    indent=self._indent(1),
                    name=self._get_var_name(var),
                    input_=self._input_code_for_token(var.type)))

        elif isinstance(pattern, TwoDimensionalPattern):
            lines.append(_loop_header(representative_var, False))
            lines.append("{indent}{line}".format(indent=self._indent(1),
                                                 line=_loop_header(
                                                     representative_var,
                                                     True)))
            for var in pattern.all_vars():
                lines.append("{indent}{name} = {input_}".format(
                    indent=self._indent(2),
                    name=self._get_var_name(var),
                    input_=self._input_code_for_token(var.type)))

        else:
            raise NotImplementedError
        return lines
예제 #5
0
    def _input_code_for_non_single_pattern(self, pattern: Pattern) -> List[str]:
        lines = []
        for var in pattern.all_vars():
            lines.append(self._generate_declaration(var))
        representative_var = pattern.all_vars()[0]

        if isinstance(pattern, SingularPattern):
            assert False

        elif isinstance(pattern, ParallelPattern):
            lines.append(_loop_header(representative_var, False))
            for var in pattern.all_vars():
                lines.append("{indent}{name} = {input_}".format(indent=self._indent(1),
                                                                name=self._get_var_name(
                                                                    var),
                                                                input_=self._input_code_for_token(var.type)))

        elif isinstance(pattern, TwoDimensionalPattern):
            lines.append(_loop_header(representative_var, False))
            lines.append(
                "{indent}{line}".format(indent=self._indent(1), line=_loop_header(representative_var, True)))
            for var in pattern.all_vars():
                lines.append("{indent}{name} = {input_}".format(indent=self._indent(2),
                                                                name=self._get_var_name(
                                                                    var),
                                                                input_=self._input_code_for_token(var.type)))

        else:
            raise NotImplementedError
        return lines
예제 #6
0
    def _render_pattern(self, pattern: Pattern):
        lines = []
        for var in pattern.all_vars():
            decl = self._generate_declaration(var)
            if decl:
                lines.append(decl)

        representative_var = pattern.all_vars()[0]
        if isinstance(pattern, SingularPattern):
            lines.append(self._input_code_for_var(representative_var))
        elif isinstance(pattern, ParallelPattern):
            line = _loop_header(representative_var, False) + ' { '
            for var in pattern.all_vars():
                line += '{input} '.format(
                    input=self._input_code_for_declared_var(var))
            line += '}'
            lines.append(line)
        elif isinstance(pattern, TwoDimensionalPattern):
            line = _loop_header(representative_var, False) + ' '
            line += _loop_header(representative_var, True) + ' { '
            for var in pattern.all_vars():
                line += '{input} '.format(
                    input=self._input_code_for_declared_var(var))
            line += '}'
            lines.append(line)
        else:
            raise NotImplementedError

        return lines
예제 #7
0
    def _input_code_for_single_pattern(self, pattern: Pattern) -> str:
        assert len(pattern.all_vars()) == 1
        var = pattern.all_vars()[0]

        if isinstance(pattern, SingularPattern):
            input_ = self._input_code_for_token(var.type)

        elif isinstance(pattern, ParallelPattern):
            input_ = "[ {input_} for _ in range({length}) ]".format(
                input_=self._input_code_for_token(var.type),
                length=var.first_index.get_length())

        elif isinstance(pattern, TwoDimensionalPattern):
            input_ = "[ [ {input_} for _ in range({second_length}) ] for _ in range({first_length}) ]".format(
                input_=self._input_code_for_token(var.type),
                first_length=var.first_index.get_length(),
                second_length=var.second_index.get_length())

        else:
            raise NotImplementedError

        return "{name} = {input_}  # type: {type_}".format(
            name=var.name,
            input_=input_,
            type_=self._get_declaration_type(var))
 def _append_declaration_and_allocation(self, lines, pattern: Pattern,
                                        global_mode):
     if global_mode:
         for var in pattern.all_vars():
             self._append(lines, self._generate_allocation(var))
     else:
         for var in pattern.all_vars():
             self._append(lines,
                          self._generate_declaration_and_allocation(var))
 def _append_singular_pattern(self, lines, pattern: Pattern, global_mode):
     var = pattern.all_vars()[0]
     if not global_mode:
         if var.type == Type.int:
             if hasattr(self.info, "declare_and_input_int"):
                 self._append(
                     lines,
                     self.info.declare_and_input_int.format(name=var.name))
                 return
         elif var.type == Type.float:
             if hasattr(self.info, "declare_and_input_float"):
                 self._append(
                     lines,
                     self.info.declare_and_input_float.format(
                         name=var.name))
                 return
         elif var.type == Type.str:
             if hasattr(self.info, "declare_and_input_string"):
                 self._append(
                     lines,
                     self.info.declare_and_input_string.format(
                         name=var.name))
                 return
         else:
             raise NotImplementedError
     self._append_declaration_and_allocation(lines, pattern, global_mode)
     self._append(lines, self._input_code_for_var(var))
예제 #10
0
 def _render_pattern(self, pattern: Pattern):
     if len(pattern.all_vars()) == 1:
         return [self._input_code_for_single_pattern(pattern)]
     else:
         return self._input_code_for_non_single_pattern(pattern)
예제 #11
0
 def _render_pattern(self, pattern: Pattern):
     if len(pattern.all_vars()) == 1:
         return [self._input_code_for_single_pattern(pattern)]
     else:
         return self._input_code_for_non_single_pattern(pattern)
    def _render_pattern(self, pattern: Pattern, global_mode):
        lines = []

        representative_var = pattern.all_vars()[0]
        if isinstance(pattern, SingularPattern):
            self._append_singular_pattern(lines, pattern, global_mode)
        elif isinstance(pattern, ParallelPattern):
            added = False
            if len(pattern.all_vars()) == 1:
                var = pattern.all_vars()[0]
                if global_mode:
                    if hasattr(self.info, "allocate_and_input_seq"):
                        self._append(
                            lines,
                            self.info.allocate_and_input_seq.format(
                                input_func=self._get_input_func(var.type),
                                length=self._get_length(var.first_index),
                                name=var.name))
                        added = True
                else:
                    if hasattr(self.info,
                               "declare_and_allocate_and_input_seq"):
                        self._append(
                            lines,
                            self.info.declare_and_allocate_and_input_seq.
                            format(input_func=self._get_input_func(var.type),
                                   length=self._get_length(var.first_index),
                                   name=var.name,
                                   type=self._convert_type(var.type)))

                        added = True
            if not added:
                self._append_declaration_and_allocation(
                    lines, pattern, global_mode)
                self._append(lines, self._loop_header(representative_var,
                                                      False))
                for var in pattern.all_vars():
                    self._append(lines, self._input_code_for_var(var), 1)
                self._append(lines, self.info.loop_footer)
        elif isinstance(pattern, TwoDimensionalPattern):
            added = False
            if len(pattern.all_vars()) == 1:
                var = pattern.all_vars()[0]
                if global_mode:
                    if hasattr(self.info, "allocate_and_input_2d_seq"):
                        self._append(
                            lines,
                            self.info.allocate_and_input_2d_seq.format(
                                input_func=self._get_input_func(var.type),
                                length_i=self._get_length(var.first_index),
                                length_j=self._get_length(var.second_index),
                                name=var.name))
                        added = True
                else:
                    if hasattr(self.info,
                               "declare_and_allocate_and_input_2d_seq"):
                        self._append(
                            lines,
                            self.info.declare_and_allocate_and_input_2d_seq.
                            format(input_func=self._get_input_func(var.type),
                                   length_i=self._get_length(var.first_index),
                                   length_j=self._get_length(var.second_index),
                                   name=var.name,
                                   type=self._convert_type(var.type)))
                        added = True
            if not added:
                self._append_declaration_and_allocation(
                    lines, pattern, global_mode)
                self._append(lines, self._loop_header(representative_var,
                                                      False))
                self._append(lines, self._loop_header(representative_var,
                                                      True), 1)
                for var in pattern.all_vars():
                    self._append(lines, self._input_code_for_var(var), 2)
                self._append(lines, self.info.loop_footer, 1)
                self._append(lines, self.info.loop_footer)
        else:
            raise NotImplementedError

        return lines