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: Make and & or aliases for && and ||. #19788

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
(eval `(define ,(symbol (string "is-" name "?")) (Set ,name))))
prec-names)

(define (augment-prec-with-infix prec-list . extra-infix)
(let ((compare-ops (Set prec-list)))
(lambda (t)
(or (compare-ops t) (member t extra-infix)))))

(define is-prec-comparison?
(augment-prec-with-infix prec-comparison 'in 'isa))
(define is-prec-lazy-and?
(augment-prec-with-infix prec-lazy-and 'and))
(define is-prec-lazy-or?
(augment-prec-with-infix prec-lazy-or 'or))

;; hash table of binary operators -> precedence
(define prec-table (let ((t (table)))
(define (pushprec L prec)
Expand All @@ -65,6 +77,12 @@
(pushprec (cdr L) (+ prec 1)))))
(pushprec (map eval prec-names) 1)
t))

(put! prec-table 'in (get prec-table '== 0)) ; add `in` to the prec-table
(put! prec-table 'isa (get prec-table '== 0)) ; add `isa` to the prec-table
(put! prec-table 'and (get prec-table '&& 0)) ; add `and` to the prec-table
(put! prec-table 'or (get prec-table '|\|\|| 0)) ; add `and` to the prec-table

(define (operator-precedence op) (get prec-table op 0))

(define unary-ops (append! '(|<:| |>:|)
Expand All @@ -76,7 +94,7 @@
; operators that are special forms, not function names
(define syntactic-operators
(append! (add-dots '(= += -= *= /= //= |\\=| ^= ÷= %= <<= >>= >>>= |\|=| &= ⊻=))
'(:= --> $= && |\|\|| |.| ... ->)))
'(:= --> $= && 'and |\|\|| 'or |.| ... ->)))
(define syntactic-unary-operators '($ & |::|))

(define syntactic-op? (Set syntactic-operators))
Expand Down Expand Up @@ -774,8 +792,17 @@
; parse-comma is needed for commas outside parens, for example a = b,c
(define (parse-comma s) (parse-Nary s parse-cond '(#\,) 'tuple (lambda (x) #f) #f #f))
(define (parse-arrow s) (parse-RtoL s parse-or is-prec-arrow? (eq? t '-->) parse-arrow))
(define (parse-or s) (parse-RtoL s parse-and is-prec-lazy-or? #t parse-or))
(define (parse-and s) (parse-RtoL s parse-comparison is-prec-lazy-and? #t parse-and))
(define (normalize-or ex)
(if (and (list? ex) (eq? (car ex) 'or))
(cons '|\|\|| (cdr ex))
ex))
(define (normalize-and ex)
(if (and (list? ex) (eq? (car ex) 'and))
(cons '&& (cdr ex))
ex))

(define (parse-or s) (normalize-or (parse-RtoL s parse-and is-prec-lazy-or? #t parse-or)))
(define (parse-and s) (normalize-and (parse-RtoL s parse-comparison is-prec-lazy-and? #t parse-and)))

;; parse left to right chains of a certain binary operator
;; returns a list of arguments
Expand Down
16 changes: 16 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,19 @@ module Test21607
x
end === 1.0
end

# Check `and` & `or`, issue #5238, PR #19788
@test :(a and b) == :(a && b)
@test true and :foo == :foo
@test false or :bar == :bar
@test true and false or true == true
@test (x = 5; x > 4) or (x = 0) == 0

function fact_test(n::Int)
n >= 0 or error("n must be non-negative")
n == 0 and return 1
n * fact_test(n - 1)
end

@test fact_test(5) == 120
@test_throws ErrorException fact_test(-1)