<commit_before>(library
  (harlan front expand-primitives)
  (export expand-primitives)
  (import
   (rnrs)
   (elegant-weapons helpers)
   (elegant-weapons compat))

  (define externs (make-parameter '()))

  (define (add-externs prim)
    (case prim
      ((write-pgm)
       (externs
         (append
           `((extern open_outfile (str) -> (ptr ofstream))
             (extern write_str (str (ptr ofstream)) -> void)
             (extern write_int (int (ptr ofstream)) -> void))
           (externs))))))

  (define-match expand-primitives
    ((module ,[expand-prim-decl -> decl*] ...)
     `(module ,(externs) ... ,decl* ...)))
    
  (define-match expand-prim-decl
    ((fn ,name ,args ,t ,[expand-prim-stmt -> stmt])
     `(fn ,name ,args ,t ,stmt))
    ((extern ,name ,args -> ,rtype)
     `(extern ,name ,args -> ,rtype)))

  (define-match expand-prim-stmt
    ((let ((,x* ,t* ,[expand-prim-expr -> e*]) ...) ,[body])
     `(let ((,x* ,t* ,e*) ...) ,body))
    ((set! ,[expand-prim-expr -> lhs] ,[expand-prim-expr -> rhs])
     `(set! ,lhs ,rhs))
    ((vector-set! ,t
       ,[expand-prim-expr -> v]
       ,[expand-prim-expr -> i]
       ,[expand-prim-expr -> e])
     `(vector-set! ,t ,v ,i ,e))
    ((if ,[expand-prim-expr -> test] ,[conseq] ,[altern])
     `(if ,test ,conseq ,altern))
    ((if ,[expand-prim-expr -> test] ,[conseq])
     `(if ,test ,conseq))
    ((while ,[expand-prim-expr -> test] ,[body])
     `(while ,test ,body))
    ((for (,x ,[expand-prim-expr -> start]
            ,[expand-prim-expr -> stop]) ,[body])
     `(for (,x ,start ,stop) ,body))
    ((begin ,[stmt*] ...)
     `(begin . ,stmt*))
    ((print (vec ,m (vec ,n ,t)) ,[expand-prim-expr -> e])
     (let ((v (gensym 'v)) (v-row (gensym 'vrow))
           (i (gensym 'i)) (j (gensym 'j)))
       `(let ((,v (vec ,m (vec ,n ,t)) ,e))
          (for (,i (int 0) (length (var (vec ,m (vec ,n ,t)) ,v)))
            (let ((,v-row
                   (vec ,n ,t)
                   (vector-ref (vec ,n ,t)
                     (var (vec ,m (vec ,n ,t)) ,v)
                     (var int ,i))))
              (begin
                (print (str "[ "))
                (for (,j (int 0) (length (var (vec ,n ,t) ,v-row)))
                  (begin
                    (print (vector-ref
                             ,t (var (vec ,n ,t) ,v-row) (var int ,j)))
                    (print (str " "))))
                (print (str "]\n"))))))))
    ((print ,t ,[expand-prim-expr -> e])
     `(print ,e))
    ((assert ,[expand-prim-expr -> e])
     `(assert ,e))<commit_msg>Changed the code that expands print a little bit so that it can print multidimensional vectors as well as one dimensional vectors.  Only tested it on 1 and 2d vectors, but when we start needing 3d vectors, we can reevaluate it.  This closes #13<commit_after>(library
  (harlan front expand-primitives)
  (export expand-primitives)
  (import
   (rnrs)
   (elegant-weapons helpers)
   (elegant-weapons compat))

  (define externs (make-parameter '()))

  (define (add-externs prim)
    (case prim
      ((write-pgm)
       (externs
         (append
           `((extern open_outfile (str) -> (ptr ofstream))
             (extern write_str (str (ptr ofstream)) -> void)
             (extern write_int (int (ptr ofstream)) -> void))
           (externs))))))

  (define-match expand-primitives
    ((module ,[expand-prim-decl -> decl*] ...)
     `(module ,(externs) ... ,decl* ...)))
    
  (define-match expand-prim-decl
    ((fn ,name ,args ,t ,[expand-prim-stmt -> stmt])
     `(fn ,name ,args ,t ,stmt))
    ((extern ,name ,args -> ,rtype)
     `(extern ,name ,args -> ,rtype)))

  (define-match expand-prim-stmt
    ((let ((,x* ,t* ,[expand-prim-expr -> e*]) ...) ,[body])
     `(let ((,x* ,t* ,e*) ...) ,body))
    ((set! ,[expand-prim-expr -> lhs] ,[expand-prim-expr -> rhs])
     `(set! ,lhs ,rhs))
    ((vector-set! ,t
       ,[expand-prim-expr -> v]
       ,[expand-prim-expr -> i]
       ,[expand-prim-expr -> e])
     `(vector-set! ,t ,v ,i ,e))
    ((if ,[expand-prim-expr -> test] ,[conseq] ,[altern])
     `(if ,test ,conseq ,altern))
    ((if ,[expand-prim-expr -> test] ,[conseq])
     `(if ,test ,conseq))
    ((while ,[expand-prim-expr -> test] ,[body])
     `(while ,test ,body))
    ((for (,x ,[expand-prim-expr -> start]
            ,[expand-prim-expr -> stop]) ,[body])
     `(for (,x ,start ,stop) ,body))
    ((begin ,[stmt*] ...)
     `(begin . ,stmt*))
    ((print (vec ,n ,t) ,[expand-prim-expr -> e])
     (let ((v (gensym 'v)) 
           (i (gensym 'i)))
       `(let ((,v (vec ,n ,t) ,e))
          (begin
            (print (str "["))
            (for (,i (int 0) (length (var (vec ,n ,t) ,v)))
              (begin
                ,(if (scalar-type? t)
                     `(if (> (var int ,i) (int 0)) (print (str " ")))
                     `(if (> (var int ,i) (int 0)) (print (str " \n "))))
                ,(expand-prim-stmt
                   `(print ,t
                      (vector-ref ,t
                        (var (vec ,n ,t) ,v) (var int ,i)))) ))
            (print (str "]"))))))
    ((print ,t ,[expand-prim-expr -> e])
     `(print ,e))
    ((assert ,[expand-prim-expr -> e])
     `(assert ,e))
{'fraction_non_alphanumeric': 0.21450557906887263, 'fraction_numerical': 0.001923816852635629, 'mean_word_length': 1.8979933110367893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4872418', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1828, 'n_words': 508}