-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.js
More file actions
157 lines (135 loc) · 4.37 KB
/
preprocess.js
File metadata and controls
157 lines (135 loc) · 4.37 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
function isDigit(ch) { return /[0-9]/.test(ch); }
function isAlpha(ch) { return /[A-Za-z_]/.test(ch); }
function tokenizeWithGroups(src) {
let i = 0;
const len = src.length;
function skipSpaces() {
while (i < len && /\s/.test(src[i])) i++;
}
function readNumber() {
let start = i;
while (i < len && /[0-9.]/.test(src[i])) i++;
return { type: 'number', value: src.slice(start, i) };
}
function readIdent() {
let start = i;
while (i < len && /[A-Za-z0-9_]/.test(src[i])) i++;
return { type: 'ident', value: src.slice(start, i) };
}
function parseGroup() {
i++;
const inner = [];
while (i < len) {
skipSpaces();
if (i >= len) break;
const ch = src[i];
if (ch === ')') { i++; break; }
if (ch === '(') {
inner.push(parseGroup());
continue;
}
if (isDigit(ch)) { inner.push(readNumber()); continue; }
if (isAlpha(ch)) { inner.push(readIdent()); continue; }
if ("+-*/^=,".includes(ch)) { inner.push({ type: 'op', value: ch }); i++; continue; }
if (ch === '!') { inner.push({ type: 'op', value: '!' }); i++; continue; }
inner.push({ type: 'op', value: ch }); i++;
}
return { type: 'group', value: inner };
}
const tokens = [];
while (i < len) {
skipSpaces();
if (i >= len) break;
const ch = src[i];
if (ch === '(') { tokens.push(parseGroup()); continue; }
if (isDigit(ch)) { tokens.push(readNumber()); continue; }
if (isAlpha(ch)) { tokens.push(readIdent()); continue; }
if ("+-*/^=,()".includes(ch)) { tokens.push({ type: 'op', value: ch }); i++; continue; }
if (ch === '!') { tokens.push({ type: 'op', value: '!' }); i++; continue; }
tokens.push({ type: 'op', value: ch });
i++;
}
return tokens;
}
function isValueToken(t) {
return !t || t.type === 'number' || t.type === 'ident' || t.type === 'group' || (t.type === 'op' && t.value === ')');
}
function stringifyTokens(tokens) {
const out = [];
for (let i = 0; i < tokens.length; i++) {
const t = tokens[i];
if (t.type === 'number' || t.type === 'ident') out.push(t.value);
else if (t.type === 'op') out.push(t.value);
else if (t.type === 'group') out.push('(' + stringifyTokens(t.value) + ')');
else out.push(String(t.value));
}
return out.join('');
}
function factorial(n) {
if (n < 0 || !Number.isInteger(n)) return null;
let f = 1;
for (let i = 2; i <= n; i++) f *= i;
return f;
}
function tryEvaluateSimpleGroup(groupToken) {
if (!groupToken || groupToken.type !== 'group') return null;
const inner = groupToken.value;
if (inner.length === 1 && inner[0].type === 'number') {
return Number(inner[0].value);
}
return null;
}
function expandFactorials(tokens) {
const out = [];
for (let i = 0; i < tokens.length; i++) {
const t = tokens[i];
if (t.type === 'op' && t.value === '!') {
if (out.length === 0) {
out.push(t);
continue;
}
const prev = out.pop();
if (prev.type === 'number') {
const n = Number(prev.value);
const f = factorial(n);
if (f !== null) { out.push({ type: 'number', value: String(f) }); continue; }
}
const ev = tryEvaluateSimpleGroup(prev);
if (ev !== null) {
const f = factorial(ev);
if (f !== null) { out.push({ type: 'number', value: String(f) }); continue; }
}
out.push(prev);
out.push(t);
continue;
}
if (t.type === 'group') {
out.push({ type: 'group', value: expandFactorials(t.value) });
continue;
}
out.push(t);
}
return out;
}
function insertImplicitMultiplication(tokens) {
const out = [];
for (let i = 0; i < tokens.length; i++) {
const cur = tokens[i];
out.push(cur);
const next = tokens[i+1];
if (!next) continue;
const curIsValue = (cur.type === 'number' || cur.type === 'ident' || cur.type === 'group' || (cur.type === 'op' && cur.value === ')'));
const nextIsValue = (next.type === 'number' || next.type === 'ident' || next.type === 'group' || (next.type === 'op' && next.value === '('));
if (curIsValue && nextIsValue) {
out.push({ type: 'op', value: '*' });
}
}
return out;
}
function preprocessExpression(src) {
let tokens = tokenizeWithGroups(src);
tokens = expandFactorials(tokens);
tokens = insertImplicitMultiplication(tokens);
return stringifyTokens(tokens);
}
export { preprocessExpression };