Permalink
Browse files

Support comprehensions

  • Loading branch information...
TotalVerb committed May 23, 2016
1 parent e1bcc0b commit e8fad9acc348a1b5510aa38d807fdcbd828a98cb
Showing with 52 additions and 3 deletions.
  1. +1 −1 README.md
  2. +6 −2 src/PythonSyntax.jl
  3. +13 −0 src/expr.jl
  4. +7 −0 test/arrays.jl
  5. +23 −0 test/magic.jl
  6. +2 −0 test/runtests.jl
View
@@ -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")
View
@@ -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)
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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.