-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
205 lines (189 loc) · 5.34 KB
/
calculator.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var calculator = function (s) {
var parts = s.split(/ *; */);
if (parts.length === 0) {
return 'No expressions entered.';
};
var plural;
if (parts.length === 1) {
plural = 'Result';
} else {
plural = 'Results';
}
return plural + ' for ' + s + ':<br/>' + parts.map(calculator_one_thing).filter(function (x) {
return x !== null;
}).join('<br/>')
}
var calculator_one_thing = function (s) {
if (s === '') {
return null;
}
var result = paren_calculator('(' + s + ')');
if (typeof result === 'string') {
return 'Error evaluating ' + s + ': ' + result;
} else {
return s + ' = ' + result;
}
}
var paren_calculator = function (s) {
return evaluate_parsed(paren_parse(s));
}
var paren_parse = function (x) {
if (x[0] !== '(') {
if (/^-?\d+$/.exec(x)) {
return parseInt(x, 10);
} else if (/^-?(\d+\.\d*|\d*\.\d+)$/.exec(x)) {
return parseFloat(x);
} else if (x in op_dict) {
return op_dict[x];
} else {
return 'Cannot recogize \'' + x + '\'';
}
} else {
var end = x.length - 1;
var parts = [[]];
var depth = 0;
for (var i = 1; i < end; i++) {
var c = x[i];
if (depth === 0 && c === ' ') {
parts.push([]);
} else {
last(parts).push(c);
if (c === '(') {
depth++;
} else if (c === ')') {
depth--;
}
}
if (depth < 0) {
return 'Too many close parentheses.';
}
}
if (depth > 0) {
return 'Too many open parentheses.';
}
var result = parts.map(function (part) {
return paren_parse(part.join(''));
});
if (result.some(is_string)) {
return result.filter(is_string).join(', ');
} else {
return operator_precedence_handling(result);
}
}
}
var is_embeddable_calc = function (x) {
return typeof x === 'object' && 'embeddable' in x && x.embeddable === true;
}
var is_number_calc = function (x) {
return typeof x === 'number' || !('name' in x); // operator detection, maybe clean
}
var should_embed_in = function (precedence, assoc, x) {
if (assoc === 'left') {
return precedence > x.op.precedence;
} else if (assoc === 'right') {
return precedence >= x.op.precedence;
}
}
var operator_precedence_handling = function (x) {
if (x.length % 2 === 0) {
return 'Numbers and operators do not alternate properly.';
}
var i;
for (i = 0; i < x.length; i++) {
if (i % 2 === 0) {
if (!is_number_calc(x[i])) {
return 'Operator where number should be.';
}
} else if (i % 2 === 1) {
if (is_number_calc(x[i])) {
return 'Number where operator should be.';
}
}
}
if (x.length === 1) {
return x[0];
}
var result = {
'op': x[1],
'left': x[0],
'right': x[2],
'embeddable': true
};
for (i = 3; i < x.length; i += 2) {
var precedence = x[i].precedence;
var assoc = x[i].assoc;
if (!should_embed_in(precedence, assoc, result)) {
result = {
'op': x[i],
'left': result,
'right': x[i + 1],
'embeddable': true
}
} else {
var descending = result;
while (is_embeddable_calc(descending.right)
&& should_embed_in(precedence, assoc, descending.right)) {
descending = descending.right;
}
descending.right = {
'op': x[i],
'left': descending.right,
'right': x[i + 1],
'embeddable': true
}
}
}
return {
'paren': result
}
}
var evaluate_parsed = function (parsed) {
if (is_string(parsed)) {
// error
return parsed;
} else if (is_number(parsed)) {
// just a number
return parsed;
} else if ('paren' in parsed) {
return evaluate_parsed(parsed.paren);
} else {
return parsed.op.f(evaluate_parsed(parsed.left), evaluate_parsed(parsed.right));
}
}
// Some base conversion stuff.
var base_convert = function (s) {
var nums = s.match(/-?\d+/g);
var from = parseInt(nums[0], 10);
var to = parseInt(nums[1], 10);
var nums_to_convert = nums.slice(2);
return nums_to_convert.map(function (x) {
return x + ' -> ' + parseInt(x, from).toString(to);
}).join('<br/>');
}
var get_precedence = function (x) {
if (x in op_dict) {
return x + ' has precedence ' + op_dict[x].precedence;
} else {
return x + ' is not an operator';
}
}
var get_assoc = function (x) {
if (x in op_dict) {
return x + ' has assoc ' + op_dict[x].assoc;
} else {
return x + ' is not an operator';
}
}
// General calculator operation generator.
var calc_op = function (f) {
return {
'f': function (x) {
var result = f(x);
if (result !== null) {
el('other').innerHTML = result;
}
},
'arg': true
}
}