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 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 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.
|
•
|
For more information about how the CodeGeneration package translates Maple code, see Translation Details.
|
|
|
Examples
|
|
>
|
|
Translate a simple expression.
>
|
|
Scope( nametab,
StatementSequence(
Assignment(GeneratedName("cg"), Sum(
Name("x"),
Product(Name("y"), Name("z")),
Negation(Product(
Integer(2),
Name("x"),
Name("z")
))
))
)
)
| |
Translate a list.
>
|
|
Scope( nametab,
StatementSequence(
ArrayInitialization(GeneratedName("cg0"), Array(ArrayRanges(1 .. 2, 1 .. 2), Values(
ArrayIndices(Integer(1),Integer(1)) = Name("x"),
ArrayIndices(Integer(1),Integer(2)) = Product(Integer(2),Name("y")),
ArrayIndices(Integer(2),Integer(1)) = Integer(5),
ArrayIndices(Integer(2),Integer(2)) = Name("z")
)))
)
)
| |
Translate a computation sequence.
>
|
|
>
|
|
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:
|
>
|
|
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(
Statement(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:
|
>
|
|
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"))
)
),
Statement(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:
|
>
|
|
Scope( nametab,
AssignedName(Name("f"), Scope( nametab,
Procedure(
ParameterSequence(Declaration(Name("x"), ArrayType(
Type(numeric),
ArrayRanges(5 .. 7),
ArrayOptions()
))),
LocalSequence(),
OptionSequence(),
ExpressionSequence(),
StatementSequence(
Statement(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)
)
))
)
| |
|
|