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