Skip to content

Commit 2d49f82

Browse files
committed
support dots for unary operators (closes JuliaLang#14161)
1 parent 1303dfb commit 2d49f82

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/julia-parser.scm

+7-6
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@
6565
t))
6666
(define (operator-precedence op) (get prec-table op 0))
6767

68-
(define unary-ops '(+ - ! ¬ ~ |<:| |>:| √ ∛ ∜))
68+
(define unary-ops (append! '(|<:| |>:| ~)
69+
(add-dots '(+ - ! ¬ √ ∛ ∜))))
6970

7071
; operators that are both unary and binary
71-
(define unary-and-binary-ops '(+ - $ & ~))
72+
(define unary-and-binary-ops '(+ - $ & ~ |.+| |.-|))
7273

7374
; operators that are special forms, not function names
7475
(define syntactic-operators
@@ -93,9 +94,9 @@
9394

9495
(define operators
9596
(filter (lambda (x) (not (is-word-operator? x)))
96-
(list* '~ '! '-> '√ '∛ '∜ ctrans-op trans-op vararg-op
97-
(delete-duplicates
98-
(apply append (map eval prec-names))))))
97+
(delete-duplicates
98+
(list* '-> ctrans-op trans-op vararg-op
99+
(append unary-ops (apply append (map eval prec-names)))))))
99100

100101
(define op-chars
101102
(delete-duplicates
@@ -200,7 +201,7 @@
200201
(loop newop (peek-char port)))
201202
str))
202203
str))))
203-
(if (or (equal? str "--") (equal? str ".!"))
204+
(if (equal? str "--")
204205
(error (string "invalid operator \"" str "\"")))
205206
(string->symbol str))))
206207

test/broadcast.jl

+5
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ let g = Int[], ⊕ = (a,b) -> let c=a+2b; push!(g, c); c; end
302302
@test g == [21,221,24,424,27,627] # test for loop fusion
303303
end
304304

305+
# Fused unary operators
306+
@test .√[3,4,5] == sqrt.([3,4,5])
307+
@test .![true, true, false] == [false, false, true]
308+
@test .-[1,2,3] == -[1,2,3] == .+[-1,-2,-3] == [-1,-2,-3]
309+
305310
# PR 16988
306311
@test Base.promote_op(+, Bool) === Int
307312
@test isa(broadcast(+, [true]), Array{Int,1})

0 commit comments

Comments
 (0)