Skip to content
Open
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
1 change: 1 addition & 0 deletions macropy/core/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def detect_macro(self, in_tree):
in_tree.body, call_args,
{'target': wi.optional_vars},
name)
in_tree.body = new_tree

if new_tree:
if isinstance(new_tree, ast.expr):
Expand Down
16 changes: 14 additions & 2 deletions macropy/core/test/macros/basic_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from macropy.core.test.macros.basic_block_macro import macros, my_macro
from macropy.core.test.macros.basic_block_macro import macros, my_macro, my_nested_outer, my_nested_inner

def run():
x = 10
with my_macro as y:
x = x + 1
return x

z = 10
with my_nested_outer:
with my_nested_inner:
z = z + 1 # should get replaced by z = z * 2
assert z == 80

z = 10
with my_nested_inner, my_nested_outer:
z = z + 1
assert z == 80

return x
9 changes: 9 additions & 0 deletions macropy/core/test/macros/basic_block_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ def my_macro(tree, target, **kw):
assert macropy.core.unparse(target) == "y"
assert macropy.core.unparse(tree).strip() == "x = (x + 1)", macropy.core.unparse(tree)
return tree * 3

@macros.block
def my_nested_outer(tree, **kw):
assert macropy.core.unparse(tree).strip() == "z = (z * 2)", macropy.core.unparse(tree)
return tree * 3

@macros.block
def my_nested_inner(tree, **kw): # important: generate new tree from scratch for this test
return [macropy.core.parse_stmt("z = z * 2")]