See CodeGenerationOptions for a description of the options used in the following examples.
>
|
|
Translate a simple expression and assign to the name "w" in the target code.
>
|
|
w = -2 * x * z + y * z + x
| |
Translate a list and assign to an array with name "w" in the target code.
>
|
|
w(1,1) = x
w(1,2) = 2 * y
w(2,1) = 5
w(2,2) = z
| |
Translate a computation sequence. Optimize the input first.
>
|
|
>
|
|
s = 0.10D1 + x
t1 = log(s)
t2 = exp(-x)
t = t2 * t1
r = x * t + t2
| |
Declare that x is a float and y is an integer. Return the result in a string.
>
|
|
| (1) |
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:
|
>
|
|
integer function f (x, y, z)
integer x
integer y
integer z
f = y * x - y * z + x * z
return
end
| |
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:
|
doubleprecision function f (n)
integer n
doubleprecision x
integer i
doubleprecision cgret
x = 0.0D0
do 100, i = 1, n, 1
x = x + dble(i)
cgret = x
100 continue
f = cgret
return
end
| |
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:
|
doubleprecision function f (x)
doubleprecision x(5:7)
f = x(5) + x(6) + x(7)
return
end
| |
Translate a module with one exported and one local procedure.
>
|
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:
|
>
|
|
doubleprecision function q (x)
doubleprecision x
q = sin(x) ** 2
return
end
integer function p (x, y)
doubleprecision x
integer y
if (0 .lt. y) then
p = int(aint(x))
return
else
p = int(ceil(x))
return
end if
end
program m
end
| |
Translate a procedure with no return value, containing an output statement.
>
|
f := proc(N)
printf("%d is a Number.\n", N);
end proc:
|
subroutine f (N)
doubleprecision N
print *, N, ' is a Number.'
end
| |
Use names longer than 6 characters with the limitvariablelength option.
>
|
p:=proc() local longvar: end proc;
|
| (2) |
subroutine p
doubleprecision cg
end
| |
>
|
|
subroutine p
doubleprecision longvar
end
| |