| 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 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => new MapLiteral(entries); | 13 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => new MapLiteral(entries); |
| 14 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => | 14 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => |
| 15 new MapLiteralEntry(key, value); | 15 new MapLiteralEntry(key, value); |
| 16 Identifier ident(String v) => new Identifier(v); | 16 Identifier ident(String v) => new Identifier(v); |
| 17 ParenthesizedExpression paren(Expression e) => new ParenthesizedExpression(e); | 17 ParenthesizedExpression paren(Expression e) => new ParenthesizedExpression(e); |
| 18 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); | 18 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); |
| 19 BinaryOperator binary(Expression l, String op, Expression r) => | 19 BinaryOperator binary(Expression l, String op, Expression r) => |
| 20 new BinaryOperator(l, op, r); | 20 new BinaryOperator(l, op, r); |
| 21 Invoke invoke(Expression e, String m, [List<Expression> a]) => | 21 Getter getter(Expression e, String m) => new Getter(e, m); |
| 22 Index index(Expression e, Expression a) => new Index(e, a); |
| 23 Invoke invoke(Expression e, String m, List<Expression> a) => |
| 22 new Invoke(e, m, a); | 24 new Invoke(e, m, a); |
| 23 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); | 25 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); |
| 24 | 26 |
| 25 | 27 |
| 26 class AstFactory { | 28 class AstFactory { |
| 27 EmptyExpression empty() => const EmptyExpression(); | 29 EmptyExpression empty() => const EmptyExpression(); |
| 28 | 30 |
| 29 Literal literal(v) => new Literal(v); | 31 Literal literal(v) => new Literal(v); |
| 30 | 32 |
| 31 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => | 33 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => |
| 32 new MapLiteral(entries); | 34 new MapLiteral(entries); |
| 33 | 35 |
| 34 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => | 36 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) => |
| 35 new MapLiteralEntry(key, value); | 37 new MapLiteralEntry(key, value); |
| 36 | 38 |
| 37 Identifier identifier(String v) => new Identifier(v); | 39 Identifier identifier(String v) => new Identifier(v); |
| 38 | 40 |
| 39 ParenthesizedExpression parenthesized(Expression e) => | 41 ParenthesizedExpression parenthesized(Expression e) => |
| 40 new ParenthesizedExpression(e); | 42 new ParenthesizedExpression(e); |
| 41 | 43 |
| 42 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); | 44 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e); |
| 43 | 45 |
| 44 BinaryOperator binary(Expression l, String op, Expression r) => | 46 BinaryOperator binary(Expression l, String op, Expression r) => |
| 45 new BinaryOperator(l, op, r); | 47 new BinaryOperator(l, op, r); |
| 46 | 48 |
| 47 Invoke invoke(Expression e, String m, [List<Expression> a]) => | 49 Getter getter(Expression g, String n) => new Getter(g, n); |
| 50 |
| 51 Index index(Expression e, Expression a) => new Index(e, a); |
| 52 |
| 53 Invoke invoke(Expression e, String m, List<Expression> a) => |
| 48 new Invoke(e, m, a); | 54 new Invoke(e, m, a); |
| 49 | 55 |
| 50 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); | 56 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r); |
| 51 } | 57 } |
| 52 | 58 |
| 53 /// Base class for all expressions | 59 /// Base class for all expressions |
| 54 abstract class Expression { | 60 abstract class Expression { |
| 55 const Expression(); | 61 const Expression(); |
| 56 accept(Visitor v); | 62 accept(Visitor v); |
| 57 } | 63 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 accept(Visitor v) => v.visitInExpression(this); | 182 accept(Visitor v) => v.visitInExpression(this); |
| 177 | 183 |
| 178 String toString() => '($left in $right)'; | 184 String toString() => '($left in $right)'; |
| 179 | 185 |
| 180 bool operator ==(o) => o is InExpression && o.left == left | 186 bool operator ==(o) => o is InExpression && o.left == left |
| 181 && o.right == right; | 187 && o.right == right; |
| 182 | 188 |
| 183 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); | 189 int get hashCode => _JenkinsSmiHash.hash2(left.hashCode, right.hashCode); |
| 184 } | 190 } |
| 185 | 191 |
| 192 class Index extends Expression { |
| 193 final Expression receiver; |
| 194 final Expression argument; |
| 195 |
| 196 Index(this.receiver, this.argument); |
| 197 |
| 198 accept(Visitor v) => v.visitIndex(this); |
| 199 |
| 200 String toString() => '$receiver[$argument]'; |
| 201 |
| 202 bool operator ==(o) => |
| 203 o is Index |
| 204 && o.receiver == receiver |
| 205 && o.argument == argument; |
| 206 |
| 207 int get hashCode => |
| 208 _JenkinsSmiHash.hash2(receiver.hashCode, argument.hashCode); |
| 209 } |
| 210 |
| 211 class Getter extends Expression { |
| 212 final Expression receiver; |
| 213 final String name; |
| 214 |
| 215 Getter(this.receiver, this.name); |
| 216 |
| 217 accept(Visitor v) => v.visitGetter(this); |
| 218 |
| 219 String toString() => '$receiver.$name'; |
| 220 |
| 221 bool operator ==(o) => |
| 222 o is Getter |
| 223 && o.receiver == receiver |
| 224 && o.name == name; |
| 225 |
| 226 int get hashCode => _JenkinsSmiHash.hash2(receiver.hashCode, name.hashCode); |
| 227 |
| 228 } |
| 229 |
| 186 /** | 230 /** |
| 187 * Represents a function or method invocation. If [method] is null, then | 231 * Represents a function or method invocation. If [method] is null, then |
| 188 * [receiver] is an expression that should evaluate to a function. If [method] | 232 * [receiver] is an expression that should evaluate to a function. If [method] |
| 189 * is not null, then [receiver] is an expression that should evaluate to an | 233 * is not null, then [receiver] is an expression that should evaluate to an |
| 190 * object that has an appropriate method. | 234 * object that has an appropriate method. |
| 191 */ | 235 */ |
| 192 class Invoke extends Expression { | 236 class Invoke extends Expression { |
| 193 final Expression receiver; | 237 final Expression receiver; |
| 194 final String method; | 238 final String method; |
| 195 final List<Expression> arguments; | 239 final List<Expression> arguments; |
| 196 | 240 |
| 197 Invoke(this.receiver, this.method, [this.arguments]); | 241 Invoke(this.receiver, this.method, this.arguments) { |
| 242 assert(arguments != null); |
| 243 } |
| 198 | 244 |
| 199 accept(Visitor v) => v.visitInvoke(this); | 245 accept(Visitor v) => v.visitInvoke(this); |
| 200 | 246 |
| 201 bool get isGetter => arguments == null; | |
| 202 | |
| 203 String toString() => '$receiver.$method($arguments)'; | 247 String toString() => '$receiver.$method($arguments)'; |
| 204 | 248 |
| 205 bool operator ==(o) => | 249 bool operator ==(o) => |
| 206 o is Invoke | 250 o is Invoke |
| 207 && o.receiver == receiver | 251 && o.receiver == receiver |
| 208 && o.method == method | 252 && o.method == method |
| 209 && _listEquals(o.arguments, arguments); | 253 && _listEquals(o.arguments, arguments); |
| 210 | 254 |
| 211 int get hashCode => _JenkinsSmiHash.hash3(receiver.hashCode, method.hashCode, | 255 int get hashCode => _JenkinsSmiHash.hash3(receiver.hashCode, method.hashCode, |
| 212 _hashList(arguments)); | 256 _hashList(arguments)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 236 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | 280 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 237 return hash ^ (hash >> 6); | 281 return hash ^ (hash >> 6); |
| 238 } | 282 } |
| 239 | 283 |
| 240 static int finish(int hash) { | 284 static int finish(int hash) { |
| 241 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 285 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 242 hash = hash ^ (hash >> 11); | 286 hash = hash ^ (hash >> 11); |
| 243 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 287 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 244 } | 288 } |
| 245 | 289 |
| 246 static int hash2(a, b) => finish(combine(combine(0, a), b)); | 290 static int hash2(int a, int b) => finish(combine(combine(0, a), b)); |
| 247 | 291 |
| 248 static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); | 292 static int hash3(int a, int b, int c) => |
| 293 finish(combine(combine(combine(0, a), b), c)); |
| 249 | 294 |
| 250 static int hash4(a, b, c, d) => | 295 static int hash4(int a, int b, int c, int d) => |
| 251 finish(combine(combine(combine(combine(0, a), b), c), d)); | 296 finish(combine(combine(combine(combine(0, a), b), c), d)); |
| 252 } | 297 } |
| OLD | NEW |