|
| 1 | +from atcodertools.codegen.models.code_gen_args import CodeGenArgs |
| 2 | +from atcodertools.codegen.code_generators.cpp import CppCodeGenerator |
| 3 | +from atcodertools.codegen.template_engine import render |
| 4 | +from atcodertools.fmtprediction.models.format import Pattern, SingularPattern, ParallelPattern, TwoDimensionalPattern |
| 5 | +from atcodertools.fmtprediction.models.type import Type |
| 6 | +from atcodertools.fmtprediction.models.variable import Variable |
| 7 | + |
| 8 | + |
| 9 | +# RustCodeGenerator uses part of CppCodeGenerator just for less code clone. |
| 10 | + |
| 11 | + |
| 12 | +def _loop_header(var: Variable, for_second_index: bool): |
| 13 | + if for_second_index: |
| 14 | + index = var.second_index |
| 15 | + loop_var = "j" |
| 16 | + else: |
| 17 | + index = var.first_index |
| 18 | + loop_var = "i" |
| 19 | + |
| 20 | + return "for {loop_var} in 0..({length}) as usize {{".format( |
| 21 | + loop_var=loop_var, |
| 22 | + length=index.get_length() |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +class RustCodeGenerator(CppCodeGenerator): |
| 27 | + |
| 28 | + def _input_part(self): |
| 29 | + lines = ["let con = read_string();", |
| 30 | + "let mut scanner = Scanner::new(&con);"] |
| 31 | + for pattern in self._format.sequence: |
| 32 | + lines += self._render_pattern(pattern) |
| 33 | + return "\n{indent}".format(indent=self._indent(1)).join(lines) |
| 34 | + |
| 35 | + def _convert_type(self, type_: Type) -> str: |
| 36 | + if type_ == Type.float: |
| 37 | + return "f64" |
| 38 | + elif type_ == Type.int: |
| 39 | + return "i64" |
| 40 | + elif type_ == Type.str: |
| 41 | + return "String" |
| 42 | + else: |
| 43 | + raise NotImplementedError |
| 44 | + |
| 45 | + def _get_declaration_type(self, var: Variable): |
| 46 | + if var.dim_num() == 0: |
| 47 | + template = "{type}" |
| 48 | + elif var.dim_num() == 1: |
| 49 | + template = "Vec<{type}>" |
| 50 | + elif var.dim_num() == 2: |
| 51 | + template = "Vec<Vec<{type}>>" |
| 52 | + else: |
| 53 | + raise NotImplementedError |
| 54 | + return template.format(type=self._convert_type(var.type)) |
| 55 | + |
| 56 | + def _actual_arguments(self) -> str: |
| 57 | + return ", ".join([v.name for v in self._format.all_vars()]) |
| 58 | + |
| 59 | + def _formal_arguments(self): |
| 60 | + """ |
| 61 | + :return the string form of formal arguments e.g. "N: i64, K: i64, a: Vec<i64>" |
| 62 | + """ |
| 63 | + return ", ".join([ |
| 64 | + "{name}: {decl_type}".format( |
| 65 | + decl_type=self._get_declaration_type(v), |
| 66 | + name=v.name) |
| 67 | + for v in self._format.all_vars() |
| 68 | + ]) |
| 69 | + |
| 70 | + def _generate_declaration(self, var: Variable): |
| 71 | + if var.dim_num() == 0: |
| 72 | + constructor = "" |
| 73 | + elif var.dim_num() == 1: |
| 74 | + if var.type == Type.str: |
| 75 | + constructor = " = vec![String::new(); ({size}) as usize]".format( |
| 76 | + type=self._convert_type(var.type), |
| 77 | + size=var.first_index.get_length() |
| 78 | + ) |
| 79 | + else: |
| 80 | + constructor = " = vec![0{type}; ({size}) as usize]".format( |
| 81 | + type=self._convert_type(var.type), |
| 82 | + size=var.first_index.get_length() |
| 83 | + ) |
| 84 | + elif var.dim_num() == 2: |
| 85 | + if var.type == Type.str: |
| 86 | + constructor = " = vec![vec![String::new(); ({col_size}) as usize]; ({row_size}) as usize]".format( |
| 87 | + type=self._convert_type(var.type), |
| 88 | + row_size=var.first_index.get_length(), |
| 89 | + col_size=var.second_index.get_length() |
| 90 | + ) |
| 91 | + else: |
| 92 | + constructor = " = vec![vec![0{type}; ({col_size}) as usize]; ({row_size}) as usize]".format( |
| 93 | + type=self._convert_type(var.type), |
| 94 | + row_size=var.first_index.get_length(), |
| 95 | + col_size=var.second_index.get_length() |
| 96 | + ) |
| 97 | + else: |
| 98 | + raise NotImplementedError |
| 99 | + |
| 100 | + line = "let mut {name}: {decl_type}{constructor};".format( |
| 101 | + name=var.name, |
| 102 | + decl_type=self._get_declaration_type(var), |
| 103 | + constructor=constructor |
| 104 | + ) |
| 105 | + return line |
| 106 | + |
| 107 | + def _input_code_for_var(self, var: Variable) -> str: |
| 108 | + name = self._get_var_name(var) |
| 109 | + return '{name} = scanner.next();'.format(name=name) |
| 110 | + |
| 111 | + def _render_pattern(self, pattern: Pattern): |
| 112 | + lines = [] |
| 113 | + for var in pattern.all_vars(): |
| 114 | + lines.append(self._generate_declaration(var)) |
| 115 | + |
| 116 | + representative_var = pattern.all_vars()[0] |
| 117 | + if isinstance(pattern, SingularPattern): |
| 118 | + lines.append(self._input_code_for_var(representative_var)) |
| 119 | + elif isinstance(pattern, ParallelPattern): |
| 120 | + lines.append(_loop_header(representative_var, False)) |
| 121 | + for var in pattern.all_vars(): |
| 122 | + lines.append("{indent}{line}".format(indent=self._indent(1), |
| 123 | + line=self._input_code_for_var(var))) |
| 124 | + lines.append("}") |
| 125 | + elif isinstance(pattern, TwoDimensionalPattern): |
| 126 | + lines.append(_loop_header(representative_var, False)) |
| 127 | + lines.append( |
| 128 | + "{indent}{line}".format(indent=self._indent(1), line=_loop_header(representative_var, True))) |
| 129 | + for var in pattern.all_vars(): |
| 130 | + lines.append("{indent}{line}".format(indent=self._indent(2), |
| 131 | + line=self._input_code_for_var(var))) |
| 132 | + lines.append("{indent}}}".format(indent=self._indent(1))) |
| 133 | + lines.append("}") |
| 134 | + else: |
| 135 | + raise NotImplementedError |
| 136 | + |
| 137 | + return lines |
| 138 | + |
| 139 | + |
| 140 | +def main(args: CodeGenArgs) -> str: |
| 141 | + code_parameters = RustCodeGenerator( |
| 142 | + args.format, args.config).generate_parameters() |
| 143 | + return render( |
| 144 | + args.template, |
| 145 | + mod=args.constants.mod, |
| 146 | + yes_str=args.constants.yes_str, |
| 147 | + no_str=args.constants.no_str, |
| 148 | + **code_parameters |
| 149 | + ) |
0 commit comments