Open maxkoretskyi opened 2 months ago
Hi @maxkoretskyi, thanks for asking this. Astring does not alter the structure of the AST it generates. It only removes parentheses if the precedence remains unchanged without them. For example, (1 + 2) + 3
and 1 + 2 + 3
are evaluated in the same order, so Astring generates the latter form, which is what you are looking for. However, 1 + (2 + 3)
is evaluated differently from 1 + 2 + 3
. While this difference is inconsequential for numbers, it could be significant for references or other expressions.
Therefore, I recommend updating the AST before passing it to Astring to ensure the desired output.
thanks a lot for you answer!
For example, (1 + 2) + 3 and 1 + 2 + 3 are evaluated in the same order... However, 1 + (2 + 3) is evaluated differently from 1 + 2 + 3.
what defines that? it's not associativity ?
I recommend updating the AST before passing it to Astring to ensure the desired output.
I did a search, but couldn't find any library that allows tranformation of AST to drop parenthesis? do you maybe know any?
The library is great in that it can drop unncessary parentheses for operators with different precedence, e.g.
'1+(2*3)' -> 1+2*3
.I'm wondering though, if there is any way to enable dropping parenthesis in the binary expression that has the same operator precedence? For example, the expression
'1+(2+3)'
, in which both the parent and child node has the same operator precedence for the+
operator.here ideally I'd want to get
'1+2+3'
.This seems tricky, since in the case of the expression
'1-(2+3)'
, where the operator-
has the same precendence as+
, the parenthesis should be preserved (it's the way the library works now).