DEVLOG #20
Completed and wired Unparser
I completed the unparser now with the precedence based approach and I can’t believe how much simpler it is and how bad my initial approach was.
The thing that makes it work is the new needs_paren function which basically does all the precedence based logic of telling if the child needs parenthesis or not. Basically if the child’s precedence is lower than the parent op then we need a parenthesis. For example: if the child is a + node and the parent is a * node then we would need parenthesis on the child. For example:
(x+1) * 3 -> x+1 needed parenthesis.
The main part that made me think a bit was the case when precedence of the child and parent is equal. There are basically 3 cases here:
-
When op is
+or*, the child never needs parenthesis. -
When op is
-or/, the child only need parenthesis if it is on the right side. For example:
precedence of+==-,a + b - c->a+bis on the left side and hence does not needs parenthesis. Whereas,c - (a+b)->a+bon the right side needs parenthesis because this basically meansc - a - band notc - a + b. Hence, parenthesis is required. -
When op is
^, parenthesis is only required when the child is on the left and not when it is on the right. For example:
a^b^cdoes not need parenthesis (even though it could be written asa^(b^c)) but,(a^b)^cneeds parenthesis because this meansa^(b*c)as the powers multiply.
I have also wired in the unparser function to derivative and simplify too so now the result is an expression instead of a tuple.
Now I will update my README.md and explain the working of the unparser and then I will work on adding the guide page to my website.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.