Skip to content

Commit

Permalink
make radical operators juxtaposable (JuliaLang#40173)
Browse files Browse the repository at this point in the history
* make radical operators juxtaposable

* fix test

* update NEWS.md [skip ci]

Co-authored-by: Jameson Nash <[email protected]>
  • Loading branch information
2 people authored and ElOceanografo committed May 4, 2021
1 parent 02a5ae6 commit 9e76f30
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ New language features

* `(; a, b) = x` can now be used to destructure properties `a` and `b` of `x`. This syntax is equivalent to `a = getproperty(x, :a)`
and similarly for `b`. ([#39285])
* Implicit multiplication by juxtaposition is now allowed for radical symbols (e.g., `x√y` and `x∛y`). ([#40173])

Language changes
----------------
Expand Down
6 changes: 4 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@

(define unary-op? (Set unary-ops))

(define radical-op? (Set '(√ ∛ ∜)))

; operators that are both unary and binary
(define unary-and-binary-ops (append! '($ & ~)
(add-dots '(+ - ⋆ ± ∓))))
Expand Down Expand Up @@ -973,7 +975,7 @@
(not (memv t '(#\( #\[ #\{))))
)
(not (ts:space? s))
(not (operator? t))
(or (not (operator? t)) (radical-op? t))
(not (closing-token? t))
(not (newline? t))
(or (and (not (string? expr)) (not (eqv? t #\")))
Expand All @@ -996,7 +998,7 @@
(begin
#;(if (and (number? ex) (= ex 0))
(error "juxtaposition with literal \"0\""))
(let ((next (parse-factor s)))
(let ((next (if (radical-op? next) (parse-unary s) (parse-factor s))))
(loop `(call * ,ex ,next)
(cons next args))))
(if (length= args 1)
Expand Down
6 changes: 5 additions & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,6 @@ end
@test Meta.parse("√3x^2") == Expr(:call, :*, Expr(:call, :, 3), Expr(:call, :^, :x, 2))
@test Meta.parse("-3x^2") == Expr(:call, :*, -3, Expr(:call, :^, :x, 2))
@test_throws ParseError Meta.parse("2!3")
@test_throws ParseError Meta.parse("2√3")

# issue #27914
@test Meta.parse("2f(x)") == Expr(:call, :*, 2, Expr(:call, :f, :x))
Expand Down Expand Up @@ -2752,6 +2751,11 @@ end
@test_throws ErrorException("syntax: SSAValue objects should not occur in an AST") eval(:(x = $(Core.SSAValue(1))))
@test_throws ErrorException("syntax: Slot objects should not occur in an AST") eval(:(x = $(Core.SlotNumber(1))))

# juxtaposition of radical symbols (#40094)
@test Meta.parse("2√3") == Expr(:call, :*, 2, Expr(:call, :, 3))
@test Meta.parse("2∛3") == Expr(:call, :*, 2, Expr(:call, :, 3))
@test Meta.parse("2∜3") == Expr(:call, :*, 2, Expr(:call, :, 3))

macro m_underscore_hygiene()
return :(_ = 1)
end
Expand Down

0 comments on commit 9e76f30

Please sign in to comment.