Skip to content

Fix print with precedence #678

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Mar 7, 2025

Conversation

zhuliquan
Copy link
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

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

expr/ast/print.go

Lines 86 to 88 in 9713880

if operator.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
Member

Super! Thanks a lot.

@antonmedv antonmedv merged commit af32277 into expr-lang:master Mar 7, 2025
15 checks passed
@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>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants