The patch below is a first shot at better unparse.  The last hunk
add parentheses around single argument -- this fixes the sin x+1
problem.  The rest of the patch is rewritten binop2String.
I hope that large class of expressions (including polynomials)
will be printed without useless parentheses, however all cases
where we are unsure if parenthesis are needed will now get
them.

Note: the patch changes only printing of function applications
and expressions involving binary operators, other things
should be printed as before.

Note2: minimally tested -- since almost all types are printed
differently differences in testsuite output are quite large
and I did not have time to check them.

diff -ru dist.bb/src/interp/format.boot dist/src/interp/format.boot
--- dist.bb/src/interp/format.boot      2008-09-17 00:09:51.000000000 +0000
+++ dist/src/interp/format.boot 2008-09-17 04:54:37.000000000 +0000
@@ -437,10 +437,66 @@
   application2String(op,[form2String1 x for x in argl], u1)
 
 binop2String x ==
-    $exp2FortTempVarIndex : local := 0
-    $fortName : fluid := newFortranTempVar()
-    $fortInts2Floats : fluid := nil
-    fortranCleanUp exp2Fort1 exp2FortOptimize x
+    x is ["=", arg1, arg2] or x is ['"=", arg1, arg2] =>
+        concat(sumOrParen(arg1), '"=", sumOrParen(arg2))
+        sumOrParen(x)
+    sumOrParen(x)
+
+sumOrParen(x) ==
+   x is [op, arg1, arg2] =>
+       op = "+" or op = '"+" =>
+          concat(sumOrParen(arg1), '"+", productOrParen(arg2))
+       op = "-" or op = '"-" =>
+           concat(sumOrParen(arg1), '"-", productOrParen(arg2))
+       op = "/" or op = '"/" =>
+           concat(appOrParen(arg1), '"/", appOrParen(arg2))
+       productOrParen(x)
+   productOrParen(x)
+
+productOrParen(x) ==
+   x is [op, arg1, arg2] =>
+       op = "*" or op ='"*" =>
+           concat(productOrParen(arg1), '"*",  powerOrParen(arg2))
+       powerOrParen(x)
+   powerOrParen(x)
+
+powerOrParen(x) ==
+   x is [op, arg1, arg2] =>
+      op = "**" or op = '"**" or op = "^" or op = '"^"  =>
+           concat(appOrParen(arg1), '"^", appOrParen(arg2))
+      appOrParen(x)
+   appOrParen(x)
+
+appOrParen(x) ==
+   SYMBOLP(x) and not(constructor? x) => toString formWrapId x
+   INTEGERP(x) => WRITE_-TO_-STRING x
+   ATOM(x) => concat('"(", form2String0(x), '")")
+   [op, :argl] := x
+   isBinaryInfix op => BREAK()
+   -- Put parenthesis around anything special
+   not(SYMBOLP op) or GET(op, 'LED) or GET(op, 'NUD)_
+     or op= 'mkCategory or constructor? op or op = "SEGMENT" _
+     or op = 'construct or op = 'COLLECT or op = "SIGNATURE"_
+     or op = 'BRACKET or op = 'AGGLST or op = "ATTRIBUTE"_
+     or op = 'Join or op = "#" =>
+        concat('"(", form2String0(x), '")")
+   op = "Zero" => '"0"
+   op = "One" => '"1"
+   toString0(form2String1 x)
+
+form2String0(x) == toString0(form2String1 x)
+
+toString0(x) ==
+   ATOM(x) => toString x
+   res := '""
+   for s in x repeat
+       res := concat(res, toString s)
+   res
+
+toString s ==
+   STRINGP(s) => s
+   SYMBOLP(s) => STRING(s)
+   BREAK()
 
 formWrapId id == 
   $formatSigAsTeX = 1 => id
@@ -627,7 +683,7 @@
     app2StringWrap(formWrapId op, linkInfo)
   1=#argl =>
     first argl is ["<",:.] => concat(op,first argl)
-    concat(app2StringWrap(formWrapId op, linkInfo)," ",first argl)
+    concat(app2StringWrap(formWrapId op, linkInfo), "(", first argl, ")")
 --op in '(UP SM) =>
 --  newop:= (op = "UP" => "P";"M")
 --  concat(newop,concat(lbrkSch(),argl.0,rbrkSch(),argl.1))

-- 
                              Waldek Hebisch
[EMAIL PROTECTED] 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/fricas-devel?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to