Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
151 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function transpilefor(t) | ||
if length(t[:orelse]) > 0 | ||
error("PythonSyntax cannot currently handle else for loops.") | ||
end | ||
quote | ||
for $(transpile(t[:target])) in $(transpile(t[:iter])) | ||
$(transpile(t[:body])) | ||
end | ||
end | ||
end | ||
|
||
function transpilewhile(t) | ||
if length(t[:orelse]) > 0 | ||
error("PythonSyntax cannot currently handle else for loops.") | ||
end | ||
quote | ||
while $(transpile(t[:test])) | ||
$(transpile(t[:body])) | ||
end | ||
end | ||
end | ||
|
||
function transpilewith(t) | ||
# FIXME: support | ||
error("PythonSyntax does not yet support with.") | ||
end | ||
|
||
function transpileraise(t) | ||
if t[:exc] == nothing | ||
quote error() end | ||
else | ||
quote throw($(transpile(t[:exc]))) end | ||
end | ||
end | ||
|
||
function transpiletry(t) | ||
# FIXME: support | ||
error("PythonSyntax does not yet support try.") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
function transpileassign(t) | ||
targets = transpile.(t[:targets]) | ||
lhs = if length(targets) == 1 | ||
targets[1] | ||
else | ||
Expr(:tuple, targets...) | ||
end | ||
|
||
Expr(:(=), lhs, transpile(t[:value])) | ||
end | ||
|
||
function transpileaugassign(t) | ||
lhs = transpile(t[:target]) | ||
op = jlaugop(t[:op]) | ||
rhs = transpile(t[:value]) | ||
Expr(op, lhs, rhs) | ||
end | ||
|
||
|
||
function transpilecmp(t) | ||
args = Any[transpile(t[:left])] | ||
for (op, val) in zip(t[:ops], t[:comparators]) | ||
push!(args, jlop(op), transpile(val)) | ||
end | ||
Expr(:comparison, args...) | ||
end | ||
|
||
function transpileslice(t) | ||
@match pytypeof(t) begin | ||
ast.Slice => [Expr(:(:), | ||
t[:lower] == nothing ? 1 : transpile(t[:lower]), | ||
t[:step] == nothing ? 1 : transpile(t[:step]), | ||
t[:upper] == nothing ? :end : transpile(t[:upper]))] | ||
ast.ExtSlice => transpileslice.(t[:dims]) | ||
ast.Index => [transpile(t[:value])] | ||
end | ||
end | ||
|
||
transpilerawcall(t) = Expr(:call, transpile(t[:func]), transpile.(t[:args])...) | ||
|
||
""" | ||
Transpiles a call. Traps some dunder names, like __jl_macro__. | ||
""" | ||
function transpilecall(t) | ||
if pytypeof(t[:func]) == ast.Name | ||
@match t[:func][:id] begin | ||
"__jlmc__" => Expr(:macrocall, | ||
Symbol('@', jlident(t[:args][1][:id])), | ||
transpile.(t[:args][2:end])...) | ||
_ => transpilerawcall(t) | ||
end | ||
else | ||
transpilerawcall(t) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@testset "Control" begin | ||
|
||
@test pysyntax"1 if True else 0" == 1 | ||
@test pysyntax"1 if False else 0" == 0 | ||
|
||
@test pysyntax" | ||
x = 1 | ||
if 1 + 1 == 2: | ||
x *= 3 | ||
x" == 3 | ||
|
||
@test pysyntax" | ||
x = 1 | ||
if 1 + 1 == 2: | ||
x *= 3 | ||
x" == 3 | ||
end # testset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters