@tsplc/compiler 0.2.11 → 0.2.13

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/index.js CHANGED
@@ -2396,6 +2396,9 @@ class Layouts {
2396
2396
  }
2397
2397
  return this.text(type, value, hint);
2398
2398
  }
2399
+ fieldText(property) {
2400
+ return this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), this.fieldSample(property), pascal2(property.name));
2401
+ }
2399
2402
  fieldSample(property) {
2400
2403
  for (const node of this.model.nodes) {
2401
2404
  const captured = node.captured;
@@ -2999,10 +3002,11 @@ class Specializer {
2999
3002
  if (arg.kind === "code" || arg.kind === "virtual" && !this.isKnownShape(arg)) {
3000
3003
  const local = frame.emitter.local(name.text);
3001
3004
  const iec = this.layouts.iecName(name.parent);
3002
- parameters.push(`${local}: ${iec ?? this.layouts.text(declared)}`);
3005
+ const layout = arg.kind === "code" ? arg.layout : undefined;
3006
+ parameters.push(`${local}: ${iec ?? layout ?? this.layouts.text(declared)}`);
3003
3007
  runtimeArgs.push(this.assignmentText(arg, declared, frame, iec));
3004
- frame.env.set(symbol, { kind: "code", text: local, declared, place: true });
3005
- keys.push(`${name.text}=code`);
3008
+ frame.env.set(symbol, { kind: "code", text: local, declared, place: true, layout });
3009
+ keys.push(`${name.text}=code${layout ? `:${layout}` : ""}`);
3006
3010
  return;
3007
3011
  }
3008
3012
  if (arg.kind === "const" && !(declared.flags & ts11.TypeFlags.Undefined) && typeof arg.value !== "undefined") {
@@ -3087,8 +3091,12 @@ class Specializer {
3087
3091
  this.switch(node, frame);
3088
3092
  return;
3089
3093
  }
3090
- if (ts11.isForStatement(node) || ts11.isWhileStatement(node)) {
3091
- throw new SpecializeError(node, "Use for…of over an array; counted and while loops are not lowered yet.");
3094
+ if (ts11.isForStatement(node)) {
3095
+ this.forLoop(node, frame);
3096
+ return;
3097
+ }
3098
+ if (ts11.isWhileStatement(node)) {
3099
+ throw new SpecializeError(node, "Use a counted for loop or for…of; while loops have no bound to prove.");
3092
3100
  }
3093
3101
  if (ts11.isTryStatement(node)) {
3094
3102
  emitter.open("try {");
@@ -3189,6 +3197,14 @@ class Specializer {
3189
3197
  return Boolean(type.flags & ts11.TypeFlags.Object) && !this.layouts.literalEnum(type);
3190
3198
  }
3191
3199
  forOf(node, frame) {
3200
+ const call = node.expression;
3201
+ if (ts11.isCallExpression(call) && ts11.isPropertyAccessExpression(call.expression) && call.expression.name.text === "entries" && !call.arguments.length) {
3202
+ const array = this.expression(call.expression.expression, frame);
3203
+ if (array.kind === "code") {
3204
+ this.forEntries(node, array.text, frame);
3205
+ return;
3206
+ }
3207
+ }
3192
3208
  const iterable = this.expression(node.expression, frame);
3193
3209
  const items = this.items(iterable);
3194
3210
  const declaration = node.initializer;
@@ -3220,6 +3236,60 @@ class Specializer {
3220
3236
  this.inner(node.statement, frame);
3221
3237
  frame.emitter.close();
3222
3238
  }
3239
+ forEntries(node, array, frame) {
3240
+ const declaration = node.initializer;
3241
+ const binding = ts11.isVariableDeclarationList(declaration) && declaration.declarations.length === 1 ? declaration.declarations[0].name : undefined;
3242
+ const names = binding && ts11.isArrayBindingPattern(binding) ? binding.elements.map((element) => ts11.isBindingElement(element) && ts11.isIdentifier(element.name) ? element.name : undefined) : [];
3243
+ const [indexName, itemName] = names;
3244
+ if (names.length !== 2 || !indexName || !itemName)
3245
+ throw new SpecializeError(node.initializer, "Destructure [index, item] from array.entries().");
3246
+ const [index, item] = [indexName, itemName].map((name) => {
3247
+ const local = frame.emitter.local(name.text);
3248
+ frame.env.set(this.checker.getSymbolAtLocation(name), {
3249
+ kind: "code",
3250
+ text: local,
3251
+ declared: this.checker.getTypeAtLocation(name),
3252
+ place: true
3253
+ });
3254
+ return local;
3255
+ });
3256
+ frame.emitter.open(`for (const [${index}, ${item}] of ${array}.entries()) {`);
3257
+ this.inner(node.statement, frame);
3258
+ frame.emitter.close();
3259
+ }
3260
+ forLoop(node, frame) {
3261
+ const { condition, incrementor, initializer } = node;
3262
+ if (!initializer || !ts11.isVariableDeclarationList(initializer) || !(initializer.flags & ts11.NodeFlags.Let) || initializer.declarations.length !== 1)
3263
+ throw new SpecializeError(node, "Declare one let counter in the for header.");
3264
+ const declaration = initializer.declarations[0];
3265
+ const name = declaration.name;
3266
+ if (!ts11.isIdentifier(name) || !declaration.initializer)
3267
+ throw new SpecializeError(declaration, "Initialize the loop counter.");
3268
+ if (!condition || !ts11.isBinaryExpression(condition) || !incrementor)
3269
+ throw new SpecializeError(node, "Compare the counter to a bound and step it in the for header.");
3270
+ const declared = this.checker.getTypeAtLocation(name);
3271
+ const iec = this.layouts.iecName(declaration);
3272
+ const start = this.code(this.expression(declaration.initializer, frame, declared), declared, frame);
3273
+ const local = frame.emitter.local(name.text);
3274
+ frame.env.set(this.checker.getSymbolAtLocation(name), {
3275
+ kind: "code",
3276
+ text: local,
3277
+ declared,
3278
+ place: true,
3279
+ iec
3280
+ });
3281
+ const test = `${this.code(this.expression(condition.left, frame), declared, frame)} ${condition.operatorToken.getText()} ${this.code(this.expression(condition.right, frame), declared, frame)}`;
3282
+ let step;
3283
+ if (ts11.isPrefixUnaryExpression(incrementor) || ts11.isPostfixUnaryExpression(incrementor))
3284
+ step = `${local}${ts11.tokenToString(incrementor.operator)}`;
3285
+ else if (ts11.isBinaryExpression(incrementor))
3286
+ step = `${local} ${incrementor.operatorToken.getText()} ${this.code(this.expression(incrementor.right, frame), declared, frame)}`;
3287
+ else
3288
+ throw new SpecializeError(incrementor, "Use i++, i--, i += step or i -= step in the for header.");
3289
+ frame.emitter.open(`for (let ${local}: ${iec ?? this.localType(declared)} = ${start}; ${test}; ${step}) {`);
3290
+ this.inner(node.statement, frame);
3291
+ frame.emitter.close();
3292
+ }
3223
3293
  items(value) {
3224
3294
  if (value.kind === "virtual" && value.items)
3225
3295
  return value.items;
@@ -3604,7 +3674,9 @@ class Specializer {
3604
3674
  text: `${object.text}.${name}`,
3605
3675
  declared,
3606
3676
  place: object.place,
3607
- iec: this.layouts.iecName(property?.valueDeclaration)
3677
+ iec: this.layouts.iecName(property?.valueDeclaration),
3678
+ elementIec: this.elementIec(property?.valueDeclaration),
3679
+ layout: property && declared && this.checker.isArrayType(declared) ? this.layouts.fieldText(property) : undefined
3608
3680
  };
3609
3681
  }
3610
3682
  if (object.kind === "const" && object.value === undefined)
@@ -4271,6 +4343,7 @@ var binaryToken = (kind) => ({
4271
4343
  [ts11.SyntaxKind.PlusToken]: "+",
4272
4344
  [ts11.SyntaxKind.MinusToken]: "-",
4273
4345
  [ts11.SyntaxKind.AsteriskToken]: "*",
4346
+ [ts11.SyntaxKind.PercentToken]: "%",
4274
4347
  [ts11.SyntaxKind.LessThanToken]: "<",
4275
4348
  [ts11.SyntaxKind.LessThanEqualsToken]: "<=",
4276
4349
  [ts11.SyntaxKind.GreaterThanToken]: ">",
@@ -4289,6 +4362,8 @@ var foldNumber = (kind, left, right) => {
4289
4362
  return left - right;
4290
4363
  case ts11.SyntaxKind.AsteriskToken:
4291
4364
  return left * right;
4365
+ case ts11.SyntaxKind.PercentToken:
4366
+ return right === 0 ? undefined : left % right;
4292
4367
  case ts11.SyntaxKind.LessThanToken:
4293
4368
  return left < right;
4294
4369
  case ts11.SyntaxKind.LessThanEqualsToken:
@@ -5989,7 +6064,7 @@ class Analyzer {
5989
6064
  }
5990
6065
  };
5991
6066
  }
5992
- if (node.name.text === "length") {
6067
+ if (node.name.text === "length" && !this.variables.has(this.resolver.symbol(node.name))) {
5993
6068
  const base = this.expression(node.expression), type = this.resolver.types.get(base.type);
5994
6069
  if (type?.kind === "array")
5995
6070
  return { ...origin, type: "DINT", kind: "length", operand: base };
@@ -6878,5 +6953,5 @@ export {
6878
6953
  typeCheckOnly
6879
6954
  };
6880
6955
 
6881
- //# debugId=1915DD200BDBBFDB64756E2164756E21
6956
+ //# debugId=E0C7AD33512FBE9E64756E2164756E21
6882
6957
  //# sourceMappingURL=index.js.map