Skip to content

Commit 431552c

Browse files
committed
add dbg macro
1 parent dc74e6a commit 431552c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

unpythonic/syntax/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
dlet as _dlet, dletseq as _dletseq, dletrec as _dletrec, \
2525
blet as _blet, bletseq as _bletseq, bletrec as _bletrec
2626
from .letsyntax import let_syntax_expr, let_syntax_block, block, expr
27-
from .nb import nb as _nb
27+
from .nb import nb as _nb, dbg as _dbg
2828
from .prefix import prefix as _prefix
2929
from .tailtools import autoreturn as _autoreturn, tco as _tco, \
3030
continuations as _continuations, call_cc
@@ -1424,6 +1424,11 @@ def nb(tree, args, **kw):
14241424
"""
14251425
return _nb(body=tree, args=args)
14261426

1427+
@macros.block
1428+
def dbg(tree, args, **kw):
1429+
"""[syntax, block] TODO: document""" # TODO: document
1430+
return _dbg(body=tree, args=args)
1431+
14271432
# -----------------------------------------------------------------------------
14281433

14291434
@macros.block

unpythonic/syntax/nb.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
# This is the kind of thing thinking with macros does to your program. ;)
77

8-
from ast import Expr
8+
from ast import Expr, Call, Name
99

10-
from macropy.core.quotes import macros, q, ast_literal
10+
from macropy.core.quotes import macros, q, u, ast_literal
11+
from macropy.core.walkers import Walker
1112

1213
def nb(body, args):
1314
p = args[0] if args else q[print] # custom print function hook
@@ -26,3 +27,27 @@ def nb(body, args):
2627
theprint(_)
2728
newbody.extend(newstmts)
2829
return newbody
30+
31+
# with dbg:
32+
# x = 3
33+
# print(x) # --> "x: <value>"
34+
#
35+
# with dbg(prt):
36+
# x = 3
37+
# prt(x) # --> prt(name, value)
38+
#
39+
def dbg(body, args):
40+
p = args[0] if args else q[print] # custom print function hook
41+
if type(p) is not Name:
42+
assert False, "The print function can only be specified by a bare name"
43+
theid = p.id
44+
45+
@Walker
46+
def transform(tree, *, stop, **kw):
47+
if type(tree) is Call and type(tree.func) is Name and tree.func.id == theid and \
48+
len(tree.args) == 1 and type(tree.args[0]) is Name:
49+
varname = q[u[tree.args[0].id]]
50+
tree.args.insert(0, varname)
51+
return tree
52+
53+
return [transform.recurse(stmt) for stmt in body]

unpythonic/syntax/test/test_nb.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from ...syntax import macros, nb
3+
from ...syntax import macros, nb, dbg
44

55
def test():
66
with nb:
@@ -23,6 +23,15 @@ def test():
2323
3 * _
2424
assert _ == 3 * x * y
2525

26+
with dbg:
27+
x = 3
28+
print(x)
29+
30+
prt = lambda *args: print(*args)
31+
with dbg(prt):
32+
x = 5
33+
prt(x)
34+
2635
print("All tests PASSED")
2736

2837
if __name__ == '__main__':

0 commit comments

Comments
 (0)