Each fortran code contains several objects ; variables of course but also labels, functions and subroutines for example. A simple name is not sufficient to describe one of those objects since some homonyms can exist in several units. We use the undocumented type fsym to reference those objects. An fsym is composed of two parts; a name and an unit. The unit is taken in the current forlib. The name can be a string or an integer (i.e. for labels). An fsym can be used as an entry in the symbol table associated to the fortran unit. A simple string can be used instead of a fsym when the unit is $cunit.
Example 22: Displays the
name of all the variables used in the current unit.
SCRIPT ShowSymbolTable() nb := $cunit.SYMTABSIZE cpt := 0 WHILE(cpt < nb) fsym := $cunit.SYMTABENTRY(cpt) IF (fsym.TYPEOFSYMBOL == "variable") THEN PRINT fsym.LOWERNAME ENDIF cpt := cpt + 1 ENDWHILE ENDSCRIPT
Example 23:A set of declarations
DECLARE("include 'mpif.h'") DECLARE("integer ") DECLARE("include 'mpif.h'")
Example 24:increase the first dimension of the variable X.
d1 := "X".DIMENSION(1) newd1 := PARSEEXPR("%e+1",d1) d1 <- newd1 REBUILDSYMTAB($cunit) PRINT "X".DIMENSION(1)
Example 25:With the Fortran declaration integer X(m,n), the expression
"X".TYPE returns the string "integer".
Example 26:With the Fortran declaration integer X(m,n), the expression
"X".TYPESIZE returns 4 (i.e. if the size of default integer is 4).
Example 27:With the Fortran declaration integer X(m,n), the expression
"X".DIMENSION returns 2.
Example 28:Exchange the 2 dimensions of the selected variable name.
SCRIPT exchangeDims () var := $csel IF (var.VARIANT == "array_decl") THEN // Select the name of the declaration var := var.CHILD(1) ENDIF IF (var.VARIANT == "name") THEN // Get the variable name because we do not // to apply the transformation to an array // access such as A(i,j) varname := var.SYMBOL dim1 := COPY(varname.DIMENSION(1)) dim2 := COPY(varname.DIMENSION(2)) varname.DIMENSION(1) <- dim2 varname.DIMENSION(2) <- dim1 ENDIF ENDSCRIPT
Example 29:Some examples of results given by the attribute LOWERB. The target is underlined in each fortran expression:
AAAA AAAAAAAAAAAAA AA AAAAAAAAA
integer A(x+1) 1 (the constant)
integer B(m:n) m (a AST)
print *,C(1:n) 1 (a AST)
print *,n 1 (the constant)
varname := "X" lb := varname.DIMENSION(1).LOWERB ub := varname.DIMENSION(1).UPPERB expr := PARSEEXPR("%e-%e+1",ub,lb)
Warning: In the left part of a AST assignment (see the previous example), the
DIMENSION attribute has an effect on the symbol table. A common error is
to insert a temporary variable.
Example 31:Both forms are not equivalent. The second does not update the
symbol table and so is incorrect.
varname.DIMENSION(1) <- dim
tmp := varname.DIMENSION(1) tmp <- dim
Remark: The attribute DIMENSION can be applied to an array acces (i.e. in a statement such as A=B(i,j)) to get or to change the indexes i or j. Then, it has no effect on the symbol table.