For a description of the options used in the following examples, see CodeGenerationOptions.
Translate a simple expression and assign to the name in the target code.
w = -2 * x * z + y * z + x;
| |
Translate a list and assign to an array with name in the target code.
w[0][0] = x;
w[0][1] = 2 * y;
w[1][0] = 5;
w[1][1] = z;
| |
Translate a computation sequence. Optimize the input first.
s = 0.10e1 + x;
t1 = Math.log(s);
t2 = Math.exp(-x);
t = t2 * t1;
r = x * t + t2;
| |
Declare that is a float and is an integer. Return the result in a string.
Translate a procedure. Assume that all untyped variables have type integer.
>
|
f := proc(x, y, z) return x*y-y*z+x*z; end proc:
|
class CodeGenerationClass {
public static int f (int x, int y, int z)
{
return(y * x - y * z + x * z);
}
}
| |
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:
|
class CodeGenerationClass {
public static double f (int n)
{
double x;
int i;
double cgret;
x = 0.0e0;
for (i = 1; i <= n; i++)
{
x = x + (double) i;
cgret = x;
}
return(cgret);
}
}
| |
Translate a procedure accepting an Array as a parameter. Note that the indices are renumbered so that the Java array starts at index 0.
>
|
f := proc(x::Array(numeric, 5..7))
return x[5]+x[6]+x[7];
end proc:
|
class CodeGenerationClass {
public static double f (double[] x)
{
return(x[0] + x[1] + x[2]);
}
}
| |
Translate a module.
>
|
m := module() export p; local q;
p := proc(x,y) if y>0 then trunc(x); else ceil(x); end if; end proc:
q := proc(x) sin(x)^2; end proc:
end module:
|
import java.lang.Math;
class m {
public static int p (double x, int y)
{
if (0 < y)
return((int)(x));
else
return((int)Math.ceil(x));
}
private static double q (double x)
{
return(Math.pow(Math.sin(x), 0.2e1));
}
}
| |
Translate a linear combination of hyperbolic trigonometric functions.
cg0 = 0.2e1 * (Math.exp(x) + Math.exp((-0.1e1) * x)) / 0.2e1 - 0.7e1 * (Math.exp(0.2e1 * x) - 0.1e1) / (Math.exp(0.2e1 * x) + 0.1e1);
| |
Translate a procedure with no return value containing a printf statement.
>
|
f := proc(a::integer, p::integer)
printf("The integer remainder of %d divided by %d is: %d\n", a, p, irem(a, p));
end proc:
|
class CodeGenerationClass {
public static void f (int a, int p)
{
System.out.println("The integer remainder of " + a + " divided by " + p + " is: " + a % p);
}
}
| |