Next: Modifying the fortran code
Up: Statements
Previous: Type casts
Most of Fortran codes are described by ASTs. Some functions are provided to
help to the creation of such ASTs :
-
COPY
-
The function creates a copy of its first argument (a
AST).
-
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:
- int_cst - an integer constant. The function TOINT is
implicitly applied to n.
- real_cst - a simple precision real constant. The function TOREAL is
implicitly applied to n.
- double_cst - a double precision real constant. The TSF
language does dot supports the double precision type but the parameter
n can be a string (i.e. for example "314.15D87").
- fstring_cst - a constant string. The function TOSTRING is
implicitly applied to n.
- name - a variable name. The parameter n must be a string.
-
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.
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.
-
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.
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:
- Each occurrence of
?var
is the pattern variable
?var
.
- Each occurrence of
#var
is the TSF variable var
.
Example 34:Creation of the expression (n+1)*(n-3)
lhs := CREATEEXPR("n+1")
rhs := CREATEEXPR("n-3")
expr := CREATEEXPR("#lhs * #rhs")
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)
-
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.
-
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).
- %e or %E - The corresponding argument describes an expression. It
can be a AST, a string interpreted a variable name or a numerical
value.
- %s or %S - The corresponding argument must be a AST that
describes a statement.
- %l or % - The corresponding argument must be an integer that
describes a label.
- %% - The % character is inserted. No argument is used.
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.
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)
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
Question: What is the statement produced by this command ?
stat:=PARSE("stat","call execute(10,20)")\\
Answer: Nothing. This string will be interpreted as a fortran comment.
The correct command is
stat:=PARSE("stat"," call execute(10,20)")\\
Remark: The function call PARSEEXPR(format,p1,...,pn)
is stricly equivalent to
PARSE("expr",format,p1,...,pn).
Remark: The function call
PARSESTAT(format,p1,...,pn)
is stricly equivalent to
PARSE("stat",format,p1,...,pn).
Next: Modifying the fortran code
Up: Statements
Previous: Type casts
Yann Mevel
1999-04-30