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
268
269
270
|
import {
type ContinuationExpression,
type PrimitiveOperationExpression,
type ApplicationExpression,
type RecordExpression,
type Program,
type Value,
type RecordExpressionTuple,
type SelectExpression,
} from '@/parser';
import { Environment, type Denotable, type DenotableRecord } from '.';
import {
BadArgumentError,
InvalidStateError,
InvalidType,
NotImplementedError,
type TracingLogger,
} from '@/utils';
import { putBuiltinsOnEnvironemtn } from './builtins';
const evaluateValue = (
value: Value,
env: Environment,
_logger: TracingLogger,
): Denotable => {
if (typeof value === 'string') {
return { type: 'string', value };
}
if ('real' in value) {
return { type: 'real', value: value.real };
}
if ('int' in value) {
return { type: 'int', value: value.int };
}
if ('bool' in value) {
return { type: 'bool', value: value.bool };
}
if ('name' in value) {
return env.get(value.name);
}
throw new InvalidStateError(`Invalid value: ${value}`);
};
const evaluateApplicationExpression = (
{ application }: ApplicationExpression,
env: Environment,
logger: TracingLogger,
): Denotable => {
const { fn, args } = application;
const argValues = args.map(arg =>
evaluateValue(arg, env, logger.createChild('evaluateValue')),
);
return env.apply(fn.name, argValues);
};
const evaluatePrimitiveOperation = (
{ primitiveOperation }: PrimitiveOperationExpression,
env: Environment,
logger: TracingLogger,
) => {
const { opr, operands, resultBindings, continuations } = primitiveOperation;
const operandValues = operands.map(operand =>
evaluateValue(operand, env, logger.createChild('evaluteValue')),
);
const result = env.apply(opr, operandValues);
const continuationEnvironment = env.createChild();
for (const { name } of resultBindings) {
continuationEnvironment.set(name, result);
}
if (result.type === 'bool') {
if (continuations.length > 2) {
throw new BadArgumentError(
`Expected <= 2 continuations for boolean result, got ${continuations.length}`,
);
}
if (continuations.length !== 2) {
logger.warn(
`Expected 2 continuations for boolean result, got ContinuationLength=(${continuations.length})`,
);
return result;
}
const [trueContinuation, falseContinuation] = continuations;
const childLogger = logger.createChild('continuation[true]');
const continuation = result.value ? trueContinuation : falseContinuation;
return evaluteContinuationExpression(
continuation,
continuationEnvironment,
childLogger,
);
}
if (continuations.length > 1) {
throw new BadArgumentError(
`Expected <= 1 continuations for non-boolean result, got ${continuations.length}`,
);
} else if (continuations.length === 0) {
logger.warn(
"Expected 1 continuation for non-boolean result, but there wasn't any. Implicitly returning the result", // technically undefined behavior
);
return result;
}
const [continuation] = continuations;
const childLogger = logger.createChild('continuation');
return evaluteContinuationExpression(
continuation,
continuationEnvironment,
childLogger,
);
};
export const evaluateRecordTuple = (
tuple: RecordExpressionTuple,
env: Environment,
logger: TracingLogger,
): Denotable => {
const { value, accessPath } = tuple;
const record = evaluateValue(value, env, logger.createChild('evaluateValue'));
if (record.type === 'record') {
// TODO: Implement nested records at accessPath
logger.debug(JSON.stringify(accessPath, null, 2));
throw new NotImplementedError('Nested records are not yet supported');
}
return record;
};
export const evaluateRecordExpression = (
{ record }: RecordExpression,
env: Environment,
logger: TracingLogger,
): Denotable => {
const {
records,
address: { name: address },
body,
} = record;
const childEnv = env.createChild();
const recordTuple = records.map(record =>
evaluateRecordTuple(
record,
childEnv,
logger.createChild('evaluateRecordTuple'),
),
);
const length = recordTuple.length;
childEnv.set(address, {
type: 'record',
value: { length, record: recordTuple },
});
return evaluteContinuationExpression(
body,
childEnv,
logger.createChild('evaluteContinuationExpression'),
);
};
export const evaluateSelectExpression = (
{ select }: SelectExpression,
env: Environment,
logger: TracingLogger,
): Denotable => {
const {
index: { int: index },
record,
bind,
continuation,
} = select;
const childEnv = env.createChild();
const recordValue = evaluateValue(
record,
env,
logger.createChild('evaluateValue'),
);
if (recordValue.type !== 'record') {
throw new InvalidType('Expected record type');
}
const { value } = recordValue as { value: DenotableRecord };
const selected = value.record[index];
childEnv.set(bind.name, selected);
return evaluteContinuationExpression(
continuation,
childEnv,
logger.createChild('evaluteContinuationExpression'),
);
};
const evaluteContinuationExpression = (
expr: ContinuationExpression,
env: Environment,
logger: TracingLogger,
): Denotable => {
if ('primitiveOperation' in expr) {
logger.debug('Evaluating primitive operation');
return evaluatePrimitiveOperation(
expr,
env,
logger.createChild('evaluatePrimitiveOperation'),
);
}
if ('application' in expr) {
logger.debug('Evaluating function application');
return evaluateApplicationExpression(
expr,
env,
logger.createChild('evaluateApplicationExpression'),
);
}
if ('record' in expr) {
logger.debug('Evaluating record');
return evaluateRecordExpression(
expr,
env,
logger.createChild('evaluateRecordExpression'),
);
}
if ('select' in expr) {
logger.debug('Evaluating select');
return evaluateSelectExpression(
expr,
env,
logger.createChild('evaluateSelectExpression'),
);
}
if ('offset' in expr) {
throw new NotImplementedError('Continuation offset is not supported yet');
}
if ('switch' in expr) {
throw new NotImplementedError('Continuation switch is not supported yet');
}
if ('fix' in expr) {
throw new NotImplementedError('Continuation fix is not supported yet');
}
throw new InvalidStateError(`Invalid continuation expression: ${expr}`);
};
export const evaluate = async (
ast: Program,
logger: TracingLogger,
): Promise<Denotable> => {
const globalEnvironment = putBuiltinsOnEnvironemtn(
new Environment(logger.createChild('RootEnv')),
);
return ast.reduce((_, continuation, i) => {
const exprLogger = logger.createChild(`statement[${i}]`);
return evaluteContinuationExpression(
continuation as ContinuationExpression,
globalEnvironment,
exprLogger,
);
}, null);
};
|