<commit_before>(ns logic.util)

(def transform-map
  {:atom  (fn [& a] (symbol (apply str a)))
   :not   #(list 'not %)
   :and   #(list 'and %1 %2)
   :nand  #(list 'nand %1 %2)
   :or    #(list 'or %1 %2)
   :nor   #(list 'nor %1 %2)
   :impl  #(list 'impl %1 %2)
   :nimpl #(list 'nimpl %1 %2)
   :if    #(list 'rimpl %1 %2)
   :nif   #(list 'nrimpl %1 %2)
   :equiv #(list 'equiv %1 %2)
   :xor   #(list 'xor %1 %2)
   :false (fn [] false)
   :true  (fn [] true)
   })

(defn transform-ast
  "Takes an ast as produced by instaparse and transforms it into evaluatable clojure code."
  [ast]
  (insta/transform transform-map ast))

(defn- flat
  [ast]
  (if (and
        (list? ast)
        (n-ary? (first ast)))
    (do
      (println :can-maybe-flat (rest ast))
      ast) ; TODO
    ast))
  
(defn flatten-ast
  [ast]
  (postwalk flat ast))<commit_msg>Flatten n-ary operators (work in progress)<commit_after>(ns logic.util)

(def transform-map
  {:atom  (fn [& a] (symbol (apply str a)))
   :not   #(list 'not %)
   :and   #(list 'and %1 %2)
   :nand  #(list 'nand %1 %2)
   :or    #(list 'or %1 %2)
   :nor   #(list 'nor %1 %2)
   :impl  #(list 'impl %1 %2)
   :nimpl #(list 'nimpl %1 %2)
   :if    #(list 'rimpl %1 %2)
   :nif   #(list 'nrimpl %1 %2)
   :equiv #(list 'equiv %1 %2)
   :xor   #(list 'xor %1 %2)
   :false (fn [] false)
   :true  (fn [] true)
   })

(defn transform-ast
  "Takes an ast as produced by instaparse and transforms it into evaluatable clojure code."
  [ast]
  (insta/transform transform-map ast))

(defn- bar
  [o l]
  (if (list? l)
    (= o (first l))
    false))

(defn- foo
  [ast]
  (let [o (first ast) a (rest ast)] ; operator and arguments
    (let [flat (map #(rest %) (filter (partial bar o) a))
          not-flat (filter (partial (complement bar) o) a)]
      (println :ast ast)
      (println :flat flat)
      (println :not-flat not-flat)
      (println (apply concat  `((~o) ~@flat ~not-flat))))
      ast))
    
  

(defn- flat
  [ast]
  (if (and
        (list? ast)
        (n-ary? (first ast)))
    (do
      (println :can-maybe-flat (rest ast))
      (foo ast)) ; TODO
    ast))
  
(defn flatten-ast
  [ast]
  (postwalk flat ast))
{'fraction_non_alphanumeric': 0.1779000458505273, 'fraction_numerical': 0.018340210912425492, 'mean_word_length': 2.388198757763975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1272471', 'n_tokens_mistral': 913, 'n_tokens_neox': 902, 'n_words': 289}