Skip to content

Commit

Permalink
preserve do syntax in AST. part of #21774
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Sep 14, 2017
1 parent ac714a1 commit f0d8480
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Language changes
assignment or `block` of assignments, and the second argument is a block of
statements ([#21774]).

* `do` syntax now parses to an expression with head `:do`, instead of as a function
call ([#21774]).

* Parsed and lowered forms of type definitions have been synchronized with their
new keywords ([#23157]). Expression heads are renamed as follows:

Expand Down
13 changes: 12 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ function show_unquoted_quote_expr(io::IO, value, indent::Int, prec::Int)
print(io, "end")
else
print(io, ":(")
show_unquoted(io, value, indent+indent_width, -1)
show_unquoted(io, value, indent+2, -1) # +2 for `:(`
print(io, ")")
end
end
Expand Down Expand Up @@ -958,6 +958,17 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
elseif head === :function && nargs == 1
print(io, "function ", args[1], " end")

elseif head === :do && nargs == 2
show_unquoted(io, args[1], indent, -1)
print(io, " do ")
show_list(io, args[2].args[1].args, ", ", 0)
for stmt in args[2].args[2].args
print(io, '\n', " "^(indent + indent_width))
show_unquoted(io, stmt, indent + indent_width, -1)
end
print(io, '\n', " "^indent)
print(io, "end")

# block with argument
elseif head in (:for,:while,:function,:if,:elseif,:let) && nargs==2
show_block(io, head, args[1], args[2], indent)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/devdocs/ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ f(x) do a,b
end
```

parses as `(call f (-> (tuple a b) (block body)) x)`.
parses as `(do (call f x) (-> (tuple a b) (block body)))`.

### Operators

Expand Down
2 changes: 1 addition & 1 deletion src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@
(if (eq? (peek-token s) 'do)
(begin
(take-token s)
`(call ,ex ,@params ,(parse-do s) ,@args))
`(do (call ,ex ,@params ,@args) ,(parse-do s)))
`(call ,ex ,@al))))))
(if macrocall?
(map (lambda (x) ;; parse `a=b` as `=` instead of `kw` in macrocall
Expand Down
11 changes: 11 additions & 0 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,17 @@
(map expand-forms e))))
(map expand-forms e)))

'do
(lambda (e)
(let* ((call (cadr e))
(f (cadr call))
(argl (cddr call))
(af (caddr e)))
(expand-forms
(if (has-parameters? argl)
`(call ,f ,(car argl) ,af ,@(cdr argl))
`(call ,f ,af ,@argl)))))

'tuple
(lambda (e)
(if (and (length> e 1) (pair? (cadr e)) (eq? (caadr e) 'parameters))
Expand Down
20 changes: 18 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ struct T5589
end
@test replstr(T5589(Array{String,1}(100))) == "$(curmod_prefix)T5589([#undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef … #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef])"

@test replstr(parse("mutable struct X end")) == ":(mutable struct X\n #= none:1 =#\n end)"
@test replstr(parse("struct X end")) == ":(struct X\n #= none:1 =#\n end)"
@test replstr(parse("mutable struct X end")) == ":(mutable struct X\n #= none:1 =#\n end)"
@test replstr(parse("struct X end")) == ":(struct X\n #= none:1 =#\n end)"
let s = "ccall(:f, Int, (Ptr{Void},), &x)"
@test replstr(parse(s)) == ":($s)"
end
Expand Down Expand Up @@ -275,6 +275,22 @@ f
end
"""

@test_repr """f(x, y) do z, w
# line meta
a
# line meta
b
end
"""

@test_repr """f(x, y) do z
# line meta
a
# line meta
b
end
"""

# issue #7188
@test sprint(show, :foo) == ":foo"
@test sprint(show, Symbol("foo bar")) == "Symbol(\"foo bar\")"
Expand Down

0 comments on commit f0d8480

Please sign in to comment.