CodeGeneration - Maple Programming Help

Home : Support : Online Help : Programming : Code Generation Package : CodeGeneration/IntermediateCode

CodeGeneration

 IntermediateCode
 translate Maple code to CodeGeneration intermediate code

 Calling Sequence IntermediateCode(x, cgopts)

Parameters

 x - expression, list, array, rtable, procedure or module cgopts - (optional) one or more CodeGeneration options

Description

 • The IntermediateCode(x, cgopts) command translates Maple code to an intermediate form, as described in IntermediateCodeStructure, that is used by all the CodeGeneration translators. This command is useful when you want to extend the CodeGeneration translation facilities.  For an overview of code translation, see LanguageDefinitionOverview.
 - If the parameter x is an algebraic expression, then a StatementSequence structure containing an assignment of the expression to a variable is generated.
 - If x is a list, rtable or Maple array of algebraic expressions, then a StatementSequence structure containing an assignment of the elements to an array is produced.  Only the initialized elements of the rtable or Maple array are translated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$ where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, this is understood to mean a sequence of assignment statements.  In this case, a StatementSequence structure containing the equivalent sequence of assignments is generated.
 - If x is a procedure or module, then a Procedure or Module structure is generated.
 • Procedures, modules and computation sequences are contained within Scope structures.  Each Scope structure's first argument is a table containing name and type information needed for code translation.  The contents of this table are not printed by the IntermediateCode command; instead, the table appears simply as the name $"nametab"$ in the output.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions. Some of the options are relevant only when translation to a specific target language is requested and may be ignored by the IntermediateCode command.

Examples

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

Translate a simple expression.

 > $\mathrm{IntermediateCode}\left(x+yz-2xz\right)$
 Scope( nametab,   StatementSequence(     Assignment(GeneratedName("cg"), Sum(       Negation(Product(         Integer(2),         Name("x"),         Name("z")       )),       Product(Name("y"), Name("z")),       Name("x")     ))   ) )

Translate a list.

 > $\mathrm{IntermediateCode}\left(\left[\left[x,2y\right],\left[5,z\right]\right]\right)$
 Scope( nametab,   StatementSequence(     Assignment(GeneratedName("cg0"), List(List(Name("x"), Product(Integer(2), Name("y"))), List(Integer(5), Name("z"))))   ) )

Translate a computation sequence.

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right){ⅇ}^{-x},r={ⅇ}^{-x}+xt\right]:$
 > $\mathrm{IntermediateCode}\left(\mathrm{cs}\right)$
 Warning, the function names {exp, ln} are not recognized in the target language Scope( nametab,   StatementSequence(     Assignment(Name("s"), Sum(Float(10, -1), Name("x"))),     Assignment(Name("t"), Product(FunctionCall(Name("ln"), ExpressionSequence(Name("s")), unknown), FunctionCall(Name("exp"), ExpressionSequence(Negation(Name("x"))), unknown))),     Assignment(Name("r"), Sum(FunctionCall(Name("exp"), ExpressionSequence(Negation(Name("x"))), unknown), Product(Name("x"), Name("t"))))   ) )

Translate a procedure.

 > f := proc(x, y, z) return x*y-y*z+x*z; end proc:
 > $\mathrm{IntermediateCode}\left(f\right)$
 Scope( nametab,   AssignedName(Name("f"), Scope( nametab,     Procedure(       ParameterSequence(         Declaration(Name("x"), Type(numeric)),         Declaration(Name("y"), Type(numeric)),         Declaration(Name("z"), Type(numeric))       ),       LocalSequence(),       OptionSequence(),       ExpressionSequence(),       StatementSequence(         Return(Sum(           Product(Name("x"), Name("y")),           Negation(Product(Name("y"), Name("z"))),           Product(Name("x"), Name("z"))         ))       ),       DescriptionSequence(),       GlobalSequence(),       LexicalSequence(),       Type(numeric)     )   )) )

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $\mathrm{IntermediateCode}\left(f\right)$
 Scope( nametab,   AssignedName(Name("f"), Scope( nametab,     Procedure(       ParameterSequence(Declaration(Name("n"), Type(integer))),       LocalSequence(         Declaration(Name("x"), Type(numeric)),         Declaration(Name("i"), Type(integer)),         Declaration(GeneratedName("cgret"), Type(numeric))       ),       OptionSequence(),       ExpressionSequence(),       StatementSequence(         Assignment(Name("x"), Float(0, 0)),         ForFromLoop(           Name("i"),           Integer(1),           Integer(1),           Name("n"),           StatementSequence(             Assignment(Name("x"), Sum(Name("x"), Coercion(Name("i"), Type(numeric)))),             Assignment(GeneratedName("cgret"), Name("x"))           )         ),         Return(GeneratedName("cgret"))       ),       DescriptionSequence(),       GlobalSequence(),       LexicalSequence(),       Type(numeric)     )   )) )

Translate a procedure accepting an Array as a parameter.

 > f := proc(x::Array(numeric, 5..7))   return x[5]+x[6]+x[7]; end proc:
 > $\mathrm{IntermediateCode}\left(f\right)$
 Scope( nametab,   AssignedName(Name("f"), Scope( nametab,     Procedure(       ParameterSequence(Declaration(Name("x"), ArrayType(         Type(numeric),         ArrayRanges(5 .. 7),         ArrayOptions()       ))),       LocalSequence(),       OptionSequence(),       ExpressionSequence(),       StatementSequence(         Return(Sum(           ArrayVariable(Name("x"), ArrayIndices(Integer(5))),           ArrayVariable(Name("x"), ArrayIndices(Integer(6))),           ArrayVariable(Name("x"), ArrayIndices(Integer(7)))         ))       ),       DescriptionSequence(),       GlobalSequence(),       LexicalSequence(),       Type(numeric)     )   )) )