-
Notifications
You must be signed in to change notification settings - Fork 396
Factor out expression codegen from function codegen #155
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e62ad68
please work
7a987d2
moved back
67d6f9d
same trick
2d2c3d6
move back
5bd43bb
factor expr codegen out
4f50a63
add visit_, remove expr tests from func
166f6b2
suggestiosn
a2d8de9
codegen test
d3cc202
using generic visit
1fb3d4c
extract method
446262a
only stmt codegen
e9c52de
reduce stop param
b252026
Merge branch 'master' into factor-out-expr-codegen
f8ed449
fix compatability issues
8f381aa
abc
28425f1
Visit an XYZ node.
0548b15
standard test method names
3e19894
suggestions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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__}" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Tests for latexify.codegen.codegen_utils.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import pytest | ||
|
||
from latexify import exceptions | ||
from latexify.codegen.codegen_utils import convert_constant | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"constant,latex", | ||
[ | ||
(None, r"\mathrm{None}"), | ||
(True, r"\mathrm{True}"), | ||
(False, r"\mathrm{False}"), | ||
(123, "123"), | ||
(456.789, "456.789"), | ||
(-3 + 4j, "(-3+4j)"), | ||
("string", r'\textrm{"string"}'), | ||
(..., r"\cdots"), | ||
], | ||
) | ||
def test_convert_constant(constant: Any, latex: str) -> None: | ||
assert convert_constant(constant) == latex | ||
|
||
|
||
def test_convert_constant_unsupported_constant() -> None: | ||
with pytest.raises( | ||
exceptions.LatexifyNotSupportedError, match="^Unrecognized constant: " | ||
): | ||
convert_constant({}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.