(node *Node)
| 13 | } |
| 14 | |
| 15 | func (fold *fold) Visit(node *Node) { |
| 16 | patch := func(newNode Node) { |
| 17 | fold.applied = true |
| 18 | patchWithType(node, newNode) |
| 19 | } |
| 20 | patchCopy := func(newNode Node) { |
| 21 | fold.applied = true |
| 22 | patchCopyType(node, newNode) |
| 23 | } |
| 24 | |
| 25 | switch n := (*node).(type) { |
| 26 | case *UnaryNode: |
| 27 | switch n.Operator { |
| 28 | case "-": |
| 29 | if i, ok := n.Node.(*IntegerNode); ok { |
| 30 | patch(&IntegerNode{Value: -i.Value}) |
| 31 | } |
| 32 | if i, ok := n.Node.(*FloatNode); ok { |
| 33 | patch(&FloatNode{Value: -i.Value}) |
| 34 | } |
| 35 | case "+": |
| 36 | if i, ok := n.Node.(*IntegerNode); ok { |
| 37 | patch(&IntegerNode{Value: i.Value}) |
| 38 | } |
| 39 | if i, ok := n.Node.(*FloatNode); ok { |
| 40 | patch(&FloatNode{Value: i.Value}) |
| 41 | } |
| 42 | case "!", "not": |
| 43 | if a := toBool(n.Node); a != nil { |
| 44 | patch(&BoolNode{Value: !a.Value}) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | case *BinaryNode: |
| 49 | switch n.Operator { |
| 50 | case "+": |
| 51 | { |
| 52 | a := toInteger(n.Left) |
| 53 | b := toInteger(n.Right) |
| 54 | if a != nil && b != nil { |
| 55 | patch(&IntegerNode{Value: a.Value + b.Value}) |
| 56 | } |
| 57 | } |
| 58 | { |
| 59 | a := toInteger(n.Left) |
| 60 | b := toFloat(n.Right) |
| 61 | if a != nil && b != nil { |
| 62 | patch(&FloatNode{Value: float64(a.Value) + b.Value}) |
| 63 | } |
| 64 | } |
| 65 | { |
| 66 | a := toFloat(n.Left) |
| 67 | b := toInteger(n.Right) |
| 68 | if a != nil && b != nil { |
| 69 | patch(&FloatNode{Value: a.Value + float64(b.Value)}) |
| 70 | } |
| 71 | } |
| 72 | { |
nothing calls this directly
no test coverage detected