codegen
maple2intrep
converts a Maple procedure to an abstract syntax tree
intrep2maple
converts an abstract syntax tree to a Maple procedure
Calling Sequence
Parameters
Description
Examples
maple2intrep(f)
intrep2maple(t)
f
-
Maple procedure
t
expression (an abstract syntax tree)
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.
with⁡codegen:
f := proc(x) local t; t := sin(x); x*t end proc:
tree≔maple2intrep⁡f
tree≔Proc⁡Name⁡f..float,Parameters⁡x..float,Options⁡,Description⁡,Locals⁡t..float,Globals⁡,StatSeq⁡Assign⁡t,sin⁡x,x⁢t
op⁡2,tree
Parameters⁡x..float
op⁡5,tree
Locals⁡t..float
op⁡6,tree
Globals⁡
intrep2maple⁡tree
procxlocalt;t ≔ sin⁡x;x*tend proc
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≔Proc⁡Name⁡f..List⁡1..n,float,Parameters⁡x..float,n..integer,Options⁡,Description⁡,Locals⁡i..integer,t..float,A..List⁡1..n,float,Globals⁡,StatSeq⁡Assign⁡A,Array⁡1..n,Assign⁡t,1,For⁡i,1,1,n,true,StatSeq⁡Assign⁡t,x⁢t,Assign⁡Ai,t,false,A
tree
Error, integer dimensions required
eval⁡tree,1
Proc⁡Name⁡f..List⁡1..n,float,Parameters⁡x..float,n..integer,Options⁡,Description⁡,Locals⁡i..integer,t..float,A..List⁡1..n,float,Globals⁡,StatSeq⁡Assign⁡A,Array⁡1..n,Assign⁡t,1,For⁡i,1,1,n,true,StatSeq⁡Assign⁡t,x⁢t,Assign⁡Ai,t,false,A
intrep2maple⁡eval⁡tree,1
procx,nlocalA,i,t;A ≔ Array⁡1..n;t ≔ 1;foritondot ≔ x*t;A[i] ≔ tend do;Aend proc
Rename all symbols in the program with new names.
macro⁡R=`tools/rename`
R
macro⁡U=`tools/unrename`
R,U
tree≔R⁡maple2intrep⁡f
has⁡tree,A
false
has⁡tree,R⁡A
true
Make a simple transformation on the program represented in the intermediate representation. Make the array A into a global variable.
globals≔R⁡Globals⁡A
globals≔Globals⁡A
locals≔subsop⁡3=NULL,op⁡5,tree
locals≔Locals⁡i..integer,t..float
tree≔subsop⁡5=locals,6=globals,tree
tree≔Proc⁡Name⁡f..List⁡1..n,float,Parameters⁡x..float,n..integer,Options⁡,Description⁡,Locals⁡i..integer,t..float,Globals⁡A,StatSeq⁡Assign⁡A,Array⁡1..n,Assign⁡t,1,For⁡i,1,1,n,true,StatSeq⁡Assign⁡t,x⁢t,Assign⁡Ai,t,false,A
intrep2maple⁡U⁡tree
procx,nlocali,t;globalA;A ≔ Array⁡1..n;t ≔ 1;foritondot ≔ x*t;A[i] ≔ tend do;Aend proc
See Also
codegen[makeproc]
FromInert
ToInert
Download Help Document