Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: frog/leg/operations.dart

Issue 9810027: Allow constant folding of && and ||. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « frog/leg/compile_time_constants.dart ('k') | tests/co19/co19-leg.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
111 if (left.isBool() && right.isBool()) {
112 BoolConstant leftBool = left;
113 BoolConstant rightBool = right;
114 bool resultValue = foldBools(leftBool.value, rightBool.value);
115 if (resultValue === null) return null;
ngeoffray 2012/03/26 13:19:09 I believe this can never be true.
karlklose 2012/03/26 13:24:52 Not in the current subclasses, that is correct. I
116 return new BoolConstant(resultValue);
117 }
118 return null;
119 }
120
121 abstract bool foldBools(bool left, bool right);
122 }
123
124 class BooleanAnd extends BinaryBoolOperation {
125 const BooleanAnd();
126 bool foldBools(bool left, bool right) => left && right;
127 }
128
129 class BooleanOr extends BinaryBoolOperation {
130 const BooleanOr();
131 bool foldBools(bool left, bool right) => left || right;
132 }
133
108 class ArithmeticNumOperation implements BinaryOperation { 134 class ArithmeticNumOperation implements BinaryOperation {
109 const ArithmeticNumOperation(); 135 const ArithmeticNumOperation();
110 Constant fold(Constant left, Constant right) { 136 Constant fold(Constant left, Constant right) {
111 if (left.isNum() && right.isNum()) { 137 if (left.isNum() && right.isNum()) {
112 NumConstant leftNum = left; 138 NumConstant leftNum = left;
113 NumConstant rightNum = right; 139 NumConstant rightNum = right;
114 num foldedValue; 140 num foldedValue;
115 if (left.isInt() && right.isInt()) { 141 if (left.isInt() && right.isInt()) {
116 foldedValue = foldInts(leftNum.value, rightNum.value); 142 foldedValue = foldInts(leftNum.value, rightNum.value);
117 } else { 143 } else {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 return new BoolConstant(left == right); 271 return new BoolConstant(left == right);
246 } 272 }
247 } 273 }
248 274
249 class IdentityOperation implements BinaryOperation { 275 class IdentityOperation implements BinaryOperation {
250 const IdentityOperation(); 276 const IdentityOperation();
251 Constant fold(Constant left, Constant right) { 277 Constant fold(Constant left, Constant right) {
252 return new BoolConstant(left == right); 278 return new BoolConstant(left == right);
253 } 279 }
254 } 280 }
OLDNEW
« no previous file with comments | « frog/leg/compile_time_constants.dart ('k') | tests/co19/co19-leg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698