next up previous contents index
Next: Modifying the fortran code Up: Statements Previous: Type casts

Creating Fortran statements and expressions

Most of Fortran codes are described by ASTs. Some functions are provided to help to the creation of such ASTs :

  $\blacktriangleright$ COPY
 
The function   creates a copy of its first argument (a AST).
  $\blacktriangleright$ CREATEVALUED(v,n)
 
The function   is used to create a node without child but with a value. Nodes with the following variants v can be created:
  $\blacktriangleright$ CREATE(v,c1,...,cn)
 
The function   creates a node with a specified variant. Its first argument v is the node variant. The remaining arguments c1,...,cn are the children of the node. The number of children depends on the variant. Those parameters are implicitly transformed into ASTs by the function TOTREE. 
$\vartriangleright$ Example 32:Creation of an assignment a:=2    
    varname := "a" 
    stat := CREATE("ass",CREATEVALUED("name",varname),2)

Those functions are sufficient to create all ASTs. However, some convenience functions are provided to create ASTs from fortran strings.

  $\blacktriangleright$ CREATEEXPR(expr)
 
The obsolete function call   creates a AST from the fortran expression in the string expr. This function is obsolete and is emulated by the new function PARSE.

$\vartriangleright$ Example 33:Create a simple expression

 

    expr := CREATEEXPR("4*n+3")

The CREATEEXPR function does not use the symbol table associated to the current fortran unit. Especially, some wrong ASTs can be generated for functions call. For instance, the expression "send(i,j)" will be interpreted as a matrix access even if "send" is known as a function. When the string is constant (i.e. not a variable), it is possible to reference some TSF variables:

$\vartriangleright$ Example 34:Creation of the expression (n+1)*(n-3)

       lhs := CREATEEXPR("n+1") 
       rhs := CREATEEXPR("n-3") 
       expr := CREATEEXPR("#lhs * #rhs")

$\vartriangleright$ Example 35:A wrong version without a constant string (see PARSEXPR)

       lhs := CREATEEXPR("n+1")  
       rhs := CREATEEXPR("n-3")  
       str := "#lhs * #rhs"  
       expr := CREATEEXPR(str)
  $\blacktriangleright$ CREATESTAT(label,stat)
 
The obsolete function call   and CREATESTAT(stat) creates a AST from the fortran statement in the string stat. CREATESTAT works like CREATEEXPR but it is not possible to create more than one statement or a complex statement such as an IF or a DO.
  $\blacktriangleright$ PARSE(type,format,p1,...pn)
 
The new function call   is an improved version of CREATEEXPR and CREATESTAT. Its first argument type must be the string "expr" to create an expression or "stat" to create a statement. The argument format is a string with a fortran code. The arguments p1,...,pn are inserted where the following tokens are found (in lexical order). When an argument is not a root AST (i.e. the attribute PARENT returns an AST) then a copy is performed before the insertion. In doubt, it is recommended to create explicitaly a copy of the argument with the COPY function. The function PARSE can create structured statements such as DO loops but can not create sequences of statements. In fact, it is always possible to create a structured statement and to extract its body. For statements, the format respects the fixed Fortran format. $\vartriangleright$ Example 36:Creation of a DO statement  
        start:=PARSE("expr","1")
        end:=PARSE("expr","n")
        stat:=PARSE("stat",
        "C This is a comment
                       DO i:=%e,%e 
                         PRINT *,i 
                       ENDDO",
           start,end)
$\vartriangleright$ Example 37:Creation of a sequence of statement. an IF statement is used to encapsulate the sequence.
        // Create the IF statement
        ifstat:=PARSE("stat",
        "          IF (.TRUE.) THEN
                     PRINT *,i 
                     PRINT *,j 
                   ENDIF")
        // Get and detach the sequence
        stat := ifstat.THENCLAUSE
        stat <- EMPTY

  $\vartriangleright$ Question: What is the statement produced by this command ?

       stat:=PARSE("stat","call execute(10,20)")\\
  $\vartriangleright$ Answer: Nothing. This string will be interpreted as a fortran comment. The correct command is

       stat:=PARSE("stat","          call execute(10,20)")\\

  $\vartriangleright$ Remark: The function call PARSEEXPR(format,p1,...,pn)   is stricly equivalent to PARSE("expr",format,p1,...,pn).

 
  $\vartriangleright$ Remark: The function call PARSESTAT(format,p1,...,pn)   is stricly equivalent to PARSE("stat",format,p1,...,pn).


next up previous contents index
Next: Modifying the fortran code Up: Statements Previous: Type casts
Yann Mevel
1999-04-30