circuitscript 0.5.1 → 0.5.3

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.
@@ -7,6 +7,7 @@ const CircuitScriptParserVisitor_js_1 = require("./antlr/CircuitScriptParserVisi
7
7
  const execute_js_1 = require("./execute.js");
8
8
  const logger_js_1 = require("./logger.js");
9
9
  const ClassComponent_js_1 = require("./objects/ClassComponent.js");
10
+ const Net_js_1 = require("./objects/Net.js");
10
11
  const NumericValue_js_1 = require("./objects/NumericValue.js");
11
12
  const PercentageValue_js_1 = require("./objects/PercentageValue.js");
12
13
  const types_js_1 = require("./objects/types.js");
@@ -15,12 +16,12 @@ const utils_js_1 = require("./utils.js");
15
16
  const builtinMethods_js_1 = require("./builtinMethods.js");
16
17
  const utils_js_2 = require("./utils.js");
17
18
  const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
18
- const PinDefinition_js_1 = require("./objects/PinDefinition.js");
19
19
  const hash_js_1 = require("./cache/hash.js");
20
20
  const storage_js_1 = require("./cache/storage.js");
21
21
  const serializer_js_1 = require("./cache/serializer.js");
22
22
  const deserializer_js_1 = require("./cache/deserializer.js");
23
23
  const importResolver_js_1 = require("./importResolver.js");
24
+ const Frame_js_1 = require("./objects/Frame.js");
24
25
  class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVisitor {
25
26
  constructor(silent = false, onErrorHandler = null, environment) {
26
27
  super();
@@ -93,20 +94,7 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
93
94
  currentReference = executor.resolveVariable(this.executionStack, atomId);
94
95
  this.log('reference:', currentReference.name, 'found:', currentReference.found);
95
96
  }
96
- if (currentReference !== undefined && currentReference.found) {
97
- ctx.trailer().forEach(ctxTrailer => {
98
- this.setResult(ctxTrailer, {
99
- reference: currentReference,
100
- netNamespace: passedNetNamespace
101
- });
102
- currentReference = this.visitResult(ctxTrailer);
103
- });
104
- }
105
- else {
106
- if (ctx.trailer().length > 0 && ctx.trailer(0).LParen()) {
107
- this.throwWithContext(ctx, `Function not found: ${ctx.getText()}`);
108
- }
109
- }
97
+ currentReference = this.resolveTrailersForReference(currentReference, passedNetNamespace, ctx);
110
98
  let resultValue = currentReference;
111
99
  if (!keepReference) {
112
100
  if (currentReference.type !== globals_js_1.ReferenceTypes.pinType) {
@@ -154,68 +142,18 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
154
142
  this.setResult(ctx, nextReference);
155
143
  };
156
144
  this.visitAssignment_expr = (ctx) => {
145
+ const lhsCtx = ctx.callable_expr();
146
+ this.setResult(lhsCtx, { keepReference: true });
147
+ const leftSideReference = this.visitResult(lhsCtx);
157
148
  if (ctx.Assign()) {
158
- const lhsCtx = ctx.callable_expr();
159
- this.setResult(lhsCtx, { keepReference: true });
160
- const leftSideReference = this.visitResult(lhsCtx);
161
149
  const rhsCtx = ctx.data_expr();
162
150
  const rhsCtxResult = this.visitResult(rhsCtx);
151
+ const sequenceParts = [];
163
152
  if ((0, utils_js_1.isReference)(rhsCtxResult) && !rhsCtxResult.found) {
164
153
  this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
165
154
  }
166
155
  const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
167
- const { trailers = [] } = leftSideReference;
168
- const sequenceParts = [];
169
- if (trailers.length === 0) {
170
- this.getScope().setVariable(leftSideReference.name, rhsValue);
171
- let itemType = '';
172
- if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
173
- itemType = globals_js_1.ReferenceTypes.instance;
174
- this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
175
- }
176
- else {
177
- itemType = globals_js_1.ReferenceTypes.variable;
178
- this.getScope().setVariable(leftSideReference.name, rhsValue);
179
- this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
180
- }
181
- sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
182
- }
183
- else {
184
- if (leftSideReference.rootValue instanceof ClassComponent_js_1.ClassComponent) {
185
- this.setInstanceParam(leftSideReference.rootValue, trailers, rhsValue);
186
- this.log2(`assigned component param ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
187
- sequenceParts.push(...['instance', [leftSideReference.rootValue, trailers], rhsValue]);
188
- if (leftSideReference.rootValue.typeProp === globals_js_1.ComponentTypes.net) {
189
- const net = this.getScope().getNet(leftSideReference.rootValue, new PinDefinition_js_1.PinId(1));
190
- if (net) {
191
- const trailerValue = trailers.join(".");
192
- net.params.set(trailerValue, rhsValue);
193
- }
194
- }
195
- }
196
- else if (leftSideReference.rootValue instanceof Object) {
197
- if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
198
- if (Array.isArray(leftSideReference.rootValue)) {
199
- const arrayIndexValue = trailers[0][1];
200
- leftSideReference.rootValue[arrayIndexValue] = rhsValue;
201
- this.log2(`assigned array index ${leftSideReference.rootValue} index: ${arrayIndexValue} value: ${rhsValue}`);
202
- }
203
- else {
204
- this.throwWithContext(lhsCtx, "Invalid array");
205
- }
206
- }
207
- else {
208
- let expandedValue = leftSideReference.rootValue;
209
- trailers.slice(0, -1).forEach(trailer => {
210
- expandedValue = expandedValue[trailer];
211
- });
212
- const lastTrailer = trailers.slice(-1)[0];
213
- expandedValue[lastTrailer] = rhsValue;
214
- this.log2(`assigned object ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
215
- }
216
- sequenceParts.push(...['variable', [leftSideReference.rootValue, trailers], rhsValue]);
217
- }
218
- }
156
+ this.assignValueToReference(sequenceParts, leftSideReference, lhsCtx, rhsValue);
219
157
  if (sequenceParts.length > 0) {
220
158
  this.getScope().sequence.push([
221
159
  ExecutionScope_js_1.SequenceAction.Assign, ...sequenceParts
@@ -224,24 +162,21 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
224
162
  this.setResult(ctx, rhsValue);
225
163
  }
226
164
  else {
227
- const ctxCallable = ctx.callable_expr();
228
- this.setResult(ctxCallable, { keepReference: true });
229
- const reference = this.visitResult(ctx.callable_expr());
230
165
  const value = this.visitResult(ctx.data_expr());
231
- if (!reference.found) {
232
- this.throwWithContext(ctx, 'Undefined reference: ' + reference.name);
166
+ if (!leftSideReference.found) {
167
+ this.throwWithContext(ctx, 'Undefined reference: ' + leftSideReference.name);
233
168
  }
234
- const trailers = reference.trailers ?? [];
169
+ const trailers = leftSideReference.trailers ?? [];
235
170
  let currentValue = null;
236
171
  if (trailers.length === 0) {
237
- currentValue = this.getExecutor().scope.variables.get(reference.name);
172
+ currentValue = this.getExecutor().scope.variables.get(leftSideReference.name);
238
173
  }
239
174
  else {
240
- if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
241
- currentValue = this.getInstanceParam(reference.value, trailers);
175
+ if (leftSideReference.value instanceof ClassComponent_js_1.ClassComponent) {
176
+ currentValue = this.getInstanceParam(leftSideReference.value, trailers);
242
177
  }
243
- else if (reference.value instanceof Object) {
244
- currentValue = reference.value[trailers.join('.')];
178
+ else if (leftSideReference.value instanceof Object) {
179
+ currentValue = leftSideReference.value[trailers.join('.')];
245
180
  }
246
181
  }
247
182
  if (currentValue === null) {
@@ -267,79 +202,10 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
267
202
  else {
268
203
  this.throwWithContext(ctx, 'Operator assignment failed: could not perform operator');
269
204
  }
270
- if (trailers.length === 0) {
271
- this.getExecutor().scope.setVariable(reference.name, newValue);
272
- }
273
- else {
274
- if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
275
- this.setInstanceParam(reference.value, trailers, newValue);
276
- }
277
- else if (reference.value instanceof Object) {
278
- reference.value[trailers.join('.')] = newValue;
279
- }
280
- }
205
+ this.assignValueToReference([], leftSideReference, lhsCtx, newValue);
281
206
  this.setResult(ctx, newValue);
282
207
  }
283
208
  };
284
- this.visitTrailer_expr2 = (ctx) => {
285
- const reference = this.getResult(ctx);
286
- const ctxID = ctx.ID();
287
- const ctxDataExpr = ctx.data_expr();
288
- const useValue = reference.value;
289
- let nextReference;
290
- if (ctxID) {
291
- reference.trailers.push(ctxID.getText());
292
- const useRootValue = reference.rootValue ?? reference.value;
293
- const useTrailerIndex = reference.trailerIndex ?? 0;
294
- nextReference = this.getExecutor().resolveTrailers(reference.type, useRootValue, reference.trailers);
295
- nextReference.name =
296
- [reference.name,
297
- ...reference.trailers.slice(useTrailerIndex)].join('.');
298
- }
299
- else if (ctxDataExpr) {
300
- const arrayIndex = this.visitResult(ctxDataExpr);
301
- if (arrayIndex instanceof NumericValue_js_1.NumericValue) {
302
- const arrayIndexValue = arrayIndex.toNumber();
303
- const foundValue = useValue[arrayIndexValue];
304
- const refType = foundValue instanceof ClassComponent_js_1.ClassComponent
305
- ? globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.variable;
306
- nextReference = new types_js_1.AnyReference({
307
- found: true,
308
- type: refType,
309
- value: foundValue,
310
- trailers: [[globals_js_1.TrailerArrayIndex, arrayIndexValue]],
311
- rootValue: useValue
312
- });
313
- }
314
- }
315
- this.setResult(ctx, nextReference);
316
- };
317
- this.visitAtom_expr = (ctx) => {
318
- const executor = this.getExecutor();
319
- const firstId = ctx.ID(0);
320
- const atomId = firstId.getText();
321
- let currentReference;
322
- if (globals_js_1.PinTypesList.indexOf(atomId) !== -1) {
323
- currentReference = new types_js_1.AnyReference({
324
- found: true,
325
- value: atomId,
326
- type: globals_js_1.ReferenceTypes.pinType,
327
- });
328
- }
329
- else {
330
- this.log('resolve variable ctx: ' + ctx.getText(), 'atomId', atomId);
331
- currentReference = executor.resolveVariable(this.executionStack, atomId);
332
- this.log('reference:', currentReference.name, 'found:', currentReference.found);
333
- }
334
- if (currentReference !== undefined && currentReference.found) {
335
- ctx.trailer_expr2().forEach(ctxTrailer => {
336
- this.setResult(ctxTrailer, currentReference);
337
- currentReference = this.visitResult(ctxTrailer);
338
- });
339
- }
340
- this.setResult(ctx, currentReference);
341
- this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
342
- };
343
209
  this.visitValue_expr = (ctx) => {
344
210
  const sign = ctx.Minus() ? -1 : 1;
345
211
  const ctxIntegerValue = ctx.INTEGER_VALUE();
@@ -634,6 +500,72 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
634
500
  }
635
501
  }
636
502
  }
503
+ resolveTrailersForReference(reference, passedNetNamespace, ctx) {
504
+ if (reference !== undefined && reference.found) {
505
+ ctx.trailer().forEach(ctxTrailer => {
506
+ this.setResult(ctxTrailer, {
507
+ reference: reference,
508
+ netNamespace: passedNetNamespace
509
+ });
510
+ reference = this.visitResult(ctxTrailer);
511
+ });
512
+ }
513
+ else {
514
+ if (ctx.trailer().length > 0 && ctx.trailer(0).LParen()) {
515
+ this.throwWithContext(ctx, `Function not found: ${ctx.getText()}`);
516
+ }
517
+ }
518
+ return reference;
519
+ }
520
+ assignValueToReference(sequenceParts, leftSideReference, lhsCtx, rhsValue) {
521
+ const { trailers = [] } = leftSideReference;
522
+ if (trailers.length === 0) {
523
+ this.getScope().setVariable(leftSideReference.name, rhsValue);
524
+ let itemType = '';
525
+ if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
526
+ itemType = globals_js_1.ReferenceTypes.instance;
527
+ this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
528
+ }
529
+ else {
530
+ itemType = globals_js_1.ReferenceTypes.variable;
531
+ this.getScope().setVariable(leftSideReference.name, rhsValue);
532
+ this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
533
+ }
534
+ sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
535
+ }
536
+ else {
537
+ if (leftSideReference.rootValue instanceof ClassComponent_js_1.ClassComponent) {
538
+ this.setInstanceParam(leftSideReference.rootValue, trailers, rhsValue);
539
+ this.log2(`assigned component param ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
540
+ sequenceParts.push(...['instance', [leftSideReference.rootValue, trailers], rhsValue]);
541
+ }
542
+ else if (leftSideReference.rootValue instanceof Frame_js_1.Frame) {
543
+ leftSideReference.rootValue.parameters.set(trailers[0], rhsValue);
544
+ }
545
+ else if (leftSideReference.rootValue instanceof Object) {
546
+ if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
547
+ if (Array.isArray(leftSideReference.rootValue)) {
548
+ const arrayIndexValue = trailers[0][1];
549
+ leftSideReference.rootValue[arrayIndexValue] = rhsValue;
550
+ this.log2(`assigned array index ${leftSideReference.rootValue} index: ${arrayIndexValue} value: ${rhsValue}`);
551
+ }
552
+ else {
553
+ this.throwWithContext(lhsCtx, "Invalid array");
554
+ }
555
+ }
556
+ else {
557
+ let expandedValue = leftSideReference.rootValue;
558
+ trailers.slice(0, -1).forEach(trailer => {
559
+ expandedValue = expandedValue[trailer];
560
+ });
561
+ const lastTrailer = trailers.slice(-1)[0];
562
+ expandedValue[lastTrailer] = rhsValue;
563
+ this.log2(`assigned object ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
564
+ }
565
+ sequenceParts.push(...['variable', [leftSideReference.rootValue, trailers], rhsValue]);
566
+ }
567
+ }
568
+ }
637
569
  getReference(ctx) {
638
570
  const atomStr = ctx.getText();
639
571
  if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
@@ -1048,9 +980,40 @@ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVis
1048
980
  return result;
1049
981
  }
1050
982
  setInstanceParam(object, trailers, value) {
1051
- const paramName = trailers[0];
1052
- object.setParam(paramName, value);
1053
- this.log2(`set instance ${object.instanceName} param ${paramName} to ${value}`);
983
+ if (trailers.length === 1) {
984
+ const paramName = trailers[0];
985
+ if (paramName === globals_js_1.ParamKeys.flipX || paramName == globals_js_1.ParamKeys.flipY) {
986
+ if (typeof value === "boolean") {
987
+ value = value ? (0, NumericValue_js_1.numeric)(1) : (0, NumericValue_js_1.numeric)(0);
988
+ }
989
+ }
990
+ object.setParam(paramName, value);
991
+ const unitModifiers = [
992
+ globals_js_1.ParamKeys.angle,
993
+ globals_js_1.ParamKeys.flip,
994
+ globals_js_1.ParamKeys.flipX,
995
+ globals_js_1.ParamKeys.flipY,
996
+ ];
997
+ if (unitModifiers.indexOf(paramName) !== -1) {
998
+ object.getUnit().setParam(paramName, value);
999
+ }
1000
+ this.log2(`set instance ${object.instanceName} param ${paramName} to ${value}`);
1001
+ }
1002
+ else {
1003
+ let reference = null;
1004
+ for (let i = 0; i < trailers.length; i++) {
1005
+ const trailer = trailers[i];
1006
+ if (i === 0) {
1007
+ reference = object.getParam(trailer);
1008
+ }
1009
+ else {
1010
+ if (reference instanceof Net_js_1.Net) {
1011
+ const remainingTrailers = trailers.slice(i);
1012
+ reference.params.set(remainingTrailers.join("."), value);
1013
+ }
1014
+ }
1015
+ }
1016
+ }
1054
1017
  }
1055
1018
  getInstanceParam(object, trailers) {
1056
1019
  const paramName = trailers[0];