@@ -66,13 +66,13 @@ const PY_MACRO_UNOPS = Dict(
66
66
:tuple => (pytuple, true ),
67
67
:type => (pytype, true ),
68
68
# builtins converting to julia
69
- :jlascii => (x-> pyascii (String,x), false ),
69
+ :jlascii => (x -> pyascii (String, x), false ),
70
70
:jlbool => (pytruth, false ),
71
- :jlbytes => (x-> pybytes (Base. CodeUnits,x), false ),
71
+ :jlbytes => (x -> pybytes (Base. CodeUnits, x), false ),
72
72
:jlhash => (pyhash, false ),
73
73
: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 ),
76
76
# jlcomplex
77
77
)
78
78
@@ -92,13 +92,13 @@ const PY_MACRO_BINOPS = Dict(
92
92
:(⊻ ) => (pyxor, true ),
93
93
:(== ) => (pyeq, true ),
94
94
:(!= ) => (pyne, true ),
95
- :(≠ ) => (pyne, true ),
95
+ :(≠ ) => (pyne, true ),
96
96
:(<= ) => (pyle, true ),
97
- :(≤ ) => (pyle, true ),
98
- :(< ) => (pylt, true ),
97
+ :(≤ ) => (pyle, true ),
98
+ :(< ) => (pylt, true ),
99
99
:(>= ) => (pyge, true ),
100
- :(≥ ) => (pyge, true ),
101
- :(> ) => (pygt, true ),
100
+ :(≥ ) => (pyge, true ),
101
+ :(> ) => (pygt, true ),
102
102
:(=== ) => (pyis, false ),
103
103
:(≡ ) => (pyis, false ),
104
104
:(!= = ) => (pyisnot, false ),
@@ -129,10 +129,10 @@ const PY_MACRO_TERNOPS = Dict(
129
129
)
130
130
131
131
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} = []
136
136
end
137
137
138
138
function py_macro_err (st, ex, msg= nothing )
@@ -147,48 +147,51 @@ end
147
147
148
148
py_macro_assign (body, ans, ex) = push! (body, :($ ans = $ ex))
149
149
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
151
154
152
155
ismacroexpr (ex, name) = isexpr (ex, :macrocall ) && (ex. args[1 ] === Symbol (name) || ex. args[1 ] === GlobalRef (Base. Core, Symbol (name)))
153
156
154
157
function py_macro_lower (st, body, ans, ex; flavour= :expr )
155
158
156
159
# 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}
158
161
x = get! (pynew, st. consts, ex)
159
162
py_macro_assign (body, ans, x)
160
163
return false
161
164
162
- # Int128 literals
165
+ # Int128 literals
163
166
elseif ismacroexpr (ex, " @int128_str" )
164
167
value = parse (Int128, ex. args[3 ])
165
168
x = get! (pynew, st. consts, value)
166
169
py_macro_assign (body, ans, x)
167
170
return false
168
171
169
- # UInt128 literals
172
+ # UInt128 literals
170
173
elseif ismacroexpr (ex, " @uint128_str" )
171
174
value = parse (UInt128, ex. args[3 ])
172
175
x = get! (pynew, st. consts, value)
173
176
py_macro_assign (body, ans, x)
174
177
return false
175
178
176
- # big integer literals
179
+ # big integer literals
177
180
elseif ismacroexpr (ex, " @big_str" )
178
181
value = parse (BigInt, ex. args[3 ])
179
182
x = get! (pynew, st. consts, value)
180
183
py_macro_assign (body, ans, x)
181
184
return false
182
185
183
- # __file__
186
+ # __file__
184
187
elseif ex === :__file__
185
188
return py_macro_lower (st, body, ans, string (st. src. file))
186
189
187
- # __line__
190
+ # __line__
188
191
elseif ex === :__line__
189
192
return py_macro_lower (st, body, ans, st. src. line)
190
193
191
- # x
194
+ # x
192
195
elseif ex isa Symbol
193
196
if ex in BUILTINS
194
197
py_macro_assign (body, ans, :($ pybuiltins.$ ex))
@@ -197,37 +200,37 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
197
200
end
198
201
return false
199
202
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_)
202
205
@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)
206
209
py_macro_assign (body, ans, :($ pyslice ($ x, $ y, $ z)))
207
210
py_macro_del (body, x, tx)
208
211
py_macro_del (body, y, ty)
209
212
py_macro_del (body, z, tz)
210
213
return true
211
214
212
- # x:y
213
- elseif flavour== :index && @capture (ex, ax_: ay_)
215
+ # x:y
216
+ elseif flavour == :index && @capture (ex, ax_: ay_)
214
217
@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)
217
220
py_macro_assign (body, ans, :($ pyslice ($ x, $ y)))
218
221
py_macro_del (body, x, tx)
219
222
py_macro_del (body, y, ty)
220
223
return true
221
224
222
- # x + y + z + ...
225
+ # x + y + z + ...
223
226
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... )))
225
228
226
- # x * y * z * ...
229
+ # x * y * z * ...
227
230
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... )))
229
232
230
- # f(args...; kwargs...)
233
+ # f(args...; kwargs...)
231
234
elseif isexpr (ex, :call )
232
235
af = ex. args[1 ]
233
236
# is it a special operator?
@@ -333,22 +336,22 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
333
336
return true
334
337
end
335
338
336
- # (...)
339
+ # (...)
337
340
elseif isexpr (ex, :tuple )
338
341
if any (isexpr (arg, :... ) for arg in ex. args)
339
342
py_macro_err (st, ex, " splatting into tuples not implemented" )
340
343
else
341
344
py_macro_assign (body, ans, :($ pynulltuple ($ (length (ex. args)))))
342
345
@gensym a
343
346
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)))
346
349
py_macro_del (body, a, ta)
347
350
end
348
351
return true
349
352
end
350
353
351
- # [...]
354
+ # [...]
352
355
elseif isexpr (ex, :vect )
353
356
if any (isexpr (arg, :... ) for arg in ex. args)
354
357
py_macro_err (st, ex, " splatting into tuples not implemented" )
@@ -357,13 +360,13 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
357
360
@gensym a
358
361
for (i, aa) in enumerate (ex. args)
359
362
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)))
361
364
py_macro_del (body, a, ta)
362
365
end
363
366
return true
364
367
end
365
368
366
- # {...}
369
+ # {...}
367
370
elseif isexpr (ex, :braces )
368
371
# Like Python, we allow braces to be set or dict literals.
369
372
#
@@ -398,7 +401,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
398
401
push! (body, :($ pydict_setitem ($ ans, $ k, $ v)))
399
402
py_macro_del (body, k, tk)
400
403
py_macro_del (body, v, tv)
401
- elseif @capture (aa, ak_ : av_)
404
+ elseif @capture (aa, ak_: av_)
402
405
tk = py_macro_lower (st, body, k, ak)
403
406
tv = py_macro_lower (st, body, v, av)
404
407
push! (body, :($ pydict_setitem ($ ans, $ k, $ v)))
@@ -423,7 +426,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
423
426
return true
424
427
end
425
428
426
- # x.k
429
+ # x.k
427
430
elseif isexpr (ex, :.)
428
431
ax, ak = ex. args
429
432
@gensym x k
@@ -438,7 +441,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
438
441
py_macro_del (body, k, tk)
439
442
return true
440
443
441
- # x[k]
444
+ # x[k]
442
445
elseif @capture (ex, ax_[ak__])
443
446
@gensym x k
444
447
tx = py_macro_lower (st, body, x, ax)
@@ -452,13 +455,13 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
452
455
py_macro_del (body, k, tk)
453
456
return true
454
457
455
- # x = y
458
+ # x = y
456
459
elseif @capture (ex, ax_ = ay_)
457
460
ty = py_macro_lower (st, body, ans, ay)
458
461
py_macro_lower_assign (st, body, ax, ans)
459
462
return ty
460
463
461
- # @del x, y, ...
464
+ # @del x, y, ...
462
465
elseif @capture (ex, @del (args__,))
463
466
464
467
for arg in args
@@ -477,7 +480,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
477
480
py_macro_del (body, x, tx)
478
481
py_macro_del (body, k, tk)
479
482
480
- # @del x[k]
483
+ # @del x[k]
481
484
elseif @capture (arg, ax_[ak__])
482
485
@gensym x k
483
486
tx = py_macro_lower (st, body, x, ax)
@@ -497,26 +500,26 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
497
500
py_macro_assign (body, ans, nothing )
498
501
return false
499
502
500
- # @del x
503
+ # @del x
501
504
elseif @capture (ex, @del arg_)
502
505
return py_macro_lower (st, body, ans, :(@del ($ arg,)))
503
506
504
- # @jl x
507
+ # @jl x
505
508
elseif @capture (ex, @jl ax_)
506
509
y = py_macro_lower_jl (st, ax)
507
510
py_macro_assign (body, ans, y)
508
511
return false
509
512
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__)
512
515
x = pynew ()
513
516
args = [isexpr (arg, :(= )) ? Expr (:kw , arg. args... ) : arg for arg in args]
514
517
filename = " $(st. src. file) :$(st. src. line) "
515
518
push! (st. inits, :($ pycopy! ($ x, $ pybuiltins. compile ($ code, filename= $ filename, mode= $ mode, $ (args... )))))
516
519
py_macro_assign (body, ans, x)
517
520
return false
518
521
519
- # @exec code ...
522
+ # @exec code ...
520
523
elseif @capture (ex, @exec code_String args__)
521
524
args = [isexpr (arg, :(= )) ? Expr (:kw , arg. args... ) : arg for arg in args]
522
525
ex2 = Expr (:macrocall , Symbol (" @compile" ), st. src, code, :(mode = " exec" ))
@@ -527,14 +530,14 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
527
530
py_macro_assign (body, ans, nothing )
528
531
return false
529
532
530
- # @eval code ...
533
+ # @eval code ...
531
534
elseif @capture (ex, @eval code_String args__)
532
535
args = [isexpr (arg, :(= )) ? Expr (:kw , arg. args... ) : arg for arg in args]
533
536
ex2 = Expr (:macrocall , Symbol (" @compile" ), st. src, code, :(mode = " eval" ))
534
537
ex2 = Expr (:call , :eval , ex2, args... )
535
538
return py_macro_lower (st, body, ans, ex2)
536
539
537
- # begin; ...; end
540
+ # begin; ...; end
538
541
elseif isexpr (ex, :block )
539
542
if isempty (ex. args)
540
543
py_macro_assign (body, ans, nothing )
@@ -560,7 +563,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
560
563
@assert false
561
564
end
562
565
563
- # if x; ...; end
566
+ # if x; ...; end
564
567
elseif isexpr (ex, :if , :elseif )
565
568
if length (ex. args) == 2
566
569
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)
581
584
return t
582
585
end
583
586
584
- # x && y
587
+ # x && y
585
588
elseif isexpr (ex, :&& )
586
589
ax, ay = ex. args
587
590
tx = py_macro_lower (st, body, ans, ax)
@@ -597,7 +600,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
597
600
push! (body, Expr (:if , :($ pytruth ($ ans)), Expr (:block , body2... ), Expr (:block , body3... )))
598
601
return t
599
602
600
- # x || y
603
+ # x || y
601
604
elseif isexpr (ex, :|| )
602
605
ax, ay = ex. args
603
606
tx = py_macro_lower (st, body, ans, ax)
@@ -613,7 +616,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
613
616
push! (body, Expr (:if , :($ pytruth ($ ans)), Expr (:block , body2... ), Expr (:block , body3... )))
614
617
return t
615
618
616
- # while x; ...; end
619
+ # while x; ...; end
617
620
elseif isexpr (ex, :while )
618
621
ax, ay = ex. args
619
622
@gensym x y
@@ -626,8 +629,10 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
626
629
py_macro_assign (body, ans, nothing )
627
630
return false
628
631
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 )
631
636
@gensym y i v z
632
637
ty = py_macro_lower (st, body, y, ay)
633
638
push! (body, :($ i = $ pyiter ($ y)))
@@ -644,7 +649,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
644
649
py_macro_assign (body, ans, nothing )
645
650
return false
646
651
647
- # import ...
652
+ # import ...
648
653
elseif isexpr (ex, :import )
649
654
for aa in ex. args
650
655
if isexpr (aa, :as )
@@ -680,7 +685,7 @@ function py_macro_lower(st, body, ans, ex; flavour=:expr)
680
685
py_macro_assign (body, ans, nothing )
681
686
return false
682
687
683
- # "...$foo..."
688
+ # "...$foo..."
684
689
elseif isexpr (ex, :string )
685
690
args = [a isa String ? a : :(str ($ a)) for a in ex. args]
686
691
return py_macro_lower (st, body, ans, :(" " . join (($ (args... ),))))
@@ -795,8 +800,9 @@ Evaluate the given expression using Pythonic semantics.
795
800
For example:
796
801
- `f(x, y)` is translated to `pycall(f, x, y)`
797
802
- `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)
799
804
- `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)
800
806
801
807
Compound statements such as `begin`, `if`, `while` and `for` are supported.
802
808
0 commit comments