/
evaluate-binary-expression.ts
213 lines (179 loc) · 7.51 KB
/
evaluate-binary-expression.ts
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
import {EvaluatorOptions} from "./evaluator-options.js";
import {getDotPathFromNode} from "../lexical-environment/get-dot-path-from-node.js";
import {findLexicalEnvironmentInSameContext, setInLexicalEnvironment} from "../lexical-environment/lexical-environment.js";
import {Literal} from "../literal/literal.js";
import {UnexpectedNodeError} from "../error/unexpected-node-error/unexpected-node-error.js";
import {UndefinedLeftValueError} from "../error/undefined-left-value-error/undefined-left-value-error.js";
import {TS} from "../../type/ts.js";
import {getInnerNode} from "../util/node/get-inner-node.js";
/**
* Evaluates, or attempts to evaluate, a BinaryExpression
*/
export function evaluateBinaryExpression(options: EvaluatorOptions<TS.BinaryExpression>): Literal {
const {node, environment, evaluate, logger, typeChecker, statementTraversalStack, reporting, typescript} = options;
const leftValue = evaluate.expression(node.left, environment, statementTraversalStack) as number;
const rightValue = evaluate.expression(node.right, environment, statementTraversalStack) as number;
const leftIdentifier = getDotPathFromNode({...options, node: node.left});
const operator = node.operatorToken.kind;
switch (operator) {
case typescript.SyntaxKind.AmpersandToken: {
return leftValue & rightValue;
}
case typescript.SyntaxKind.AmpersandAmpersandToken: {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
return leftValue && rightValue;
}
case typescript.SyntaxKind.AmpersandEqualsToken:
case typescript.SyntaxKind.CaretEqualsToken:
case typescript.SyntaxKind.BarEqualsToken:
case typescript.SyntaxKind.MinusEqualsToken:
case typescript.SyntaxKind.PlusEqualsToken:
case typescript.SyntaxKind.PercentEqualsToken:
case typescript.SyntaxKind.SlashEqualsToken:
case typescript.SyntaxKind.AsteriskEqualsToken:
case typescript.SyntaxKind.AsteriskAsteriskEqualsToken:
case typescript.SyntaxKind.LessThanLessThanEqualsToken:
case typescript.SyntaxKind.GreaterThanGreaterThanEqualsToken:
case typescript.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
case typescript.SyntaxKind.QuestionQuestionEqualsToken:
case typescript.SyntaxKind.BarBarEqualsToken:
case typescript.SyntaxKind.AmpersandAmpersandEqualsToken: {
// There's nothing in the engine restricting you from applying this kind of arithmetic operation on non-numeric data types
let computedValue = leftValue;
switch (operator) {
case typescript.SyntaxKind.AmpersandEqualsToken:
computedValue &= rightValue;
break;
case typescript.SyntaxKind.CaretEqualsToken:
computedValue ^= rightValue;
break;
case typescript.SyntaxKind.BarEqualsToken:
computedValue |= rightValue;
break;
case typescript.SyntaxKind.AsteriskEqualsToken:
computedValue *= rightValue;
break;
case typescript.SyntaxKind.AsteriskAsteriskEqualsToken:
computedValue **= rightValue;
break;
case typescript.SyntaxKind.LessThanLessThanEqualsToken:
computedValue <<= rightValue;
break;
case typescript.SyntaxKind.GreaterThanGreaterThanEqualsToken:
computedValue >>= rightValue;
break;
case typescript.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
computedValue >>>= rightValue;
break;
case typescript.SyntaxKind.MinusEqualsToken:
computedValue -= rightValue;
break;
case typescript.SyntaxKind.PlusEqualsToken:
computedValue += rightValue;
break;
case typescript.SyntaxKind.PercentEqualsToken:
computedValue %= rightValue;
break;
case typescript.SyntaxKind.SlashEqualsToken:
computedValue /= rightValue;
break;
case typescript.SyntaxKind.QuestionQuestionEqualsToken:
computedValue = leftValue == null ? rightValue : leftValue;
break;
case typescript.SyntaxKind.BarBarEqualsToken:
if (!leftValue) {
computedValue = rightValue;
}
break;
case typescript.SyntaxKind.AmpersandAmpersandEqualsToken:
if (leftValue) {
computedValue = rightValue;
}
break;
}
// Update to the left-value within the environment if it exists there and has been updated
if (leftIdentifier != null) {
setInLexicalEnvironment({env: environment, path: leftIdentifier, value: computedValue, reporting, node});
}
// Return the computed value
return computedValue;
}
case typescript.SyntaxKind.AsteriskToken: {
return leftValue * rightValue;
}
case typescript.SyntaxKind.AsteriskAsteriskToken: {
return leftValue ** rightValue;
}
case typescript.SyntaxKind.BarToken: {
return leftValue | rightValue;
}
case typescript.SyntaxKind.BarBarToken: {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
return leftValue || rightValue;
}
case typescript.SyntaxKind.CaretToken: {
return leftValue ^ rightValue;
}
case typescript.SyntaxKind.CommaToken: {
return rightValue;
}
case typescript.SyntaxKind.MinusToken:
return leftValue - rightValue;
case typescript.SyntaxKind.PlusToken:
logger.logResult(leftValue + rightValue, "BinaryExpression (PlusToken)");
return leftValue + rightValue;
case typescript.SyntaxKind.PercentToken:
return leftValue % rightValue;
case typescript.SyntaxKind.SlashToken:
return leftValue / rightValue;
case typescript.SyntaxKind.EqualsToken: {
// Update to the left-value within the environment if it exists there and has been updated
if (leftIdentifier != null) {
const innerLeftIdentifier = getInnerNode(node.left, typescript);
const leftIdentifierSymbol = typeChecker?.getSymbolAtLocation(innerLeftIdentifier);
const leftIdentifierValueDeclaration = leftIdentifierSymbol?.valueDeclaration;
const bestLexicalEnvironment =
leftIdentifierValueDeclaration == null ? environment : findLexicalEnvironmentInSameContext(environment, leftIdentifierValueDeclaration, typescript) ?? environment;
setInLexicalEnvironment({env: bestLexicalEnvironment, path: leftIdentifier, value: rightValue, reporting, node});
logger.logBinding(leftIdentifier, rightValue, "Assignment");
} else {
throw new UndefinedLeftValueError({node: node.left});
}
// The return value of an assignment is always the assigned value
return rightValue;
}
case typescript.SyntaxKind.EqualsEqualsToken: {
// eslint-disable-next-line eqeqeq
return leftValue == rightValue;
}
case typescript.SyntaxKind.EqualsEqualsEqualsToken: {
return leftValue === rightValue;
}
case typescript.SyntaxKind.ExclamationEqualsToken: {
// eslint-disable-next-line eqeqeq
return leftValue != rightValue;
}
case typescript.SyntaxKind.ExclamationEqualsEqualsToken: {
return leftValue !== rightValue;
}
case typescript.SyntaxKind.GreaterThanToken:
return leftValue > rightValue;
case typescript.SyntaxKind.GreaterThanEqualsToken:
return leftValue >= rightValue;
case typescript.SyntaxKind.LessThanToken:
return leftValue < rightValue;
case typescript.SyntaxKind.LessThanEqualsToken:
return leftValue <= rightValue;
case typescript.SyntaxKind.InKeyword: {
return leftValue in (rightValue as unknown as Record<string, unknown>);
}
// Nullish coalescing (A ?? B)
case typescript.SyntaxKind.QuestionQuestionToken:
return leftValue != null ? leftValue : rightValue;
case typescript.SyntaxKind.InstanceOfKeyword: {
return (leftValue as unknown as Record<string, unknown>) instanceof (rightValue as unknown as CallableFunction);
}
}
// Throw if the operator couldn't be handled
throw new UnexpectedNodeError({node: node.operatorToken, typescript});
}