Browse Source

added function to detect cycles in variable references (integration incomplete)

Brandon Wong 4 years ago
parent
commit
b3d9cf5d5f
1 changed files with 40 additions and 5 deletions
  1. 40 5
      frontend/src/microtables_frontend/core.cljs

+ 40 - 5
frontend/src/microtables_frontend/core.cljs

@@ -58,19 +58,54 @@
                               r (.parseInt js/window (re-find #"[0-9]+$" s))]
                           {:row r :col c}))))
 
+;TODO: deal with lowercase cell references
 (defn find-cell [data c r]
   (some #(if (and (= (:col %) c) (= (:row %) r)) %) data))
+(defn find-ref [data cell-ref]
+  (some (fn [{:keys [row col] :as datum}] (if (and (= row (:row cell-ref)) (= col (:col cell-ref))) datum)) data))
+(defn copy-display-values [data display-values]
+  (let [removed (map #(-> % (dissoc :vars) (dissoc :refs) (dissoc :found) (dissoc :inputs)) display-values)]
+    (into data removed)))
+
+;TODO: TEST THIS
+;TODO: memoize dynamically?
+(defn find-cycle [data datum ances]
+  (let [cur {:row (:row datum) :col (:col datum)}
+        this-and-above (conj ances cur)
+        refs (:refs datum)
+        found (not (empty? (clojure.set/intersection this-and-above (set refs))))
+        ]
+  (println "searching for cycle" datum ances found)
+    (if found
+      :cycle-error
+      (some (fn [cell]
+              (find-cycle data (find-ref data cell) this-and-above)
+              ) refs)
+      )
+    )
+  )
+
+(defn find-formula-problems [data l]
+  ;(println "searching for potential problems" l (some #(= % {:row (:row l) :col (:col l)}) (:refs l)))
+  (println "searching for potential problems" (find-cycle data l #{}))
+  ;(loop []
+    
+    ;)
+  )
+
 (defn find-val [data c r]
   (let [l (find-cell data c r)
-        v (get l :display (get l :value))]
+        v (get l :display (get l :value))
+        formula? (and (string? v) (= (first v) "="))
+        formula-value (if formula? (find-formula-problems data l))
+        ]
     (println "found?" c r v l (get l :value) (get l :display))
     (cond
       (nil? v) 0
-      (and (string? v) (= (first v) "=")) :not-yet
+      formula? :not-yet
       :else v)))
-(defn copy-display-values [data display-values]
-  (let [removed (map #(-> % (dissoc :vars) (dissoc :refs) (dissoc :found) (dissoc :inputs)) display-values)]
-    (into data removed)))
+
+
 
 ;TODO: figure out how to re-evaluate only when the cell modified affects other cells
 (defn re-evaluate [data]