 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Helpers
 
+(declare read-formula)
+
 (defn- formula? [s]
   (string? (first (re-matches #"^=.*" s))))
 
 (defn- write-formula [s state]
-  (let [formula (get-in state [:cells (:focus state) :formula])]
+  (let [formula (get-in state [(:focus state) :formula])]
     (str formula " " s)))
 
 (defn- cell-value [xy state]
-  (let [cell (get-in state [:cells xy])]
+  (let [cell (state xy)]
     (if (:formula cell)
       (read-formula (:formula cell) state)
       (:value cell))))

(def ops {"/" / "*" * "+" + "-" -})

(defn resolve-var [xy env]
  (get-in env [:cells xy :value]))

(defn string->token [s env]
  (or (ops s)

  (match message

    [[:display xy] :click _]
    (case (:mode state)

      :formula
      (let [formula (write-formula xy state)]
        [[(source-mode xy)
          (display-mode (:source state))
          [[:edit (:focus state)] :value formula]]
         (assoc state :source xy (:focus state) {:formula formula})])

      [[(edit-mode xy)
        (display-mode (:edit state))]
       (assoc state :edit xy)])
