Skip to content

Commit 662e7ae

Browse files
author
Christopher Doris
committed
improve pymacro docstring
1 parent b9aafe9 commit 662e7ae

File tree

1 file changed

+69
-63
lines changed

1 file changed

+69
-63
lines changed

Diff for: src/PyMacro/PyMacro.jl

+69-63
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ const PY_MACRO_UNOPS = Dict(
6666
:tuple => (pytuple, true),
6767
:type => (pytype, true),
6868
# builtins converting to julia
69-
:jlascii => (x->pyascii(String,x), false),
69+
:jlascii => (x -> pyascii(String, x), false),
7070
:jlbool => (pytruth, false),
71-
:jlbytes => (x->pybytes(Base.CodeUnits,x), false),
71+
:jlbytes => (x -> pybytes(Base.CodeUnits, x), false),
7272
:jlhash => (pyhash, false),
7373
:jllen => (pylen, false),
74-
:jlrepr => (x->pyrepr(String,x), false),
75-
:jlstr => (x->pystr(String,x), false),
74+
:jlrepr => (x -> pyrepr(String, x), false),
75+
:jlstr => (x -> pystr(String, x), false),
7676
# jlcomplex
7777
)
7878

@@ -92,13 +92,13 @@ const PY_MACRO_BINOPS = Dict(
9292
:() => (pyxor, true),
9393
:(==) => (pyeq, true),
9494
:(!=) => (pyne, true),
95-
:( ) => (pyne, true),
95+
:() => (pyne, true),
9696
:(<=) => (pyle, true),
97-
:( ) => (pyle, true),
98-
:(< ) => (pylt, true),
97+
:() => (pyle, true),
98+
:(<) => (pylt, true),
9999
:(>=) => (pyge, true),
100-
:( ) => (pyge, true),
101-
:(> ) => (pygt, true),
100+
:() => (pyge, true),
101+
:(>) => (pygt, true),
102102
:(===) => (pyis, false),
103103
:() => (pyis, false),
104104
:(!==) => (pyisnot, false),
@@ -129,10 +129,10 @@ const PY_MACRO_TERNOPS = Dict(
129129
)
130130

131131
Base.@kwdef mutable struct PyMacroState
132-
mod :: Module
133-
src :: LineNumberNode
134-
consts :: IdDict{Any,Py} = IdDict{Any,Py}()
135-
inits :: Vector{Any} = []
132+
mod::Module
133+
src::LineNumberNode
134+
consts::IdDict{Any,Py} = IdDict{Any,Py}()
135+
inits::Vector{Any} = []
136136
end
137137

138138
function py_macro_err(st, ex, msg=nothing)
@@ -147,48 +147,51 @@ end
147147

148148
py_macro_assign(body, ans, ex) = push!(body, :($ans = $ex))
149149

150-
py_macro_del(body, var, tmp) = if tmp; push!(body, :($pydel!($var))); end
150+
py_macro_del(body, var, tmp) =
151+
if tmp
152+
push!(body, :($pydel!($var)))
153+
end
151154

152155
ismacroexpr(ex, name) = isexpr(ex, :macrocall) && (ex.args[1] === Symbol(name) || ex.args[1] === GlobalRef(Base.Core, Symbol(name)))
153156

154157
function py_macro_lower(st, body, ans, ex; flavour=:expr)
155158

156159
# scalar literals
157-
if ex isa Union{Nothing, String, Bool, Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128, BigInt, Float16, Float32, Float64}
160+
if ex isa Union{Nothing,String,Bool,Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt,Float16,Float32,Float64}
158161
x = get!(pynew, st.consts, ex)
159162
py_macro_assign(body, ans, x)
160163
return false
161164

162-
# Int128 literals
165+
# Int128 literals
163166
elseif ismacroexpr(ex, "@int128_str")
164167
value = parse(Int128, ex.args[3])
165168
x = get!(pynew, st.consts, value)
166169
py_macro_assign(body, ans, x)
167170
return false
168171

169-
# UInt128 literals
172+
# UInt128 literals
170173
elseif ismacroexpr(ex, "@uint128_str")
171174
value = parse(UInt128, ex.args[3])
172175
x = get!(pynew, st.consts, value)
173176
py_macro_assign(body, ans, x)
174177
return false
175178

176-
# big integer literals
179+
# big integer literals
177180
elseif ismacroexpr(ex, "@big_str")
178181
value = parse(BigInt, ex.args[3])
179182
x = get!(pynew, st.consts, value)
180183
py_macro_assign(body, ans, x)
181184
return false
182185

183-
# __file__
186+
# __file__
184187
elseif ex === :__file__
185188
return py_macro_lower(st, body, ans, string(st.src.file))
186189

187-
# __line__
190+
# __line__
188191
elseif ex === :__line__
189192
return py_macro_lower(st, body, ans, st.src.line)
190193

191-
# x
194+
# x
192195
elseif ex isa Symbol
193196
if ex in BUILTINS
194197
py_macro_assign(body, ans, :($pybuiltins.$ex))
@@ -197,37 +200,37 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
197200
end
198201
return false
199202

200-
# x:y:z
201-
elseif flavour==:index && @capture(ex, ax_:ay_:az_)
203+
# x:y:z
204+
elseif flavour == :index && @capture(ex, ax_:ay_:az_)
202205
@gensym x y z
203-
tx = py_macro_lower(st, body, x, ax===:_ ? :None : ax)
204-
ty = py_macro_lower(st, body, y, ay===:_ ? :None : ay)
205-
tz = py_macro_lower(st, body, z, az===:_ ? :None : az)
206+
tx = py_macro_lower(st, body, x, ax === :_ ? :None : ax)
207+
ty = py_macro_lower(st, body, y, ay === :_ ? :None : ay)
208+
tz = py_macro_lower(st, body, z, az === :_ ? :None : az)
206209
py_macro_assign(body, ans, :($pyslice($x, $y, $z)))
207210
py_macro_del(body, x, tx)
208211
py_macro_del(body, y, ty)
209212
py_macro_del(body, z, tz)
210213
return true
211214

212-
# x:y
213-
elseif flavour==:index && @capture(ex, ax_:ay_)
215+
# x:y
216+
elseif flavour == :index && @capture(ex, ax_:ay_)
214217
@gensym x y
215-
tx = py_macro_lower(st, body, x, ax===:_ ? :None : ax)
216-
ty = py_macro_lower(st, body, y, ay===:_ ? :None : ay)
218+
tx = py_macro_lower(st, body, x, ax === :_ ? :None : ax)
219+
ty = py_macro_lower(st, body, y, ay === :_ ? :None : ay)
217220
py_macro_assign(body, ans, :($pyslice($x, $y)))
218221
py_macro_del(body, x, tx)
219222
py_macro_del(body, y, ty)
220223
return true
221224

222-
# x + y + z + ...
225+
# x + y + z + ...
223226
elseif @capture(ex, +(ax_, ay_, az_, args__))
224-
return py_macro_lower(st, body, ans, foldl((x, y)->:($x+$y), (ax, ay, az, args...)))
227+
return py_macro_lower(st, body, ans, foldl((x, y) -> :($x + $y), (ax, ay, az, args...)))
225228

226-
# x * y * z * ...
229+
# x * y * z * ...
227230
elseif @capture(ex, *(ax_, ay_, az_, args__))
228-
return py_macro_lower(st, body, ans, foldl((x, y)->:($x*$y), (ax, ay, az, args...)))
231+
return py_macro_lower(st, body, ans, foldl((x, y) -> :($x * $y), (ax, ay, az, args...)))
229232

230-
# f(args...; kwargs...)
233+
# f(args...; kwargs...)
231234
elseif isexpr(ex, :call)
232235
af = ex.args[1]
233236
# is it a special operator?
@@ -333,22 +336,22 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
333336
return true
334337
end
335338

336-
# (...)
339+
# (...)
337340
elseif isexpr(ex, :tuple)
338341
if any(isexpr(arg, :...) for arg in ex.args)
339342
py_macro_err(st, ex, "splatting into tuples not implemented")
340343
else
341344
py_macro_assign(body, ans, :($pynulltuple($(length(ex.args)))))
342345
@gensym a
343346
for (i, aa) in enumerate(ex.args)
344-
ta = py_macro_lower(st, body, a, aa, flavour = flavour==:index ? :index : :expr)
345-
push!(body, :($pytuple_setitem($ans, $(i-1), $a)))
347+
ta = py_macro_lower(st, body, a, aa, flavour=flavour == :index ? :index : :expr)
348+
push!(body, :($pytuple_setitem($ans, $(i - 1), $a)))
346349
py_macro_del(body, a, ta)
347350
end
348351
return true
349352
end
350353

351-
# [...]
354+
# [...]
352355
elseif isexpr(ex, :vect)
353356
if any(isexpr(arg, :...) for arg in ex.args)
354357
py_macro_err(st, ex, "splatting into tuples not implemented")
@@ -357,13 +360,13 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
357360
@gensym a
358361
for (i, aa) in enumerate(ex.args)
359362
ta = py_macro_lower(st, body, a, aa)
360-
push!(body, :($pylist_setitem($ans, $(i-1), $a)))
363+
push!(body, :($pylist_setitem($ans, $(i - 1), $a)))
361364
py_macro_del(body, a, ta)
362365
end
363366
return true
364367
end
365368

366-
# {...}
369+
# {...}
367370
elseif isexpr(ex, :braces)
368371
# Like Python, we allow braces to be set or dict literals.
369372
#
@@ -398,7 +401,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
398401
push!(body, :($pydict_setitem($ans, $k, $v)))
399402
py_macro_del(body, k, tk)
400403
py_macro_del(body, v, tv)
401-
elseif @capture(aa, ak_ : av_)
404+
elseif @capture(aa, ak_:av_)
402405
tk = py_macro_lower(st, body, k, ak)
403406
tv = py_macro_lower(st, body, v, av)
404407
push!(body, :($pydict_setitem($ans, $k, $v)))
@@ -423,7 +426,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
423426
return true
424427
end
425428

426-
# x.k
429+
# x.k
427430
elseif isexpr(ex, :.)
428431
ax, ak = ex.args
429432
@gensym x k
@@ -438,7 +441,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
438441
py_macro_del(body, k, tk)
439442
return true
440443

441-
# x[k]
444+
# x[k]
442445
elseif @capture(ex, ax_[ak__])
443446
@gensym x k
444447
tx = py_macro_lower(st, body, x, ax)
@@ -452,13 +455,13 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
452455
py_macro_del(body, k, tk)
453456
return true
454457

455-
# x = y
458+
# x = y
456459
elseif @capture(ex, ax_ = ay_)
457460
ty = py_macro_lower(st, body, ans, ay)
458461
py_macro_lower_assign(st, body, ax, ans)
459462
return ty
460463

461-
# @del x, y, ...
464+
# @del x, y, ...
462465
elseif @capture(ex, @del (args__,))
463466

464467
for arg in args
@@ -477,7 +480,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
477480
py_macro_del(body, x, tx)
478481
py_macro_del(body, k, tk)
479482

480-
# @del x[k]
483+
# @del x[k]
481484
elseif @capture(arg, ax_[ak__])
482485
@gensym x k
483486
tx = py_macro_lower(st, body, x, ax)
@@ -497,26 +500,26 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
497500
py_macro_assign(body, ans, nothing)
498501
return false
499502

500-
# @del x
503+
# @del x
501504
elseif @capture(ex, @del arg_)
502505
return py_macro_lower(st, body, ans, :(@del ($arg,)))
503506

504-
# @jl x
507+
# @jl x
505508
elseif @capture(ex, @jl ax_)
506509
y = py_macro_lower_jl(st, ax)
507510
py_macro_assign(body, ans, y)
508511
return false
509512

510-
# @compile code mode=mode ...
511-
elseif @capture(ex, @compile code_String mode=mode_String args__)
513+
# @compile code mode=mode ...
514+
elseif @capture(ex, @compile code_String mode = mode_String args__)
512515
x = pynew()
513516
args = [isexpr(arg, :(=)) ? Expr(:kw, arg.args...) : arg for arg in args]
514517
filename = "$(st.src.file):$(st.src.line)"
515518
push!(st.inits, :($pycopy!($x, $pybuiltins.compile($code, filename=$filename, mode=$mode, $(args...)))))
516519
py_macro_assign(body, ans, x)
517520
return false
518521

519-
# @exec code ...
522+
# @exec code ...
520523
elseif @capture(ex, @exec code_String args__)
521524
args = [isexpr(arg, :(=)) ? Expr(:kw, arg.args...) : arg for arg in args]
522525
ex2 = Expr(:macrocall, Symbol("@compile"), st.src, code, :(mode = "exec"))
@@ -527,14 +530,14 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
527530
py_macro_assign(body, ans, nothing)
528531
return false
529532

530-
# @eval code ...
533+
# @eval code ...
531534
elseif @capture(ex, @eval code_String args__)
532535
args = [isexpr(arg, :(=)) ? Expr(:kw, arg.args...) : arg for arg in args]
533536
ex2 = Expr(:macrocall, Symbol("@compile"), st.src, code, :(mode = "eval"))
534537
ex2 = Expr(:call, :eval, ex2, args...)
535538
return py_macro_lower(st, body, ans, ex2)
536539

537-
# begin; ...; end
540+
# begin; ...; end
538541
elseif isexpr(ex, :block)
539542
if isempty(ex.args)
540543
py_macro_assign(body, ans, nothing)
@@ -560,7 +563,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
560563
@assert false
561564
end
562565

563-
# if x; ...; end
566+
# if x; ...; end
564567
elseif isexpr(ex, :if, :elseif)
565568
if length(ex.args) == 2
566569
return py_macro_lower(st, body, ans, Expr(ex.head, ex.args..., nothing))
@@ -581,7 +584,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
581584
return t
582585
end
583586

584-
# x && y
587+
# x && y
585588
elseif isexpr(ex, :&&)
586589
ax, ay = ex.args
587590
tx = py_macro_lower(st, body, ans, ax)
@@ -597,7 +600,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
597600
push!(body, Expr(:if, :($pytruth($ans)), Expr(:block, body2...), Expr(:block, body3...)))
598601
return t
599602

600-
# x || y
603+
# x || y
601604
elseif isexpr(ex, :||)
602605
ax, ay = ex.args
603606
tx = py_macro_lower(st, body, ans, ax)
@@ -613,7 +616,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
613616
push!(body, Expr(:if, :($pytruth($ans)), Expr(:block, body2...), Expr(:block, body3...)))
614617
return t
615618

616-
# while x; ...; end
619+
# while x; ...; end
617620
elseif isexpr(ex, :while)
618621
ax, ay = ex.args
619622
@gensym x y
@@ -626,8 +629,10 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
626629
py_macro_assign(body, ans, nothing)
627630
return false
628631

629-
# for x in y; ...; end
630-
elseif @capture(ex, for ax_ in ay_; az_; end)
632+
# for x in y; ...; end
633+
elseif @capture(ex, for ax_ in ay_
634+
az_
635+
end)
631636
@gensym y i v z
632637
ty = py_macro_lower(st, body, y, ay)
633638
push!(body, :($i = $pyiter($y)))
@@ -644,7 +649,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
644649
py_macro_assign(body, ans, nothing)
645650
return false
646651

647-
# import ...
652+
# import ...
648653
elseif isexpr(ex, :import)
649654
for aa in ex.args
650655
if isexpr(aa, :as)
@@ -680,7 +685,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
680685
py_macro_assign(body, ans, nothing)
681686
return false
682687

683-
# "...$foo..."
688+
# "...$foo..."
684689
elseif isexpr(ex, :string)
685690
args = [a isa String ? a : :(str($a)) for a in ex.args]
686691
return py_macro_lower(st, body, ans, :("".join(($(args...),))))
@@ -795,8 +800,9 @@ Evaluate the given expression using Pythonic semantics.
795800
For example:
796801
- `f(x, y)` is translated to `pycall(f, x, y)`
797802
- `x + y` is translated to `pyadd(x, y)`
798-
- `x === y` is translated to `pyis(x, y)`
803+
- `x === y` is translated to `pyis(x, y)` (`x is y` in Python)
799804
- `x.foo` is translated to `pygetattr(x, "foo")`
805+
- `import x: f as g` is translated to `g = pyimport("x" => "f")` (`from x import f as g` in Python)
800806
801807
Compound statements such as `begin`, `if`, `while` and `for` are supported.
802808

0 commit comments

Comments
 (0)