From bcdf72eca54dc1467f6e58da922011deecd3079d Mon Sep 17 00:00:00 2001 From: odashi Date: Wed, 15 Nov 2023 08:06:05 +0000 Subject: [PATCH] support pass/break/continue --- src/latexify/codegen/algorithmic_codegen.py | 24 +++++++++ .../codegen/algorithmic_codegen_test.py | 52 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/latexify/codegen/algorithmic_codegen.py b/src/latexify/codegen/algorithmic_codegen.py index dede73c..3c027c1 100644 --- a/src/latexify/codegen/algorithmic_codegen.py +++ b/src/latexify/codegen/algorithmic_codegen.py @@ -150,6 +150,18 @@ def visit_While(self, node: ast.While) -> str: + self._add_indent(r"\EndWhile") ) + def visit_Pass(self, node: ast.Pass) -> str: + """Visit a Pass node.""" + return self._add_indent(r"\State $\mathbf{pass}$") + + def visit_Break(self, node: ast.Break) -> str: + """Visit a Break node.""" + return self._add_indent(r"\State $\mathbf{break}$") + + def visit_Continue(self, node: ast.Continue) -> str: + """Visit a Continue node.""" + return self._add_indent(r"\State $\mathbf{continue}$") + @contextlib.contextmanager def _increment_level(self) -> Generator[None, None, None]: """Context manager controlling indent level.""" @@ -303,6 +315,18 @@ def visit_While(self, node: ast.While) -> str: + self._add_indent(r"\mathbf{end \ while}") ) + def visit_Pass(self, node: ast.Pass) -> str: + """Visit a Pass node.""" + return self._add_indent(r"\mathbf{pass}") + + def visit_Break(self, node: ast.Break) -> str: + """Visit a Break node.""" + return self._add_indent(r"\mathbf{break}") + + def visit_Continue(self, node: ast.Continue) -> str: + """Visit a Continue node.""" + return self._add_indent(r"\mathbf{continue}") + @contextlib.contextmanager def _increment_level(self) -> Generator[None, None, None]: """Context manager controlling indent level.""" diff --git a/src/latexify/codegen/algorithmic_codegen_test.py b/src/latexify/codegen/algorithmic_codegen_test.py index 80b86d3..d7469b2 100644 --- a/src/latexify/codegen/algorithmic_codegen_test.py +++ b/src/latexify/codegen/algorithmic_codegen_test.py @@ -189,6 +189,33 @@ def test_visit_while_with_else() -> None: algorithmic_codegen.AlgorithmicCodegen().visit(node) +def test_visit_pass() -> None: + node = ast.parse("pass").body[0] + assert isinstance(node, ast.Pass) + assert ( + algorithmic_codegen.AlgorithmicCodegen().visit(node) + == r"\State $\mathbf{pass}$" + ) + + +def test_visit_break() -> None: + node = ast.parse("break").body[0] + assert isinstance(node, ast.Break) + assert ( + algorithmic_codegen.AlgorithmicCodegen().visit(node) + == r"\State $\mathbf{break}$" + ) + + +def test_visit_continue() -> None: + node = ast.parse("continue").body[0] + assert isinstance(node, ast.Continue) + assert ( + algorithmic_codegen.AlgorithmicCodegen().visit(node) + == r"\State $\mathbf{continue}$" + ) + + @pytest.mark.parametrize( "code,latex", [ @@ -342,3 +369,28 @@ def test_visit_while_with_else_ipython() -> None: match="^While statement with the else clause is not supported$", ): algorithmic_codegen.IPythonAlgorithmicCodegen().visit(node) + + +def test_visit_pass_ipython() -> None: + node = ast.parse("pass").body[0] + assert isinstance(node, ast.Pass) + assert ( + algorithmic_codegen.IPythonAlgorithmicCodegen().visit(node) == r"\mathbf{pass}" + ) + + +def test_visit_break_ipython() -> None: + node = ast.parse("break").body[0] + assert isinstance(node, ast.Break) + assert ( + algorithmic_codegen.IPythonAlgorithmicCodegen().visit(node) == r"\mathbf{break}" + ) + + +def test_visit_continue_ipython() -> None: + node = ast.parse("continue").body[0] + assert isinstance(node, ast.Continue) + assert ( + algorithmic_codegen.IPythonAlgorithmicCodegen().visit(node) + == r"\mathbf{continue}" + )