Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pass/break/continue in algorithmic codegens #192

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/latexify/codegen/algorithmic_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
52 changes: 52 additions & 0 deletions src/latexify/codegen/algorithmic_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down Expand Up @@ -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}"
)