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 |
| 73 abstract class HasIdentifier { |
| 74 String get identifier; |
| 75 Expression get expr; |
| 76 } |
| 77 |
70 class EmptyExpression extends Expression { | 78 class EmptyExpression extends Expression { |
71 const EmptyExpression(); | 79 const EmptyExpression(); |
72 accept(Visitor v) => v.visitEmptyExpression(this); | 80 accept(Visitor v) => v.visitEmptyExpression(this); |
73 } | 81 } |
74 | 82 |
75 class Literal<T> extends Expression { | 83 class Literal<T> extends Expression { |
76 final T value; | 84 final T value; |
77 | 85 |
78 Literal(this.value); | 86 Literal(this.value); |
79 | 87 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 213 |
206 bool operator ==(o) => o is TernaryOperator | 214 bool operator ==(o) => o is TernaryOperator |
207 && o.condition == condition | 215 && o.condition == condition |
208 && o.trueExpr == trueExpr | 216 && o.trueExpr == trueExpr |
209 && o.falseExpr == falseExpr; | 217 && o.falseExpr == falseExpr; |
210 | 218 |
211 int get hashCode => _JenkinsSmiHash.hash3(condition.hashCode, | 219 int get hashCode => _JenkinsSmiHash.hash3(condition.hashCode, |
212 trueExpr.hashCode, falseExpr.hashCode); | 220 trueExpr.hashCode, falseExpr.hashCode); |
213 } | 221 } |
214 | 222 |
215 class InExpression extends Expression { | 223 class InExpression extends Expression implements HasIdentifier { |
216 final Expression left; | 224 final Identifier left; |
217 final Expression right; | 225 final Expression right; |
218 | 226 |
219 InExpression(this.left, this.right); | 227 InExpression(this.left, this.right); |
220 | 228 |
221 accept(Visitor v) => v.visitInExpression(this); | 229 accept(Visitor v) => v.visitInExpression(this); |
222 | 230 |
| 231 String get identifier => left.value; |
| 232 |
| 233 Expression get expr => right; |
| 234 |
223 String toString() => '($left in $right)'; | 235 String toString() => '($left in $right)'; |
224 | 236 |
225 bool operator ==(o) => o is InExpression && o.left == left | 237 bool operator ==(o) => o is InExpression && o.left == left |
226 && o.right == right; | 238 && o.right == right; |
227 | 239 |
228 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); | 240 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); |
229 } | 241 } |
230 | 242 |
| 243 class AsExpression extends Expression implements HasIdentifier { |
| 244 final Expression left; |
| 245 final Identifier right; |
| 246 |
| 247 AsExpression(this.left, this.right); |
| 248 |
| 249 accept(Visitor v) => v.visitAsExpression(this); |
| 250 |
| 251 String get identifier => right.value; |
| 252 |
| 253 Expression get expr => left; |
| 254 |
| 255 String toString() => '($left as $right)'; |
| 256 |
| 257 bool operator ==(o) => o is AsExpression && o.left == left |
| 258 && o.right == right; |
| 259 |
| 260 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); |
| 261 } |
| 262 |
231 class Index extends Expression { | 263 class Index extends Expression { |
232 final Expression receiver; | 264 final Expression receiver; |
233 final Expression argument; | 265 final Expression argument; |
234 | 266 |
235 Index(this.receiver, this.argument); | 267 Index(this.receiver, this.argument); |
236 | 268 |
237 accept(Visitor v) => v.visitIndex(this); | 269 accept(Visitor v) => v.visitIndex(this); |
238 | 270 |
239 String toString() => '$receiver[$argument]'; | 271 String toString() => '$receiver[$argument]'; |
240 | 272 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 359 } |
328 | 360 |
329 static int hash2(int a, int b) => finish(combine(combine(0, a), b)); | 361 static int hash2(int a, int b) => finish(combine(combine(0, a), b)); |
330 | 362 |
331 static int hash3(int a, int b, int c) => | 363 static int hash3(int a, int b, int c) => |
332 finish(combine(combine(combine(0, a), b), c)); | 364 finish(combine(combine(combine(0, a), b), c)); |
333 | 365 |
334 static int hash4(int a, int b, int c, int d) => | 366 static int hash4(int a, int b, int c, int d) => |
335 finish(combine(combine(combine(combine(0, a), b), c), d)); | 367 finish(combine(combine(combine(combine(0, a), b), c), d)); |
336 } | 368 } |
OLD | NEW |