@plasius/learning-runtime 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +169 -2
  3. package/dist/chunk-2YWUDCGJ.js +189 -0
  4. package/dist/chunk-2YWUDCGJ.js.map +1 -0
  5. package/dist/chunk-XDTWUCFP.js +213 -0
  6. package/dist/chunk-XDTWUCFP.js.map +1 -0
  7. package/dist/cpp.cjs +563 -0
  8. package/dist/cpp.cjs.map +1 -0
  9. package/dist/cpp.d.cts +33 -0
  10. package/dist/cpp.d.ts +33 -0
  11. package/dist/cpp.js +535 -0
  12. package/dist/cpp.js.map +1 -0
  13. package/dist/index.cjs +244 -0
  14. package/dist/index.cjs.map +1 -0
  15. package/dist/index.d.cts +1 -0
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.js +17 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/javascript.cjs +226 -0
  20. package/dist/javascript.cjs.map +1 -0
  21. package/dist/javascript.d.cts +45 -0
  22. package/dist/javascript.d.ts +45 -0
  23. package/dist/javascript.js +13 -0
  24. package/dist/javascript.js.map +1 -0
  25. package/dist/javascript.worker.cjs +252 -0
  26. package/dist/javascript.worker.cjs.map +1 -0
  27. package/dist/javascript.worker.d.cts +2 -0
  28. package/dist/javascript.worker.d.ts +2 -0
  29. package/dist/javascript.worker.js +50 -0
  30. package/dist/javascript.worker.js.map +1 -0
  31. package/dist/maze.cjs +242 -0
  32. package/dist/maze.cjs.map +1 -0
  33. package/dist/maze.d.cts +85 -0
  34. package/dist/maze.d.ts +85 -0
  35. package/dist/maze.js +17 -0
  36. package/dist/maze.js.map +1 -0
  37. package/dist/server.cjs +232 -0
  38. package/dist/server.cjs.map +1 -0
  39. package/dist/server.d.cts +25 -0
  40. package/dist/server.d.ts +25 -0
  41. package/dist/server.js +149 -0
  42. package/dist/server.js.map +1 -0
  43. package/dist/web.cjs +600 -0
  44. package/dist/web.cjs.map +1 -0
  45. package/dist/web.d.cts +80 -0
  46. package/dist/web.d.ts +80 -0
  47. package/dist/web.js +513 -0
  48. package/dist/web.js.map +1 -0
  49. package/package.json +115 -4
package/dist/cpp.d.cts ADDED
@@ -0,0 +1,33 @@
1
+ type CppValue = number | boolean | string;
2
+ declare class CppProjectError extends Error {
3
+ readonly code: "INVALID_SOURCE" | "INVALID_INPUT" | "BUDGET_EXCEEDED" | "EXECUTION_FAILED" | "CLOSED";
4
+ constructor(code: "INVALID_SOURCE" | "INVALID_INPUT" | "BUDGET_EXCEEDED" | "EXECUTION_FAILED" | "CLOSED");
5
+ }
6
+
7
+ type CppOutputType = "number" | "boolean" | "string";
8
+ interface CppProjectOptions {
9
+ outputs: Readonly<Record<string, readonly CppOutputType[]>>;
10
+ signal?: AbortSignal;
11
+ }
12
+ interface CppProjectInput {
13
+ timeMs: number;
14
+ sensors: Readonly<Record<string, CppValue>>;
15
+ }
16
+ interface CppProjectCommand {
17
+ name: string;
18
+ args: CppValue[];
19
+ }
20
+ interface CppProjectResult {
21
+ commands: CppProjectCommand[];
22
+ globals: Record<string, CppValue>;
23
+ }
24
+ interface CppProjectSession {
25
+ readonly closed: boolean;
26
+ run(input: CppProjectInput): CppProjectResult;
27
+ dispose(): void;
28
+ }
29
+ /** Execute the closed C++ simulator subset with primitive input and structured output only. */
30
+ declare function createCppProjectSession(source: string, options: CppProjectOptions): CppProjectSession;
31
+
32
+ export { CppProjectError, createCppProjectSession };
33
+ export type { CppOutputType, CppProjectCommand, CppProjectInput, CppProjectOptions, CppProjectResult, CppProjectSession };
package/dist/cpp.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ type CppValue = number | boolean | string;
2
+ declare class CppProjectError extends Error {
3
+ readonly code: "INVALID_SOURCE" | "INVALID_INPUT" | "BUDGET_EXCEEDED" | "EXECUTION_FAILED" | "CLOSED";
4
+ constructor(code: "INVALID_SOURCE" | "INVALID_INPUT" | "BUDGET_EXCEEDED" | "EXECUTION_FAILED" | "CLOSED");
5
+ }
6
+
7
+ type CppOutputType = "number" | "boolean" | "string";
8
+ interface CppProjectOptions {
9
+ outputs: Readonly<Record<string, readonly CppOutputType[]>>;
10
+ signal?: AbortSignal;
11
+ }
12
+ interface CppProjectInput {
13
+ timeMs: number;
14
+ sensors: Readonly<Record<string, CppValue>>;
15
+ }
16
+ interface CppProjectCommand {
17
+ name: string;
18
+ args: CppValue[];
19
+ }
20
+ interface CppProjectResult {
21
+ commands: CppProjectCommand[];
22
+ globals: Record<string, CppValue>;
23
+ }
24
+ interface CppProjectSession {
25
+ readonly closed: boolean;
26
+ run(input: CppProjectInput): CppProjectResult;
27
+ dispose(): void;
28
+ }
29
+ /** Execute the closed C++ simulator subset with primitive input and structured output only. */
30
+ declare function createCppProjectSession(source: string, options: CppProjectOptions): CppProjectSession;
31
+
32
+ export { CppProjectError, createCppProjectSession };
33
+ export type { CppOutputType, CppProjectCommand, CppProjectInput, CppProjectOptions, CppProjectResult, CppProjectSession };
package/dist/cpp.js ADDED
@@ -0,0 +1,535 @@
1
+ // src/cpp-parser.ts
2
+ var CppProjectError = class extends Error {
3
+ constructor(code) {
4
+ super(`Robot program ${code.toLowerCase().replaceAll("_", " ")}.`);
5
+ this.code = code;
6
+ this.name = "CppProjectError";
7
+ }
8
+ code;
9
+ };
10
+ var TYPES = /* @__PURE__ */ new Set(["int", "double", "bool", "string", "void"]);
11
+ var RESERVED = /* @__PURE__ */ new Set([...TYPES, "const", "if", "else", "while", "for", "return", "break", "continue", "true", "false"]);
12
+ var PRECEDENCE = { "||": 1, "&&": 2, "==": 3, "!=": 3, "<": 4, "<=": 4, ">": 4, ">=": 4, "+": 5, "-": 5, "*": 6, "/": 6, "%": 6 };
13
+ function invalid() {
14
+ throw new CppProjectError("INVALID_SOURCE");
15
+ }
16
+ function tokenize(source) {
17
+ if (typeof source !== "string" || source.length > 32768 || source.includes("\0")) invalid();
18
+ const tokens = [];
19
+ let offset = 0;
20
+ while (offset < source.length) {
21
+ const rest = source.slice(offset);
22
+ if (/^\s/u.test(rest)) {
23
+ offset++;
24
+ continue;
25
+ }
26
+ if (rest.startsWith("//")) {
27
+ const end = source.indexOf("\n", offset);
28
+ offset = end < 0 ? source.length : end + 1;
29
+ continue;
30
+ }
31
+ if (rest.startsWith("/*")) {
32
+ const end = source.indexOf("*/", offset + 2);
33
+ if (end < 0) invalid();
34
+ offset = end + 2;
35
+ continue;
36
+ }
37
+ const number2 = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/u.exec(rest);
38
+ const identifier = /^[A-Za-z_][A-Za-z0-9_]*/u.exec(rest);
39
+ if (number2) {
40
+ tokens.push({ text: number2[0], kind: "literal", value: Number(number2[0]), numberType: /[.eE]/u.test(number2[0]) ? "double" : "int" });
41
+ offset += number2[0].length;
42
+ } else if (identifier) {
43
+ if (identifier[0].length > 64) invalid();
44
+ tokens.push({ text: identifier[0], kind: "identifier" });
45
+ offset += identifier[0].length;
46
+ } else if (rest[0] === '"') {
47
+ const quoted = /^"(?:[^"\\\r\n]|\\.)*"/u.exec(rest);
48
+ if (!quoted) invalid();
49
+ let value;
50
+ try {
51
+ value = JSON.parse(quoted[0]);
52
+ } catch {
53
+ invalid();
54
+ }
55
+ if (typeof value !== "string" || value.length > 256 || value.includes("\0")) invalid();
56
+ tokens.push({ text: quoted[0], kind: "literal", value });
57
+ offset += quoted[0].length;
58
+ } else {
59
+ const operator = /^(?:==|!=|<=|>=|&&|\|\||\+\+|--|\+=|-=|\*=|\/=|%=|[{}();,=+*/%!<>-])/u.exec(rest);
60
+ if (!operator) invalid();
61
+ tokens.push({ text: operator[0], kind: "symbol" });
62
+ offset += operator[0].length;
63
+ }
64
+ if (tokens.length > 4096) invalid();
65
+ }
66
+ tokens.push({ text: "<end>", kind: "symbol" });
67
+ return tokens;
68
+ }
69
+ var Parser = class {
70
+ constructor(tokens) {
71
+ this.tokens = tokens;
72
+ }
73
+ tokens;
74
+ #offset = 0;
75
+ #depth = 0;
76
+ current() {
77
+ return this.tokens[this.#offset];
78
+ }
79
+ is(text) {
80
+ return this.current().text === text;
81
+ }
82
+ take() {
83
+ if (this.is("<end>")) invalid();
84
+ return this.tokens[this.#offset++];
85
+ }
86
+ accept(text) {
87
+ if (!this.is(text)) return false;
88
+ this.take();
89
+ return true;
90
+ }
91
+ expect(text) {
92
+ if (!this.accept(text)) invalid();
93
+ }
94
+ name() {
95
+ const token = this.take();
96
+ if (token.kind !== "identifier" || RESERVED.has(token.text)) invalid();
97
+ return token.text;
98
+ }
99
+ type() {
100
+ const token = this.take();
101
+ if (!TYPES.has(token.text)) invalid();
102
+ return token.text;
103
+ }
104
+ nested(operation) {
105
+ if (++this.#depth > 64) invalid();
106
+ try {
107
+ return operation();
108
+ } finally {
109
+ this.#depth--;
110
+ }
111
+ }
112
+ program() {
113
+ const globals = [];
114
+ const functions = /* @__PURE__ */ new Map();
115
+ const names = /* @__PURE__ */ new Set();
116
+ while (!this.is("<end>")) {
117
+ const constant = this.accept("const");
118
+ const type = this.type();
119
+ const name = this.name();
120
+ if (names.has(name) || names.size >= 64) invalid();
121
+ names.add(name);
122
+ if (this.accept("(")) {
123
+ if (constant || functions.size >= 32) invalid();
124
+ const parameters = [];
125
+ if (!this.is(")")) {
126
+ do {
127
+ const parameterType = this.type();
128
+ const parameterName = this.name();
129
+ if (parameterType === "void" || parameters.some((parameter) => parameter.name === parameterName) || parameters.length >= 8) invalid();
130
+ parameters.push({ type: parameterType, name: parameterName });
131
+ } while (this.accept(","));
132
+ }
133
+ this.expect(")");
134
+ if (!this.is("{")) invalid();
135
+ functions.set(name, { name, type, parameters, body: this.statement() });
136
+ } else {
137
+ if (type === "void") invalid();
138
+ const value = this.accept("=") ? this.expression() : void 0;
139
+ if (constant && !value) invalid();
140
+ this.expect(";");
141
+ globals.push({ kind: "declare", type, constant, name, value });
142
+ }
143
+ }
144
+ const loop = functions.get("loop");
145
+ const setup = functions.get("setup");
146
+ if (!loop || loop.type !== "void" || loop.parameters.length !== 0 || setup && (setup.type !== "void" || setup.parameters.length !== 0)) invalid();
147
+ return { globals, functions };
148
+ }
149
+ declaration() {
150
+ const constant = this.accept("const");
151
+ const type = this.type();
152
+ const name = this.name();
153
+ if (type === "void") invalid();
154
+ const value = this.accept("=") ? this.expression() : void 0;
155
+ if (constant && !value) invalid();
156
+ return { kind: "declare", type, constant, name, value };
157
+ }
158
+ simple() {
159
+ if (this.is("const") || TYPES.has(this.current().text)) return this.declaration();
160
+ const value = this.expression();
161
+ const operator = this.current().text;
162
+ if (["=", "+=", "-=", "*=", "/=", "%=", "++", "--"].includes(operator)) {
163
+ this.take();
164
+ if (value.kind !== "variable") invalid();
165
+ const right = operator === "++" || operator === "--" ? { kind: "literal", value: 1 } : this.expression();
166
+ return { kind: "assign", name: value.name, value: operator === "=" ? right : { kind: "binary", operator: operator[0], left: value, right } };
167
+ }
168
+ return { kind: "expression", value };
169
+ }
170
+ statement() {
171
+ return this.nested(() => {
172
+ if (this.accept("{")) {
173
+ const statements = [];
174
+ while (!this.accept("}")) statements.push(this.statement());
175
+ return { kind: "block", statements };
176
+ }
177
+ if (this.accept("if")) {
178
+ this.expect("(");
179
+ const condition = this.expression();
180
+ this.expect(")");
181
+ const then = this.statement();
182
+ const otherwise = this.accept("else") ? this.statement() : void 0;
183
+ return { kind: "if", condition, then, otherwise };
184
+ }
185
+ if (this.accept("while")) {
186
+ this.expect("(");
187
+ const condition = this.expression();
188
+ this.expect(")");
189
+ return { kind: "while", condition, body: this.statement() };
190
+ }
191
+ if (this.accept("for")) {
192
+ this.expect("(");
193
+ const initial = this.is(";") ? void 0 : this.simple();
194
+ this.expect(";");
195
+ const condition = this.is(";") ? void 0 : this.expression();
196
+ this.expect(";");
197
+ const increment = this.is(")") ? void 0 : this.simple();
198
+ this.expect(")");
199
+ return { kind: "for", initial, condition, increment, body: this.statement() };
200
+ }
201
+ if (this.accept("return")) {
202
+ const value = this.is(";") ? void 0 : this.expression();
203
+ this.expect(";");
204
+ return { kind: "return", value };
205
+ }
206
+ if (this.is("break") || this.is("continue")) {
207
+ const kind = this.take().text;
208
+ this.expect(";");
209
+ return { kind };
210
+ }
211
+ if (this.accept(";")) return { kind: "block", statements: [] };
212
+ const result = this.simple();
213
+ this.expect(";");
214
+ return result;
215
+ });
216
+ }
217
+ expression(minimum = 0) {
218
+ return this.nested(() => {
219
+ let left;
220
+ const token = this.take();
221
+ if (["!", "-", "+"].includes(token.text)) left = { kind: "unary", operator: token.text, value: this.expression(7) };
222
+ else if (token.text === "(") {
223
+ left = this.expression();
224
+ this.expect(")");
225
+ } else if (token.kind === "literal") left = { kind: "literal", value: token.value, numberType: token.numberType };
226
+ else if (token.text === "true" || token.text === "false") left = { kind: "literal", value: token.text === "true" };
227
+ else if (token.kind === "identifier" && !RESERVED.has(token.text)) {
228
+ if (this.accept("(")) {
229
+ const args = [];
230
+ if (!this.is(")")) do {
231
+ if (args.length >= 8) invalid();
232
+ args.push(this.expression());
233
+ } while (this.accept(","));
234
+ this.expect(")");
235
+ left = { kind: "call", name: token.text, args };
236
+ } else left = { kind: "variable", name: token.text };
237
+ } else invalid();
238
+ while ((PRECEDENCE[this.current().text] ?? -1) >= minimum) {
239
+ const operator = this.take().text;
240
+ left = { kind: "binary", operator, left, right: this.expression(PRECEDENCE[operator] + 1) };
241
+ }
242
+ return left;
243
+ });
244
+ }
245
+ };
246
+ function parseCppProject(source) {
247
+ return new Parser(tokenize(source)).program();
248
+ }
249
+
250
+ // src/cpp.ts
251
+ var BUILTINS = /* @__PURE__ */ new Set(["millis", "numberSensor", "boolSensor", "textSensor", "min", "max", "abs"]);
252
+ var NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/u;
253
+ function fail(code = "EXECUTION_FAILED") {
254
+ throw new CppProjectError(code);
255
+ }
256
+ function record(value, maximum) {
257
+ if (typeof value !== "object" || value === null || ![Object.prototype, null].includes(Object.getPrototypeOf(value))) fail("INVALID_INPUT");
258
+ const descriptors = Object.getOwnPropertyDescriptors(value);
259
+ const keys = Reflect.ownKeys(descriptors);
260
+ if (keys.length > maximum) fail("INVALID_INPUT");
261
+ const result = /* @__PURE__ */ Object.create(null);
262
+ for (const key of keys) {
263
+ if (typeof key !== "string" || !NAME.test(key)) fail("INVALID_INPUT");
264
+ const descriptor = descriptors[key];
265
+ if (!Object.hasOwn(descriptor, "value") || !descriptor.enumerable) fail("INVALID_INPUT");
266
+ result[key] = descriptor.value;
267
+ }
268
+ return result;
269
+ }
270
+ function primitive(value) {
271
+ if (typeof value === "boolean") return value;
272
+ if (typeof value === "number" && Number.isFinite(value) && Math.abs(value) <= 1e9) return value;
273
+ if (typeof value === "string" && value.length <= 256 && !value.includes("\0")) return value;
274
+ return fail("INVALID_INPUT");
275
+ }
276
+ function numeric(value) {
277
+ if (!value || value.type !== "int" && value.type !== "double" || typeof value.value !== "number") fail();
278
+ return value.value;
279
+ }
280
+ function boolean(value) {
281
+ if (!value || value.type !== "bool") fail();
282
+ return value.value;
283
+ }
284
+ function number(value, type = "double") {
285
+ if (!Number.isFinite(value) || Math.abs(value) > 1e9) fail();
286
+ return { type, value: type === "int" ? Math.trunc(value) : value };
287
+ }
288
+ function convert(type, value) {
289
+ if (type === "void") {
290
+ if (value !== void 0) fail();
291
+ return void 0;
292
+ }
293
+ if (!value) fail();
294
+ if (type === "int" || type === "double") return number(numeric(value), type);
295
+ if (value.type !== type) fail();
296
+ return value;
297
+ }
298
+ function lookup(scope, name) {
299
+ const binding = scope.bindings.get(name);
300
+ if (binding) return binding;
301
+ if (scope.parent) return lookup(scope.parent, name);
302
+ return fail();
303
+ }
304
+ function outputSignatures(value) {
305
+ const entries = record(value, 16);
306
+ const result = /* @__PURE__ */ new Map();
307
+ for (const [name, signature] of Object.entries(entries)) {
308
+ if (BUILTINS.has(name) || name === "loop" || name === "setup" || !Array.isArray(signature) || Object.getPrototypeOf(signature) !== Array.prototype || signature.length > 8) fail("INVALID_INPUT");
309
+ const descriptors = Object.getOwnPropertyDescriptors(signature);
310
+ if (Reflect.ownKeys(descriptors).length !== signature.length + 1) fail("INVALID_INPUT");
311
+ const types = [];
312
+ for (let index = 0; index < signature.length; index++) {
313
+ const descriptor = descriptors[String(index)];
314
+ if (!descriptor || !Object.hasOwn(descriptor, "value") || !["number", "boolean", "string"].includes(descriptor.value)) fail("INVALID_INPUT");
315
+ types.push(descriptor.value);
316
+ }
317
+ result.set(name, types);
318
+ }
319
+ return result;
320
+ }
321
+ function createCppProjectSession(source, options) {
322
+ const program = parseCppProject(source);
323
+ const configuration = record(options, 2);
324
+ if (!Object.hasOwn(configuration, "outputs") || Object.keys(configuration).some((key) => key !== "outputs" && key !== "signal") || configuration.signal !== void 0 && !(configuration.signal instanceof AbortSignal)) fail("INVALID_INPUT");
325
+ const signal = configuration.signal;
326
+ const outputs = outputSignatures(configuration.outputs);
327
+ for (const name of program.functions.keys()) if (BUILTINS.has(name) || outputs.has(name)) fail("INVALID_SOURCE");
328
+ for (const node of program.globals) if (node.kind === "declare" && (BUILTINS.has(node.name) || outputs.has(node.name))) fail("INVALID_SOURCE");
329
+ const globals = { bindings: /* @__PURE__ */ new Map() };
330
+ let closed = false;
331
+ let initialized = false;
332
+ let lastTime = -1;
333
+ let timeMs = 0;
334
+ let sensors = /* @__PURE__ */ Object.create(null);
335
+ let commands = [];
336
+ let remaining = 2e3;
337
+ let depth = 0;
338
+ function step() {
339
+ if (closed || signal?.aborted) fail("CLOSED");
340
+ if (--remaining < 0) fail("BUDGET_EXCEEDED");
341
+ }
342
+ function declare(scope, name, type, constant, value) {
343
+ if (type === "void" || scope.bindings.has(name) || scope.bindings.size >= 64) fail();
344
+ scope.bindings.set(name, { type, constant, value: convert(type, value) });
345
+ }
346
+ function expression(node, scope) {
347
+ step();
348
+ if (node.kind === "literal") {
349
+ if (typeof node.value === "number") return number(node.value, node.numberType ?? "int");
350
+ return { type: typeof node.value === "boolean" ? "bool" : "string", value: node.value };
351
+ }
352
+ if (node.kind === "variable") return lookup(scope, node.name).value;
353
+ if (node.kind === "call") return call(node.name, node.args.map((argument) => expression(argument, scope)));
354
+ if (node.kind === "unary") {
355
+ const value = expression(node.value, scope);
356
+ if (node.operator === "!") return { type: "bool", value: !boolean(value) };
357
+ return number(numeric(value) * (node.operator === "-" ? -1 : 1), value.type);
358
+ }
359
+ const left = expression(node.left, scope);
360
+ if (node.operator === "&&") return { type: "bool", value: boolean(left) && boolean(expression(node.right, scope)) };
361
+ if (node.operator === "||") return { type: "bool", value: boolean(left) || boolean(expression(node.right, scope)) };
362
+ const right = expression(node.right, scope);
363
+ if (!left || !right) fail();
364
+ if (node.operator === "==" || node.operator === "!=") {
365
+ if (typeof left.value !== typeof right.value) fail();
366
+ const equal = left.value === right.value;
367
+ return { type: "bool", value: node.operator === "==" ? equal : !equal };
368
+ }
369
+ const a = numeric(left);
370
+ const b = numeric(right);
371
+ const type = left.type === "int" && right.type === "int" ? "int" : "double";
372
+ switch (node.operator) {
373
+ case "<":
374
+ return { type: "bool", value: a < b };
375
+ case "<=":
376
+ return { type: "bool", value: a <= b };
377
+ case ">":
378
+ return { type: "bool", value: a > b };
379
+ case ">=":
380
+ return { type: "bool", value: a >= b };
381
+ case "+":
382
+ return number(a + b, type);
383
+ case "-":
384
+ return number(a - b, type);
385
+ case "*":
386
+ return number(a * b, type);
387
+ case "/":
388
+ return number(a / b, type);
389
+ case "%":
390
+ if (type !== "int") fail();
391
+ return number(a % b, "int");
392
+ default:
393
+ return fail();
394
+ }
395
+ }
396
+ function call(name, args) {
397
+ step();
398
+ if (args.some((argument) => !argument)) fail();
399
+ if (name === "millis") {
400
+ if (args.length !== 0) fail();
401
+ return number(timeMs, "int");
402
+ }
403
+ if (name === "numberSensor" || name === "boolSensor" || name === "textSensor") {
404
+ if (args.length !== 1 || args[0].type !== "string") fail();
405
+ const value = sensors[args[0].value];
406
+ const type = name === "numberSensor" ? "number" : name === "boolSensor" ? "boolean" : "string";
407
+ if (typeof value !== type) fail();
408
+ return { type: type === "number" ? "double" : type === "boolean" ? "bool" : "string", value };
409
+ }
410
+ if (name === "min" || name === "max" || name === "abs") {
411
+ if (args.length !== (name === "abs" ? 1 : 2)) fail();
412
+ const values = args.map(numeric);
413
+ return number(name === "abs" ? Math.abs(values[0]) : name === "min" ? Math.min(...values) : Math.max(...values), args.every((arg) => arg.type === "int") ? "int" : "double");
414
+ }
415
+ const signature = outputs.get(name);
416
+ if (signature) {
417
+ if (signature.length !== args.length || signature.some((type, index) => typeof args[index].value !== type)) fail();
418
+ if (commands.length >= 64) fail("BUDGET_EXCEEDED");
419
+ commands.push({ name, args: args.map((arg) => arg.value) });
420
+ return void 0;
421
+ }
422
+ const fn = program.functions.get(name);
423
+ if (!fn || fn.parameters.length !== args.length) fail();
424
+ if (++depth > 16) fail("BUDGET_EXCEEDED");
425
+ try {
426
+ const scope = { bindings: /* @__PURE__ */ new Map(), parent: globals };
427
+ fn.parameters.forEach((parameter, index) => declare(scope, parameter.name, parameter.type, false, args[index]));
428
+ const flow = statement(fn.body, scope);
429
+ if (flow && flow.kind !== "return") fail();
430
+ return convert(fn.type, flow?.kind === "return" ? flow.value : void 0);
431
+ } finally {
432
+ depth--;
433
+ }
434
+ }
435
+ function statement(node, scope) {
436
+ step();
437
+ switch (node.kind) {
438
+ case "block": {
439
+ const child = { bindings: /* @__PURE__ */ new Map(), parent: scope };
440
+ for (const item of node.statements) {
441
+ const flow = statement(item, child);
442
+ if (flow) return flow;
443
+ }
444
+ return void 0;
445
+ }
446
+ case "declare": {
447
+ const initial = node.type === "bool" ? { type: "bool", value: false } : node.type === "string" ? { type: "string", value: "" } : number(0, node.type === "int" ? "int" : "double");
448
+ declare(scope, node.name, node.type, node.constant, node.value ? expression(node.value, scope) : initial);
449
+ return void 0;
450
+ }
451
+ case "assign": {
452
+ const binding = lookup(scope, node.name);
453
+ if (binding.constant) fail();
454
+ binding.value = convert(binding.type, expression(node.value, scope));
455
+ return void 0;
456
+ }
457
+ case "expression":
458
+ expression(node.value, scope);
459
+ return void 0;
460
+ case "if": {
461
+ const branch = boolean(expression(node.condition, scope)) ? node.then : node.otherwise;
462
+ return branch ? statement(branch, { bindings: /* @__PURE__ */ new Map(), parent: scope }) : void 0;
463
+ }
464
+ case "for":
465
+ case "while": {
466
+ const loopScope = { bindings: /* @__PURE__ */ new Map(), parent: scope };
467
+ if (node.kind === "for" && node.initial) statement(node.initial, loopScope);
468
+ while (!node.condition || boolean(expression(node.condition, loopScope))) {
469
+ step();
470
+ const flow = statement(node.body, { bindings: /* @__PURE__ */ new Map(), parent: loopScope });
471
+ if (flow?.kind === "return") return flow;
472
+ if (flow?.kind === "break") return void 0;
473
+ if (node.kind === "for" && node.increment) statement(node.increment, loopScope);
474
+ }
475
+ return void 0;
476
+ }
477
+ case "return":
478
+ return { kind: "return", value: node.value ? expression(node.value, scope) : void 0 };
479
+ case "break":
480
+ case "continue":
481
+ return { kind: node.kind };
482
+ }
483
+ }
484
+ function dispose() {
485
+ closed = true;
486
+ commands = [];
487
+ sensors = /* @__PURE__ */ Object.create(null);
488
+ globals.bindings.clear();
489
+ program.functions.clear();
490
+ program.globals.length = 0;
491
+ signal?.removeEventListener("abort", dispose);
492
+ }
493
+ signal?.addEventListener("abort", dispose, { once: true });
494
+ if (signal?.aborted) dispose();
495
+ return {
496
+ get closed() {
497
+ return closed;
498
+ },
499
+ dispose,
500
+ run(rawInput) {
501
+ try {
502
+ remaining = 2e3;
503
+ step();
504
+ const input = record(rawInput, 2);
505
+ if (Object.keys(input).length !== 2 || !Object.hasOwn(input, "timeMs") || !Object.hasOwn(input, "sensors") || !Number.isSafeInteger(input.timeMs) || input.timeMs < lastTime || input.timeMs < 0 || input.timeMs > 1e9) fail("INVALID_INPUT");
506
+ const snapshot = record(input.sensors, 32);
507
+ sensors = Object.fromEntries(Object.entries(snapshot).map(([name, value]) => [name, primitive(value)]));
508
+ Object.setPrototypeOf(sensors, null);
509
+ timeMs = input.timeMs;
510
+ lastTime = timeMs;
511
+ remaining = 2e3;
512
+ commands = [];
513
+ if (!initialized) {
514
+ for (const global of program.globals) statement(global, globals);
515
+ if (program.functions.has("setup")) call("setup", []);
516
+ initialized = true;
517
+ }
518
+ call("loop", []);
519
+ return {
520
+ commands: commands.map((command) => ({ name: command.name, args: [...command.args] })),
521
+ globals: Object.fromEntries([...globals.bindings].map(([name, binding]) => [name, binding.value.value]))
522
+ };
523
+ } catch (error) {
524
+ dispose();
525
+ if (error instanceof CppProjectError) throw error;
526
+ throw new CppProjectError("EXECUTION_FAILED");
527
+ }
528
+ }
529
+ };
530
+ }
531
+ export {
532
+ CppProjectError,
533
+ createCppProjectSession
534
+ };
535
+ //# sourceMappingURL=cpp.js.map