/*
   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;
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;

genfloat: true;
true;

(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 */
genfloat: false;
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 25002 i=1,n,-1
          v(i)=0.0
25002 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=i+1,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=a*pow(x,2.0)+b*x+c;
"$

/* [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(y-x)
      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=(2*x**2-5*x+6)/(4*x-5)
"$

/*
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

*/

/* SF bug #4631: "Lisp error in gentran with gentranlang = c (attempting symbol operation on non-symbol)" */

(gentranlang: 'c,
 var:-((sqrt(m_piM^4+W^8*cost^2-W^8+4*P0^2*W^6)+((cost^2-1)*m_nuc^2+W^2*cost^2-W^2)*m_piM^4+sqrt(m_piM^2+P0^2)*(4*W^2*m_nuc*m_piM^2-4*W^2*m_nuc^3+4*W^4*m_nuc)+((2-2*cost^2)*m_nuc^4-4*W^2*cost^2*m_nuc^2-2*W^4*cost^2-2*W^4)*m_piM^2+(cost^2-1)*m_nuc^6+(W^2-W^2*cost^2)*m_nuc^4+(W^4-W^4*cost^2)*m_nuc^2+W^6*cost^2-W^6)/((cost^2-1)*m_piM^4+((2-2*cost^2)*m_nuc^2-2*W^2*cost^2-2*W^2)*m_piM^2+(cost^2-1)*m_nuc^4+(2*W^2-2*W^2*cost^2)*m_nuc^2+W^4*cost^2-W^4)),
 errcatch (gentran (eval (var))));
[true];
