Skip to content

Fix print with precedence - #678

Merged
antonmedv merged 6 commits into
expr-lang:masterfrom
zhuliquan:fix_print_with_precedence
Mar 7, 2025
Merged

Fix print with precedence#678
antonmedv merged 6 commits into
expr-lang:masterfrom
zhuliquan:fix_print_with_precedence

Conversation

@zhuliquan

Copy link
Copy Markdown
Contributor

consider the precedence of operators and their associative law when printing unary and binary nodes
fix error expr print:
1、(-(1+1)) ** 2 => -(1 + 1) ** 2
2、(2 ** 2) ** 3 => 2 ** 2 ** 3
3、(3 + 5) / (5 % 3) => (3 + 5) / 5 % 3

Beside,I think should remove below code:

expr/ast/print.go

Lines 75 to 80 in 9713880

iflb.Operator=="??" {
lwrap=true
}
ifoperator.IsBoolean(lb.Operator) &&n.Operator!=lb.Operator {
lwrap=true
}

expr/ast/print.go

Lines 86 to 88 in 9713880

ifoperator.IsBoolean(rb.Operator) &&n.Operator!=rb.Operator {
rwrap=true
}

because, these code logical can be covered with the logical of precedence and associative.

@antonmedv

Copy link
Copy Markdown
Member

Super! Thanks a lot.

@antonmedv
antonmedv merged commit af32277 into expr-lang:masterMar 7, 2025
@zhuliquan
zhuliquan deleted the fix_print_with_precedence branch March 8, 2025 13:25
SimFG pushed a commit to SimFG/expr that referenced this pull request Apr 15, 2025
* feat: extract code for compiling equal operator
* fix: fix unary and binary node print
consider the precedence of operators and their associative law when printing unary and binary nodes
---------
Co-authored-by: Anton Medvedev <anton@medv.io>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@zhuliquan@antonmedv