Skip to content

Commit 6a45ade

Browse files
author
Christopher Doris
committed
updates py macro ops
1 parent d5a96b5 commit 6a45ade

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/py_macro.jl

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,41 @@
1111
# - generator functions??
1212
# - splatting
1313

14+
const PY_MACRO_NILOPS = Dict(
15+
:help => (pyhelp, false),
16+
:int => (pyint, true),
17+
:float => (pyfloat, true),
18+
:complex => (pycomplex, true),
19+
:tuple => (pytuple, true),
20+
:list => (pylist, true),
21+
:dict => (pydict, true),
22+
:set => (pyset, true),
23+
:frozenset => (pyfrozenset, true),
24+
)
25+
1426
const PY_MACRO_UNOPS = Dict(
1527
# operators
1628
:(+) => (pypos, true),
1729
:(-) => (pyneg, true),
1830
:(~) => (pyinv, true),
1931
# builtins
2032
:abs => (pyabs, true),
33+
:all => (pyall, false),
34+
:any => (pyany, false),
2135
:ascii => (pyascii, true),
2236
:bytes => (pybytes, true),
2337
:bool => (pybool, true),
38+
:callable => (pycallable, false),
2439
:complex => (pycomplex, true),
2540
:dict => (pydict, true),
2641
:dir => (pydir, true),
2742
:float => (pyfloat, true),
2843
:frozenset => (pyfrozenset, true),
44+
:help => (pyhelp, false),
2945
:int => (pyint, true),
3046
:iter => (pyiter, true),
3147
:list => (pylist, true),
48+
:next => (pynext, true),
3249
:range => (pyrange, true),
3350
:repr => (pyrepr, true),
3451
:set => (pyset, true),
@@ -201,10 +218,18 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
201218
af = ex.args[1]
202219
# is it a special operator?
203220
isop = false
221+
if haskey(PY_MACRO_NILOPS, af)
222+
isop = true
223+
if length(ex.args) == 1
224+
op, t = PY_MACRO_NILOPS[af]
225+
py_macro_assign(body, ans, :($op()))
226+
return t
227+
end
228+
end
204229
if haskey(PY_MACRO_UNOPS, af)
205230
isop = true
206-
op, t = PY_MACRO_UNOPS[af]
207231
if length(ex.args) == 2
232+
op, t = PY_MACRO_UNOPS[af]
208233
ax, = ex.args[2:end]
209234
@gensym x
210235
tx = py_macro_lower(st, body, x, ax)
@@ -215,8 +240,8 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
215240
end
216241
if haskey(PY_MACRO_BINOPS, af)
217242
isop = true
218-
op, t = PY_MACRO_BINOPS[af]
219243
if length(ex.args) == 3
244+
op, t = PY_MACRO_BINOPS[af]
220245
ax, ay = ex.args[2:end]
221246
@gensym x y
222247
tx = py_macro_lower(st, body, x, ax)
@@ -229,8 +254,8 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
229254
end
230255
if haskey(PY_MACRO_TERNOPS, af)
231256
isop = true
232-
op, t = PY_MACRO_TERNOPS[af]
233257
if length(ex.args) == 4
258+
op, t = PY_MACRO_TERNOPS[af]
234259
ax, ay, az = ex.args[2:end]
235260
@gensym x y z
236261
tx = py_macro_lower(st, body, x, ax)
@@ -285,7 +310,14 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
285310
py_macro_assign(body, ans, :($pycallargs($f)))
286311
py_macro_del(body, f, tf)
287312
end
288-
return true
313+
if af === :print
314+
# treat print as a special case since it is variadic
315+
push!(body, :($pydel!($ans)))
316+
py_macro_assign(body, ans, nothing)
317+
return false
318+
else
319+
return true
320+
end
289321

290322
# (...)
291323
elseif isexpr(ex, :tuple)

0 commit comments

Comments
 (0)