CodeGeneration
Translate
translate Maple code to code in a specified language
Calling Sequence
Parameters
Description
Examples
Translate(x, language = lang, cgopts)
x
-
expression, list, rtable, procedure or module
lang
string
cgopts
(optional) one or more CodeGeneration options
The Translate(x, language = lang, cgopts) calling sequence translates Maple code to code in the language lang. The language lang must correspond to a language recognized by CodeGeneration, that is, a built-in language or a language added with Add. If option language = lang is not supplied, then the language "default" is used.
- If the parameter x is an algebraic expression, then a statement in the language lang assigning the expression to a variable is generated.
- If x is a list of equations nm=expr where nm is a name and expr is an algebraic expression, this is understood to mean a sequence of assignment statements. In this case, the equivalent sequence of assignment statements in the language lang is generated.
The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.
The CodeGeneration package is distributed with support for a number of languages. Though each predefined language has a corresponding CodeGeneration function for performing translations, for example, CodeGeneration[C], you can also use Translate with a predefined language. Note, however, that the two methods may produce different output, as the CodeGeneration functions may make use of specialized options.
For more information about how the CodeGeneration package translates Maple code to predefined languages, see Translation Details.
Perform a translation to the predefined language Java using the Translate and Java functions respectively.
with⁡CodeGeneration:
Java( proc(x) sin(x)^2-cos(x)^2; end proc);
import java.lang.Math; class CodeGenerationClass { public static double cg (double x) { return(Math.pow(Math.sin(x), 0.2e1) - Math.pow(Math.cos(x), 0.2e1)); } }
Translate( proc(x) sin(x)^2-cos(x)^2; end proc, language = "Java" );
import java.lang.Math; class CodeGenerationClass { public static double cg0 (double x) { return(Math.pow(Math.sin(x), 0.2e1) - Math.pow(Math.cos(x), 0.2e1)); } }
Define a custom language named "MyLanguage" and translate a simple expression and a procedure to this language.
LanguageDefinition[Define]("MyLanguage", extend = "default", SetLanguageAttribute( "Procedure_Start" = proc(rettype,pname,params) (Printer:-Indent(),"func ",pname," ",params," : ",rettype,";\n") end proc, "Procedure_End" = proc() (Printer:-Indent(),"end func\n") end proc, "ParameterSequence_Start" = "(", "ParameterSequence_End" = ")", "ParameterSequence_Delimiter" = ",", "ParameterSequence_MaxOnLine" = 3, "Statement_End" = ";", "Block_Start" = "begin\n", "Block_End" = "end\n" ), AddType( 'integer' = table(['single' = "integer", 'double' = "long"]), 'numeric' = table(['single' = "real", 'double' = "real"]) ), AddFunction("sqrt", [numeric]::numeric, x->Printer:-Print("surd(",x,",2)") ), AddOperator(Names:-Assignment = ":="), AddPrintHandler( Names:-Return = proc(x) Printer:-Print(Printer:-Indent(),"return ",x,";\n") end proc ) ): Translate(sqrt(x), language = "MyLanguage");
cg1 := surd(x,2);
Translate(proc(x) x; end proc, language = "MyLanguage");
procedure cg2 (real x) :: real return x; end end func
See Also
CodeGenerationOptions
LanguageDefinitionOverview
Translation Details
Download Help Document