From 98fbc7dc32918271f75ace5723c46209c23fc46a Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 15 Mar 2016 13:44:01 -0700 Subject: [PATCH] parse 2-arg comparisons as calls instead of `comparison` exprs --- NEWS.md | 3 +++ base/linalg/cholesky.jl | 4 ++-- base/linalg/dense.jl | 6 +++--- base/sparse/cholmod.jl | 2 +- base/sparse/linalg.jl | 4 ++-- base/sparse/sparsematrix.jl | 2 +- base/strings/basic.jl | 2 +- src/julia-parser.scm | 24 +++++++++++++++--------- src/julia-syntax.scm | 1 - test/parse.jl | 8 ++++---- 10 files changed, 32 insertions(+), 24 deletions(-) diff --git a/NEWS.md b/NEWS.md index 28de3044263b8..f501d039b7e8b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,9 @@ Language changes * `A <: B` is parsed as `Expr(:(<:), :A, :B)` in all cases ([#9503]). This also applies to the `>:` operator. + * Simple 2-argument comparisons like `A < B` are parsed as calls intead of using the + `:comparison` expression type. + Command-line option changes --------------------------- diff --git a/base/linalg/cholesky.jl b/base/linalg/cholesky.jl index d70d5a8d880a5..416845e7b0724 100644 --- a/base/linalg/cholesky.jl +++ b/base/linalg/cholesky.jl @@ -276,7 +276,7 @@ end function det(C::Cholesky) dd = one(eltype(C)) - for i in 1:size(C.factors,1) dd *= abs2(C.factors[i,i]) end + for i in 1:size(C.factors,1); dd *= abs2(C.factors[i,i]) end dd end @@ -284,7 +284,7 @@ det(C::CholeskyPivoted) = C.rank < size(C.factors, 1) ? real(zero(eltype(C))) : function logdet(C::Cholesky) dd = zero(eltype(C)) - for i in 1:size(C.factors,1) dd += log(C.factors[i,i]) end + for i in 1:size(C.factors,1); dd += log(C.factors[i,i]) end dd + dd # instead of 2.0dd which can change the type end diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 1918511cd202a..7462da43a7846 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -248,7 +248,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T}) LAPACK.gesv!(V-U, X) if s > 0 # squaring to reverse dividing by power of 2 - for t=1:si X *= X end + for t=1:si; X *= X end end end @@ -264,10 +264,10 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T}) end if ilo > 1 # apply lower permutations in reverse order - for j in (ilo-1):-1:1 rcswap!(j, Int(scale[j]), X) end + for j in (ilo-1):-1:1; rcswap!(j, Int(scale[j]), X) end end if ihi < n # apply upper permutations in forward order - for j in (ihi+1):n rcswap!(j, Int(scale[j]), X) end + for j in (ihi+1):n; rcswap!(j, Int(scale[j]), X) end end X end diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index dbf65105356fb..7f0d6510430a6 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -1517,7 +1517,7 @@ end function logdet{Tv<:VTypes}(F::Factor{Tv}) f = unsafe_load(get(F.p)) res = zero(Tv) - for d in diag(F) res += log(abs(d)) end + for d in diag(F); res += log(abs(d)) end f.is_ll!=0 ? 2res : res end diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index 36303403bb918..338484f55d883 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -6,14 +6,14 @@ import Base.LinAlg: checksquare # Convert from 1-based to 0-based indices function decrement!{T<:Integer}(A::AbstractArray{T}) - for i in 1:length(A) A[i] -= one(T) end + for i in 1:length(A); A[i] -= one(T) end A end decrement{T<:Integer}(A::AbstractArray{T}) = decrement!(copy(A)) # Convert from 0-based to 1-based indices function increment!{T<:Integer}(A::AbstractArray{T}) - for i in 1:length(A) A[i] += one(T) end + for i in 1:length(A); A[i] += one(T) end A end increment{T<:Integer}(A::AbstractArray{T}) = increment!(copy(A)) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 76016b87d2292..29b361245516a 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -3084,7 +3084,7 @@ spdiagm(B::AbstractVector, d::Number=0) = spdiagm((B,), (d,)) function expandptr{T<:Integer}(V::Vector{T}) if V[1] != 1 throw(ArgumentError("first index must be one")) end res = similar(V, (Int64(V[end]-1),)) - for i in 1:(length(V)-1), j in V[i]:(V[i+1] - 1) res[j] = i end + for i in 1:(length(V)-1), j in V[i]:(V[i+1] - 1); res[j] = i end res end diff --git a/base/strings/basic.jl b/base/strings/basic.jl index 4375b827d6da8..2cf4179b0f457 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -39,7 +39,7 @@ getindex(s::AbstractString, i::Integer) = s[Int(i)] getindex{T<:Integer}(s::AbstractString, r::UnitRange{T}) = s[Int(first(r)):Int(last(r))] # TODO: handle other ranges with stride ±1 specially? getindex(s::AbstractString, v::AbstractVector) = - sprint(length(v), io->(for i in v write(io,s[i]) end)) + sprint(length(v), io->(for i in v; write(io,s[i]) end)) symbol(s::AbstractString) = symbol(bytestring(s)) diff --git a/src/julia-parser.scm b/src/julia-parser.scm index e5643cbe457f6..c7735026d32f1 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -793,7 +793,7 @@ (arg2 (cadddr ex))) (if (or (eq? op '|<:|) (eq? op '|>:|)) `(,op ,arg1 ,arg2) - ex))) + `(call ,op ,arg1 ,arg2)))) (else ex))))) (define closing-token? @@ -1400,14 +1400,20 @@ ;; as above, but allows both "i=r" and "i in r" (define (parse-iteration-spec s) - (let ((r (parse-eq* s))) - (cond ((and (pair? r) (eq? (car r) '=)) r) - ((eq? r ':) r) - ((and (length= r 4) (eq? (car r) 'comparison) - (or (eq? (caddr r) 'in) (eq? (caddr r) '∈))) - `(= ,(cadr r) ,(cadddr r))) - (else - (error "invalid iteration specification"))))) + (let* ((lhs (parse-pipes s)) + (t (peek-token s))) + (cond ((memq t '(= in ∈)) + (take-token s) + (let* ((rhs (parse-pipes s)) + (t (peek-token s))) + #;(if (not (or (closing-token? t) (newline? t))) + ;; should be: (error "invalid iteration specification") + (syntax-deprecation s (string "for " (deparse `(= ,lhs ,rhs)) " " t) + (string "for " (deparse `(= ,lhs ,rhs)) "; " t))) + `(= ,lhs ,rhs))) + ((and (eq? lhs ':) (closing-token? t)) + ':) + (else (error "invalid iteration specification"))))) (define (parse-comma-separated-iters s) (let loop ((ranges '())) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index ff0088fa4daf1..7a7bb95419cf9 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -1522,7 +1522,6 @@ (lambda (e) `(call (top getfield) ,(expand-forms (cadr e)) ,(expand-forms (caddr e)))) - 'in syntactic-op-to-call '|<:| syntactic-op-to-call '|>:| syntactic-op-to-call diff --git a/test/parse.jl b/test/parse.jl index 7cf818a39c4c5..35e00542bb8c6 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -22,9 +22,9 @@ let ("5.≥x", "5.>=x"), ("5.≤x", "5.<=x")] ex1 = parse(ex1); ex2 = parse(ex2) - @test ex1.head === :comparison && (ex1.head === ex2.head) - @test ex1.args[1] === 5 && ex2.args[1] === 5 - @test is(eval(Main, ex1.args[2]), eval(Main, ex2.args[2])) + @test ex1.head === :call && (ex1.head === ex2.head) + @test ex1.args[2] === 5 && ex2.args[2] === 5 + @test is(eval(Main, ex1.args[1]), eval(Main, ex2.args[1])) @test ex1.args[3] === :x && (ex1.args[3] === ex2.args[3]) end end @@ -291,7 +291,7 @@ for T in (UInt8,UInt16,UInt32,UInt64) @test_throws OverflowError parse(T,string(big(typemax(T))+1)) end -@test parse("1 == 2|>3") == Expr(:comparison, 1, :(==), Expr(:call, :(|>), 2, 3)) +@test parse("1 == 2|>3") == Expr(:call, :(==), 1, Expr(:call, :(|>), 2, 3)) # issue #12501 and pr #12502 parse("""