@tsplc/compiler 0.5.4 → 0.5.6
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.
- package/dist/bun-runtime.js +14 -2
- package/dist/bun-runtime.js.map +4 -4
- package/dist/codesys.js +46 -2
- package/dist/codesys.js.map +3 -3
- package/dist/collections.js +395 -0
- package/dist/collections.js.map +10 -0
- package/dist/elaborate-capture.js +64 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +5345 -1074
- package/dist/index.js.map +32 -25
- package/dist/ir.js +122 -1
- package/dist/ir.js.map +6 -6
- package/package.json +14 -9
- package/src/codesys/numeric.ts +61 -0
- package/src/frontend/control-flow.ts +752 -0
- package/src/frontend/coroutines.ts +171 -7
- package/src/frontend/elaborate/array-searches.ts +216 -0
- package/src/frontend/elaborate/assignments.ts +352 -0
- package/src/frontend/elaborate/calls.ts +488 -49
- package/src/frontend/elaborate/capture-protocol.ts +19 -0
- package/src/frontend/elaborate/capture.ts +82 -3
- package/src/frontend/elaborate/dialect.ts +118 -4
- package/src/frontend/elaborate/expressions.ts +927 -80
- package/src/frontend/elaborate/filtered-arrays.ts +607 -0
- package/src/frontend/elaborate/frame.ts +44 -1
- package/src/frontend/elaborate/generics.ts +137 -0
- package/src/frontend/elaborate/graph.ts +91 -0
- package/src/frontend/elaborate/imports.ts +14 -15
- package/src/frontend/elaborate/index.ts +30 -5
- package/src/frontend/elaborate/initializers.ts +15 -1
- package/src/frontend/elaborate/iterators.ts +1677 -0
- package/src/frontend/elaborate/layouts.ts +41 -19
- package/src/frontend/elaborate/model.ts +64 -8
- package/src/frontend/elaborate/references.ts +749 -0
- package/src/frontend/elaborate/specialize.ts +459 -29
- package/src/frontend/elaborate/statements.ts +552 -133
- package/src/frontend/elaborate/storage.ts +151 -10
- package/src/frontend/elaborate/values.ts +72 -0
- package/src/frontend/elaborate/writes.ts +302 -1
- package/src/frontend/intrinsics.ts +32 -0
- package/src/frontend/module-graph.ts +2 -1
- package/src/frontend/positions.ts +12 -5
- package/src/frontend/sdk.ts +7 -1
- package/src/frontend/typescript.ts +5 -1
- package/src/ir/core-model.ts +2 -6
- package/src/ir/fold.ts +30 -1
- package/src/ir/index.ts +5 -0
- package/src/ir/model.ts +53 -2
- package/src/ir/scalars.ts +126 -0
- package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
- package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
- package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
- package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
- package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
- package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
- package/stdlib/collections.ts +514 -0
- package/types/index.d.ts +18 -0
|
@@ -43,6 +43,12 @@ const terminated = (node: ts.Node) =>
|
|
|
43
43
|
ts.isThrowStatement(node) ||
|
|
44
44
|
ts.isBreakOrContinueStatement(node);
|
|
45
45
|
|
|
46
|
+
/** The operator a compound update in a loop header applies. */
|
|
47
|
+
const compoundSteps = new Map<ts.SyntaxKind, string>([
|
|
48
|
+
[ts.SyntaxKind.PlusEqualsToken, "+"],
|
|
49
|
+
[ts.SyntaxKind.MinusEqualsToken, "-"],
|
|
50
|
+
]);
|
|
51
|
+
|
|
46
52
|
/** The call an await waits on. */
|
|
47
53
|
const awaitedCall = (node: ts.Node) =>
|
|
48
54
|
ts.isAwaitExpression(node) ? node.expression : node;
|
|
@@ -76,8 +82,12 @@ class Coroutine {
|
|
|
76
82
|
private readonly returnType: string;
|
|
77
83
|
private readonly resultAggregate: boolean;
|
|
78
84
|
private readonly fields = new Map<ts.Symbol, Field>();
|
|
85
|
+
/** The index each for…of that suspends has reached in its array. */
|
|
86
|
+
private readonly positionFields = new Map<ts.ForOfStatement, string>();
|
|
79
87
|
private readonly parameters: (Field & { field: string })[];
|
|
80
88
|
private readonly states = new Map<number, Mapped>();
|
|
89
|
+
/** The loops that suspend, innermost last, with the steps their break and continue go to. */
|
|
90
|
+
private readonly loops: { breakStep: number; continueStep: number }[] = [];
|
|
81
91
|
private readonly yielding = new Set<number>();
|
|
82
92
|
private readonly steps: Step[] = [];
|
|
83
93
|
private readonly children: string[] = [];
|
|
@@ -196,15 +206,16 @@ class Coroutine {
|
|
|
196
206
|
|
|
197
207
|
/** Moves the routine's locals and caught errors onto the block, and rejects what cannot suspend. */
|
|
198
208
|
private collect(node: ts.Node): void {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
);
|
|
205
|
-
}
|
|
209
|
+
// A loop that doesn't suspend runs within one step, with its own locals.
|
|
210
|
+
if (
|
|
211
|
+
ts.isIterationStatement(node, false) &&
|
|
212
|
+
!containsAwait(node, this.file.source)
|
|
213
|
+
) {
|
|
206
214
|
return;
|
|
207
215
|
}
|
|
216
|
+
if (ts.isForOfStatement(node)) {
|
|
217
|
+
this.collectForOf(node);
|
|
218
|
+
}
|
|
208
219
|
if (ts.isFunctionLike(node) && node !== this.fn) {
|
|
209
220
|
this.context.fail(
|
|
210
221
|
node,
|
|
@@ -254,6 +265,29 @@ class Coroutine {
|
|
|
254
265
|
ts.forEachChild(node, (child) => this.collect(child));
|
|
255
266
|
}
|
|
256
267
|
|
|
268
|
+
/** A for…of that suspends keeps its position in the array, and its element, between steps. */
|
|
269
|
+
private collectForOf(node: ts.ForOfStatement): void {
|
|
270
|
+
const declaration = ts.isVariableDeclarationList(node.initializer)
|
|
271
|
+
? node.initializer.declarations[0]
|
|
272
|
+
: undefined;
|
|
273
|
+
if (!declaration || !ts.isIdentifier(declaration.name)) {
|
|
274
|
+
this.context.fail(
|
|
275
|
+
node.initializer,
|
|
276
|
+
"A for…of that awaits declares one named element.",
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
const { checker } = this.context;
|
|
280
|
+
const element = checker.getTypeAtLocation(declaration.name);
|
|
281
|
+
this.fields.set(this.context.symbol(declaration.name)!, {
|
|
282
|
+
name: `local${this.fields.size}`,
|
|
283
|
+
type: declaration.type
|
|
284
|
+
? this.text(declaration.type)
|
|
285
|
+
: checker.typeToString(element),
|
|
286
|
+
aggregate: Boolean(element.flags & ts.TypeFlags.Object),
|
|
287
|
+
});
|
|
288
|
+
this.positionFields.set(node, `position${this.positionFields.size}`);
|
|
289
|
+
}
|
|
290
|
+
|
|
257
291
|
/** An unannotated local's type: BOOL for a boolean literal, or the result of the routine it awaits. */
|
|
258
292
|
private inferredType(
|
|
259
293
|
node: ts.VariableDeclaration,
|
|
@@ -767,6 +801,20 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
767
801
|
if (ts.isTryStatement(statement)) {
|
|
768
802
|
return this.tryStatement(statement, next, reject);
|
|
769
803
|
}
|
|
804
|
+
if (
|
|
805
|
+
ts.isIterationStatement(statement, false) &&
|
|
806
|
+
containsAwait(statement, this.file.source)
|
|
807
|
+
) {
|
|
808
|
+
return this.loop(statement, next, reject);
|
|
809
|
+
}
|
|
810
|
+
const loop = this.loops.at(-1);
|
|
811
|
+
if (loop && ts.isBreakOrContinueStatement(statement) && !statement.label) {
|
|
812
|
+
return this.state(
|
|
813
|
+
`this.step = ${ts.isBreakStatement(statement) ? loop.breakStep : loop.continueStep};`,
|
|
814
|
+
statement,
|
|
815
|
+
"statement",
|
|
816
|
+
);
|
|
817
|
+
}
|
|
770
818
|
if (ts.isThrowStatement(statement)) {
|
|
771
819
|
if (
|
|
772
820
|
!checker.isTypeAssignableTo(
|
|
@@ -820,6 +868,121 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
820
868
|
);
|
|
821
869
|
}
|
|
822
870
|
|
|
871
|
+
/**
|
|
872
|
+
* A loop that suspends runs as steps: its test, its body's steps and its update, which leads back to
|
|
873
|
+
* the test. Each scan takes a bounded number of steps, so its iterations spread across scans.
|
|
874
|
+
*/
|
|
875
|
+
private loop(
|
|
876
|
+
statement: ts.IterationStatement,
|
|
877
|
+
next: number,
|
|
878
|
+
reject: Reject,
|
|
879
|
+
): number {
|
|
880
|
+
const test = this.reserve(statement, "statement");
|
|
881
|
+
const update =
|
|
882
|
+
ts.isForStatement(statement) && statement.incrementor
|
|
883
|
+
? this.state(
|
|
884
|
+
mapped`${this.updateText(statement.incrementor)}; this.step = ${test};`,
|
|
885
|
+
statement.incrementor,
|
|
886
|
+
"statement",
|
|
887
|
+
)
|
|
888
|
+
: ts.isForOfStatement(statement)
|
|
889
|
+
? this.state(
|
|
890
|
+
`this.${this.positionFields.get(statement)} = this.${this.positionFields.get(statement)} + 1; this.step = ${test};`,
|
|
891
|
+
statement,
|
|
892
|
+
"statement",
|
|
893
|
+
)
|
|
894
|
+
: test;
|
|
895
|
+
this.loops.push({ breakStep: next, continueStep: update });
|
|
896
|
+
const body = this.block([statement.statement], update, reject);
|
|
897
|
+
this.loops.pop();
|
|
898
|
+
this.fill(test, statement, this.loopTest(statement, body, next));
|
|
899
|
+
if (ts.isDoStatement(statement)) {
|
|
900
|
+
return body;
|
|
901
|
+
}
|
|
902
|
+
if (ts.isForStatement(statement) && statement.initializer) {
|
|
903
|
+
const start = ts.isVariableDeclarationList(statement.initializer)
|
|
904
|
+
? statement.initializer.declarations.reduceRight(
|
|
905
|
+
(following, declaration) =>
|
|
906
|
+
this.localInitializer(declaration, following, reject),
|
|
907
|
+
test,
|
|
908
|
+
)
|
|
909
|
+
: this.state(
|
|
910
|
+
mapped`${this.rewrite(statement.initializer)}; this.step = ${test};`,
|
|
911
|
+
statement.initializer,
|
|
912
|
+
"statement",
|
|
913
|
+
);
|
|
914
|
+
return start;
|
|
915
|
+
}
|
|
916
|
+
if (ts.isForOfStatement(statement)) {
|
|
917
|
+
return this.state(
|
|
918
|
+
`this.${this.positionFields.get(statement)} = 0; this.step = ${test};`,
|
|
919
|
+
statement,
|
|
920
|
+
"statement",
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
return test;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/** A loop's update as an assignment, the one form a step's statement takes: `i++` is `i = i + 1`. */
|
|
927
|
+
private updateText(incrementor: ts.Expression): Mapped {
|
|
928
|
+
if (
|
|
929
|
+
(ts.isPrefixUnaryExpression(incrementor) ||
|
|
930
|
+
ts.isPostfixUnaryExpression(incrementor)) &&
|
|
931
|
+
(incrementor.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
932
|
+
incrementor.operator === ts.SyntaxKind.MinusMinusToken)
|
|
933
|
+
) {
|
|
934
|
+
const target = this.rewrite(incrementor.operand);
|
|
935
|
+
return mapped`${target} = ${target} ${incrementor.operator === ts.SyntaxKind.PlusPlusToken ? "+" : "-"} 1`;
|
|
936
|
+
}
|
|
937
|
+
const compound =
|
|
938
|
+
ts.isBinaryExpression(incrementor) &&
|
|
939
|
+
compoundSteps.get(incrementor.operatorToken.kind);
|
|
940
|
+
if (ts.isBinaryExpression(incrementor) && compound) {
|
|
941
|
+
const target = this.rewrite(incrementor.left);
|
|
942
|
+
return mapped`${target} = ${target} ${compound} ${this.rewrite(incrementor.right)}`;
|
|
943
|
+
}
|
|
944
|
+
return this.rewrite(incrementor);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/** The step a loop takes before each iteration: into its body while it goes on, else past it. */
|
|
948
|
+
private loopTest(
|
|
949
|
+
statement: ts.IterationStatement,
|
|
950
|
+
body: number,
|
|
951
|
+
next: number,
|
|
952
|
+
): Mapped {
|
|
953
|
+
if (ts.isForOfStatement(statement)) {
|
|
954
|
+
const position = `this.${this.positionFields.get(statement)}`;
|
|
955
|
+
const array = this.rewrite(statement.expression);
|
|
956
|
+
const declaration = ts.isVariableDeclarationList(statement.initializer)
|
|
957
|
+
? statement.initializer.declarations[0]
|
|
958
|
+
: undefined;
|
|
959
|
+
const field =
|
|
960
|
+
declaration && this.fields.get(this.context.symbol(declaration.name)!);
|
|
961
|
+
return mapped`if (${position} < ${array}.length) { this.${field?.name ?? ""} = ${copyValue(mapped`${array}[${position}]`, field?.aggregate ?? false)}; this.step = ${body}; } else { this.step = ${next}; }`;
|
|
962
|
+
}
|
|
963
|
+
const condition = ts.isForStatement(statement)
|
|
964
|
+
? statement.condition
|
|
965
|
+
: ts.isWhileStatement(statement) || ts.isDoStatement(statement)
|
|
966
|
+
? statement.expression
|
|
967
|
+
: undefined;
|
|
968
|
+
if (!condition) {
|
|
969
|
+
return mapped`this.step = ${body};`;
|
|
970
|
+
}
|
|
971
|
+
return mapped`if (${this.rewrite(condition)}) this.step = ${body}; else this.step = ${next};`;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/** A step whose body is written once what follows it is known, as a loop's test is. */
|
|
975
|
+
private reserve(origin: ts.Node, kind: Step["kind"]): number {
|
|
976
|
+
return this.state("", origin, kind);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
private fill(step: number, origin: ts.Node, body: Part): void {
|
|
980
|
+
if (!this.states.has(step)) {
|
|
981
|
+
throw new Error(`Unknown step ${step}.`);
|
|
982
|
+
}
|
|
983
|
+
this.states.set(step, from(origin, body));
|
|
984
|
+
}
|
|
985
|
+
|
|
823
986
|
/** A failure inside `try` moves to the catch steps with the error code; a cancel still fails. */
|
|
824
987
|
private tryStatement(
|
|
825
988
|
statement: ts.TryStatement,
|
|
@@ -922,6 +1085,7 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
922
1085
|
return mapped`${aliases}\n${exported}class ${this.text(fn.name)} extends FunctionBlock<{cancel:BOOL}> {
|
|
923
1086
|
private step:UINT=0; private cancelling:BOOL=false; private busy:BOOL=false; private done:BOOL=false; private error:UDINT=0; private value:${returnType}=defaultValue<${returnType}>();
|
|
924
1087
|
${[...this.fields.values()].map((f) => `private ${f.name}:${f.type}=defaultValue<${f.type}>();`).join("\n")}
|
|
1088
|
+
${[...this.positionFields.values()].map((name) => `private ${name}:DINT=0;`).join("\n")}
|
|
925
1089
|
${this.children.join("\n")}
|
|
926
1090
|
start(${parameters.map((p) => `${p.name}:${p.type}`).join(",")}):BOOL {if(this.busy || this.done)return false; ${parameters.map((p) => `this.${p.field} = ${copyValue(p.name, p.aggregate)};`).join(" ")} ${this.resets.join(" ")} this.value = defaultValue<${returnType}>(); this.error=0;this.cancelling=false;this.busy=true;this.done=false;this.step=${entry};return true;}
|
|
927
1091
|
reset():void {${this.resets.join(" ")} this.busy=false;this.cancelling=false;this.done=false;this.error=0;this.step=0;}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import { joined, type Mapped, mapped } from "../positions";
|
|
3
|
+
import type { DialectText } from "./dialect";
|
|
4
|
+
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
5
|
+
import type { Frame } from "./frame";
|
|
6
|
+
import { constant, SpecializeError, type Value } from "./values";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Searches and writes over an array setup built, decided at run time. Setup fixed the array's
|
|
10
|
+
* length, so each runs as one test or store per element, in the order JavaScript visits them.
|
|
11
|
+
*/
|
|
12
|
+
export class ArraySearches {
|
|
13
|
+
private readonly checker: ts.TypeChecker;
|
|
14
|
+
private readonly dialect: DialectText;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
context: SpecializeContext,
|
|
18
|
+
private readonly expressions: ExpressionSpecializer,
|
|
19
|
+
) {
|
|
20
|
+
this.checker = context.checker;
|
|
21
|
+
this.dialect = context.dialect;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `includes(target)`: whether any element equals it. */
|
|
25
|
+
includes(
|
|
26
|
+
items: readonly Value[],
|
|
27
|
+
node: ts.CallExpression,
|
|
28
|
+
frame: Frame,
|
|
29
|
+
): Value {
|
|
30
|
+
const target = this.target(node, frame);
|
|
31
|
+
const tests: (string | Mapped)[] = [];
|
|
32
|
+
for (const item of items) {
|
|
33
|
+
const test = this.expressions.equality(item, target, { negate: false });
|
|
34
|
+
if (test.kind === "const") {
|
|
35
|
+
if (test.value) {
|
|
36
|
+
return constant(true);
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
tests.push(this.dialect.condition(test, node));
|
|
41
|
+
}
|
|
42
|
+
if (!tests.length) {
|
|
43
|
+
return constant(false);
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
kind: "code",
|
|
47
|
+
text: mapped`(${joined(tests, " || ")})`,
|
|
48
|
+
declared: this.checker.getBooleanType(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `indexOf(target)` and `lastIndexOf(target)`: the first or last index of an equal element, or -1. */
|
|
53
|
+
indexOf(
|
|
54
|
+
items: readonly Value[],
|
|
55
|
+
node: ts.CallExpression,
|
|
56
|
+
frame: Frame,
|
|
57
|
+
{ last }: { last: boolean },
|
|
58
|
+
): Value {
|
|
59
|
+
const target = this.target(node, frame);
|
|
60
|
+
return this.firstIndex(items, frame, { last }, (item) =>
|
|
61
|
+
this.expressions.equality(item, target, { negate: false }),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `findIndex(test)` and `findLastIndex(test)`: the test runs on elements until one passes. */
|
|
66
|
+
findIndex(
|
|
67
|
+
items: readonly Value[],
|
|
68
|
+
frame: Frame,
|
|
69
|
+
{ last }: { last: boolean },
|
|
70
|
+
apply: (args: Value[]) => Value,
|
|
71
|
+
): Value {
|
|
72
|
+
return this.firstIndex(items, frame, { last }, (item, index) =>
|
|
73
|
+
apply([item, constant(index)]),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `find(test)` and `findLast(test)` over values: the element that passed, or undefined. */
|
|
78
|
+
find(
|
|
79
|
+
items: readonly Value[],
|
|
80
|
+
node: ts.CallExpression,
|
|
81
|
+
frame: Frame,
|
|
82
|
+
{ last }: { last: boolean },
|
|
83
|
+
apply: (args: Value[]) => Value,
|
|
84
|
+
): Value {
|
|
85
|
+
const declared = this.checker.getTypeAtLocation(node);
|
|
86
|
+
const element = this.checker.getNonNullableType(declared);
|
|
87
|
+
const found = frame.emitter.temporary();
|
|
88
|
+
const result = frame.emitter.temporary();
|
|
89
|
+
frame.emitter.line(`let ${found}: boolean = false;`);
|
|
90
|
+
frame.emitter.line(
|
|
91
|
+
mapped`let ${result}: ${this.expressions.localType(declared)} = ${this.dialect.assignmentText(constant(undefined), declared)};`,
|
|
92
|
+
);
|
|
93
|
+
for (const [index, item] of ordered(items, { last })) {
|
|
94
|
+
if (item.kind === "static" || item.kind === "function") {
|
|
95
|
+
throw new SpecializeError(
|
|
96
|
+
node,
|
|
97
|
+
"find picks one of the objects setup built, which scan code can't hold; use findIndex and index the array.",
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
frame.emitter.open(`if (!${found}) {`);
|
|
101
|
+
const test = apply([item, constant(index)]);
|
|
102
|
+
if (test.kind !== "const" || test.value) {
|
|
103
|
+
const passed =
|
|
104
|
+
test.kind === "const" ? "true" : this.dialect.condition(test, node);
|
|
105
|
+
frame.emitter.open(mapped`if (${passed}) {`);
|
|
106
|
+
frame.emitter.line(
|
|
107
|
+
mapped`${result} = ${this.dialect.wrapDefined(this.dialect.code(item, element), declared)};`,
|
|
108
|
+
);
|
|
109
|
+
frame.emitter.line(`${found} = true;`);
|
|
110
|
+
frame.emitter.close();
|
|
111
|
+
}
|
|
112
|
+
frame.emitter.close();
|
|
113
|
+
}
|
|
114
|
+
return { kind: "code", text: result, declared, place: true };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** `fill(value, start?, end?)` stores the value in each element in range and is worth the array. */
|
|
118
|
+
fill(
|
|
119
|
+
receiver: Value,
|
|
120
|
+
items: readonly Value[],
|
|
121
|
+
node: ts.CallExpression,
|
|
122
|
+
frame: Frame,
|
|
123
|
+
): Value {
|
|
124
|
+
const [valueNode, startNode, endNode] = node.arguments;
|
|
125
|
+
if (!valueNode) {
|
|
126
|
+
throw new SpecializeError(node, "fill needs a value.");
|
|
127
|
+
}
|
|
128
|
+
const value = this.expressions.held(
|
|
129
|
+
this.expressions.expression(valueNode, frame),
|
|
130
|
+
frame,
|
|
131
|
+
);
|
|
132
|
+
const [start, end] = [startNode, endNode].map((bound) =>
|
|
133
|
+
bound ? this.constantIndex(bound, items.length, frame) : undefined,
|
|
134
|
+
);
|
|
135
|
+
for (const item of items.slice(start ?? 0, end ?? items.length)) {
|
|
136
|
+
this.expressions.assignments.storeValue(item, value, node, frame);
|
|
137
|
+
}
|
|
138
|
+
return receiver;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `at(index)` with an index setup fixed, counting back from the end when negative. */
|
|
142
|
+
at(items: readonly Value[], node: ts.CallExpression, frame: Frame): Value {
|
|
143
|
+
const [indexNode] = node.arguments;
|
|
144
|
+
if (!indexNode) {
|
|
145
|
+
throw new SpecializeError(node, "at needs an index.");
|
|
146
|
+
}
|
|
147
|
+
const index = this.constantIndex(indexNode, items.length, frame);
|
|
148
|
+
return items[index] ?? constant(undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The index of the first element whose test passes, or -1; tests stop once one has. */
|
|
152
|
+
private firstIndex(
|
|
153
|
+
items: readonly Value[],
|
|
154
|
+
frame: Frame,
|
|
155
|
+
{ last }: { last: boolean },
|
|
156
|
+
test: (item: Value, index: number) => Value,
|
|
157
|
+
): Value {
|
|
158
|
+
const result = frame.emitter.temporary();
|
|
159
|
+
frame.emitter.line(`let ${result}: number = -1;`);
|
|
160
|
+
for (const [index, item] of ordered(items, { last })) {
|
|
161
|
+
frame.emitter.open(`if (${result} === -1) {`);
|
|
162
|
+
const passed = test(item, index);
|
|
163
|
+
if (passed.kind === "const") {
|
|
164
|
+
if (passed.value) {
|
|
165
|
+
frame.emitter.line(`${result} = ${index};`);
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
frame.emitter.line(
|
|
169
|
+
mapped`if (${this.dialect.condition(passed, frame.emitter.origin)}) { ${result} = ${index}; }`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
frame.emitter.close();
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
kind: "code",
|
|
176
|
+
text: result,
|
|
177
|
+
declared: this.checker.getNumberType(),
|
|
178
|
+
place: true,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private target(node: ts.CallExpression, frame: Frame): Value {
|
|
183
|
+
const [argument] = node.arguments;
|
|
184
|
+
if (!argument) {
|
|
185
|
+
throw new SpecializeError(node, "Pass the value to look for.");
|
|
186
|
+
}
|
|
187
|
+
return this.expressions.held(
|
|
188
|
+
this.expressions.expression(argument, frame),
|
|
189
|
+
frame,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private constantIndex(
|
|
194
|
+
node: ts.Expression,
|
|
195
|
+
length: number,
|
|
196
|
+
frame: Frame,
|
|
197
|
+
): number {
|
|
198
|
+
const value = this.expressions.expression(node, frame);
|
|
199
|
+
if (value.kind !== "const" || typeof value.value !== "number") {
|
|
200
|
+
throw new SpecializeError(
|
|
201
|
+
node,
|
|
202
|
+
"Index with a value setup fixed; the element is chosen when the program compiles.",
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return value.value < 0 ? length + value.value : value.value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Elements with their indexes, first to last or, for the `last` searches, last to first. */
|
|
210
|
+
const ordered = (
|
|
211
|
+
items: readonly Value[],
|
|
212
|
+
{ last }: { last: boolean },
|
|
213
|
+
): [number, Value][] => {
|
|
214
|
+
const indexed = [...items.entries()];
|
|
215
|
+
return last ? indexed.reverse() : indexed;
|
|
216
|
+
};
|