reformatting source file



On 4/30/10, John Lapeyre <john.lapeyre at gmail.com> wrote:

> The mathematica translator writes unformatted Maxima
> code. Does someone have an idea about how to reformat
> a file of Maxima code? Something like using grind on
> the whole file ? stringout("fname",functions),
> stringout("fname",values). But the ordering in
> the original file is not respected.

Given that this is part of an automatic program generator,
sounds like you really want to avoid evaluating or simplifying
the expressions in the file, therefore you should avoid load
and batch.

The functions to only read or write Maxima expressions are
not exposed at the Maxima user level, but they are not too
deeply hidden. MREAD reads an expression and MGRIND
grinds it. I;ve appended a program to call those functions.
Enclose grind_file in with_stdout to capture output to another file.
Here is a sample session.

(%i1) load ("/maxima-programs/grind_file.lisp");
(%o1)                  /maxima-programs/grind_file.lisp
(%i2) grind_file ("./tmp1.mac");
true$
false$
12345$
"blurf"$
f("x",y/x+1)$
g(x):=?foobar(x,y,z)$
g(1)$
h(x)::=buildq([z:x],sin(z))$
h(0)$
17/19$
rat(17/19)$
xyz:1-a$
sin(xyz)$
sin(0)$
1+1$
1/0$
lambda([x],1+x)(0)$
w+%pi+a+h+2+x+1$
1*x*2*h*a*%pi*w$
(%o2)                                done
(%i3) printfile ("./tmp1.mac");
true;
false;
12345;
"blurf";
f("x",1+y/x);
g(x):=?foobar(x,y,z);
g(1);
h(x)::=buildq([z:x],sin(z));
h(0);
17/19;
rat(17/19);
xyz:1-a;
sin(''xyz);
sin(0);
1+1;
1/0;
lambda([x], x + 1)(0);
1 + x + 2 + h + a + %pi + w;
1 * x * 2 * h * a * %pi * w;
(%o3)                             ./tmp1.mac
(%i4) with_stdout ("./tmp2.mac", grind_file ("./tmp1.mac"));
(%o4)                                done

Note that grind_file does not preserve every bit of the input:
the quote-quote operator is processed when an expression is
read, not when it is evaluated or simplified. Also we see at
least one display flag (namely powerdisp) in action here,
in "+" expressions. There may be other differences; I'd have
to read the parser (src/nparse.lisp) to see if anything else is
processed by the parser.

Probably there should be a way to exactly represent the input
in parsed expressions, but maybe this is close enough.

HTH

Robert Dodier

PS.
;; grind_file.lisp -- grind each expression in a file
;; copyright 2010 by Robert Dodier
;; I release this work under terms of the GNU General Public License

(defun $grind_file (filename)
  (with-open-file (s filename)
    (let ((*mread-prompt* nil) x)
      (loop while (setq x (mread s))
        do
          (mgrind (third x) nil)
          (format t "$~%"))))
  '$done)