Access _all_ downstream annotations for node in Rascal

I am reading this question trying to do something like this. The answer given there does not solve my problem.

I want to use the visit operator to determine the "mass" of each subtree, so for each node I want to sum the masses of all descendants. For example, the visit step that encounters this node expression with a list in it:

\anode([\bnode()[@mass=1], \bnode()[@mass=2]], \cnode()[@mass=5])

should do this:

\anode([\bnode()[@mass=1], \bnode()[@mass=2]], \cnode()[@mass=5])[@mass=8]

Therefore, I do not just want to filter out non-nodes, but actually cross them. Including a case for lists in my visit statement does not work (at least not obvious) because the lists cannot contain annotations.

Is there a natural way to spread the entire annotation of information on the tree?

+4
source share
1

, :

x = \anode([\bnode()[@mass=1], \bnode()[@mass=2]], \cnode()[@mass=5]);

visit(x) {
  case n:\anode(l) => n[@mass=(0 | it + e@mass | e <- l)]
}

(, )

node, :

visit(x) {
  case n:str _(l) => n[@mass=(0 | it + e@mass | e <- l)]
}

, , .., :

import Node;
int computeMass(list[value] x) {
  mass = 0;
  top-down-break visit(x) {
    case node x : mass += (x@mass?) ? x@mass : 0;
  }
  return mass;
}

visit(x) {
  case node n => n[@mass=computeMass(getChildren(n))]
}

, .

, " " ; , , \cnode(mass=2) \code()[@mass=2]. , :

data N() 
  = anode(list[N] children, int mass=(0 | it + c.mass | c <- children)) 
  | cnode(int mass=0)
  ;

anode([cnode(mass=1),cnode(mass=2)]).mass == 3
+2
source

All Articles