Skip to content

Commit d3d25fa

Browse files
committed
add transplier code for generatorExp
1 parent 1d60d56 commit d3d25fa

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/astx/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
ForRangeLoopExpr,
6868
ForRangeLoopStmt,
6969
GotoStmt,
70+
GeneratorExpr,
7071
IfExpr,
7172
IfStmt,
7273
SwitchStmt,
@@ -241,6 +242,7 @@ def get_version() -> str:
241242
"FunctionDef",
242243
"FunctionPrototype",
243244
"FunctionReturn",
245+
"GeneratorExpr",
244246
"GotoStmt",
245247
"Identifier",
246248
"IfExpr",

src/astx/flows.py

-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ def get_struct(self, simplified: bool = False) -> ReprStruct:
730730
value: ReprStruct = {
731731
"element": self.element.get_struct(simplified),
732732
"target": self.target.get_struct(simplified),
733-
# "iterator": self.iterator.get_struct(simplified)
734733
}
735734
if isinstance(self.iterator, (LiteralList, LiteralTuple, LiteralSet)):
736735
value["iterator"] = {

src/astx/tools/transpilers/python.py

+5
Original file line numberDiff line numberDiff line change
@@ -674,3 +674,8 @@ def visit(self, node: astx.LiteralDict) -> str:
674674
for key, value in node.elements.items()
675675
)
676676
return f"{{{items_code}}}"
677+
678+
@dispatch # type: ignore[no-redef]
679+
def visit(self, node: astx.GeneratorExpr) -> str:
680+
"""Handle GeneratorExr nodes."""
681+
return f"({self.visit(node.element)} for {self.visit(node.target)} in {self.visit(node.iterator)})".strip()

tests/test_flows.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
"""Tests for control flow statements."""
22

3-
import os,sys
4-
# print(os.path.abspath(os.path.join(os.getcwd(),".", "src")))
5-
sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), ".", "src")))
6-
73
import astx
84
import pytest
95

tests/tools/transpilers/test_python.py

+16
Original file line numberDiff line numberDiff line change
@@ -1397,3 +1397,19 @@ def test_transpiler_literal_dict() -> None:
13971397
1: 10,
13981398
2: 20,
13991399
}, f"Expected '{expected_code}', but got '{generated_code}'"
1400+
1401+
def test_generator_expression() -> None:
1402+
gen = astx.GeneratorExpr(
1403+
astx.Identifier('x*x'),
1404+
astx.Identifier('x'),
1405+
astx.LiteralList(
1406+
list = [
1407+
astx.LiteralInt32(4),
1408+
astx.LiteralInt32(1),
1409+
astx.LiteralInt32(5)
1410+
]
1411+
)
1412+
)
1413+
generated_code = transpiler.visit(gen)
1414+
expected_code = "(x*x for x in [4, 1, 5])"
1415+
assert generated_code == expected_code , f"expected: {expected_code} ; generated: {generated_code}"

0 commit comments

Comments
 (0)