Skip to content

Commit 8d76566

Browse files
committed
parse 2-arg comparisons as calls instead of comparison exprs
1 parent eb82b4f commit 8d76566

11 files changed

+37
-24
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Language changes
3535
* `A <: B` is parsed as `Expr(:(<:), :A, :B)` in all cases ([#9503]). This also applies to the
3636
`>:` operator.
3737

38+
* Simple 2-argument comparisons like `A < B` are parsed as calls intead of using the
39+
`:comparison` expression type.
40+
3841
Command-line option changes
3942
---------------------------
4043

base/linalg/cholesky.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ end
276276

277277
function det(C::Cholesky)
278278
dd = one(eltype(C))
279-
for i in 1:size(C.factors,1) dd *= abs2(C.factors[i,i]) end
279+
for i in 1:size(C.factors,1); dd *= abs2(C.factors[i,i]) end
280280
dd
281281
end
282282

283283
det(C::CholeskyPivoted) = C.rank < size(C.factors, 1) ? real(zero(eltype(C))) : prod(abs2(diag(C.factors)))
284284

285285
function logdet(C::Cholesky)
286286
dd = zero(eltype(C))
287-
for i in 1:size(C.factors,1) dd += log(C.factors[i,i]) end
287+
for i in 1:size(C.factors,1); dd += log(C.factors[i,i]) end
288288
dd + dd # instead of 2.0dd which can change the type
289289
end
290290

base/linalg/dense.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T})
248248
LAPACK.gesv!(V-U, X)
249249

250250
if s > 0 # squaring to reverse dividing by power of 2
251-
for t=1:si X *= X end
251+
for t=1:si; X *= X end
252252
end
253253
end
254254

@@ -264,10 +264,10 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T})
264264
end
265265

266266
if ilo > 1 # apply lower permutations in reverse order
267-
for j in (ilo-1):-1:1 rcswap!(j, Int(scale[j]), X) end
267+
for j in (ilo-1):-1:1; rcswap!(j, Int(scale[j]), X) end
268268
end
269269
if ihi < n # apply upper permutations in forward order
270-
for j in (ihi+1):n rcswap!(j, Int(scale[j]), X) end
270+
for j in (ihi+1):n; rcswap!(j, Int(scale[j]), X) end
271271
end
272272
X
273273
end

base/sparse/cholmod.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ end
15171517
function logdet{Tv<:VTypes}(F::Factor{Tv})
15181518
f = unsafe_load(get(F.p))
15191519
res = zero(Tv)
1520-
for d in diag(F) res += log(abs(d)) end
1520+
for d in diag(F); res += log(abs(d)) end
15211521
f.is_ll!=0 ? 2res : res
15221522
end
15231523

base/sparse/linalg.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import Base.LinAlg: checksquare
66

77
# Convert from 1-based to 0-based indices
88
function decrement!{T<:Integer}(A::AbstractArray{T})
9-
for i in 1:length(A) A[i] -= one(T) end
9+
for i in 1:length(A); A[i] -= one(T) end
1010
A
1111
end
1212
decrement{T<:Integer}(A::AbstractArray{T}) = decrement!(copy(A))
1313

1414
# Convert from 0-based to 1-based indices
1515
function increment!{T<:Integer}(A::AbstractArray{T})
16-
for i in 1:length(A) A[i] += one(T) end
16+
for i in 1:length(A); A[i] += one(T) end
1717
A
1818
end
1919
increment{T<:Integer}(A::AbstractArray{T}) = increment!(copy(A))

base/sparse/sparsematrix.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3084,7 +3084,7 @@ spdiagm(B::AbstractVector, d::Number=0) = spdiagm((B,), (d,))
30843084
function expandptr{T<:Integer}(V::Vector{T})
30853085
if V[1] != 1 throw(ArgumentError("first index must be one")) end
30863086
res = similar(V, (Int64(V[end]-1),))
3087-
for i in 1:(length(V)-1), j in V[i]:(V[i+1] - 1) res[j] = i end
3087+
for i in 1:(length(V)-1), j in V[i]:(V[i+1] - 1); res[j] = i end
30883088
res
30893089
end
30903090

base/strings/basic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getindex(s::AbstractString, i::Integer) = s[Int(i)]
3939
getindex{T<:Integer}(s::AbstractString, r::UnitRange{T}) = s[Int(first(r)):Int(last(r))]
4040
# TODO: handle other ranges with stride ±1 specially?
4141
getindex(s::AbstractString, v::AbstractVector) =
42-
sprint(length(v), io->(for i in v write(io,s[i]) end))
42+
sprint(length(v), io->(for i in v; write(io,s[i]) end))
4343

4444
symbol(s::AbstractString) = symbol(bytestring(s))
4545

base/test.jl

+5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ Returns a `Pass` `Result` if it does, a `Fail` `Result` if it is
151151
"""
152152
macro test(ex)
153153
orig_ex = Expr(:quote,ex)
154+
# Normalize comparison operator calls to :comparison expressions
155+
if isa(ex, Expr) && ex.head == :call && length(ex.args)==3 &&
156+
Base.operator_precedence(ex.args[1]) == Base.operator_precedence(:(==))
157+
ex = Expr(:comparison, ex.args[2], ex.args[1], ex.args[3])
158+
end
154159
# If the test is a comparison
155160
if isa(ex, Expr) && ex.head == :comparison
156161
# Generate a temporary for every term in the expression

src/julia-parser.scm

+15-9
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@
793793
(arg2 (cadddr ex)))
794794
(if (or (eq? op '|<:|) (eq? op '|>:|))
795795
`(,op ,arg1 ,arg2)
796-
ex)))
796+
`(call ,op ,arg1 ,arg2))))
797797
(else ex)))))
798798

799799
(define closing-token?
@@ -1400,14 +1400,20 @@
14001400

14011401
;; as above, but allows both "i=r" and "i in r"
14021402
(define (parse-iteration-spec s)
1403-
(let ((r (parse-eq* s)))
1404-
(cond ((and (pair? r) (eq? (car r) '=)) r)
1405-
((eq? r ':) r)
1406-
((and (length= r 4) (eq? (car r) 'comparison)
1407-
(or (eq? (caddr r) 'in) (eq? (caddr r) '∈)))
1408-
`(= ,(cadr r) ,(cadddr r)))
1409-
(else
1410-
(error "invalid iteration specification")))))
1403+
(let* ((lhs (parse-pipes s))
1404+
(t (peek-token s)))
1405+
(cond ((memq t '(= in ∈))
1406+
(take-token s)
1407+
(let* ((rhs (parse-pipes s))
1408+
(t (peek-token s)))
1409+
#;(if (not (or (closing-token? t) (newline? t)))
1410+
;; should be: (error "invalid iteration specification")
1411+
(syntax-deprecation s (string "for " (deparse `(= ,lhs ,rhs)) " " t)
1412+
(string "for " (deparse `(= ,lhs ,rhs)) "; " t)))
1413+
`(= ,lhs ,rhs)))
1414+
((and (eq? lhs ':) (closing-token? t))
1415+
':)
1416+
(else (error "invalid iteration specification")))))
14111417

14121418
(define (parse-comma-separated-iters s)
14131419
(let loop ((ranges '()))

src/julia-syntax.scm

-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,6 @@
15221522
(lambda (e)
15231523
`(call (top getfield) ,(expand-forms (cadr e)) ,(expand-forms (caddr e))))
15241524

1525-
'in syntactic-op-to-call
15261525
'|<:| syntactic-op-to-call
15271526
'|>:| syntactic-op-to-call
15281527

test/parse.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ let
2222
("5.≥x", "5.>=x"),
2323
("5.≤x", "5.<=x")]
2424
ex1 = parse(ex1); ex2 = parse(ex2)
25-
@test ex1.head === :comparison && (ex1.head === ex2.head)
26-
@test ex1.args[1] === 5 && ex2.args[1] === 5
27-
@test is(eval(Main, ex1.args[2]), eval(Main, ex2.args[2]))
25+
@test ex1.head === :call && (ex1.head === ex2.head)
26+
@test ex1.args[2] === 5 && ex2.args[2] === 5
27+
@test is(eval(Main, ex1.args[1]), eval(Main, ex2.args[1]))
2828
@test ex1.args[3] === :x && (ex1.args[3] === ex2.args[3])
2929
end
3030
end
@@ -291,7 +291,7 @@ for T in (UInt8,UInt16,UInt32,UInt64)
291291
@test_throws OverflowError parse(T,string(big(typemax(T))+1))
292292
end
293293

294-
@test parse("1 == 2|>3") == Expr(:comparison, 1, :(==), Expr(:call, :(|>), 2, 3))
294+
@test parse("1 == 2|>3") == Expr(:call, :(==), 1, Expr(:call, :(|>), 2, 3))
295295

296296
# issue #12501 and pr #12502
297297
parse("""

0 commit comments

Comments
 (0)