Skip to content

Commit db8d743

Browse files
committed
fixing issue #21, allow only [a,b,...] and [a b;c d] syntaxes for arrays
1 parent e5bd44b commit db8d743

File tree

3 files changed

+72
-44
lines changed

3 files changed

+72
-44
lines changed

src/julia-parser.scm

+67-39
Original file line numberDiff line numberDiff line change
@@ -703,50 +703,78 @@
703703
ranges))
704704

705705
; parse [] concatenation expressions and {} cell expressions
706-
(define (parse-cat s closer)
707-
(with-normal-ops
708-
(with-space-sensitive
709-
(parse-cat- s closer))))
710-
(define (parse-cat- s closer)
706+
(define (parse-vcat s first closer)
707+
(let loop ((lst '())
708+
(nxt first))
709+
(let ((t (require-token s)))
710+
(if (eqv? t closer)
711+
(begin (take-token s)
712+
(cons 'vcat (reverse (cons nxt lst))))
713+
(case t
714+
((#\,)
715+
(take-token s)
716+
(if (eqv? (require-token s) closer)
717+
;; allow ending with ,
718+
(begin (take-token s)
719+
(cons 'vcat (reverse (cons nxt lst))))
720+
(loop (cons nxt lst) (parse-eq* s))))
721+
((#\;)
722+
(error "unexpected semicolon in array expression"))
723+
((#\] #\})
724+
(error (string "unexpected " t)))
725+
(else
726+
(error "missing separator in array expression")))))))
727+
728+
(define (parse-matrix s first closer)
711729
(define (fix head v) (cons head (reverse v)))
712-
(let loop ((vec '())
713-
(outer '())
714-
(first #t))
715-
(let ((update-outer (lambda (v)
716-
(cond ((null? v) outer)
717-
((null? (cdr v)) (cons (car v) outer))
718-
(else (cons (fix 'hcat v) outer))))))
719-
(if (eqv? (require-token s) closer)
730+
(define (update-outer v outer)
731+
(cond ((null? v) outer)
732+
((null? (cdr v)) (cons (car v) outer))
733+
(else (cons (fix 'hcat v) outer))))
734+
(let loop ((vec (list first))
735+
(outer '()))
736+
(let ((t (if (eqv? (peek-token s) #\newline)
737+
#\newline
738+
(require-token s))))
739+
(if (eqv? t closer)
720740
(begin (take-token s)
721741
(if (pair? outer)
722-
(fix 'vcat (update-outer vec))
742+
(fix 'vcat (update-outer vec outer))
723743
(if (or (null? vec) (null? (cdr vec)))
724744
(fix 'vcat vec) ; [x] => (vcat x)
725-
(fix 'hcat vec)))) ; [x,y] => (hcat x y)
726-
(let ((nv (cons (if first
727-
(without-bitor (parse-eq* s))
728-
(parse-eq* s))
729-
vec)))
730-
(case (if (eqv? (peek-token s) #\newline)
731-
#\newline
732-
(require-token s))
733-
((#\]) (if (eqv? closer #\])
734-
(loop nv outer #f)
735-
(error "unexpected ]")))
736-
((#\}) (if (eqv? closer #\})
737-
(loop nv outer #f)
738-
(error "unexpected }")))
739-
((|\||)
740-
(begin (take-token s)
741-
(let ((r (parse-comma-separated-assignments s)))
742-
(if (not (eqv? (require-token s) closer))
743-
(error (string "expected " closer))
744-
(take-token s))
745-
`(comprehension ,(car nv) ,@(colons-to-ranges r)))))
746-
((#\, #\; #\newline)
747-
(begin (take-token s) (loop '() (update-outer nv) #f)))
748-
(else
749-
(begin (loop nv outer #f)))))))))
745+
(fix 'hcat vec)))) ; [x y] => (hcat x y)
746+
(case t
747+
((#\; #\newline)
748+
(take-token s) (loop '() (update-outer vec outer)))
749+
((#\,)
750+
(error "unexpected comma in matrix expression"))
751+
((#\] #\})
752+
(error (string "unexpected " t)))
753+
(else
754+
(loop (cons (parse-eq* s) vec) outer)))))))
755+
756+
(define (parse-cat s closer)
757+
(with-normal-ops
758+
(with-space-sensitive
759+
(parse-cat- s closer))))
760+
(define (parse-cat- s closer)
761+
(if (eqv? (require-token s) closer)
762+
(begin (take-token s)
763+
(list 'vcat)) ; [] => (vcat)
764+
(let ((first (without-bitor (parse-eq* s))))
765+
(case (peek-token s)
766+
;; dispatch to array syntax, comprehension, or matrix syntax
767+
((#\,)
768+
(parse-vcat s first closer))
769+
((|\||)
770+
(take-token s)
771+
(let ((r (parse-comma-separated-assignments s)))
772+
(if (not (eqv? (require-token s) closer))
773+
(error (string "expected " closer))
774+
(take-token s))
775+
`(comprehension ,first ,@(colons-to-ranges r))))
776+
(else
777+
(parse-matrix s first closer))))))
750778

751779
; for sequenced evaluation inside expressions: e.g. (a;b, c;d)
752780
(define (parse-stmts-within-expr s)

test/perf.j

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function randmatstat(t)
115115
c = randn(n, n)
116116
d = randn(n, n)
117117
P = [a b c d]
118-
Q = [a b, c d]
118+
Q = [a b; c d]
119119
v[i] = trace((P.'*P)^4)
120120
w[i] = trace((Q.'*Q)^4)
121121
end

test/tests.j

+4-4
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ glotest()
713713
X = [ i+2j | i=1:5, j=1:5 ]
714714
@assert X[2,3] == 8
715715
@assert X[4,5] == 14
716-
@assert ones(2,3) * ones(2,3)' == [3. 3., 3. 3.]
717-
@assert [ [1,2] | i=1:2, : ] == [1 2, 1 2]
716+
@assert ones(2,3) * ones(2,3)' == [3. 3.; 3. 3.]
717+
@assert [ [1,2] | i=1:2, : ] == [1 2; 1 2]
718718
# where element type is a Union. try to confuse type inference.
719719
foo32_64(x) = (x<2) ? int32(x) : int64(x)
720720
boo32_64() = [ foo32_64(i) | i=1:2 ]
@@ -723,8 +723,8 @@ let a36 = boo32_64()
723723
end
724724
725725
# concatenation
726-
@assert [ones(2,2) 2*ones(2,1)] == [1 1 2, 1 1 2]
727-
@assert [ones(2,2), 2*ones(1,2)] == [1 1, 1 1, 2 2]
726+
@assert [ones(2,2) 2*ones(2,1)] == [1 1 2; 1 1 2]
727+
@assert [ones(2,2), 2*ones(1,2)] == [1 1; 1 1; 2 2]
728728
729729
# "end"
730730
X = [ i+2j | i=1:5, j=1:5 ]

0 commit comments

Comments
 (0)