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

Side by Side Diff: frog/leg/lib/core.dart

Issue 9284022: Operators. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 void print(var obj) { 5 void print(var obj) {
6 obj = obj.toString(); 6 obj = obj.toString();
7 var hasConsole = JS("bool", @"typeof console == 'object'"); 7 var hasConsole = JS("bool", @"typeof console == 'object'");
8 if (hasConsole) { 8 if (hasConsole) {
9 JS("void", @"console.log($0)", obj); 9 JS("void", @"console.log($0)", obj);
10 } else { 10 } else {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 return JS("num", @"$0 + $1", a, b); 49 return JS("num", @"$0 + $1", a, b);
50 } else if (JS("bool", @"typeof $0 === 'string'", a)) { 50 } else if (JS("bool", @"typeof $0 === 'string'", a)) {
51 b = b.toString(); 51 b = b.toString();
52 if (JS("bool", @"typeof $0 === 'string'", b)) { 52 if (JS("bool", @"typeof $0 === 'string'", b)) {
53 return JS("String", @"$0 + $1", a, b); 53 return JS("String", @"$0 + $1", a, b);
54 } 54 }
55 // The following line is too long, but we can't break it using the + 55 // The following line is too long, but we can't break it using the +
56 // operator, since that's what we are defining here. 56 // operator, since that's what we are defining here.
57 throw "calling toString() on right hand operand of operator + did not return a String"; 57 throw "calling toString() on right hand operand of operator + did not return a String";
58 } 58 }
59 throw "Unimplemented user-defined +."; 59 return UNINTERCEPTED(a + b);
60 } 60 }
61 61
62 div(var a, var b) { 62 div(var a, var b) {
63 if (checkNumbers(a, b, "num/ expects a number as second operand.")) { 63 if (checkNumbers(a, b, "num/ expects a number as second operand.")) {
64 return JS("num", @"$0 / $1", a, b); 64 return JS("num", @"$0 / $1", a, b);
65 } 65 }
66 throw "Unimplemented user-defined /."; 66 return UNINTERCEPTED(a / b);
67 } 67 }
68 68
69 mul(var a, var b) { 69 mul(var a, var b) {
70 if (checkNumbers(a, b, "num* expects a number as second operand.")) { 70 if (checkNumbers(a, b, "num* expects a number as second operand.")) {
71 return JS("num", @"$0 * $1", a, b); 71 return JS("num", @"$0 * $1", a, b);
72 } 72 }
73 throw "Unimplemented user-defined *."; 73 return UNINTERCEPTED(a * b);
74 } 74 }
75 75
76 sub(var a, var b) { 76 sub(var a, var b) {
77 if (checkNumbers(a, b, "num- expects a number as second operand.")) { 77 if (checkNumbers(a, b, "num- expects a number as second operand.")) {
78 return JS("num", @"$0 - $1", a, b); 78 return JS("num", @"$0 - $1", a, b);
79 } 79 }
80 throw "Unimplemented user-defined binary -."; 80 return UNINTERCEPTED(a - b);
81 } 81 }
82 82
83 mod(var a, var b) { 83 mod(var a, var b) {
84 if (checkNumbers(a, b, "int% expects an int as second operand.")) { 84 if (checkNumbers(a, b, "int% expects an int as second operand.")) {
85 // Euclidean Modulo. 85 // Euclidean Modulo.
86 int result = JS("num", @"$0 % $1", a, b); 86 int result = JS("num", @"$0 % $1", a, b);
87 if (result == 0) return 0; // Make sure we don't return -0.0. 87 if (result == 0) return 0; // Make sure we don't return -0.0.
88 if (result > 0) return result; 88 if (result > 0) return result;
89 if (b < 0) { 89 if (b < 0) {
90 return result - b; 90 return result - b;
91 } else { 91 } else {
92 return result + b; 92 return result + b;
93 } 93 }
94 } 94 }
95 throw "Unimplemented user-defined %."; 95 return UNINTERCEPTED(a % b);
96 } 96 }
97 97
98 tdiv(var a, var b) { 98 tdiv(var a, var b) {
99 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) { 99 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) {
100 var tmp = a / b; 100 var tmp = a / b;
101 // TODO(ngeoffray): Use tmp.floor and tmp.ceil when 101 // TODO(ngeoffray): Use tmp.floor and tmp.ceil when
102 // we can handle them. 102 // we can handle them.
103 if (tmp < 0) { 103 if (tmp < 0) {
104 return JS("num", @"Math.ceil($0)", tmp); 104 return JS("num", @"Math.ceil($0)", tmp);
105 } else { 105 } else {
106 return JS("num", @"Math.floor($0)", tmp); 106 return JS("num", @"Math.floor($0)", tmp);
107 } 107 }
108 } 108 }
109 throw "Unimplemented user-defined ~/."; 109 return UNINTERCEPTED(a ~/ b);
110 } 110 }
111 111
112 eq(var a, var b) { 112 eq(var a, var b) {
113 if (JS("bool", @"typeof $0 === 'object'", a)) { 113 if (JS("bool", @"typeof $0 === 'object'", a)) {
114 if (JS("bool", @"$0.$equals", a)) { 114 // TODO(ngeoffray): If we start using more property checks like
115 return JS("bool", @"$0.$equals($1) === true", a, b); 115 // this, add a foreign PROPERTY_CHECK element.
116 if (JS("bool", @"$0.operator$eq$1", a)) {
Lasse Reichstein Nielsen 2012/01/27 09:42:34 I recommend doing the foreign PREOPRTY_CHECK macro
ngeoffray 2012/01/27 10:50:04 As discussed, added a JS_HAS_EQUALS to avoid the m
117 return UNINTERCEPTED(a == b) === true;
116 } else { 118 } else {
117 return JS("bool", @"$0 === $1", a, b); 119 return JS("bool", @"$0 === $1", a, b);
118 } 120 }
119 } 121 }
120 // TODO(lrn): is NaN === NaN ? Is -0.0 === 0.0 ? 122 // TODO(lrn): is NaN === NaN ? Is -0.0 === 0.0 ?
121 return JS("bool", @"$0 === $1", a, b); 123 return JS("bool", @"$0 === $1", a, b);
122 } 124 }
123 125
124 eqq(var a, var b) { 126 eqq(var a, var b) {
125 return JS("bool", @"$0 === $1", a, b); 127 return JS("bool", @"$0 === $1", a, b);
126 } 128 }
127 129
128 eqNull(var a) { 130 eqNull(var a) {
129 if (JS("bool", @"typeof $0 === 'object'", a)) { 131 if (JS("bool", @"typeof $0 === 'object'", a)) {
130 if (JS("bool", @"$0.$equals", a)) { 132 if (JS("bool", @"$0.operator$eq$1", a)) {
Lasse Reichstein Nielsen 2012/01/27 09:42:34 Ditto here.
ngeoffray 2012/01/27 10:50:04 Done.
131 return JS("bool", @"$0.$equals(void 0) === true", a); 133 return UNINTERCEPTED(a == null) === true;
132 } else { 134 } else {
133 return false; 135 return false;
134 } 136 }
135 } else { 137 } else {
136 return JS("bool", @"typeof $0 === 'undefined'", a); 138 return JS("bool", @"typeof $0 === 'undefined'", a);
137 } 139 }
138 } 140 }
139 141
140 gt(var a, var b) { 142 gt(var a, var b) {
141 if (checkNumbers(a, b, "num> expects a number as second operand.")) { 143 if (checkNumbers(a, b, "num> expects a number as second operand.")) {
142 return JS("bool", @"$0 > $1", a, b); 144 return JS("bool", @"$0 > $1", a, b);
143 } 145 }
144 throw "Unimplemented user-defined binary >."; 146 return UNINTERCEPTED(a > b);
145 } 147 }
146 148
147 ge(var a, var b) { 149 ge(var a, var b) {
148 if (checkNumbers(a, b, "num>= expects a number as second operand.")) { 150 if (checkNumbers(a, b, "num>= expects a number as second operand.")) {
149 return JS("bool", @"$0 >= $1", a, b); 151 return JS("bool", @"$0 >= $1", a, b);
150 } 152 }
151 throw "Unimplemented user-defined binary >=."; 153 return UNINTERCEPTED(a >= b);
152 } 154 }
153 155
154 lt(var a, var b) { 156 lt(var a, var b) {
155 if (checkNumbers(a, b, "num< expects a number as second operand.")) { 157 if (checkNumbers(a, b, "num< expects a number as second operand.")) {
156 return JS("bool", @"$0 < $1", a, b); 158 return JS("bool", @"$0 < $1", a, b);
157 } 159 }
158 throw "Unimplemented user-defined binary <."; 160 return UNINTERCEPTED(a < b);
159 } 161 }
160 162
161 le(var a, var b) { 163 le(var a, var b) {
162 if (checkNumbers(a, b, "num<= expects a number as second operand.")) { 164 if (checkNumbers(a, b, "num<= expects a number as second operand.")) {
163 return JS("bool", @"$0 <= $1", a, b); 165 return JS("bool", @"$0 <= $1", a, b);
164 } 166 }
165 throw "Unimplemented user-defined binary <=."; 167 return UNINTERCEPTED(a <= b);
166 } 168 }
167 169
168 shl(var a, var b) { 170 shl(var a, var b) {
169 // TODO(floitsch): inputs must be integers. 171 // TODO(floitsch): inputs must be integers.
170 if (checkNumbers(a, b, "int<< expects an int as second operand.")) { 172 if (checkNumbers(a, b, "int<< expects an int as second operand.")) {
171 return JS("num", @"$0 << $1", a, b); 173 return JS("num", @"$0 << $1", a, b);
172 } 174 }
173 throw "Unimplemented user-defined binary <<."; 175 return UNINTERCEPTED(a << b);
174 } 176 }
175 177
176 shr(var a, var b) { 178 shr(var a, var b) {
177 // TODO(floitsch): inputs must be integers. 179 // TODO(floitsch): inputs must be integers.
178 if (checkNumbers(a, b, "int>> expects an int as second operand.")) { 180 if (checkNumbers(a, b, "int>> expects an int as second operand.")) {
179 return JS("num", @"$0 >> $1", a, b); 181 return JS("num", @"$0 >> $1", a, b);
180 } 182 }
181 throw "Unimplemented user-defined binary >>."; 183 return UNINTERCEPTED(a >> b);
182 } 184 }
183 185
184 and(var a, var b) { 186 and(var a, var b) {
185 // TODO(floitsch): inputs must be integers. 187 // TODO(floitsch): inputs must be integers.
186 if (checkNumbers(a, b, "int& expects an int as second operand.")) { 188 if (checkNumbers(a, b, "int& expects an int as second operand.")) {
187 return JS("num", @"$0 & $1", a, b); 189 return JS("num", @"$0 & $1", a, b);
188 } 190 }
189 throw "Unimplemented user-defined binary &."; 191 return UNINTERCEPTED(a & b);
190 } 192 }
191 193
192 or(var a, var b) { 194 or(var a, var b) {
193 // TODO(floitsch): inputs must be integers. 195 // TODO(floitsch): inputs must be integers.
194 if (checkNumbers(a, b, "int| expects an int as second operand.")) { 196 if (checkNumbers(a, b, "int| expects an int as second operand.")) {
195 return JS("num", @"$0 | $1", a, b); 197 return JS("num", @"$0 | $1", a, b);
196 } 198 }
197 throw "Unimplemented user-defined binary |."; 199 return UNINTERCEPTED(a | b);
198 } 200 }
199 201
200 xor(var a, var b) { 202 xor(var a, var b) {
201 // TODO(floitsch): inputs must be integers. 203 // TODO(floitsch): inputs must be integers.
202 if (checkNumbers(a, b, "int^ expects an int as second operand.")) { 204 if (checkNumbers(a, b, "int^ expects an int as second operand.")) {
203 return JS("num", @"$0 ^ $1", a, b); 205 return JS("num", @"$0 ^ $1", a, b);
204 } 206 }
205 throw "Unimplemented user-defined binary ^."; 207 return UNINTERCEPTED(a ^ b);
206 } 208 }
207 209
208 not(var a) { 210 not(var a) {
209 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a); 211 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a);
210 throw "Unimplemented user-defined ~."; 212 return UNINTERCEPTED(~a);
211 } 213 }
212 214
213 neg(var a) { 215 neg(var a) {
214 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a); 216 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a);
215 throw "Unimplemented user-defined unary-."; 217 return UNINTERCEPTED(-a);
216 } 218 }
217 219
218 index(var a, var index) { 220 index(var a, var index) {
219 if (JS("bool", @"typeof $0 === 'string'", a) || isJSArray(a)) { 221 if (JS("bool", @"typeof $0 === 'string'", a) || isJSArray(a)) {
220 if (!isInt(index)) $throw('Illegal argument'); 222 if (!isInt(index)) $throw('Illegal argument');
221 if (index < 0 || index >= a.length) $throw('Out of bounds'); 223 if (index < 0 || index >= a.length) $throw('Out of bounds');
222 return JS("Object", @"$0[$1]", a, index); 224 return JS("Object", @"$0[$1]", a, index);
223 } 225 }
224 throw "Unimplemented user-defined []."; 226 return UNINTERCEPTED(a[index]);
225 } 227 }
226 228
227 indexSet(var a, var index, var value) { 229 indexSet(var a, var index, var value) {
228 if (isJSArray(a)) { 230 if (isJSArray(a)) {
229 if (!isInt(index)) $throw('Illegal argument'); 231 if (!isInt(index)) $throw('Illegal argument');
230 if (index < 0 || index >= a.length) $throw('Out of bounds'); 232 if (index < 0 || index >= a.length) $throw('Out of bounds');
231 return JS("Object", @"$0[$1] = $2", a, index, value); 233 return JS("Object", @"$0[$1] = $2", a, index, value);
232 } 234 }
233 throw "Unimplemented user-defined []=."; 235 return UNINTERCEPTED(a[index] = value);
234 } 236 }
235 237
236 builtin$add$1(var receiver, var value) { 238 builtin$add$1(var receiver, var value) {
237 if (isJSArray(receiver)) { 239 if (isJSArray(receiver)) {
238 JS("Object", @"$0.push($1)", receiver, value); 240 JS("Object", @"$0.push($1)", receiver, value);
239 return; 241 return;
240 } 242 }
241 throw "Unimplemented user-defined add()."; 243 return UNINTERCEPTED(receiver.add(value));
242 } 244 }
243 245
244 builtin$removeLast$0(var receiver) { 246 builtin$removeLast$0(var receiver) {
245 if (isJSArray(receiver)) { 247 if (isJSArray(receiver)) {
246 return JS("Object", @"$0.pop()", receiver); 248 return JS("Object", @"$0.pop()", receiver);
247 } 249 }
248 throw "Unimplemented user-defined removeLast()."; 250 return UNINTERCEPTED(receiver.removeLast());
249 } 251 }
250 252
251 builtin$filter$1(var receiver, var predicate) { 253 builtin$filter$1(var receiver, var predicate) {
252 if (isJSArray(receiver)) { 254 if (isJSArray(receiver)) {
253 return JS("Object", @"$0.filter(function(v) { return $1(v) === true; })", 255 return JS("Object", @"$0.filter(function(v) { return $1(v) === true; })",
254 receiver, predicate); 256 receiver, predicate);
255 } 257 }
256 throw "Unimplemented user-defined filter()."; 258 return UNINTERCEPTED(receiver.filter(predicate));
257 } 259 }
258 260
259 261
260 builtin$get$length(var receiver) { 262 builtin$get$length(var receiver) {
261 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { 263 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) {
262 return JS("num", @"$0.length", receiver); 264 return JS("num", @"$0.length", receiver);
263 } 265 }
264 throw "Unimplemented user-defined length."; 266 return UNINTERCEPTED(receiver.length);
265 } 267 }
266 268
267 269
268 builtin$toString$0(var value) { 270 builtin$toString$0(var value) {
269 if (JS("bool", @"typeof $0 == 'object'", value)) { 271 if (JS("bool", @"typeof $0 == 'object'", value)) {
270 if (isJSArray(value)) { 272 if (isJSArray(value)) {
271 return "Instance of 'List'"; 273 return "Instance of 'List'";
272 } 274 }
273 return UNINTERCEPTED(value.toString()); 275 return UNINTERCEPTED(value.toString());
274 } 276 }
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 static void equals(var expected, var actual) { 382 static void equals(var expected, var actual) {
381 if (expected == actual) return; 383 if (expected == actual) return;
382 _fail('Expect.equals(expected: <$expected>, actual:<$actual> fails.'); 384 _fail('Expect.equals(expected: <$expected>, actual:<$actual> fails.');
383 } 385 }
384 386
385 static void fail(String message) { 387 static void fail(String message) {
386 _fail("Expect.fail('$message')"); 388 _fail("Expect.fail('$message')");
387 } 389 }
388 390
389 static void _fail(String message) { 391 static void _fail(String message) {
390 throw message; 392 $throw(message);
391 } 393 }
392 394
393 static void isTrue(var actual) { 395 static void isTrue(var actual) {
Lasse Reichstein Nielsen 2012/01/27 09:42:34 Add a TODO to add the optional message parameter w
ngeoffray 2012/01/27 10:50:04 Done.
394 if (actual === true) return; 396 if (actual === true) return;
395 _fail("Expect.isTrue($actual) fails."); 397 _fail("Expect.isTrue($actual) fails.");
396 } 398 }
399
400 static void isFalse(var actual) {
401 if (actual === false) return;
402 _fail("Expect.isFalse($actual) fails.");
403 }
397 } 404 }
398 405
399 class Stopwatch { 406 class Stopwatch {
400 double startMs; 407 double startMs;
401 double elapsedMs; 408 double elapsedMs;
402 409
403 Stopwatch() { 410 Stopwatch() {
404 elapsedMs = 0.0; 411 elapsedMs = 0.0;
405 } 412 }
406 413
(...skipping 29 matching lines...) Expand all
436 } else { 443 } else {
437 return 444 return
438 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); 445 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs);
439 } 446 }
440 } 447 }
441 448
442 int frequency() { 449 int frequency() {
443 return 1000; 450 return 1000;
444 } 451 }
445 } 452 }
OLDNEW
« no previous file with comments | « frog/leg/emitter.dart ('k') | frog/leg/namer.dart » ('j') | frog/leg/resolver.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698