/*
   References:
   [1] share/contrib/gentran/man/MANUAL.ps
*/
(kill(all), 'done);
'done$

(load("gentran"), 'done);
'done$

gentranlang('fortran);
?fortran$

gentranlang('ratfor);
?ratfor$

gentranlang('c);
?c$

(gentranlang('c), ?\*gentranlang);
?c$

/* helper function to get gentran output as a string */
(sgentran(['L]):= block([s: make_string_output_stream(),
    ?genstmtno\*:  25000,
    ?fortcurrind\*:    0,
    ?ratcurrind\*:     0,
    ?currind\*:        0],
  gentranpush(s),
  apply(gentran, L),
  gentranpop(s),
  get_output_stream_string(s)),
'done);
'done;

(gentranlang('fortran), sgentran(a : 42));
"a=42.0
"$

(gentranlang('ratfor), sgentran(a : 42));
"a=42.0
"$

(gentranlang('c),      sgentran(a : 42));
"a=42.0;
"$

/* do not convert integers to float */
(off('float), ?\*float);
false$

(gentranlang('c), sgentran(a : 42));
"a=42;
"$

/* [1] (p. 6) */
(gentranlang('fortran), sgentran(for i:1 thru n do v[i]:0.0 ));
"do 25001 i=1,n
    v(i)=0.0
25001 continue
"$

(gentranlang('fortran), sgentran(for i:1 thru n step -1 do v[i]:0.0));
"do 25001 i=1,n,-1
    v(i)=0.0
25001 continue
"$

/* [1] (p. 6) */
(gentranlang('ratfor),
  sgentran(
    for i:1 thru n do
    for j:i+1 thru n do
    (
      x[j,i] : x[i,j],
      y[j,i] : y[i,j])));
"do i=1,n
    do j=1+i,n
        {
            x(j,i)=x(i,j)
            y(j,i)=y(i,j)
        }
"$

/* [1] (p. 7) */
(gentranlang('c),
  sgentran(p : a*x^2 + b*x + c ));
"p=c+b*x+a*pow(x,2);
"$

/* [1] (p. 8) */
(gentranlang('fortran),
  sgentran( function("real", f(x,y)),
    body( x:x^2, y:y^2, f:abs(x-y) ) ) );
"real function f(x,y)
x=x**2
y=y**2
f=abs(-x+y)
return
end
"$

/* [1] (p. 9) */
(gentranlang('c),
  sgentran(cprocedure("float", f(x,y)) ));
"float f(x,y)
"$

/* [13] (p. 13, without float) */
block([f: 2*x^2 - 5*x + 6],
  gentranlang('fortran),
  sgentran( q : eval(f)/eval(diff(f,x)) ));
"q=(6-5*x+2*x**2)/(-5+4*x)
"$

/*
Gentran is confused by batch

batch("rtest_gentran.mac", 'test);
(gentranlang('fortran), gentran(a : 42));

returns
true

One should reload
load("gentran")$
(gentranlang('fortran), gentran(a : 42))$

prints
a=42.0

*/
