-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
267 lines (227 loc) · 6.47 KB
/
function.js
File metadata and controls
267 lines (227 loc) · 6.47 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
Lexer.defunct = function (chr) {
throw new Error("Unexpected character at index " + (this.index - 1) + ": " + chr);
};
try {
Lexer.engineHasStickySupport = typeof /(?:)/.sticky == 'boolean';
} catch (ignored) {
Lexer.engineHasStickySupport = false;
}
try {
Lexer.engineHasUnicodeSupport = typeof /(?:)/.unicode == 'boolean';
} catch (ignored) {
Lexer.engineHasUnicodeSupport = false;
}
function Lexer(defunct) {
if (typeof defunct !== "function") defunct = Lexer.defunct;
var tokens = [];
var rules = [];
var remove = 0;
this.state = 0;
this.index = 0;
this.input = "";
this.addRule = function (pattern, action, start) {
var global = pattern.global;
if (!global || Lexer.engineHasStickySupport && !pattern.sticky) {
var flags = Lexer.engineHasStickySupport ? "gy" : "g";
if (pattern.multiline) flags += "m";
if (pattern.ignoreCase) flags += "i";
if (Lexer.engineHasUnicodeSupport && pattern.unicode) flags += "u";
pattern = new RegExp(pattern.source, flags);
}
if (Object.prototype.toString.call(start) !== "[object Array]") start = [0];
rules.push({
pattern: pattern,
global: global,
action: action,
start: start
});
return this;
};
this.setInput = function (input) {
remove = 0;
this.state = 0;
this.index = 0;
tokens.length = 0;
this.input = input;
return this;
};
this.lex = function () {
if (tokens.length) return tokens.shift();
this.reject = true;
while (this.index <= this.input.length) {
var matches = scan.call(this).splice(remove);
var index = this.index;
while (matches.length) {
if (this.reject) {
var match = matches.shift();
var result = match.result;
var length = match.length;
this.index += length;
this.reject = false;
remove++;
var token = match.action.apply(this, result);
if (this.reject) this.index = result.index;
else if (typeof token !== "undefined") {
if (Object.prototype.toString.call(token) == "[object Array]") {
tokens = token.slice(1);
token = token[0];
}
if (length) remove = 0;
return token;
}
} else break;
}
var input = this.input;
if (index < input.length) {
if (this.reject) {
remove = 0;
let token = defunct.call(this, input.charAt(this.index++));
if (typeof token !== "undefined") {
if (Object.prototype.toString.call(token) === "[object Array]") {
tokens = token.slice(1);
return token[0];
} else return token;
}
} else {
if (this.index !== index) remove = 0;
this.reject = true;
}
} else if (matches.length) {
this.reject = true;
}
else break;
}
};
function scan() {
var matches = [];
var index = 0;
var state = this.state;
var lastIndex = this.index;
var input = this.input;
for (var i = 0, length = rules.length; i < length; i++) {
var rule = rules[i];
var start = rule.start;
var states = start.length;
if ((!states || start.indexOf(state) >= 0) ||
(state % 2 && states === 1 && !start[0])) {
var pattern = rule.pattern;
pattern.lastIndex = lastIndex;
var result = pattern.exec(input);
if (result && result.index === lastIndex) {
var j = matches.push({
result: result,
action: rule.action,
length: result[0].length
});
if (rule.global) index = j;
while (--j > index) {
var k = j - 1;
if (matches[j].length > matches[k].length) {
var temple = matches[j];
matches[j] = matches[k];
matches[k] = temple;
}
}
}
}
}
return matches;
}
}
class Equation {
constructor(eq) {
this.specialOperators = [
["sqrt", "~", "Math.sqrt"],
["abs", "#", "Math.abs"],
["asin", "{", "Math.asin"],
["acos", "}", "Math.acos"],
["atan", "|", "Math.atan"],
["sin", "[", "Math.sin"],
["cos", "]", "Math.cos"],
["tan", "\\", "Math.tan"],
["ln", "$", "Math.log"],
["exp", "?", "Math.exp"],
["min", "<", "Math.min"],
["max", ">", "Math.max"],
["pow", "@", "Math.pow"],
];
let punctuatorString = "";
for (let op of this.specialOperators) {
punctuatorString += "\\" + op[1];
}
const punctuatorRegex = new RegExp(`[${punctuatorString}]`, "g");
this.lexer = new Lexer();
this.lexer.addRule(/\s+/, () => {}); // skip whitespace
this.lexer.addRule(/[\+\-\*\/\(\)\,]/, lexeme => lexeme); // basic punctuators
this.lexer.addRule(punctuatorRegex, lexeme => lexeme); // special punctuators
this.lexer.addRule(/(?:0|[1-9]\d*)(?:\.\d+)?/, lexeme => +lexeme); // numbers
this.lexer.addRule(/(?:[a-zA-Z])?/, lexeme => lexeme); // variables
if (eq) this.parse(eq);
}
parse(eq) {
for (let op of this.specialOperators) {
eq = eq.replaceAll(op[0], op[1]);
}
Array.fromIterator = it => Array.from({ [Symbol.iterator]: () => it });
this.step = value => ({ done: value === undefined, value });
this.lexer.setInput(eq);
let next = () => this.step(this.lexer.lex());
let tokens = Array.fromIterator({ next });
for (let t = 0; t < tokens.length; ++t) {
let cur = String(tokens[t]), prev = (t > 0) ? String(tokens[t - 1]) : null, next = (t < tokens.length - 1) ? String(tokens[t + 1]) : null;
if (cur == "(") {
if (prev && /[0-9a-zA-Z]/.test(prev)) tokens.splice(t, 0, "*");
}
else if (cur == ")") {
if (next && /[0-9a-zA-Z\(]/.test(next)) tokens.splice(t + 1, 0, "*");
}
else if (/[a-zA-Z]/.test(cur)) {
if (prev && /[0-9a-zA-Z]/.test(prev)) tokens.splice(t, 0, "*");
else if (next && /[0-9a-zA-Z]/.test(next)) tokens.splice(t + 1, 0, "*");
}
else {
for (let op of this.specialOperators) {
if (cur == op[1]) {
if (prev && /[0-9a-zA-Z\)]/.test(prev)) tokens.splice(t, 0, "*");
break;
}
}
}
}
this.expression = tokens.join("").replaceAll("@", "Math.pow");
for (let op of this.specialOperators) {
this.expression = this.expression.replaceAll(op[1], op[2]);
}
}
}
class VectorFunction {
constructor(f1, f2) {
this.f1 = new Equation();
this.f2 = new Equation();
if (f1) this.f1.parse(f1);
if (f2) this.f2.parse(f2);
this.create();
}
create() {
if (!this.f1.expression || !this.f2.expression) {
this.eval = undefined;
}
else {
console.log("f1: " + this.f1.expression);
console.log("f2: " + this.f2.expression);
this.eval = new Function(`return function(x, y) {
let dx = ${this.f1.expression};
let dy = ${this.f2.expression};
return [dx, dy];
}`)();
}
}
setF1(f1) {
this.f1.parse(f1);
this.create();
}
setF2(f2) {
this.f2.parse(f2);
this.create();
}
}