forked from msironi/expr-eval
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexpression-to-string.ts
More file actions
219 lines (213 loc) · 8.7 KB
/
Copy pathexpression-to-string.ts
File metadata and controls
219 lines (213 loc) · 8.7 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
// cSpell:words ISCALAR IVAR IVARNAME IFUNCALL IEXPR IEXPREVAL IMEMBER IENDSTATEMENT IARRAY IARROW
// cSpell:words IFUNDEF IUNDEFINED ICASEMATCH ICASECOND IWHENCOND IWHENMATCH ICASEELSE IPROPERTY
// cSpell:words IOBJECT IOBJECTEND
// cSpell:words nstack
import { ISCALAR, IOP1, IOP2, IOP3, IVAR, IVARNAME, IFUNCALL, IFUNDEF, IARROW, IEXPR, IMEMBER, IENDSTATEMENT, IARRAY, IUNDEFINED, ICASEMATCH, ICASECOND, IWHENCOND, IWHENMATCH, ICASEELSE, IOBJECT, IOBJECTEND, IPROPERTY } from '../parsing/instruction.js';
import type { Instruction } from '../parsing/instruction.js';
export default function expressionToString(tokens: Instruction[], toJS?: boolean): string {
const nstack: string[] = [];
let n1: string, n2: string, n3: string;
let f: string, args: string[], argCount: number;
for (let i = 0; i < tokens.length; i++) {
const item = tokens[i];
const { type } = item;
if (type === ISCALAR) {
if (typeof item.value === 'number' && item.value < 0) {
nstack.push('(' + item.value + ')');
} else if (Array.isArray(item.value)) {
nstack.push('[' + item.value.map(escapeValue).join(', ') + ']');
} else {
nstack.push(escapeValue(item.value));
}
} else if (type === IOP2) {
n2 = nstack.pop()!;
n1 = nstack.pop()!;
f = item.value as string;
if (toJS) {
if (f === '^') {
nstack.push('Math.pow(' + n1 + ', ' + n2 + ')');
} else if (f === 'and' || f === '&&') {
nstack.push('(!!' + n1 + ' && !!' + n2 + ')');
} else if (f === 'or' || f === '||') {
nstack.push('(!!' + n1 + ' || !!' + n2 + ')');
} else if (f === '|') {
nstack.push('(function(a,b){ return Array.isArray(a) && Array.isArray(b) ? a.concat(b) : String(a) + String(b); }((' + n1 + '),(' + n2 + ')))');
} else if (f === '==') {
nstack.push('(' + n1 + ' === ' + n2 + ')');
} else if (f === '!=') {
nstack.push('(' + n1 + ' !== ' + n2 + ')');
} else if (f === '[') {
nstack.push(n1 + '[(' + n2 + ') | 0]');
} else {
nstack.push('(' + n1 + ' ' + f + ' ' + n2 + ')');
}
} else if (f === '[') {
nstack.push(n1 + '[' + n2 + ']');
} else {
nstack.push('(' + n1 + ' ' + f + ' ' + n2 + ')');
}
} else if (type === IOP3) {
n3 = nstack.pop()!;
n2 = nstack.pop()!;
n1 = nstack.pop()!;
f = item.value as string;
if (f === '?') {
nstack.push('(' + n1 + ' ? ' + n2 + ' : ' + n3 + ')');
} else {
throw new Error('invalid Expression');
}
} else if (type === IVAR || type === IVARNAME) {
nstack.push(item.value as string);
} else if (type === IOP1) {
n1 = nstack.pop()!;
f = item.value as string;
if (f === '-' || f === '+') {
nstack.push('(' + f + n1 + ')');
} else if (toJS) {
if (f === 'not') {
nstack.push('(' + '!' + n1 + ')');
} else if (f === '!') {
nstack.push('fac(' + n1 + ')');
} else {
nstack.push(f + '(' + n1 + ')');
}
} else if (f === '!') {
nstack.push('(' + n1 + '!)');
} else {
nstack.push('(' + f + ' ' + n1 + ')');
}
} else if (type === IFUNCALL) {
argCount = item.value as number;
args = [];
while (argCount-- > 0) {
args.unshift(nstack.pop()!);
}
f = nstack.pop()!;
nstack.push(f + '(' + args.join(', ') + ')');
} else if (type === IFUNDEF) {
n2 = nstack.pop()!;
argCount = item.value as number;
args = [];
while (argCount-- > 0) {
args.unshift(nstack.pop()!);
}
n1 = nstack.pop()!;
if (toJS) {
nstack.push('(' + n1 + ' = function(' + args.join(', ') + ') { return ' + n2 + ' })');
} else {
nstack.push('(' + n1 + '(' + args.join(', ') + ') = ' + n2 + ')');
}
} else if (type === IARROW) {
n2 = nstack.pop()!;
argCount = item.value as number;
args = [];
while (argCount-- > 0) {
args.unshift(nstack.pop()!);
}
if (toJS) {
nstack.push('((' + args.join(', ') + ') => ' + n2 + ')');
} else {
if (args.length === 1) {
nstack.push('(' + args[0] + ' => ' + n2 + ')');
} else {
nstack.push('((' + args.join(', ') + ') => ' + n2 + ')');
}
}
} else if (type === IMEMBER) {
n1 = nstack.pop()!;
nstack.push(n1 + '.' + item.value);
} else if (type === IARRAY) {
argCount = item.value as number;
args = [];
while (argCount-- > 0) {
args.unshift(nstack.pop()!);
}
nstack.push('[' + args.join(', ') + ']');
} else if (type === IEXPR) {
nstack.push('(' + expressionToString(item.value as Instruction[], toJS) + ')');
} else if (type === IENDSTATEMENT) {
// eslint-disable no-empty
} else if (type === IUNDEFINED) {
// The value of the undefined reserved work is undefined.
nstack.push('undefined');
} else if (type === ICASEMATCH || type === ICASECOND) {
// When we get here all the when conditions have already been evaluated; at this point
// the stack will look like
// toTest, condition0, value0, condition1, value1, ..., conditionN, valueN.
// Each of the condition values will be true/false.
// First we remove all the WHEN/ELSE conditions from the stack...
const whenCount = item.value as number;
const whens = nstack.splice(-whenCount, whenCount);
if (type === ICASEMATCH) {
// ...then remove the value being tested from the stack if this is a CASE $input...
n2 = nstack.pop()!;
// ...push a string for the entire case onto the stack.
nstack.push(`case ${n2} ` + whens.join(' ') + ' end');
} else {
// ...push a string for the entire case onto the stack.
nstack.push('case ' + whens.join(' ') + ' end');
}
} else if (type === IWHENCOND) {
// We are evaluating a WHEN x THEN y portion of a CASE statement; the top of the
// stack has the y value...
n1 = nstack.pop()!;
// ...The second value on the stack has the x value
n2 = nstack.pop()!;
// ..once we have the when value and the value being tested we evaluate the x value
// to see if it evaluates to a truthy value.
nstack.push(`when ${n2} then ${n1}`);
} else if (type === IWHENMATCH) {
// We are evaluating a WHEN x THEN y portion of a CASE $input statement; the top of the
// stack has the y value...
n1 = nstack.pop()!;
// ...The second value on the stack has the x value
n2 = nstack.pop()!;
// ...The last item on the stack will be the value to test for the FIRST when;
// as we have further when conditions they will pile up on the stack we will have to
// skip them...
n3 = nstack[nstack.length - 1 - ((item.value as number) * 2)];
// ..once we have the when value and the value being tested we use the == operator
// to compare them.
nstack.push(`when ${n2} then ${n1}`);
} else if (type === ICASEELSE) {
// We are evaluating a ELSE y portion of a case statement; we want to push a pair of values
// just a like a WHEN x THEN y; the first value being true to always match this condition the
// second value being the value to use.
n1 = nstack.pop()!;
nstack.push(`else ${n1}`);
} else if (type === IOBJECT) {
// We are constructing an object, push an empty object onto the stack.
nstack.push('{ ');
} else if (type === IOBJECTEND) {
// We have completed constructing an object, append the closing brace.
nstack.push(`${nstack.pop()!} }`);
} else if (type === IPROPERTY) {
// At this point the top 2 items on the stack will be the property value, and the partial
// object construction string we are building.
n1 = nstack.pop()!;
const partial = nstack.pop()!;
// We keep the entire object expression as a single entry on the stack
// If this is the FIRST property then the value on the stack will be the '{ ' from the IOBJECT
// above. If not then it will be the previous property in the object which means we need a
// comma to separate this property from the previous property.
const separator = partial.endsWith('{ ') ? '' : ', ';
nstack.push(`${partial}${separator}${item.value}: ${n1}`);
} else {
throw new Error('invalid Expression');
}
}
if (nstack.length > 1) {
if (toJS) {
return nstack.join(',');
} else {
return nstack.join(';');
}
}
return String(nstack[0]);
}
function escapeValue(v: any): string {
if (typeof v === 'string') {
return JSON.stringify(v).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
}
return v;
}