-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.dart
62 lines (49 loc) · 2.11 KB
/
term.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'sums.dart';
import 'products.dart';
/// The fundamental element in an equation.
///
/// All Term objects should be considered immutable and all operations on them should
/// return new objects except in the rare case that the result happens to equate to an
/// existing object in which case that existing object can be returned as the result.
///
/// Possible instances of Term can be:
/// - Unknown (a single unknown variable)
/// - Constant (any number with no unknowns)
/// - Product (a product of other Term objects, with a coefficient)
/// - Division (a division of exactly 2 other Term objects)
/// - Sum (a sum of other Term objects)
abstract class Term {
const Term();
/// Is this Term object naturally negative?
///
/// Used in processing of negatesGracefully()
bool isNegative();
/// Can this Term be negated gracefully without having to multiply
/// it by a new coefficient of -1?
bool negatesGracefully();
/// Return a Term object representing the negation of this one if [doNegate] is true.
Term negateIf(bool doNegate) => doNegate ? -this : this;
/// Return true if this Term is identical to the other
bool equals(Term other);
/// If the other Term object can be added directly to this Term
/// object, return a new Term object representing the sum, or
/// null if it is not possible.
Term addDirect(Term other, bool isNegated);
/// Add a [Term] to this one.
Term operator +(Term other) => Sum.add(this, other);
/// Subtract a [Term] from this one.
Term operator -(Term other) => Sum.sub(this, other);
/// Multiply this [Term] by another.
Term operator *(Term other) => Product.mul(this, other);
/// Divide this [Term] by another.
Term operator /(Term other) => Division.div(this, other);
/// Negate this term.
Term operator -();
/// Returns true if the string representation of this Term will start
/// with a minus sign.
bool startsWithMinus();
/// Returns a string that shows the structure of the Term, outlining the
/// type of this [Term] and the relationship to any component [Term]
/// objects in the expression.
String toOutline();
}