Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: parse a' as call expression #33683

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Expand Up @@ -456,6 +456,7 @@ export
startswith,

# linear algebra
var"'",
adjoint,
transpose,
kron,
Expand Down
2 changes: 2 additions & 0 deletions base/operators.jl
Expand Up @@ -533,6 +533,8 @@ for op in (:+, :*, :&, :|, :xor, :min, :max, :kron)
end
end

const var"'" = adjoint

"""
\\(x, y)

Expand Down
21 changes: 10 additions & 11 deletions base/show.jl
Expand Up @@ -1336,6 +1336,16 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
show_unquoted(io, arg1, indent, func_prec, quote_level)
end

# adjoint operator (i.e. "a'")
elseif func === :var"'" && length(func_args) == 1
arg1 = func_args[1]
if isa(arg1, Expr) || (isa(arg1, Symbol) && isoperator(arg1))
show_enclosed_list(io, '(', func_args, ", ", ')', indent, func_prec)
else
show_unquoted(io, arg1, indent, func_prec)
end
print(io, ''')

# binary operator (i.e. "x + y")
elseif func_prec > 0 # is a binary operator
na = length(func_args)
Expand Down Expand Up @@ -1658,17 +1668,6 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
parens && print(io, ")")
end

# transpose
elseif head === Symbol('\'') && nargs == 1
if isa(args[1], Symbol)
show_unquoted(io, args[1], 0, 0, quote_level)
else
print(io, "(")
show_unquoted(io, args[1], 0, 0, quote_level)
print(io, ")")
end
print(io, head)

# `where` syntax
elseif head === :where && nargs > 1
parens = 1 <= prec
Expand Down
2 changes: 1 addition & 1 deletion src/julia-parser.scm
Expand Up @@ -1236,7 +1236,7 @@
(if (ts:space? s)
(error (string "space not allowed before \"" t "\"")))
(take-token s)
(loop (list t ex)))
(loop (list 'call t ex)))
((|.'|) (error "the \".'\" operator is discontinued"))
((#\{ )
(disallow-space s ex t)
Expand Down
2 changes: 1 addition & 1 deletion test/syntax.jl
Expand Up @@ -74,7 +74,7 @@ macro test999_str(args...); args; end
@test_throws ParseError Meta.parse("sqrt(16)2")
@test_throws ParseError Meta.parse("x' y")
@test_throws ParseError Meta.parse("x 'y")
@test Meta.parse("x'y") == Expr(:call, :*, Expr(Symbol("'"), :x), :y)
@test Meta.parse("x'y") == Expr(:call, :*, Expr(:call, Symbol("'"), :x), :y)

# issue #18851
@test Meta.parse("-2[m]") == Expr(:call, :-, Expr(:ref, 2, :m))
Expand Down