<commit_before>#lang racket/base
(require (for-syntax racket/base)
         racket/match)

(provide 2dmatch)
(define-syntax (2dmatch stx)
  (syntax-case stx ()
    [(_ widths heights [(cell ...) rhs ...] ...)
     (let ()
       
       ;; coord-to-content : hash[(list num num) -o> (listof syntax)]
       (define coord-to-content (make-hash))
       (define let-bindings '()) 
       
       (define main-args #f)
       
       ;; build up the coord-to-content mapping
       ;; side-effect: record need for let bindings to
       ;; cover the the situation where multiple cells
       ;; are joined together
       ;; (this code is similar to that in cond.rkt, but
       ;; my attempt at abstracting between them was unsuccessful)
       (for ([cells-stx (in-list (syntax->list #'((cell ...) ...)))]
             [rhses (in-list (syntax->list #'((rhs ...) ...)))])
         (define cells (syntax->datum cells-stx))
         (define on-boundary? (ormap (λ (lst) (or (= 0 (list-ref lst 0))
                                                  (= 0 (list-ref lst 1))))
                                     cells))
         (define rhses-lst (syntax->list rhses))
         (cond
           [(member (list 0 0) cells) 
            (unless (and rhses-lst (= 2 (length rhses-lst)))
              (raise-syntax-error '2dmatch "cell at 0,0 must contain two expressions"))
            (set! main-args rhses)]
           [on-boundary?
            (unless (and rhses-lst (= 1 (length rhses-lst)))
              (raise-syntax-error '2dmatch 
                                  (format 
                                   "cell at ~a,~a must contain exactly one match pattern, found ~a"
                                   (list-ref (car cells) 0) (list-ref (car cells) 1)
                                   (length rhses-lst))
                                  stx))
            (hash-set! coord-to-content (car cells) (car (syntax->list rhses)))]
           [else
            (when (null? (syntax-e rhses))
              (raise-syntax-error '2dmatch 
                                  (format "cell at ~a,~a should not be empty"
                                          (list-ref (car cells) 0)
                                          (list-ref (car cells) 1))
                                  stx))
            ;; this code will duplicate the rhses expressions
            ;; in the case that there are multiple cells in `cells'
            ;; it would be better to analyze the corresponding
            ;; match patterns and then stick the code in a function
            ;; whose arguments are the intersection of the bound variables
            ;; (supplying the arguments in a call in the cell)
            (for ([cell (in-list cells)])
              (hash-set! coord-to-content 
                         cell
                         (syntax->list rhses)))]))
       
       (define num-of-cols (length (syntax->list #'widths)))
       (define num-of-rows (length (syntax->list #'heights)))
       #`(let #,let-bindings
           (match* #,main-args
             #,@(for*/list ([x (in-range 1 num-of-cols)]
                            [y (in-range 1 num-of-rows)])
                  #`[(#,(hash-ref coord-to-content (list x 0))
                      #,(hash-ref coord-to-content (list 0 y)))
                     (let () #,@(hash-ref coord-to-content (list x y)))]))))]))<commit_msg>add error message with the right form name<commit_after>#lang racket/base
(require (for-syntax racket/base)
         racket/match)

(provide 2dmatch)
(define-syntax (2dmatch stx)
  (syntax-case stx ()
    [(_ widths heights [(cell ...) rhs ...] ...)
     (let ()
       
       ;; coord-to-content : hash[(list num num) -o> (listof syntax)]
       (define coord-to-content (make-hash))
       (define let-bindings '()) 
       
       (define main-args #f)
       
       ;; build up the coord-to-content mapping
       ;; side-effect: record need for let bindings to
       ;; cover the the situation where multiple cells
       ;; are joined together
       ;; (this code is similar to that in cond.rkt, but
       ;; my attempt at abstracting between them was unsuccessful)
       (for ([cells-stx (in-list (syntax->list #'((cell ...) ...)))]
             [rhses (in-list (syntax->list #'((rhs ...) ...)))])
         (define cells (syntax->datum cells-stx))
         (define on-boundary? (ormap (λ (lst) (or (= 0 (list-ref lst 0))
                                                  (= 0 (list-ref lst 1))))
                                     cells))
         (define rhses-lst (syntax->list rhses))
         (cond
           [(member (list 0 0) cells) 
            (unless (and rhses-lst (= 2 (length rhses-lst)))
              (raise-syntax-error '2dmatch "cell at 0,0 must contain two expressions"))
            (set! main-args rhses)]
           [on-boundary?
            (unless (and rhses-lst (= 1 (length rhses-lst)))
              (raise-syntax-error '2dmatch 
                                  (format 
                                   "cell at ~a,~a must contain exactly one match pattern, found ~a"
                                   (list-ref (car cells) 0) (list-ref (car cells) 1)
                                   (length rhses-lst))
                                  stx))
            (hash-set! coord-to-content (car cells) (car (syntax->list rhses)))]
           [else
            (when (null? (syntax-e rhses))
              (raise-syntax-error '2dmatch 
                                  (format "cell at ~a,~a should not be empty"
                                          (list-ref (car cells) 0)
                                          (list-ref (car cells) 1))
                                  stx))
            ;; this code will duplicate the rhses expressions
            ;; in the case that there are multiple cells in `cells'
            ;; it would be better to analyze the corresponding
            ;; match patterns and then stick the code in a function
            ;; whose arguments are the intersection of the bound variables
            ;; (supplying the arguments in a call in the cell)
            (for ([cell (in-list cells)])
              (hash-set! coord-to-content 
                         cell
                         (syntax->list rhses)))]))
       
       (define num-of-cols (length (syntax->list #'widths)))
       (define num-of-rows (length (syntax->list #'heights)))
       #`(let #,let-bindings
           (match* #,main-args
             #,@(for*/list ([x (in-range 1 num-of-cols)]
                            [y (in-range 1 num-of-rows)])
                  #`[(#,(hash-ref coord-to-content (list x 0))
                      #,(hash-ref coord-to-content (list 0 y)))
                     (let () #,@(hash-ref coord-to-content (list x y)))])
             [(_ _)
              (2dmatch-error #,@main-args)])))]))

(define (2dmatch-error a b)
  (error '2dmatch "no matching clauses for ~e and ~e" a b))
{'fraction_non_alphanumeric': 0.12773617511520738, 'fraction_numerical': 0.007056451612903226, 'mean_word_length': 1.3670756646216768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5314766', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1834, 'n_words': 607}