OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library polymer_expressions.expression; | 5 library polymer_expressions.expression; |
6 | 6 |
7 import 'visitor.dart'; | 7 import 'visitor.dart'; |
8 | 8 |
9 // Helper functions for building expression trees programmatically | 9 // Helper functions for building expression trees programmatically |
10 | 10 |
11 EmptyExpression empty() => const EmptyExpression(); | 11 EmptyExpression empty() => const EmptyExpression(); |
12 Literal literal(v) => new Literal(v); | 12 Literal literal(v) => new Literal(v); |
13 ListLiteral listLiteral(List<Expression> items) => new ListLiteral(items); | 13 ListLiteral listLiteral(List<Expression> items) => new ListLiteral(items); |
14 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => new MapLiteral(entries); | 14 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => new MapLiteral(entries); |
15 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => | 15 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => |
16 new MapLiteralEntry(key, value); | 16 new MapLiteralEntry(key, value); |
17 Identifier ident(String v) => new Identifier(v); | 17 Identifier ident(String v) => new Identifier(v); |
18 ParenthesizedExpression paren(Expression e) => new ParenthesizedExpression(e); | 18 ParenthesizedExpression paren(Expression e) => new ParenthesizedExpression(e); |
19 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); | 19 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); |
20 BinaryOperator binary(Expression l, String op, Expression r) => | 20 BinaryOperator binary(Expression l, String op, Expression r) => |
21 new BinaryOperator(l, op, r); | 21 new BinaryOperator(l, op, r); |
22 Getter getter(Expression e, String m) => new Getter(e, m); | 22 Getter getter(Expression e, String m) => new Getter(e, m); |
23 Index index(Expression e, Expression a) => new Index(e, a); | 23 Index index(Expression e, Expression a) => new Index(e, a); |
24 Invoke invoke(Expression e, String m, List<Expression> a) => | 24 Invoke invoke(Expression e, String m, List<Expression> a) => |
25 new Invoke(e, m, a); | 25 new Invoke(e, m, a); |
26 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); | 26 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); |
| 27 AsExpression asExpr(Expression l, Expression r) => new AsExpression(l, r); |
27 TernaryOperator ternary(Expression c, Expression t, Expression f) => | 28 TernaryOperator ternary(Expression c, Expression t, Expression f) => |
28 new TernaryOperator(c, t, f); | 29 new TernaryOperator(c, t, f); |
29 | 30 |
30 class AstFactory { | 31 class AstFactory { |
31 EmptyExpression empty() => const EmptyExpression(); | 32 EmptyExpression empty() => const EmptyExpression(); |
32 | 33 |
33 Literal literal(v) => new Literal(v); | 34 Literal literal(v) => new Literal(v); |
34 | 35 |
35 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => | 36 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => |
36 new MapLiteral(entries); | 37 new MapLiteral(entries); |
(...skipping 15 matching lines...) Expand all Loading... |
52 new TernaryOperator(c, t, f); | 53 new TernaryOperator(c, t, f); |
53 | 54 |
54 Getter getter(Expression g, String n) => new Getter(g, n); | 55 Getter getter(Expression g, String n) => new Getter(g, n); |
55 | 56 |
56 Index index(Expression e, Expression a) => new Index(e, a); | 57 Index index(Expression e, Expression a) => new Index(e, a); |
57 | 58 |
58 Invoke invoke(Expression e, String m, List<Expression> a) => | 59 Invoke invoke(Expression e, String m, List<Expression> a) => |
59 new Invoke(e, m, a); | 60 new Invoke(e, m, a); |
60 | 61 |
61 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); | 62 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); |
| 63 |
| 64 AsExpression asExpr(Expression l, Expression r) => new AsExpression(l, r); |
62 } | 65 } |
63 | 66 |
64 /// Base class for all expressions | 67 /// Base class for all expressions |
65 abstract class Expression { | 68 abstract class Expression { |
66 const Expression(); | 69 const Expression(); |
67 accept(Visitor v); | 70 accept(Visitor v); |
68 } | 71 } |
69 | 72 |
70 class EmptyExpression extends Expression { | 73 class EmptyExpression extends Expression { |
71 const EmptyExpression(); | 74 const EmptyExpression(); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 accept(Visitor v) => v.visitInExpression(this); | 224 accept(Visitor v) => v.visitInExpression(this); |
222 | 225 |
223 String toString() => '($left in $right)'; | 226 String toString() => '($left in $right)'; |
224 | 227 |
225 bool operator ==(o) => o is InExpression && o.left == left | 228 bool operator ==(o) => o is InExpression && o.left == left |
226 && o.right == right; | 229 && o.right == right; |
227 | 230 |
228 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); | 231 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); |
229 } | 232 } |
230 | 233 |
| 234 class AsExpression extends Expression { |
| 235 final Expression left; |
| 236 final Expression right; |
| 237 |
| 238 AsExpression(this.left, this.right); |
| 239 |
| 240 accept(Visitor v) => v.visitAsExpression(this); |
| 241 |
| 242 String toString() => '($left as $right)'; |
| 243 |
| 244 bool operator ==(o) => o is AsExpression && o.left == left |
| 245 && o.right == right; |
| 246 |
| 247 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); |
| 248 } |
| 249 |
231 class Index extends Expression { | 250 class Index extends Expression { |
232 final Expression receiver; | 251 final Expression receiver; |
233 final Expression argument; | 252 final Expression argument; |
234 | 253 |
235 Index(this.receiver, this.argument); | 254 Index(this.receiver, this.argument); |
236 | 255 |
237 accept(Visitor v) => v.visitIndex(this); | 256 accept(Visitor v) => v.visitIndex(this); |
238 | 257 |
239 String toString() => '$receiver[$argument]'; | 258 String toString() => '$receiver[$argument]'; |
240 | 259 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 346 } |
328 | 347 |
329 static int hash2(int a, int b) => finish(combine(combine(0, a), b)); | 348 static int hash2(int a, int b) => finish(combine(combine(0, a), b)); |
330 | 349 |
331 static int hash3(int a, int b, int c) => | 350 static int hash3(int a, int b, int c) => |
332 finish(combine(combine(combine(0, a), b), c)); | 351 finish(combine(combine(combine(0, a), b), c)); |
333 | 352 |
334 static int hash4(int a, int b, int c, int d) => | 353 static int hash4(int a, int b, int c, int d) => |
335 finish(combine(combine(combine(combine(0, a), b), c), d)); | 354 finish(combine(combine(combine(combine(0, a), b), c), d)); |
336 } | 355 } |
OLD | NEW |