Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure tools.analyzer-识别最后一个叶节点?_Clojure_Abstract Syntax Tree - Fatal编程技术网

Clojure tools.analyzer-识别最后一个叶节点?

Clojure tools.analyzer-识别最后一个叶节点?,clojure,abstract-syntax-tree,Clojure,Abstract Syntax Tree,我正在努力使用tools.analyzer为我遇到的问题找到可靠的解决方案 我试图实现的是,给定一个ast节点,树中最后/最远的节点是什么?例如,如果分析了以下代码:(def a(do(+12)3)) 是否有可靠的方法将值为“3”的节点标记为此树中的最后一个节点?本质上,我试图做的是找出哪种形式最终将绑定到var b。像上面这样的问题是我几年前创建tupelo.forest库的原因 您可能希望查看: 许多 首先,让我们在将数据放入hiccup格式后,以树的形式添加数据。下面是如何进行的

我正在努力使用tools.analyzer为我遇到的问题找到可靠的解决方案

我试图实现的是,给定一个ast节点,树中最后/最远的节点是什么?例如,如果分析了以下代码:(def a(do(+12)3))


是否有可靠的方法将值为“3”的节点标记为此树中的最后一个节点?本质上,我试图做的是找出哪种形式最终将绑定到var b。

像上面这样的问题是我几年前创建
tupelo.forest
库的原因

您可能希望查看:

  • 许多
首先,让我们在将数据放入
hiccup
格式后,以树的形式添加数据。下面是如何进行的概述,单元测试显示了操作的结果:

(ns tst.tupelo.forest-examples
  (:use tupelo.core tupelo.forest tupelo.test))

(dotest-focus
  (hid-count-reset)
  (with-forest (new-forest)
    (let [data-orig     (quote (def a (do (+ 1 2) 3)))
          data-vec      (unlazy data-orig)
          root-hid      (add-tree-hiccup data-vec)
          all-paths     (find-paths root-hid [:** :*])
          max-len       (apply max (mapv #(count %) all-paths))
          paths-max-len (keep-if #(= max-len (count %)) all-paths)]
结果如下:

      (is= data-vec (quote [def a [do [+ 1 2] 3]]))
      (is= (hid->bush root-hid)
        (quote
          [{:tag def}
           [{:tag :tupelo.forest/raw, :value a}]
           [{:tag do}
            [{:tag +}
             [{:tag :tupelo.forest/raw, :value 1}]
             [{:tag :tupelo.forest/raw, :value 2}]]
            [{:tag :tupelo.forest/raw, :value 3}]]]))
      (is= all-paths
        [[1007]
         [1007 1001]
         [1007 1006]
         [1007 1006 1004]
         [1007 1006 1004 1002]
         [1007 1006 1004 1003]
         [1007 1006 1005]])
      (is= paths-max-len
        [[1007 1006 1004 1002]
         [1007 1006 1004 1003]])
      (nl)
      (is= (format-paths paths-max-len)
        (quote [[{:tag def}
                 [{:tag do} [{:tag +} [{:tag :tupelo.forest/raw, :value 1}]]]]
                [{:tag def}
                 [{:tag do} [{:tag +} [{:tag :tupelo.forest/raw, :value 2}]]]]])))))

根据您的具体目标,您可以继续进一步处理。

这是一个比您的简单问题更复杂的目标。如果您想知道哪个表单将绑定到
a
,您不需要“树中的最后一片叶子”。考虑<代码>(DEF A(+ 1 2))< /代码>。您认为
2
是将绑定到
a
的内容吗?当然不是!因此,在解决问题之前,您需要更正式地确定您的实际目标。请澄清问题re
var b