| 60 | |
| 61 | // (Expression, Expression) -> [Expression] |
| 62 | function flattenAdditive(left, right) { |
| 63 | var result = []; |
| 64 | |
| 65 | // Since .pop is faster than .shift we'll pop tasks from the end. |
| 66 | var todo = [right, left]; |
| 67 | while (todo.length > 0) { |
| 68 | match(todo.pop(), function(when) { |
| 69 | when({ |
| 70 | type: 'BinaryExpression', |
| 71 | operator: match.some('+', '-'), |
| 72 | left: match.var('left'), |
| 73 | right: match.var('right') |
| 74 | }, function(vars) { |
| 75 | todo.push(vars.right, vars.left); |
| 76 | }); |
| 77 | |
| 78 | when(match.var('operand'), function(vars) { |
| 79 | result.push(vars.operand); |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | // ----------------------------------------------------------------------------- |
| 88 | // main methods |