From 5df058f8ed245be06444e6287ef5c2afef533917 Mon Sep 17 00:00:00 2001 From: chaemon Date: Tue, 20 Aug 2019 02:18:12 +0900 Subject: [PATCH 01/14] =?UTF-8?q?default=20code=20style=E3=82=92=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atcodertools/codegen/code_style_config.py | 18 ++++++++++++++++-- atcodertools/common/language.py | 3 +++ check_autopep8 | 0 tests/check_autopep8 | 0 tests/test_codegen.py | 13 ++----------- 5 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 check_autopep8 create mode 100644 tests/check_autopep8 diff --git a/atcodertools/codegen/code_style_config.py b/atcodertools/codegen/code_style_config.py index 23ff13a9..c553fee8 100644 --- a/atcodertools/codegen/code_style_config.py +++ b/atcodertools/codegen/code_style_config.py @@ -19,8 +19,8 @@ class CodeStyleConfigInitError(Exception): class CodeStyleConfig: def __init__(self, - indent_type: str = INDENT_TYPE_SPACE, - indent_width: int = 4, + indent_type: Optional[str] = None, + indent_width: Optional[int] = None, code_generator_file: Optional[str] = None, template_file: Optional[str] = None, workspace_dir: Optional[str] = None, @@ -37,6 +37,20 @@ def __init__(self, raise CodeStyleConfigInitError( "language must be one of {}".format(ALL_LANGUAGE_NAMES)) + print(lang.default_code_style) + + if lang.default_code_style is not None: + if "indent_width" in lang.default_code_style: + indent_width = lang.default_code_style["indent_width"] + if "indent_type" in lang.default_code_style: + indent_type = lang.default_code_style["indent_type"] + + if indent_type is None: + indent_type = INDENT_TYPE_SPACE + + if indent_width is None: + indent_width = 4 + if indent_type not in [INDENT_TYPE_SPACE, INDENT_TYPE_TAB]: raise CodeStyleConfigInitError( "indent_type must be 'space' or 'tab'") diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index f8e5b619..b001747f 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -19,6 +19,7 @@ def __init__(self, submission_lang_pattern: Pattern[str], default_code_generator: Callable[[CodeGenArgs], str], default_template_path: str, + default_code_style=None ): self.name = name self.display_name = display_name @@ -26,6 +27,7 @@ def __init__(self, self.submission_lang_pattern = submission_lang_pattern self.default_code_generator = default_code_generator self.default_template_path = default_template_path + self.default_code_style = default_code_style def source_code_name(self, name_without_extension: str) -> str: # put extension to the name @@ -92,6 +94,7 @@ def from_name(cls, name: str): submission_lang_pattern=re.compile(".*Nim \\(0.*"), default_code_generator=nim.main, default_template_path=get_default_template_path('nim'), + default_code_style={"indent_type": "space", "indent_width": 2} ) ALL_LANGUAGES = [CPP, JAVA, RUST, PYTHON, NIM, DLANG] diff --git a/check_autopep8 b/check_autopep8 new file mode 100644 index 00000000..e69de29b diff --git a/tests/check_autopep8 b/tests/check_autopep8 new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_codegen.py b/tests/test_codegen.py index b25bdb44..83b8a7e6 100644 --- a/tests/test_codegen.py +++ b/tests/test_codegen.py @@ -205,15 +205,11 @@ def _compile_and_run(self, lang, format, template_file, expected_generated_code_ code_file = os.path.join(self.temp_dir, lang.source_code_name("main")) exec_file, exec_args = self._exec_file_and_args(lang) compile_cmd = self._compile_command(lang, code_file) - if lang == NIM: - cfg = CodeStyleConfig(indent_width=2) - else: - cfg = CodeStyleConfig() args = CodeGenArgs( template=load_text_file(template_file), format_=format, constants=ProblemConstantSet(123, "yes", "NO"), - config=cfg + config=CodeStyleConfig(lang=lang.name) ) code = lang.default_code_generator(args) @@ -255,11 +251,6 @@ def verify(self, self.assertEqual( load_intermediate_types(py_test_name), str(response.types)) - if lang == NIM: - cfg = CodeStyleConfig(indent_width=2) - else: - cfg = CodeStyleConfig() - self.assertEqual( load_generated_code(py_test_name, lang), self.lang_to_code_generator_func[lang]( @@ -267,7 +258,7 @@ def verify(self, self.get_template(lang, template_type), response.original_result.format, constants, - cfg) + CodeStyleConfig(lang=lang.name)) )) def get_template(self, lang: Language, template_type: str) -> str: From 937125158f634ef8413222a5ca5479ed79d031ae Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 00:31:10 +0900 Subject: [PATCH 02/14] add default_code_style --- atcodertools/common/language.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index b001747f..e27cf94b 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -94,7 +94,7 @@ def from_name(cls, name: str): submission_lang_pattern=re.compile(".*Nim \\(0.*"), default_code_generator=nim.main, default_template_path=get_default_template_path('nim'), - default_code_style={"indent_type": "space", "indent_width": 2} + default_code_style={"indent_width": 2} ) ALL_LANGUAGES = [CPP, JAVA, RUST, PYTHON, NIM, DLANG] From aacbe3f2e5f0ff28ceb146a93372eb53317cc7bf Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 00:48:52 +0900 Subject: [PATCH 03/14] add default code style --- atcodertools/codegen/code_style_config.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/atcodertools/codegen/code_style_config.py b/atcodertools/codegen/code_style_config.py index bcb40b90..39e82608 100644 --- a/atcodertools/codegen/code_style_config.py +++ b/atcodertools/codegen/code_style_config.py @@ -37,14 +37,6 @@ def __init__(self, raise CodeStyleConfigInitError( "language must be one of {}".format(ALL_LANGUAGE_NAMES)) - print(lang.default_code_style) - - if indent_type is None: - indent_type = INDENT_TYPE_SPACE - - if indent_width is None: - indent_width = 4 - if indent_type not in [INDENT_TYPE_SPACE, INDENT_TYPE_TAB]: raise CodeStyleConfigInitError( "indent_type must be 'space' or 'tab'") From 9dfb44551faf42f5019c703e1fccd91eee195a67 Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 02:16:55 +0900 Subject: [PATCH 04/14] add cs --- atcodertools/codegen/code_style_config.py | 2 +- atcodertools/common/language.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/atcodertools/codegen/code_style_config.py b/atcodertools/codegen/code_style_config.py index 39e82608..c78a339a 100644 --- a/atcodertools/codegen/code_style_config.py +++ b/atcodertools/codegen/code_style_config.py @@ -26,7 +26,7 @@ def __init__(self, workspace_dir: Optional[str] = None, lang: str = "cpp", ): - from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES, NIM + from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES code_generator_file = normalize_path(code_generator_file) template_file = normalize_path(template_file) diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index e27cf94b..04270fd4 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -1,7 +1,7 @@ import re from typing import Pattern, Callable -from atcodertools.codegen.code_generators import cpp, java, rust, python, nim, d +from atcodertools.codegen.code_generators import cpp, java, rust, python, nim, d, cs from atcodertools.codegen.models.code_gen_args import CodeGenArgs from atcodertools.tools.templates import get_default_template_path @@ -97,5 +97,15 @@ def from_name(cls, name: str): default_code_style={"indent_width": 2} ) -ALL_LANGUAGES = [CPP, JAVA, RUST, PYTHON, NIM, DLANG] +CSHARP = Language( + name="cs", + display_name="CS", + extension="cs", + submission_lang_pattern=re.compile(".*C# \\(Mono.*"), + default_code_generator=cs.main, + default_template_path=get_default_template_path('cs'), +) + + +ALL_LANGUAGES = [CPP, JAVA, RUST, PYTHON, NIM, DLANG, CSHARP] ALL_LANGUAGE_NAMES = [lang.display_name for lang in ALL_LANGUAGES] From 707fed9ac2927294644ac9bb4d9d4318694b6ef3 Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 02:26:36 +0900 Subject: [PATCH 05/14] remove nim import --- atcodertools/codegen/code_style_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atcodertools/codegen/code_style_config.py b/atcodertools/codegen/code_style_config.py index 39e82608..c78a339a 100644 --- a/atcodertools/codegen/code_style_config.py +++ b/atcodertools/codegen/code_style_config.py @@ -26,7 +26,7 @@ def __init__(self, workspace_dir: Optional[str] = None, lang: str = "cpp", ): - from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES, NIM + from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES code_generator_file = normalize_path(code_generator_file) template_file = normalize_path(template_file) From 7ee172eefae9473d5334368b54a3707df89f93ee Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 02:39:36 +0900 Subject: [PATCH 06/14] add C# --- check_autopep8 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 check_autopep8 diff --git a/check_autopep8 b/check_autopep8 deleted file mode 100644 index e69de29b..00000000 From eb0b4ed2f889c74df9e01131c3229aa0ac87e311 Mon Sep 17 00:00:00 2001 From: chaemon Date: Wed, 21 Aug 2019 02:39:56 +0900 Subject: [PATCH 07/14] add C# --- atcodertools/codegen/code_generators/cs.py | 170 ++++++++++++++++++ .../tools/templates/default_template.cs | 62 +++++++ 2 files changed, 232 insertions(+) create mode 100644 atcodertools/codegen/code_generators/cs.py create mode 100644 atcodertools/tools/templates/default_template.cs diff --git a/atcodertools/codegen/code_generators/cs.py b/atcodertools/codegen/code_generators/cs.py new file mode 100644 index 00000000..48bafc05 --- /dev/null +++ b/atcodertools/codegen/code_generators/cs.py @@ -0,0 +1,170 @@ +from typing import Dict, Any, Optional + +from atcodertools.codegen.code_style_config import CodeStyleConfig +from atcodertools.codegen.models.code_gen_args import CodeGenArgs +from atcodertools.codegen.template_engine import render +from atcodertools.fmtprediction.models.format import Pattern, SingularPattern, ParallelPattern, TwoDimensionalPattern, \ + Format +from atcodertools.fmtprediction.models.type import Type +from atcodertools.fmtprediction.models.variable import Variable + + +def _loop_header(var: Variable, for_second_index: bool): + if for_second_index: + index = var.second_index + loop_var = "j" + else: + index = var.first_index + loop_var = "i" + + return "for(int {loop_var} = 0;{loop_var} < {length};{loop_var}++)".format( + loop_var=loop_var, + length=index.get_length() + ) + + +class NimCodeGenerator: + + def __init__(self, + format_: Optional[Format[Variable]], + config: CodeStyleConfig): + self._format = format_ + self._config = config + + def generate_parameters(self) -> Dict[str, Any]: + if self._format is None: + return dict(prediction_success=False) + + return dict(formal_arguments=self._formal_arguments(), + actual_arguments=self._actual_arguments(), + input_part=self._input_part(), + prediction_success=True) + + def _input_part(self): + lines = [] + for pattern in self._format.sequence: + lines += self._render_pattern(pattern) + return "\n{indent}".format(indent=self._indent(2)).join(lines) + + def _convert_type(self, type_: Type) -> str: + if type_ == Type.float: + return "double" + elif type_ == Type.int: + return "long" + elif type_ == Type.str: + return "string" + else: + raise NotImplementedError + + def _get_declaration_type(self, var: Variable): + ctype = self._convert_type(var.type) + for _ in range(var.dim_num()): + ctype = '{}[]'.format(ctype) + return ctype + + def _actual_arguments(self) -> str: + """ + :return the string form of actual arguments e.g. "N, K, a" + """ + return ", ".join([ + v.name if v.dim_num() == 0 else '{}'.format(v.name) + for v in self._format.all_vars()]) + + def _formal_arguments(self): + """ + :return the string form of formal arguments e.g. "int N, int K, std::vector a" + """ + return ", ".join([ + "{decl_type} {name}".format( + decl_type=self._get_declaration_type(v), + name=v.name) + for v in self._format.all_vars() + ]) + + def _generate_declaration(self, var: Variable): + """ + :return: Create declaration part E.g. array[1..n] -> std::vector array = std::vector(n-1+1); + """ + if var.dim_num() == 0: + dims = [] + elif var.dim_num() == 1: + dims = [var.first_index.get_length()] + elif var.dim_num() == 2: + dims = [var.first_index.get_length(), + var.second_index.get_length()] + else: + raise NotImplementedError + ret = "{decl_type} {name}".format( + decl_type=self._get_declaration_type(var), name=var.name) + if len(dims) > 0: + t = self._convert_type(var.type) + d = [] + for dim in dims[::-1]: + d.append(str(dim)) + ret += " = new {type}[{dims}]".format(type=t, dims=",".join(d)) + ret += ";" + return ret + + def _input_code_for_var(self, var: Variable) -> str: + name = self._get_var_name(var) + if var.type == Type.float: + return '{name} = cin.ReadDouble;'.format(name=name) + elif var.type == Type.int: + return '{name} = cin.ReadLong;'.format(name=name) + elif var.type == Type.str: + return '{name} = cin.Read;'.format(name=name) + else: + raise NotImplementedError + + @staticmethod + def _get_var_name(var: Variable): + name = var.name + if var.dim_num() >= 1: + name += "[i]" + if var.dim_num() >= 2: + name += "[j]" + return name + + 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(2), + line=self._input_code_for_var(var))) + elif isinstance(pattern, TwoDimensionalPattern): + lines.append(_loop_header(representative_var, False)) + lines.append( + "{indent}{line}".format(indent=self._indent(2), line=_loop_header(representative_var, True))) + for var in pattern.all_vars(): + lines.append("{indent}{line}".format(indent=self._indent(4), + line=self._input_code_for_var(var))) + else: + raise NotImplementedError + + return lines + + def _indent(self, depth): + return self._config.indent(depth) + + +class NoPredictionResultGiven(Exception): + pass + + +def main(args: CodeGenArgs) -> str: + code_parameters = NimCodeGenerator( + args.format, args.config).generate_parameters() + return render( + args.template, + mod=args.constants.mod, + yes_str=args.constants.yes_str, + no_str=args.constants.no_str, + **code_parameters + ) diff --git a/atcodertools/tools/templates/default_template.cs b/atcodertools/tools/templates/default_template.cs new file mode 100644 index 00000000..015034c4 --- /dev/null +++ b/atcodertools/tools/templates/default_template.cs @@ -0,0 +1,62 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + {% if mod %} + const long MOD = {{ mod }}; + {% endif %} + {% if yes_str %} + const string YES = "{{ yes_str }}"; + {% endif %} + {% if no_str %} + const string NO = "{{ no_str }}"; + {% endif %} + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + {% if prediction_success %} + {{ input_part }} + new Program().Solve({{ actual_arguments }}); + {% else %} + // Failed to predict input format + {% endif %} + } + + {% if prediction_success %} + public void Solve({{ formal_arguments }}){ + + } + {% endif %} +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} From c43f20818a463a8465456a4a6eeff45d388dcd10 Mon Sep 17 00:00:00 2001 From: chaemon Date: Sun, 25 Aug 2019 17:28:10 +0900 Subject: [PATCH 08/14] add C# --- atcodertools/codegen/code_generators/cs.py | 31 +++--- .../tools/templates/default_template.cs | 1 + tests/resources/test_codegen/template.cs | 46 +++++++++ .../resources/test_codegen/template_jinja.cs | 56 +++++++++++ .../cs/echo_template.cs | 80 ++++++++++++++++ .../cs/expected_default_generated_code.cs | 72 ++++++++++++++ .../cs/expected_echo_generated_code.cs | 95 +++++++++++++++++++ .../test_float_case/cs/generated_code.txt | 61 ++++++++++++ .../test_long_case/cs/generated_code.txt | 71 ++++++++++++++ .../test_mod_case/cs/generated_code.txt | 51 ++++++++++ .../cs/generated_code.txt | 61 ++++++++++++ .../test_yes_no_case/cs/generated_code.txt | 56 +++++++++++ tests/test_codegen.py | 15 ++- 13 files changed, 681 insertions(+), 15 deletions(-) create mode 100644 tests/resources/test_codegen/template.cs create mode 100644 tests/resources/test_codegen/template_jinja.cs create mode 100644 tests/resources/test_codegen/test_default_code_generators_and_templates/cs/echo_template.cs create mode 100644 tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_default_generated_code.cs create mode 100644 tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_echo_generated_code.cs create mode 100644 tests/resources/test_codegen/test_float_case/cs/generated_code.txt create mode 100644 tests/resources/test_codegen/test_long_case/cs/generated_code.txt create mode 100644 tests/resources/test_codegen/test_mod_case/cs/generated_code.txt create mode 100644 tests/resources/test_codegen/test_two_dimensional_case/cs/generated_code.txt create mode 100644 tests/resources/test_codegen/test_yes_no_case/cs/generated_code.txt diff --git a/atcodertools/codegen/code_generators/cs.py b/atcodertools/codegen/code_generators/cs.py index 48bafc05..ad9f29f9 100644 --- a/atcodertools/codegen/code_generators/cs.py +++ b/atcodertools/codegen/code_generators/cs.py @@ -17,10 +17,10 @@ def _loop_header(var: Variable, for_second_index: bool): index = var.first_index loop_var = "i" - return "for(int {loop_var} = 0;{loop_var} < {length};{loop_var}++)".format( + return "for(int {loop_var} = 0;{loop_var} < {len};{loop_var}++)".format( loop_var=loop_var, - length=index.get_length() - ) + len=index.get_length() + ) + "{" class NimCodeGenerator: @@ -58,9 +58,10 @@ def _convert_type(self, type_: Type) -> str: def _get_declaration_type(self, var: Variable): ctype = self._convert_type(var.type) - for _ in range(var.dim_num()): - ctype = '{}[]'.format(ctype) - return ctype + if var.dim_num() == 0: + return ctype + else: + return "{}[{}]".format(ctype, ","*(var.dim_num() - 1)) def _actual_arguments(self) -> str: """ @@ -99,7 +100,7 @@ def _generate_declaration(self, var: Variable): if len(dims) > 0: t = self._convert_type(var.type) d = [] - for dim in dims[::-1]: + for dim in dims: d.append(str(dim)) ret += " = new {type}[{dims}]".format(type=t, dims=",".join(d)) ret += ";" @@ -120,9 +121,10 @@ def _input_code_for_var(self, var: Variable) -> str: def _get_var_name(var: Variable): name = var.name if var.dim_num() >= 1: - name += "[i]" - if var.dim_num() >= 2: - name += "[j]" + name += "[i" + if var.dim_num() >= 2: + name += ",j" + name += "]" return name def _render_pattern(self, pattern: Pattern): @@ -136,15 +138,18 @@ def _render_pattern(self, pattern: Pattern): 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(2), + 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(2), line=_loop_header(representative_var, True))) + "{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(4), + 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 diff --git a/atcodertools/tools/templates/default_template.cs b/atcodertools/tools/templates/default_template.cs index 015034c4..bd199cd7 100644 --- a/atcodertools/tools/templates/default_template.cs +++ b/atcodertools/tools/templates/default_template.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using static System.Console; using static System.Math; +using System.Diagnostics; public class Program{ {% if mod %} diff --git a/tests/resources/test_codegen/template.cs b/tests/resources/test_codegen/template.cs new file mode 100644 index 00000000..91004aa0 --- /dev/null +++ b/tests/resources/test_codegen/template.cs @@ -0,0 +1,46 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + ${input_part} + new Program().Solve(${actual_arguments}); + } + + public void Solve(${formal_arguments}){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/template_jinja.cs b/tests/resources/test_codegen/template_jinja.cs new file mode 100644 index 00000000..eca11f40 --- /dev/null +++ b/tests/resources/test_codegen/template_jinja.cs @@ -0,0 +1,56 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + {% if mod %} + const long MOD = {{ mod }}; + {% endif %} + {% if yes_str %} + const string YES = "{{ yes_str }}"; + {% endif %} + {% if no_str %} + const string NO = "{{ no_str }}"; + {% endif %} + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + {{ input_part }} + new Program().Solve({{ actual_arguments }}); + } + + public void Solve({{ formal_arguments }}){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/echo_template.cs b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/echo_template.cs new file mode 100644 index 00000000..3752bd4d --- /dev/null +++ b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/echo_template.cs @@ -0,0 +1,80 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; +using System.Diagnostics; + +public class Program{ + {% if mod %} + const long MOD = {{ mod }}; + {% endif %} + {% if yes_str %} + const string YES = "{{ yes_str }}"; + {% endif %} + {% if no_str %} + const string NO = "{{ no_str }}"; + {% endif %} + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + {{ input_part }} + new Program().Solve({{ actual_arguments }}); + } + + public void Solve({{ formal_arguments }}){ + WriteLine($"{N} {M}"); + Debug.Assert(H.GetLength(0) == N - 1); + for (int i = 0;i < N - 1;i++) { + Debug.Assert(H.GetLength(1) == M - 2); + for (int j = 0;j < M - 2;j++) { + Write((j > 0 ? " " : "") + $"{H[i,j]}"); + } + WriteLine(); + } + Debug.Assert(A.Length == N - 1); + Debug.Assert(B.Length == N - 1); + for(int i = 0;i < N - 1;i++){ + WriteLine($"{A[i]} {B[i]}"); + } + WriteLine(Q); + Debug.Assert(X.Length == M + Q); + for(int i = 0;i < M + Q;i++){ + WriteLine(X[i]); + } + + WriteLine(YES); + WriteLine(NO); + WriteLine(MOD); + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_default_generated_code.cs b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_default_generated_code.cs new file mode 100644 index 00000000..750d8e28 --- /dev/null +++ b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_default_generated_code.cs @@ -0,0 +1,72 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; +using System.Diagnostics; + +public class Program{ + const long MOD = 123; + const string YES = "yes"; + const string NO = "NO"; + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long N; + N = cin.ReadLong; + long M; + M = cin.ReadLong; + string[,] H = new string[N-2+1,M-1-2+1]; + for(int i = 0;i < N-2+1;i++){ + for(int j = 0;j < M-1-2+1;j++){ + H[i,j] = cin.Read; + } + } + long[] A = new long[N-2+1]; + double[] B = new double[N-2+1]; + for(int i = 0;i < N-2+1;i++){ + A[i] = cin.ReadLong; + B[i] = cin.ReadDouble; + } + long Q; + Q = cin.ReadLong; + long[] X = new long[M+Q]; + for(int i = 0;i < M+Q;i++){ + X[i] = cin.ReadLong; + } + new Program().Solve(N, M, H, A, B, Q, X); + } + + public void Solve(long N, long M, string[,] H, long[] A, double[] B, long Q, long[] X){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_echo_generated_code.cs b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_echo_generated_code.cs new file mode 100644 index 00000000..ae28b81e --- /dev/null +++ b/tests/resources/test_codegen/test_default_code_generators_and_templates/cs/expected_echo_generated_code.cs @@ -0,0 +1,95 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; +using System.Diagnostics; + +public class Program{ + const long MOD = 123; + const string YES = "yes"; + const string NO = "NO"; + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long N; + N = cin.ReadLong; + long M; + M = cin.ReadLong; + string[,] H = new string[N-2+1,M-1-2+1]; + for(int i = 0;i < N-2+1;i++){ + for(int j = 0;j < M-1-2+1;j++){ + H[i,j] = cin.Read; + } + } + long[] A = new long[N-2+1]; + double[] B = new double[N-2+1]; + for(int i = 0;i < N-2+1;i++){ + A[i] = cin.ReadLong; + B[i] = cin.ReadDouble; + } + long Q; + Q = cin.ReadLong; + long[] X = new long[M+Q]; + for(int i = 0;i < M+Q;i++){ + X[i] = cin.ReadLong; + } + new Program().Solve(N, M, H, A, B, Q, X); + } + + public void Solve(long N, long M, string[,] H, long[] A, double[] B, long Q, long[] X){ + WriteLine($"{N} {M}"); + Debug.Assert(H.GetLength(0) == N - 1); + for (int i = 0;i < N - 1;i++) { + Debug.Assert(H.GetLength(1) == M - 2); + for (int j = 0;j < M - 2;j++) { + Write((j > 0 ? " " : "") + $"{H[i,j]}"); + } + WriteLine(); + } + Debug.Assert(A.Length == N - 1); + Debug.Assert(B.Length == N - 1); + for(int i = 0;i < N - 1;i++){ + WriteLine($"{A[i]} {B[i]}"); + } + WriteLine(Q); + Debug.Assert(X.Length == M + Q); + for(int i = 0;i < M + Q;i++){ + WriteLine(X[i]); + } + + WriteLine(YES); + WriteLine(NO); + WriteLine(MOD); + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_float_case/cs/generated_code.txt b/tests/resources/test_codegen/test_float_case/cs/generated_code.txt new file mode 100644 index 00000000..c8565745 --- /dev/null +++ b/tests/resources/test_codegen/test_float_case/cs/generated_code.txt @@ -0,0 +1,61 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long L; + L = cin.ReadLong; + long N; + N = cin.ReadLong; + long M; + M = cin.ReadLong; + double[] K = new double[L]; + for(int i = 0;i < L;i++){ + K[i] = cin.ReadDouble; + } + long[] A = new long[N]; + double[] S = new double[N]; + for(int i = 0;i < N;i++){ + A[i] = cin.ReadLong; + S[i] = cin.ReadDouble; + } + new Program().Solve(L, N, M, K, A, S); + } + + public void Solve(long L, long N, long M, double[] K, long[] A, double[] S){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_long_case/cs/generated_code.txt b/tests/resources/test_codegen/test_long_case/cs/generated_code.txt new file mode 100644 index 00000000..a700243b --- /dev/null +++ b/tests/resources/test_codegen/test_long_case/cs/generated_code.txt @@ -0,0 +1,71 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long H; + H = cin.ReadLong; + long W; + W = cin.ReadLong; + long K; + K = cin.ReadLong; + long sr; + sr = cin.ReadLong; + long sc; + sc = cin.ReadLong; + string[] s = new string[H]; + for(int i = 0;i < H;i++){ + s[i] = cin.Read; + } + long N; + N = cin.ReadLong; + long[] fr = new long[N]; + long[] fc = new long[N]; + long[] F = new long[N]; + long[] D = new long[N]; + for(int i = 0;i < N;i++){ + fr[i] = cin.ReadLong; + fc[i] = cin.ReadLong; + F[i] = cin.ReadLong; + D[i] = cin.ReadLong; + } + new Program().Solve(H, W, K, sr, sc, s, N, fr, fc, F, D); + } + + public void Solve(long H, long W, long K, long sr, long sc, string[] s, long N, long[] fr, long[] fc, long[] F, long[] D){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_mod_case/cs/generated_code.txt b/tests/resources/test_codegen/test_mod_case/cs/generated_code.txt new file mode 100644 index 00000000..c7837a25 --- /dev/null +++ b/tests/resources/test_codegen/test_mod_case/cs/generated_code.txt @@ -0,0 +1,51 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + const long MOD = 998244353; + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long A; + A = cin.ReadLong; + long B; + B = cin.ReadLong; + new Program().Solve(A, B); + } + + public void Solve(long A, long B){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_two_dimensional_case/cs/generated_code.txt b/tests/resources/test_codegen/test_two_dimensional_case/cs/generated_code.txt new file mode 100644 index 00000000..9dfba539 --- /dev/null +++ b/tests/resources/test_codegen/test_two_dimensional_case/cs/generated_code.txt @@ -0,0 +1,61 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long H; + H = cin.ReadLong; + long W; + W = cin.ReadLong; + long[,] c = new long[9-0+1,9-0+1]; + for(int i = 0;i < 9-0+1;i++){ + for(int j = 0;j < 9-0+1;j++){ + c[i,j] = cin.ReadLong; + } + } + long[,] A = new long[H,W]; + for(int i = 0;i < H;i++){ + for(int j = 0;j < W;j++){ + A[i,j] = cin.ReadLong; + } + } + new Program().Solve(H, W, c, A); + } + + public void Solve(long H, long W, long[,] c, long[,] A){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/resources/test_codegen/test_yes_no_case/cs/generated_code.txt b/tests/resources/test_codegen/test_yes_no_case/cs/generated_code.txt new file mode 100644 index 00000000..704a5453 --- /dev/null +++ b/tests/resources/test_codegen/test_yes_no_case/cs/generated_code.txt @@ -0,0 +1,56 @@ +using System; +using System.Text; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using static System.Console; +using static System.Math; + +public class Program{ + const string YES = "YES"; + const string NO = "NO"; + + public static void Main(string[] args){ + ConsoleInput cin = new ConsoleInput(Console.In, ' '); + long N; + N = cin.ReadLong; + long M; + M = cin.ReadLong; + long A; + A = cin.ReadLong; + long B; + B = cin.ReadLong; + new Program().Solve(N, M, A, B); + } + + public void Solve(long N, long M, long A, long B){ + + } +} + +public class ConsoleInput{ + private readonly System.IO.TextReader _stream; + private char _separator = ' '; + private Queue inputStream; + public ConsoleInput(System.IO.TextReader stream, char separator = ' '){ + this._separator = separator; + this._stream = stream; + inputStream = new Queue(); + } + public string Read{ + get{ + if (inputStream.Count != 0) return inputStream.Dequeue(); + string[] tmp = _stream.ReadLine().Split(_separator); + for (int i = 0; i < tmp.Length; ++i) + inputStream.Enqueue(tmp[i]); + return inputStream.Dequeue(); + } + } + public string ReadLine { get { return _stream.ReadLine(); } } + public int ReadInt { get { return int.Parse(Read); } } + public long ReadLong { get { return long.Parse(Read); } } + public double ReadDouble { get { return double.Parse(Read); } } + public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;} + public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;} + public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;} +} diff --git a/tests/test_codegen.py b/tests/test_codegen.py index f3cb0e98..aa831b83 100644 --- a/tests/test_codegen.py +++ b/tests/test_codegen.py @@ -7,14 +7,14 @@ from atcodertools.client.models.problem_content import ProblemContent from atcodertools.client.models.sample import Sample -from atcodertools.common.language import ALL_LANGUAGES, Language, CPP, JAVA, RUST, PYTHON, NIM, DLANG +from atcodertools.common.language import ALL_LANGUAGES, Language, CPP, JAVA, RUST, PYTHON, NIM, DLANG, CSHARP from atcodertools.executils.run_command import run_command from atcodertools.executils.run_program import run_program from atcodertools.fileutils.create_contest_file import create_code from atcodertools.fileutils.load_text_file import load_text_file from atcodertools.fmtprediction.predict_format import predict_format -from atcodertools.codegen.code_generators import cpp, java, rust, python, nim, d +from atcodertools.codegen.code_generators import cpp, java, rust, python, nim, d, cs from atcodertools.codegen.code_style_config import CodeStyleConfig from atcodertools.codegen.models.code_gen_args import CodeGenArgs from atcodertools.codegen.template_engine import render @@ -74,6 +74,10 @@ def setUp(self): DLANG: { "old": "template.d", "jinja": "template_jinja.d", + }, + CSHARP: { + "old": "template.cs", + "jinja": "template_jinja.cs", } } self.lang_to_code_generator_func = { @@ -83,6 +87,7 @@ def setUp(self): PYTHON: python.main, NIM: nim.main, DLANG: d.main, + CSHARP: cs.main, } self.maxDiff = None @@ -182,6 +187,8 @@ def _compile_command(self, lang: Language, code_file: str): return "nim c {}".format(code_file) elif lang == DLANG: return "dmd {} -of=main".format(code_file) + elif lang == CSHARP: + return "mcs {}".format(code_file) else: raise NotImplementedError() @@ -198,6 +205,8 @@ def _exec_file_and_args(self, lang: Language) -> Tuple[str, List[str]]: return "./main", [] elif lang == DLANG: return "./main", [] + elif lang == CSHARP: + return "mono", ["main.exe"] else: raise NotImplementedError() @@ -214,6 +223,8 @@ def _clean_up(self, lang: Language): os.remove(os.path.join(self.temp_dir, "main")) elif lang == DLANG: os.remove(os.path.join(self.temp_dir, "main")) + elif lang == CSHARP: + os.remove(os.path.join(self.temp_dir, "main.exe")) else: raise NotImplementedError() From d329424daf758cce67e75e46c9fd2c202406736a Mon Sep 17 00:00:00 2001 From: chaemon Date: Sun, 25 Aug 2019 17:31:37 +0900 Subject: [PATCH 09/14] =?UTF-8?q?C#=E7=94=A8=E3=81=ABmono=20compiler?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e25302aa..0a993886 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ before_install: - sudo apt-get update --allow-insecure-repositories - sudo apt-get -y --allow-unauthenticated install --reinstall d-apt-keyring - sudo apt-get update - - sudo apt-get install rustc g++-4.9 openjdk-8-jdk nim dmd-compiler + - sudo apt-get install rustc g++-4.9 openjdk-8-jdk nim dmd-compiler mono-complete - sudo ln -f -s /usr/bin/g++-4.9 /usr/bin/g++ install: From 6cad673343aa1201a34ec6006f4cb4ec97af5ee0 Mon Sep 17 00:00:00 2001 From: chaemon Date: Sun, 25 Aug 2019 17:51:19 +0900 Subject: [PATCH 10/14] modify cs.py --- atcodertools/codegen/code_generators/cs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atcodertools/codegen/code_generators/cs.py b/atcodertools/codegen/code_generators/cs.py index ad9f29f9..c57cf893 100644 --- a/atcodertools/codegen/code_generators/cs.py +++ b/atcodertools/codegen/code_generators/cs.py @@ -61,7 +61,7 @@ def _get_declaration_type(self, var: Variable): if var.dim_num() == 0: return ctype else: - return "{}[{}]".format(ctype, ","*(var.dim_num() - 1)) + return "{}[{}]".format(ctype, "," * (var.dim_num() - 1)) def _actual_arguments(self) -> str: """ From 9e95f84124458a589f871dd0171b0b465589d7b3 Mon Sep 17 00:00:00 2001 From: chaemon Date: Tue, 27 Aug 2019 00:01:21 +0900 Subject: [PATCH 11/14] =?UTF-8?q?NIM=E3=82=92=E3=81=9D=E3=81=AE=E3=81=BE?= =?UTF-8?q?=E3=81=BE=E6=B5=81=E7=94=A8=E3=81=97=E3=81=A6=E3=81=9F=E3=81=A8?= =?UTF-8?q?=E3=81=93=E3=81=8C=E3=81=82=E3=81=A3=E3=81=9F=E3=81=AE=E3=81=A7?= =?UTF-8?q?=E3=80=81=20C#=E3=81=AE=E3=82=82=E3=81=AE=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atcodertools/codegen/code_generators/cs.py | 4 ++-- atcodertools/common/language.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/atcodertools/codegen/code_generators/cs.py b/atcodertools/codegen/code_generators/cs.py index c57cf893..9e2d3547 100644 --- a/atcodertools/codegen/code_generators/cs.py +++ b/atcodertools/codegen/code_generators/cs.py @@ -23,7 +23,7 @@ def _loop_header(var: Variable, for_second_index: bool): ) + "{" -class NimCodeGenerator: +class CsharpCodeGenerator: def __init__(self, format_: Optional[Format[Variable]], @@ -164,7 +164,7 @@ class NoPredictionResultGiven(Exception): def main(args: CodeGenArgs) -> str: - code_parameters = NimCodeGenerator( + code_parameters = CSharpCodeGenerator( args.format, args.config).generate_parameters() return render( args.template, diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index 04270fd4..5b20d2a2 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -99,7 +99,7 @@ def from_name(cls, name: str): CSHARP = Language( name="cs", - display_name="CS", + display_name="C#", extension="cs", submission_lang_pattern=re.compile(".*C# \\(Mono.*"), default_code_generator=cs.main, From 73df5c8daeded7b607caa85854cc44eef6592777 Mon Sep 17 00:00:00 2001 From: chaemon Date: Tue, 27 Aug 2019 01:48:49 +0900 Subject: [PATCH 12/14] =?UTF-8?q?C#=E3=81=AE=E3=82=B9=E3=83=9A=E3=83=AB?= =?UTF-8?q?=E3=83=9F=E3=82=B9=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atcodertools/codegen/code_generators/cs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atcodertools/codegen/code_generators/cs.py b/atcodertools/codegen/code_generators/cs.py index 9e2d3547..93e170a9 100644 --- a/atcodertools/codegen/code_generators/cs.py +++ b/atcodertools/codegen/code_generators/cs.py @@ -23,7 +23,7 @@ def _loop_header(var: Variable, for_second_index: bool): ) + "{" -class CsharpCodeGenerator: +class CSharpCodeGenerator: def __init__(self, format_: Optional[Format[Variable]], From f6dae48cd02fc9ffeec97ae18379ef087229498d Mon Sep 17 00:00:00 2001 From: chaemon Date: Sun, 1 Sep 2019 17:53:12 +0900 Subject: [PATCH 13/14] resolve conflict --- atcodertools/codegen/code_style_config.py | 4 ++-- atcodertools/common/language.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/atcodertools/codegen/code_style_config.py b/atcodertools/codegen/code_style_config.py index c78a339a..7e309f09 100644 --- a/atcodertools/codegen/code_style_config.py +++ b/atcodertools/codegen/code_style_config.py @@ -59,8 +59,8 @@ def __init__(self, if indent_width is not None: self.indent_width = indent_width - elif lang.default_code_style is not None and "indent_width" in lang.default_code_style: - self.indent_width = lang.default_code_style["indent_width"] + elif lang.default_code_style is not None and lang.default_code_style.indent_width is not None: + self.indent_width = lang.default_code_style.indent_width else: self.indent_width = 4 diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index 5b20d2a2..4eac5a2e 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -10,6 +10,13 @@ class LanguageNotFoundError(Exception): pass +class CodeStyle: + def __init__(self, + indent_width=None + ): + self.indent_width = indent_width + + class Language: def __init__(self, @@ -94,7 +101,7 @@ def from_name(cls, name: str): submission_lang_pattern=re.compile(".*Nim \\(0.*"), default_code_generator=nim.main, default_template_path=get_default_template_path('nim'), - default_code_style={"indent_width": 2} + default_code_style=CodeStyle(indent_width=2) ) CSHARP = Language( From 34fb5029da71ac6389521a13d3cf88775f39d4c4 Mon Sep 17 00:00:00 2001 From: chaemon Date: Sun, 1 Sep 2019 17:55:34 +0900 Subject: [PATCH 14/14] resolve conflict --- atcodertools/common/language.py | 1 - 1 file changed, 1 deletion(-) diff --git a/atcodertools/common/language.py b/atcodertools/common/language.py index 4eac5a2e..9d05e5ee 100644 --- a/atcodertools/common/language.py +++ b/atcodertools/common/language.py @@ -18,7 +18,6 @@ def __init__(self, class Language: - def __init__(self, name: str, display_name: str,