-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterpreter.ts
More file actions
264 lines (244 loc) · 8.03 KB
/
interpreter.ts
File metadata and controls
264 lines (244 loc) · 8.03 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
import { ExpressionError } from "./error";
import {
type BinaryExpression,
type CallExpression,
type ConditionalExpression,
type Expression,
type Identifier,
type Literal,
type MemberExpression,
NodeType,
type Program,
type UnaryExpression,
} from "./parser";
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type Context = Record<string, any>;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type Functions = Record<string, (...args: any[]) => any>;
/**
* InterpreterState represents the current state of interpretation
* @property context - Variables and values available during evaluation
* @property functions - Functions available for calling during evaluation
*/
interface InterpreterState {
context: Context;
functions: Functions;
}
/**
* Creates a new interpreter state with the provided context and functions
* @param context - Initial variable context
* @param functions - Available functions
* @returns A new interpreter state
*/
export const createInterpreterState = (
context: Context = {},
functions: Functions = {},
): InterpreterState => {
return {
context,
functions,
};
};
/**
* Evaluates an AST and returns the result
* @param ast - The AST to evaluate
* @param state - Current interpreter state
* @param context - Optional context to override the default context
* @returns The result of evaluation
* @example
* const ast = parse(tokens);
* const result = evaluate(ast, state);
*/
export const evaluateAst = (
ast: Program,
state: InterpreterState,
context?: Context,
): unknown => {
let evaluationState = state;
if (context) {
evaluationState = {
...state,
context: { ...state.context, ...context },
};
}
// Define all evaluation functions within the closure to access evaluationState
/**
* Evaluates a literal value
* @param node - Literal node
* @returns The literal value
* @example "hello" → "hello"
* @example 42 → 42
*/
const evaluateLiteral = (node: Literal): number | string | boolean | null => {
return node.value;
};
/**
* Evaluates an identifier by looking up its value in the context
* @param node - Identifier node
* @returns The value from context
* @example data → context.data
*/
const evaluateIdentifier = (node: Identifier): unknown => {
if (!(node.name in evaluationState.context)) {
throw new ExpressionError(`Undefined variable: ${node.name}`);
}
return evaluationState.context[node.name];
};
/**
* Evaluates a member expression (property access)
* @param node - MemberExpression node
* @returns The accessed property value
* @example data.value → context.data.value
* @example data["value"] → context.data["value"]
*/
const evaluateMemberExpression = (node: MemberExpression): unknown => {
const object = evaluateNode(node.object);
if (object == null) {
throw new ExpressionError("Cannot access property of null or undefined");
}
const property = node.computed
? evaluateNode(node.property)
: (node.property as Identifier).name;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return (object as any)[property as string | number];
};
/**
* Evaluates a function call
* @param node - CallExpression node
* @returns The function result
* @example @sum(1, 2) → functions.sum(1, 2)
*/
const evaluateCallExpression = (node: CallExpression): unknown => {
const func = evaluationState.functions[node.callee.name];
if (!func) {
throw new ExpressionError(`Undefined function: ${node.callee.name}`);
}
const args = node.arguments.map((arg) => evaluateNode(arg));
return func(...args);
};
/**
* Evaluates a binary expression
* @param node - BinaryExpression node
* @returns The result of the binary operation
* @example a + b → context.a + context.b
* @example x > y → context.x > context.y
*/
const evaluateBinaryExpression = (node: BinaryExpression): unknown => {
// Implement short-circuit evaluation for logical operators
if (node.operator === "&&") {
const left = evaluateNode(node.left);
// If left side is falsy, return it immediately without evaluating right side
if (!left) return left;
// Otherwise evaluate and return right side
return evaluateNode(node.right);
}
if (node.operator === "||") {
const left = evaluateNode(node.left);
// If left side is truthy, return it immediately without evaluating right side
if (left) return left;
// Otherwise evaluate and return right side
return evaluateNode(node.right);
}
// For other operators, evaluate both sides normally
const left = evaluateNode(node.left);
const right = evaluateNode(node.right);
switch (node.operator) {
case "+":
// For addition, handle both numeric addition and string concatenation
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return (left as any) + (right as any);
case "-":
return (left as number) - (right as number);
case "*":
return (left as number) * (right as number);
case "/":
return (left as number) / (right as number);
case "%":
return (left as number) % (right as number);
case "===":
return left === right;
case "!==":
return left !== right;
case ">":
return (left as number) > (right as number);
case ">=":
return (left as number) >= (right as number);
case "<":
return (left as number) < (right as number);
case "<=":
return (left as number) <= (right as number);
default:
throw new ExpressionError(`Unknown operator: ${node.operator}`);
}
};
/**
* Evaluates a unary expression
* @param node - UnaryExpression node
* @returns The result of the unary operation
* @example !valid → !context.valid
* @example -num → -context.num
*/
const evaluateUnaryExpression = (node: UnaryExpression): unknown => {
const argument = evaluateNode(node.argument);
if (node.prefix) {
switch (node.operator) {
case "!":
return !argument;
case "-":
if (typeof argument !== "number") {
throw new ExpressionError(
`Cannot apply unary - to non-number: ${argument}`,
);
}
return -argument;
default:
throw new ExpressionError(`Unknown operator: ${node.operator}`);
}
}
// Currently we don't support postfix operators
throw new ExpressionError(
`Postfix operators are not supported: ${node.operator}`,
);
};
/**
* Evaluates a conditional (ternary) expression
* @param node - ConditionalExpression node
* @returns The result of the conditional expression
* @example a ? b : c → context.a ? context.b : context.c
*/
const evaluateConditionalExpression = (
node: ConditionalExpression,
): unknown => {
const test = evaluateNode(node.test);
return test ? evaluateNode(node.consequent) : evaluateNode(node.alternate);
};
/**
* Evaluates a single AST node
* @param node - The node to evaluate
* @returns The result of evaluation
*/
const evaluateNode = (node: Expression): unknown => {
switch (node.type) {
case NodeType.Literal:
return evaluateLiteral(node);
case NodeType.Identifier:
return evaluateIdentifier(node);
case NodeType.MemberExpression:
return evaluateMemberExpression(node);
case NodeType.CallExpression:
return evaluateCallExpression(node);
case NodeType.BinaryExpression:
return evaluateBinaryExpression(node);
case NodeType.UnaryExpression:
return evaluateUnaryExpression(node);
case NodeType.ConditionalExpression:
return evaluateConditionalExpression(node);
default:
throw new ExpressionError(
`Evaluation error: Unsupported node type: ${(node as Expression).type}`,
);
}
};
// Start evaluation with the root node
return evaluateNode(ast.body);
};