Skip to content

Commit

Permalink
Support comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed May 23, 2016
1 parent e1bcc0b commit e8fad9a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FizzBuzz
def fizzbuzz(n):
# this is still Julia, even though it looks like Python!
# so range includes both endpoints
# so range includes 1 and has length n — very different from Python.
for i in range(1, n):
if i % 15 == 0:
println("FizzBuzz")
Expand Down
8 changes: 6 additions & 2 deletions src/PythonSyntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ function transpile(t::PyObject)
ast.Dict => :(Dict(zip($(transpile(:vect, t[:keys])),
$(transpile(:vect, t[:values])))))
ast.Set => :(Set($(transpile(:vect, t[:elts]))))
ast.ListComp || ast.SetComp || ast.DictComp || ast.GeneratorExp =>
error("Comprehensions are not yet supported.")
ast.ListComp => transpilecomp(t)
ast.SetComp => :(Set($(transpilecomp(t))))
ast.DictComp =>
error("Dictionary comprehensions are not yet supported.")
ast.GeneratorExp =>
error("Generators are not yet supported.")
ast.Await || ast.Yield || ast.YieldFrom =>
error("Generators are not yet supported.")
ast.Compare => transpilecmp(t)
Expand Down
13 changes: 13 additions & 0 deletions src/expr.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function transpilegen(gen)
if !isempty(gen[:ifs])
throw(ArgumentError("conditionals not supported in comprehensions"))
end
Expr(:(=), transpile(gen[:target]), transpile(gen[:iter]))
end

function transpilecomp(comp)
Expr(:comprehension,
transpile(comp[:elt]),
transpilegen.(comp[:generators])...)
end

function transpileassign(t)
targets = transpile.(t[:targets])
lhs = if length(targets) == 1
Expand Down
7 changes: 7 additions & 0 deletions test/arrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@testset "Arrays" begin

@test pysyntax"[1, 2, 3]" == [1, 2, 3]
@test pysyntax"[i for i in range(1, 10)]" == collect(1:10)
@test pysyntax"{i**2 for i in range(-5, 11)}" == Set([0, 1, 4, 9, 16, 25])

end # testset
23 changes: 23 additions & 0 deletions test/magic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@testset "Magic" begin

@test pysyntax"""
def goto():
__mc__(goto, skip)
return False
__mc__(label, skip)
return True
goto()
"""

@test pysyntax"""
def goto():
__jl__("@goto skip")
return False
__jl__("@label skip")
return True
goto()
"""

end # testset
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ include("control.jl")
include("io.jl")
include("functions.jl")
include("classes.jl")
include("magic.jl")
include("arrays.jl")
include("stl.jl")

0 comments on commit e8fad9a

Please sign in to comment.