| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 // library abstract_expressions; | 7 // library abstract_expressions; |
| 8 | 8 |
| 9 abstract class AbstractExpression {} | 9 abstract class AbstractExpression {} |
| 10 | 10 |
| 11 abstract class AbstractAddition<E> { | 11 abstract class AbstractAddition<E> { |
| 12 E operand1, operand2; | 12 E operand1, operand2; |
| 13 AbstractAddition(this.operand1, this.operand2); | 13 AbstractAddition(this.operand1, this.operand2); |
| 14 } | 14 } |
| 15 | 15 |
| 16 abstract class AbstractSubtraction<E> { | 16 abstract class AbstractSubtraction<E> { |
| 17 E operand1, operand2; | 17 E operand1, operand2; |
| 18 AbstractSubtraction(this.operand1, this.operand2); | 18 AbstractSubtraction(this.operand1, this.operand2); |
| 19 } | 19 } |
| 20 | 20 |
| 21 abstract class AbstractNumber { | 21 abstract class AbstractNumber { |
| 22 int val; | 22 int val; |
| 23 AbstractNumber(this.val); | 23 AbstractNumber(this.val); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // library evaluator; | 26 // library evaluator; |
| 27 | 27 |
| 28 abstract class ExpressionWithEval { | 28 abstract class ExpressionWithEval { |
| 29 int get eval; | 29 int get eval; |
| 30 } | 30 } |
| 31 | 31 |
| 32 abstract class AdditionWithEval<E extends ExpressionWithEval> { | 32 abstract class AdditionWithEval<E extends ExpressionWithEval> { |
| 33 E get operand1; | 33 E get operand1; |
| 34 E get operand2; | 34 E get operand2; |
| 35 int get eval => operand1.eval + operand2.eval; | 35 int get eval => operand1.eval + operand2.eval; |
| 36 } | 36 } |
| 37 | 37 |
| 38 abstract class SubtractionWithEval<E extends ExpressionWithEval>{ | 38 abstract class SubtractionWithEval<E extends ExpressionWithEval> { |
| 39 E get operand1; | 39 E get operand1; |
| 40 E get operand2; | 40 E get operand2; |
| 41 int get eval => operand1.eval - operand2.eval; | 41 int get eval => operand1.eval - operand2.eval; |
| 42 } | 42 } |
| 43 | 43 |
| 44 abstract class NumberWithEval { | 44 abstract class NumberWithEval { |
| 45 int get val; | 45 int get val; |
| 46 int get eval => val; | 46 int get eval => val; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // library multiplication; | 49 // library multiplication; |
| 50 | 50 |
| 51 abstract class AbstractMultiplication<E> { | 51 abstract class AbstractMultiplication<E> { |
| 52 E operand1, operand2; | 52 E operand1, operand2; |
| 53 AbstractMultiplication(this.operand1, this.operand2); | 53 AbstractMultiplication(this.operand1, this.operand2); |
| 54 | |
| 55 } | 54 } |
| 56 | 55 |
| 57 // library multiplicationEvaluator; | 56 // library multiplicationEvaluator; |
| 58 | 57 |
| 59 // import 'evaluator.dart' show ExpressionWithEval; | 58 // import 'evaluator.dart' show ExpressionWithEval; |
| 60 | 59 |
| 61 abstract class MultiplicationWithEval<E extends ExpressionWithEval> { | 60 abstract class MultiplicationWithEval<E extends ExpressionWithEval> { |
| 62 E get operand1; | 61 E get operand1; |
| 63 E get operand2; | 62 E get operand2; |
| 64 int get eval => operand1.eval * operand2.eval; | 63 int get eval => operand1.eval * operand2.eval; |
| 65 } | 64 } |
| 66 | 65 |
| 67 // library string_converter; | 66 // library string_converter; |
| 68 | 67 |
| 69 abstract class ExpressionWithStringConversion { | 68 abstract class ExpressionWithStringConversion { |
| 70 String toString(); | 69 String toString(); |
| 71 } | 70 } |
| 72 | 71 |
| 73 abstract class AdditionWithStringConversion<E extends ExpressionWithStringConver
sion> { | 72 abstract class AdditionWithStringConversion< |
| 73 E extends ExpressionWithStringConversion> { |
| 74 E get operand1; | 74 E get operand1; |
| 75 E get operand2; | 75 E get operand2; |
| 76 String toString() =>'($operand1 + $operand2))'; | 76 String toString() => '($operand1 + $operand2))'; |
| 77 } | 77 } |
| 78 | 78 |
| 79 abstract class SubtractionWithStringConversion<E extends ExpressionWithStringCon
version> { | 79 abstract class SubtractionWithStringConversion< |
| 80 E extends ExpressionWithStringConversion> { |
| 80 E get operand1; | 81 E get operand1; |
| 81 E get operand2; | 82 E get operand2; |
| 82 String toString() => '($operand1 - $operand2)'; | 83 String toString() => '($operand1 - $operand2)'; |
| 83 } | 84 } |
| 84 | 85 |
| 85 abstract class NumberWithStringConversion { | 86 abstract class NumberWithStringConversion { |
| 86 int get val; | 87 int get val; |
| 87 String toString() => val.toString(); | 88 String toString() => val.toString(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 abstract class MultiplicationWithStringConversion<E extends ExpressionWithString
Conversion> { | 91 abstract class MultiplicationWithStringConversion< |
| 92 E extends ExpressionWithStringConversion> { |
| 91 E get operand1; | 93 E get operand1; |
| 92 E get operand2; | 94 E get operand2; |
| 93 String toString() => '($operand1 * $operand2)'; | 95 String toString() => '($operand1 * $operand2)'; |
| 94 } | 96 } |
| 95 | 97 |
| 96 // library expressions; | 98 // library expressions; |
| 97 | 99 |
| 98 // import 'abstractExpressions.dart'; | 100 // import 'abstractExpressions.dart'; |
| 99 // import 'evaluator.dart'; | 101 // import 'evaluator.dart'; |
| 100 // import 'multiplication.dart'; | 102 // import 'multiplication.dart'; |
| 101 // import 'multiplicationEvaluator.dart'; | 103 // import 'multiplicationEvaluator.dart'; |
| 102 // import 'stringConverter.dart'; | 104 // import 'stringConverter.dart'; |
| 103 | 105 |
| 104 abstract class Expression = | 106 abstract class Expression = AbstractExpression |
| 105 AbstractExpression with ExpressionWithEval, ExpressionWithStringConversion; | 107 with ExpressionWithEval, ExpressionWithStringConversion; |
| 106 | 108 |
| 107 class Addition = | 109 class Addition = AbstractAddition<Expression> |
| 108 AbstractAddition<Expression> with AdditionWithEval<Expression>, | 110 with AdditionWithEval<Expression>, AdditionWithStringConversion<Expression> |
| 109 AdditionWithStringConversion<Expression> imp
lements Expression; | 111 implements Expression; |
| 110 | 112 |
| 111 class Subtraction = | 113 class Subtraction = AbstractSubtraction<Expression> |
| 112 AbstractSubtraction<Expression> with SubtractionWithEval<Expression>, | 114 with |
| 113 SubtractionWithStringConversion<Expressio
n> implements Expression; | 115 SubtractionWithEval<Expression>, |
| 116 SubtractionWithStringConversion<Expression> |
| 117 implements Expression; |
| 114 | 118 |
| 115 class Number = | 119 class Number = AbstractNumber |
| 116 AbstractNumber with NumberWithEval, | 120 with NumberWithEval, NumberWithStringConversion |
| 117 NumberWithStringConversion implements Expression; | 121 implements Expression; |
| 118 | 122 |
| 119 | 123 class Multiplication = AbstractMultiplication<Expression> |
| 120 class Multiplication = | 124 with |
| 121 AbstractMultiplication<Expression> with MultiplicationWithEval<Expression>, | 125 MultiplicationWithEval<Expression>, |
| 122 MultiplicationWithStringConversion<Exp
ression> implements Expression; | 126 MultiplicationWithStringConversion<Expression> |
| 123 | 127 implements Expression; |
| 124 | 128 |
| 125 void main() { | 129 void main() { |
| 126 Expression e = new Multiplication(new Addition(new Number(4), new Number(2)), | 130 Expression e = new Multiplication(new Addition(new Number(4), new Number(2)), |
| 127 new Subtraction(new Number(10), new Number(7
))); | 131 new Subtraction(new Number(10), new Number(7))); |
| 128 Expect.equals('((4 + 2)) * (10 - 7)) = 18', '$e = ${e.eval}'); | 132 Expect.equals('((4 + 2)) * (10 - 7)) = 18', '$e = ${e.eval}'); |
| 129 } | 133 } |
| OLD | NEW |