Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 interface Operation { | 5 interface Operation { |
| 6 | 6 |
| 7 } | 7 } |
| 8 | 8 |
| 9 interface UnaryOperation extends Operation { | 9 interface UnaryOperation extends Operation { |
| 10 /** Returns [:null:] if it was unable to fold the operation. */ | 10 /** Returns [:null:] if it was unable to fold the operation. */ |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 } | 98 } |
| 99 | 99 |
| 100 class ShiftRightOperation extends BinaryIntOperation { | 100 class ShiftRightOperation extends BinaryIntOperation { |
| 101 const ShiftRightOperation(); | 101 const ShiftRightOperation(); |
| 102 int foldInts(int left, int right) { | 102 int foldInts(int left, int right) { |
| 103 if (right < 0) return null; | 103 if (right < 0) return null; |
| 104 return left >> right; | 104 return left >> right; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 class BinaryBoolOperation implements BinaryOperation { | |
| 109 const BinaryBoolOperation(); | |
| 110 Constant fold(Constant left, Constant right) { | |
|
Lasse Reichstein Nielsen
2012/03/27 07:48:53
This seems like a pretty weak folding, if it requi
| |
| 111 if (left.isBool() && right.isBool()) { | |
| 112 BoolConstant leftBool = left; | |
| 113 BoolConstant rightBool = right; | |
| 114 bool resultValue = foldBools(leftBool.value, rightBool.value); | |
| 115 return new BoolConstant(resultValue); | |
| 116 } | |
| 117 return null; | |
|
Lasse Reichstein Nielsen
2012/03/27 07:48:53
If left or right are not boolean, you can still fo
| |
| 118 } | |
| 119 | |
| 120 abstract bool foldBools(bool left, bool right); | |
| 121 } | |
| 122 | |
| 123 class BooleanAnd extends BinaryBoolOperation { | |
| 124 const BooleanAnd(); | |
| 125 bool foldBools(bool left, bool right) => left && right; | |
| 126 } | |
| 127 | |
| 128 class BooleanOr extends BinaryBoolOperation { | |
| 129 const BooleanOr(); | |
| 130 bool foldBools(bool left, bool right) => left || right; | |
| 131 } | |
| 132 | |
| 108 class ArithmeticNumOperation implements BinaryOperation { | 133 class ArithmeticNumOperation implements BinaryOperation { |
| 109 const ArithmeticNumOperation(); | 134 const ArithmeticNumOperation(); |
| 110 Constant fold(Constant left, Constant right) { | 135 Constant fold(Constant left, Constant right) { |
| 111 if (left.isNum() && right.isNum()) { | 136 if (left.isNum() && right.isNum()) { |
| 112 NumConstant leftNum = left; | 137 NumConstant leftNum = left; |
| 113 NumConstant rightNum = right; | 138 NumConstant rightNum = right; |
| 114 num foldedValue; | 139 num foldedValue; |
| 115 if (left.isInt() && right.isInt()) { | 140 if (left.isInt() && right.isInt()) { |
| 116 foldedValue = foldInts(leftNum.value, rightNum.value); | 141 foldedValue = foldInts(leftNum.value, rightNum.value); |
| 117 } else { | 142 } else { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 return new BoolConstant(left == right); | 270 return new BoolConstant(left == right); |
| 246 } | 271 } |
| 247 } | 272 } |
| 248 | 273 |
| 249 class IdentityOperation implements BinaryOperation { | 274 class IdentityOperation implements BinaryOperation { |
| 250 const IdentityOperation(); | 275 const IdentityOperation(); |
| 251 Constant fold(Constant left, Constant right) { | 276 Constant fold(Constant left, Constant right) { |
| 252 return new BoolConstant(left == right); | 277 return new BoolConstant(left == right); |
| 253 } | 278 } |
| 254 } | 279 } |
| OLD | NEW |