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

Factor out expression codegen from function codegen #155

Merged
merged 18 commits into from
Dec 10, 2022
3 changes: 2 additions & 1 deletion src/latexify/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Package latexify.codegen."""

from latexify.codegen import function_codegen
from latexify.codegen import expression_codegen, function_codegen

ExpressionCodegen = expression_codegen.ExpressionCodegen
FunctionCodegen = function_codegen.FunctionCodegen
28 changes: 28 additions & 0 deletions src/latexify/codegen/codegen_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Any

from latexify import exceptions


def convert_constant(value: Any) -> str:
"""Helper to convert constant values to LaTeX.

Args:
value: A constant value.

Returns:
The LaTeX representation of `value`.
"""
if value is None or isinstance(value, bool):
return r"\mathrm{" + str(value) + "}"
if isinstance(value, (int, float, complex)):
# TODO(odashi): Support other symbols for the imaginary unit than j.
return str(value)
if isinstance(value, str):
return r'\textrm{"' + value + '"}'
if isinstance(value, bytes):
return r"\textrm{" + str(value) + "}"
if value is ...:
return r"\cdots"
raise exceptions.LatexifyNotSupportedError(
f"Unrecognized constant: {type(value).__name__}"
)
Loading