|
703 | 703 | ranges))
|
704 | 704 |
|
705 | 705 | ; 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) |
711 | 729 | (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) |
720 | 740 | (begin (take-token s)
|
721 | 741 | (if (pair? outer)
|
722 |
| - (fix 'vcat (update-outer vec)) |
| 742 | + (fix 'vcat (update-outer vec outer)) |
723 | 743 | (if (or (null? vec) (null? (cdr vec)))
|
724 | 744 | (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)))))) |
750 | 778 |
|
751 | 779 | ; for sequenced evaluation inside expressions: e.g. (a;b, c;d)
|
752 | 780 | (define (parse-stmts-within-expr s)
|
|
0 commit comments