Question: Is it possible to recursively traverse through an expression then build it from the bottom up?

Hey guys.

Basically I want to tranverse the syntax tree, replace an indexed expression based on some rule, then return the modified expression. Here is what I am trying to do. Psuedo code below --

applyRule(expr)
   if isSymbol(expr) then
       return expr;
   elif isIndexed(expr) and ruleSatisfied then
       return modified_expr;
   else
      # Apply operator on the sub-expressions
      # This is the part im having trouble with
      return op(0,expr)(op(map(applyRule, [op(expr)])));
   end if:
end proc:

The line marked with the comment above works MOST of the time, but it'll fail for indexed objects (A[i,j] becomes A(i,j)). The unapply function looked promising, but that also has some corner cases (e.g. if I try to deconstruct the expression A = B*C it will fail)

Kind regards,

woggy

Please Wait...