| 70 | } |
| 71 | |
| 72 | func (n *BinaryNode) String() string { |
| 73 | if n.Operator == ".." { |
| 74 | return fmt.Sprintf("%s..%s", n.Left, n.Right) |
| 75 | } |
| 76 | |
| 77 | var lhs, rhs string |
| 78 | var lwrap, rwrap bool |
| 79 | |
| 80 | if l, ok := n.Left.(*UnaryNode); ok { |
| 81 | if operator.Unary[l.Operator].Precedence < |
| 82 | operator.Binary[n.Operator].Precedence { |
| 83 | lwrap = true |
| 84 | } |
| 85 | } |
| 86 | if lb, ok := n.Left.(*BinaryNode); ok { |
| 87 | if operator.Less(lb.Operator, n.Operator) { |
| 88 | lwrap = true |
| 89 | } |
| 90 | if operator.Binary[lb.Operator].Precedence == |
| 91 | operator.Binary[n.Operator].Precedence && |
| 92 | operator.Binary[n.Operator].Associativity == operator.Right { |
| 93 | lwrap = true |
| 94 | } |
| 95 | if lb.Operator == "??" { |
| 96 | lwrap = true |
| 97 | } |
| 98 | if operator.IsBoolean(lb.Operator) && n.Operator != lb.Operator { |
| 99 | lwrap = true |
| 100 | } |
| 101 | } |
| 102 | if rb, ok := n.Right.(*BinaryNode); ok { |
| 103 | if operator.Less(rb.Operator, n.Operator) { |
| 104 | rwrap = true |
| 105 | } |
| 106 | if operator.Binary[rb.Operator].Precedence == |
| 107 | operator.Binary[n.Operator].Precedence && |
| 108 | operator.Binary[n.Operator].Associativity == operator.Left { |
| 109 | rwrap = true |
| 110 | } |
| 111 | if operator.IsBoolean(rb.Operator) && n.Operator != rb.Operator { |
| 112 | rwrap = true |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if _, ok := n.Left.(*ConditionalNode); ok { |
| 117 | lwrap = true |
| 118 | } |
| 119 | if _, ok := n.Right.(*ConditionalNode); ok { |
| 120 | rwrap = true |
| 121 | } |
| 122 | |
| 123 | if lwrap { |
| 124 | lhs = fmt.Sprintf("(%s)", n.Left.String()) |
| 125 | } else { |
| 126 | lhs = n.Left.String() |
| 127 | } |
| 128 | |
| 129 | if rwrap { |