 (declare produce-recent-parent-tree-atom!)
 
 (defn get-recent-tree-at-inner-path
-  [path recent-trees]
+  [path recent-trees & {:keys [skip-closed-check]}]
   (let [entry (@recent-trees path)]
-    (if (not (some->> entry
-                      deref
-                      keys
-                      (some #{:return :throw})))
+    (if (or skip-closed-check
+            (not (some->> entry
+                          deref
+                          keys
+                          (some #{:return :throw}))))
       entry
       nil)))
 
-(defn assoc-to-recent-trees!
-  [tree-atom parent-tree-atom recent-trees]
+(defn update-tree!
+  [tree recent-trees]
+  #spy/d @recent-trees
+  (reset! #spy/d (-> tree
+                     :inner-path
+                     (@recent-trees))
+          tree))
+
+(defn conj-to-parent!
+  [tree-atom parent-atom]
+  (swap! (:children @parent-atom)
+         conj
+         tree-atom))
+
+(defn push-to-recent-trees!
+  [tree-atom recent-trees]
   (swap! recent-trees
          assoc
          (:inner-path @tree-atom)
          tree-atom)
-  (when parent-tree-atom
-    (println "vvvvv")
-    #spy/d tree-atom
-    #spy/d parent-tree-atom
-    (println "^^^^^")
-    (clojure.stacktrace/print-stack-trace (Exception. "assoc-to-recent-trees!"))
-    (swap! (-> @parent-tree-atom
-               :inner-path
-               (@recent-trees)
-               deref
-               :children)
-           conj
-           tree-atom)))
+  #spy/d @recent-trees)
 
 (defn mk-recent-tree-at-inner-path
   [inner-path recent-trees]
   (if (nil? inner-path)
-    (let [new-parent (get-temp-root-parent)]
-      (assoc-to-recent-trees! new-parent
-                              nil
+    (let [new-tree (get-temp-root-tree)]
+      (push-to-recent-trees!  new-tree
                               recent-trees)
-      new-parent)
-    (let [new-grandparent (produce-recent-parent-tree-atom! inner-path
-                                                            recent-trees)
-          new-parent (-> (trace/mk-tree :parent new-grandparent)
-                         (assoc :inner-path inner-path
-                                :parent-path (:path new-grandparent))
-                         atom)]
-      (assoc-to-recent-trees! new-parent
-                              new-grandparent
-                              recent-trees)
-      new-parent)))
+      new-tree)
+    (let [parent (produce-recent-parent-tree-atom! inner-path
+                                                   recent-trees)
+          new-tree (-> (trace/mk-tree :parent @parent)
+                       (assoc :inner-path inner-path
+                              :parent-path (:path @parent))
+                       atom)]
+      (push-to-recent-trees! new-tree
+                             recent-trees)
+      (conj-to-parent! new-tree
+                       parent)
+      new-tree)))
 
 (defn produce-recent-tree-atom!
-  [inner-path recent-trees]
+  [inner-path recent-trees & {:keys [skip-closed-check]}]
   (if-let [tree-atom (get-recent-tree-at-inner-path inner-path
-                                                    recent-trees)]
+                                                         recent-trees
+                                                         :skip-closed-check skip-closed-check)]
     tree-atom
     (mk-recent-tree-at-inner-path inner-path
                                   recent-trees)))


   (-/p (xpand form1 {:name "name1" :ns "ns1"} ))

   (def form2 '(str (inc 1) 3))

   (-/p (xpand form2 {:name "name1" :ns "ns1"} ))

   (-/p (eval (xpand form1 {:name "name1" :ns "ns1"} )))

   (comment))


                       :return value
                       :throw throw ;;TODO not right
                       :ended-at (trace/now))]
      (update-tree! this'
                    recent-trees)
      value)))

(defn tr-macro
  [path [recent-trees] template mcro v]
  (def rt' @recent-trees)

  (let [tree @(produce-recent-tree-atom! path
                                         recent-trees
                                         :skip-closed-check true)]
    (-> tree
        (assoc :return v
               :inner-tags [:macro mcro])
        (merge template)
        (update-tree! recent-trees))
    v))

(defn tr-if-ret
  [path [recent-trees] template v]
  (-> path
      (produce-recent-tree-atom! recent-trees)
      deref
      (merge template)
      (assoc :return v
             :inner-tags [:if])
      (update-tree! recent-trees))
  v)

(defn tr-if-test
  [path [recent-trees] v]
  (-> path
      (produce-recent-parent-tree-atom! recent-trees)
      deref
      (assoc :args [v])
      (update-tree! recent-trees))
  v)

(defn tr-if-branch
  [path [recent-trees] test v]
  (-> path
      (produce-recent-parent-tree-atom! recent-trees)
      deref
      (assoc-in [:args 1] v)
      (update-tree! recent-trees))
  v)

(declare xpand-form)
