converts a Maple procedure to an abstract syntax tree - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : System : Libraries and Packages : Deprecated Packages and Commands : Deprecated commands : codegen/obsolete/maple2intrep(deprecated)

codegen[maple2intrep] - converts a Maple procedure to an abstract syntax tree

codegen[intrep2maple] - converts an abstract syntax tree to a Maple procedure

Calling Sequence

maple2intrep(f)

intrep2maple(t)

Parameters

f

-

Maple procedure

t

-

expression (an abstract syntax tree)

Description

• 

Important: The maple2intrep and intrep2maple commands have been deprecated.  Use the superseding commands ToInert and FromInert instead to translate between Maple expressions and corresponding inert forms.

• 

The maple2intrep function is used to convert a Maple procedure f into an intermediate representation for the procedure that is suitable for manipulating the code of f. The intermediate representation is an abstract syntax tree. The maple2intrep function also deduces the types of all parameters, local variables, and global variables in f. The intrep2maple function is used to convert an abstract syntax tree t to a Maple procedure.

• 

The abstract syntax tree has the following structure. Express it by using a modified BNF grammar where:

  

* indicates zero or more occurrences

  

+ indicates one or more occurrences

  

[] indicate optional terms, and

  

| indicates alternatives.

tree ::= Proc( Name( declaration ),

               Parameters( declaration* ),

               Options( sequence ),

               Description( string ),

               Locals( declaration* ),

               Globals( declarations* ),

               StatSeq( statement* ) )

declaration ::= name :: type

statseq ::= StatSeq( statement* )

statement ::= expression |

             Assign( name, expression ) |

             If( [condition, statseq]+, [statseq] ) |

             For( name, from, by, to, while, statseq ) |

             For( name, in, while, statseq ) |

             Return( sequence ) |

             Error( sequence ) |

             Break( ) |

             Next( ) |

             Read( string ) |

             Save( sequence ) |

             Comment( ... ) |

             Quote( expression ) |

             Ditto1( ) |

             Ditto2( ) |

             Ditto3( ) |

             tree |

             Nargs( ) | Args[i] | Args

• 

In the abstract syntax tree, the names and Maple expressions become global and will evaluate.

  

Note: In the event that the parameter or local names in the procedure have global values, and it is desirable to use local names instead (to prevent evaluation problems with respect to the variable names), the option outputvars=locals can be used.

  

When working with the abstract syntax tree, be careful not to evaluate parts of the tree, or else, use the `tools/rename` command to rename all symbols in the tree to unique names that do not evaluate. The library routine `tools/unrename` can be used to undo this renaming. An example is given below.

Examples

Important: The maple2intrep and intrep2maple commands have been deprecated.  Use the superseding commands ToInert and FromInert instead to translate between Maple expressions and corresponding inert forms.

withcodegen:

f := proc(x) local t; t := sin(x); x*t end proc:

tree:=maple2intrepf

tree:=ProcNamef..float,Parametersx..float,Options,Description,Localst..float,Globals,StatSeqAssignt,sinx,xt

(1)

op2,tree

Parametersx..float

(2)

op5,tree

Localst..float

(3)

op6,tree

Globals

(4)

intrep2mapletree

procxlocalt;t:=sinx;x*tend proc

(5)

This example shows the problem of evaluation.

f := proc(x,n) local i,t,A;
     A := array(1..n); t := 1; for i to n do t := x*t;  A[i] := t; end do; A
end proc:

tree:=maple2intrepf

tree:=ProcNamef..List1..n,float,Parametersx..float,n..integer,Options,Description,Localsi..integer,t..float,A..List1..n,float,Globals,StatSeqAssignA,array1..n,Assignt,1,Fori,1,1,n,true,StatSeqAssignt,xt,AssignAi,t,A

(6)

tree

Error, non-integer ranges in array/table creation

evaltree,1

ProcNamef..List1..n,float,Parametersx..float,n..integer,Options,Description,Localsi..integer,t..float,A..List1..n,float,Globals,StatSeqAssignA,array1..n,Assignt,1,Fori,1,1,n,true,StatSeqAssignt,xt,AssignAi,t,A

(7)

intrep2mapleevaltree,1

procx,nlocalA,i,t;A:=array1..n;t:=1;foritondot:=x*t;A[i]:=tend do;Aend proc

(8)

Rename all symbols in the program with new names.

macroR=`tools/rename`

R

(9)

macroU=`tools/unrename`

R,U

(10)

tree:=Rmaple2intrepf

tree:=ProcNamef..List1..n,float,Parametersx..float,n..integer,Options,Description,Localsi..integer,t..float,A..List1..n,float,Globals,StatSeqAssignA,array1..n,Assignt,1,Fori,1,1,n,true,StatSeqAssignt,xt,AssignAi,t,A

(11)

tree

ProcNamef..List1..n,float,Parametersx..float,n..integer,Options,Description,Localsi..integer,t..float,A..List1..n,float,Globals,StatSeqAssignA,array1..n,Assignt,1,Fori,1,1,n,true,StatSeqAssignt,xt,AssignAi,t,A

(12)

hastree,A

false

(13)

hastree,RA

true

(14)

Make a simple transformation on the program represented in the intermediate representation.  Make the array A into a global variable.

globals:=RGlobalsA

globals:=GlobalsA

(15)

locals:=subsop3=NULL,op5,tree

locals:=Localsi..integer,t..float

(16)

tree:=subsop5=locals,6=globals,tree

tree:=ProcNamef..List1..n,float,Parametersx..float,n..integer,Options,Description,Localsi..integer,t..float,GlobalsA,StatSeqAssignA,array1..n,Assignt,1,Fori,1,1,n,true,StatSeqAssignt,xt,AssignAi,t,A

(17)

intrep2mapleUtree

procx,nlocali,t;globalA;A:=array1..n;t:=1;foritondot:=x*t;A[i]:=tend do;Aend proc

(18)

This example shows the problem of evaluation in a case where use of `tools/rename` is insufficient for the task (so you must use outputvars=locals instead).

f := proc(X,n) local i; for i to n do X[i] := 0; end do; end proc;

f:=procX,nlocali;foritondoX[i]:=0end doend proc

(19)

X:=Array1..10,i→i

X:=12345678910

(20)

Rmaple2intrepf

Error, (in `intrep/type/bodyscan`) bad index into Array

tree:=maple2intrepf,outputvars='locals'

tree:=ProcNamef..integer,ParametersX..float,n..float,Options,Description,Localsi..integer,Globals,StatSeqFori,1,1,n,true,StatSeqAssignXi,0

(21)

hastree,X,n,i

false

(22)

See Also

codegen[makeproc], FromInert, ToInert


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam