POV-Ray : Newsgroups : povray.off-topic : The trick : Re: The trick Server Time
6 Sep 2024 01:25:29 EDT (-0400)
  Re: The trick  
From: Aydan
Date: 12 Mar 2009 11:55:01
Message: <web.49b92f8abf8b5ceb1ccf29180@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> OK, so does anybody know the trick to taking a parse tree such as
>
>    (2*(5^2))+((8*6)^(x-1)))
>
> and printing it out *without* several billion brackets?
>
> Printing it with brackets as above, or with no brackets at all, is quite
> trivial:
>
>    output expr = case expr of
>      Operator n x y = "(" ++ output x ++ n ++ output y ++ ")"
>      Variable n     = n
>      Constant v     = show v
>      ...
>
> But how do you "decide" when the brackets can or can't be omitted?

Just a thought:

if the next operator up the tree defers (has lower precedence) to the current
operator, omit the brackets.

eg:
(2*(5^2))+((8*6)^(x-1))) + defers to * --> omit 2*(5^2)+((8*6)^(x-1)))
(2*(5^2)) : * defers to ^ --> omit (2*5^2)
(8*6)^(x-1): ^ doesn't defer to * or - --> don't omit


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.