next up previous contents index
Next: Use the pattern-matching facilities Up: Statements Previous: Browsing in the source

Marking and finding nodes

The language provides some attributes to match nodes in equivalent ASTs.

  $\blacktriangleright$ src.PATH(dest)
 
The   expression returns a path from the node src to the node dest. If dest is not in the AST src then the expression returns FALSE. The type of the path is undocumented but it can be used by the attribute APPLYPATH.
  $\blacktriangleright$ src.APPLYPATH(path)
 
The   expression returns the node in the AST src at the position path created by PATH.

$\vartriangleright$ Example 17:Insert a copy of the selected statement in which the selected expression replaced by the integer constant 999.

       

   SCRIPT test ()
     // Create the path from the statement 
     //  to the selected expression 
     path :=  $cstat.PATH($csel)
     // Create a copy of the statement 
     stat := COPY($cstat) 
     // Find the corresponding expression
     expr := stat.APPLYPATH(path) 
     // Insert the modified statement 
     expr <- CREATEVALUED("int_cst",999)  
     PASTE("BEFORE",stat,$cstat)
   ENDSCRIPT

The language provides a mechanism to mark each node with an unique integer value (i.e. an id) .

  $\blacktriangleright$ n.ID
 
The expression     returns the integer id associated to the node n. If necessary, a new id is created for this node.
  $\blacktriangleright$ u.ALLID
 
The expression   initialize the id of each node in the fortran unit u (i.e. see the variable $cunit). The first id is 0 and the expression returns the number of created ids.
  $\blacktriangleright$ u. CLEANID
 
The expression   cleans all the ids in the fortran unit u.
  $\blacktriangleright$ u.FINDID(n)
 
The expression   returns the node of id n in the fortran unit u. in some previous versions of TSF, the obsolete VARWITHID   was used instead of FINDID.

  $\blacktriangleright$ TREECMP(t1,t2)
 
The expression   compares the equality of the ASTs t1 and t2. The result is a boolean value. There is an important difference with the == operator. TREECMP compares the structure of the ASTs while the operator == compare the AST themselves. The table 3 compares the results given by both expressions.


 
Table 3: Comparison of == and TREECMP.
Operands      ==      TREECMP
PARSEEXPR("x") , PARSEEXPR("y") FALSE FALSE
PARSEEXPR("x") , PARSEEXPR("x") FALSE TRUE
var , var (the same AST) TRUE TRUE
 

$\vartriangleright$ Example 18:Print the variant of each node in the current fortran unit

       

   SCRIPT printAllVariant () 
     nbid := $cunit.ALLID 
     cpt := 0 
     WHILE(cpt < nbid) 
       node := $cunit.FINDID(cpt)
       PRINT node.VARIANT
       cpt := cpt + 1 
     ENDWHILE
     foo := $cunit.CLEANID
   ENDSCRIPT

  $\vartriangleright$ Warning: The COPY   function does not preserve ids.


next up previous contents index
Next: Use the pattern-matching facilities Up: Statements Previous: Browsing in the source
Yann Mevel
1999-04-30