             [om-tools.dom :as dom :include-macros true]
             [om.core :as om :include-macros true]
             [clojure.string :as string]))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Helpers
+
+(defn- formula? [s]
+  (string? (first (re-matches #"^=.*" s))))
+
+(defn- write-formula [s state]
+  (let [formula (get-in state [:cells (:focus state) :formula])]
+    (str formula " " s)))
+
+(defn- cell-value [xy state]
+  (let [cell (get-in state [:cells xy])]
+    (if (:formula cell)
+      (read-formula (:formula cell) state)
+      (:value cell))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Formula interpreter

(defn string->token [s env]
  (or (ops s)
      (let [n (js/parseInt s)]
        (if (integer? n) n (resolve-var s env)))))

(defn parse-formula [s env]
  (let [[_ a b c] (string/split s #" ")]

(def source-mode  (cell-mode "source-mode"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The Brain

(defn behavior [message state]
  (println message state)
  (match message

    [[:display xy] :click _]
