This repository was archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.js
More file actions
77 lines (68 loc) · 2.31 KB
/
Copy pathexpression.js
File metadata and controls
77 lines (68 loc) · 2.31 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var Expression = (function(superClass) {
var hasProp = {}.hasOwnProperty;
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
extend(_Expression, superClass);
function _Expression(a, b, operation) {
this.a = a;
this.b = b;
this.operation = operation;
}
_Expression.prototype.getValue = function() {
switch (this.operation) {
case Operation.ADDITION:
return this.a.getValue() + this.b.getValue();
case Operation.SUBTRACTION:
return this.a.getValue() - this.b.getValue();
case Operation.MULTIPLICATION:
return this.a.getValue() * this.b.getValue();
case Operation.DIVISION:
return this.a.getValue() / this.b.getValue();
}
};
_Expression.prototype.print = function(operations) {
var out;
out = this.a.print({
rightOperation: this.operation
}) + " " + this.operation + " " + this.b.print({
leftOperation: this.operation
});
if (this._useBrackets(operations)) {
out = "(" + out + ")";
}
return out;
};
_Expression.prototype._useBrackets = function(operations) {
var useBrackets;
useBrackets = false;
if (!operations) {
return useBrackets;
}
if (operations.leftOperation) {
return this._checkLeftOperation(operations.leftOperation);
}
if (operations.rightOperation) {
return this._checkRightOperation(operations.rightOperation);
}
return useBrackets;
};
_Expression.prototype._checkLeftOperation = function(leftOperation) {
var useBrackets;
useBrackets = false;
if (leftOperation !== Operation.ADDITION) {
useBrackets = true;
}
if (leftOperation === Operation.MULTIPLICATION && this.operation === Operation.MULTIPLICATION) {
useBrackets = false;
}
return useBrackets;
};
_Expression.prototype._checkRightOperation = function(rightOperation) {
var useBrackets;
useBrackets = false;
if (rightOperation === Operation.MULTIPLICATION || rightOperation === Operation.DIVISION) {
useBrackets = true;
}
return useBrackets;
};
return _Expression;
})(NumberExp);