circuitscript 0.1.15 → 0.1.16

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.
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseVisitor = void 0;
4
4
  const big_js_1 = require("big.js");
5
- const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
6
5
  const CircuitScriptVisitor_js_1 = require("./antlr/CircuitScriptVisitor.js");
7
6
  const execute_js_1 = require("./execute.js");
8
7
  const logger_js_1 = require("./logger.js");
@@ -55,28 +54,37 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
55
54
  const ctxAtom = ctx.atom_expr();
56
55
  const ctxFuncCallRef = ctx.function_call_expr();
57
56
  let leftSideReference;
57
+ let lhsCtx;
58
58
  if (ctxAtom) {
59
59
  leftSideReference = this.getReference(ctx.atom_expr());
60
+ lhsCtx = ctxAtom;
60
61
  }
61
62
  else if (ctxFuncCallRef) {
62
63
  this.setResult(ctxFuncCallRef, { keepReference: true });
63
64
  leftSideReference = this.visitResult(ctxFuncCallRef);
65
+ lhsCtx = ctxFuncCallRef;
66
+ }
67
+ const rhsCtx = ctx.data_expr();
68
+ const rhsCtxResult = this.visitResult(rhsCtx);
69
+ if ((0, utils_js_1.isReference)(rhsCtxResult) && !rhsCtxResult.found) {
70
+ this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
64
71
  }
65
- const rhsCtxResult = this.visitResult(ctx.data_expr());
66
72
  const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
67
73
  const trailers = leftSideReference.trailers ?? [];
68
74
  const sequenceParts = [];
69
75
  if (trailers.length === 0) {
76
+ this.getScope().setVariable(leftSideReference.name, rhsValue);
77
+ let itemType = '';
70
78
  if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
71
- this.getScope().setVariable(leftSideReference.name, rhsValue);
72
- sequenceParts.push(...['instance', leftSideReference.name, rhsValue]);
79
+ itemType = globals_js_1.ReferenceTypes.instance;
73
80
  this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
74
81
  }
75
82
  else {
83
+ itemType = globals_js_1.ReferenceTypes.variable;
76
84
  this.getScope().setVariable(leftSideReference.name, rhsValue);
77
85
  this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
78
- sequenceParts.push(...['variable', leftSideReference.name, rhsValue]);
79
86
  }
87
+ sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
80
88
  }
81
89
  else {
82
90
  if (leftSideReference.parentValue instanceof ClassComponent_js_1.ClassComponent) {
@@ -92,8 +100,20 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
92
100
  }
93
101
  }
94
102
  else if (leftSideReference.parentValue instanceof Object) {
95
- leftSideReference.parentValue[trailers.join('.')] = rhsValue;
96
- this.log2(`assigned object ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
103
+ if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
104
+ if (Array.isArray(leftSideReference.parentValue)) {
105
+ const arrayIndexValue = trailers[0][1];
106
+ leftSideReference.parentValue[arrayIndexValue] = rhsValue;
107
+ this.log2(`assigned array index ${leftSideReference.parentValue} index: ${arrayIndexValue} value: ${rhsValue}`);
108
+ }
109
+ else {
110
+ this.throwWithContext(lhsCtx, "Invalid array");
111
+ }
112
+ }
113
+ else {
114
+ leftSideReference.parentValue[trailers.join('.')] = rhsValue;
115
+ this.log2(`assigned object ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
116
+ }
97
117
  sequenceParts.push(...['variable', [leftSideReference.parentValue, trailers], rhsValue]);
98
118
  }
99
119
  }
@@ -159,19 +179,39 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
159
179
  }
160
180
  this.setResult(ctx, newValue);
161
181
  };
182
+ this.visitTrailer_expr2 = (ctx) => {
183
+ const reference = this.getResult(ctx);
184
+ const ctxID = ctx.ID();
185
+ const ctxDataExpr = ctx.data_expr();
186
+ const useValue = reference.value;
187
+ let nextReference;
188
+ if (ctxID) {
189
+ reference.trailers.push(ctxID.getText());
190
+ nextReference = this.getExecutor().resolveTrailers(reference.type, useValue, reference.trailers);
191
+ }
192
+ else if (ctxDataExpr) {
193
+ const arrayIndex = this.visitResult(ctxDataExpr);
194
+ if (arrayIndex instanceof ParamDefinition_js_1.NumericValue) {
195
+ const arrayIndexValue = arrayIndex.toNumber();
196
+ const foundValue = useValue[arrayIndexValue];
197
+ const refType = foundValue instanceof ClassComponent_js_1.ClassComponent
198
+ ? globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.variable;
199
+ nextReference = new types_js_1.AnyReference({
200
+ found: true,
201
+ type: refType,
202
+ value: foundValue,
203
+ trailers: [[globals_js_1.TrailerArrayIndex, arrayIndexValue]],
204
+ parentValue: useValue
205
+ });
206
+ }
207
+ }
208
+ this.setResult(ctx, nextReference);
209
+ };
162
210
  this.visitAtom_expr = (ctx) => {
163
211
  const executor = this.getExecutor();
164
212
  const firstId = ctx.ID(0);
165
213
  const atomId = firstId.getText();
166
214
  let currentReference;
167
- const idTrailers = [];
168
- if (ctx.ID().length > 1) {
169
- const idLength = ctx.ID().length;
170
- for (let i = 1; i < idLength; i++) {
171
- const tmpCtx = ctx.ID(i);
172
- idTrailers.push(tmpCtx.getText());
173
- }
174
- }
175
215
  if (this.pinTypesList.indexOf(atomId) !== -1) {
176
216
  currentReference = new types_js_1.AnyReference({
177
217
  found: true,
@@ -180,18 +220,18 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
180
220
  });
181
221
  }
182
222
  else {
183
- currentReference = executor.resolveVariable(this.executionStack, atomId, idTrailers);
184
- }
185
- if (currentReference.found && currentReference.type === 'instance' && idTrailers.length === 0) {
186
- const tmpComponent = currentReference.value;
187
- for (const [pinId, net] of tmpComponent.pinNets) {
188
- executor.scope.setNet(tmpComponent, pinId, net);
223
+ currentReference = executor.resolveVariable(this.executionStack, atomId);
224
+ }
225
+ if (currentReference !== undefined && currentReference.found) {
226
+ const trailersLength = ctx.trailer_expr2().length;
227
+ for (let i = 0; i < trailersLength; i++) {
228
+ const trailerCtx = ctx.trailer_expr2(i);
229
+ this.setResult(trailerCtx, currentReference);
230
+ currentReference = this.visitResult(trailerCtx);
189
231
  }
190
232
  }
191
- if (ctx.parent instanceof CircuitScriptParser_js_1.ExpressionContext && !currentReference.found) {
192
- this.throwWithContext(ctx, "Unknown symbol: " + atomId);
193
- }
194
233
  this.setResult(ctx, currentReference);
234
+ this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
195
235
  };
196
236
  this.visitFunctionCallExpr = (ctx) => {
197
237
  const result = this.visitResult(ctx.function_call_expr());
@@ -266,7 +306,8 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
266
306
  value = reference;
267
307
  }
268
308
  else {
269
- if (reference.trailers && reference.trailers.length > 0) {
309
+ if ((reference.trailers && reference.trailers.length > 0)
310
+ || reference.type === globals_js_1.ReferenceTypes.function) {
270
311
  value = reference;
271
312
  }
272
313
  else {
@@ -301,11 +342,11 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
301
342
  const returnList = [];
302
343
  dataExpressions.forEach((item, index) => {
303
344
  const value = this.visitResult(item);
304
- returnList.push(['position', index, value]);
345
+ returnList.push(['position', index, (0, utils_js_1.unwrapValue)(value)]);
305
346
  });
306
347
  keywordAssignmentExpressions.forEach((item) => {
307
348
  const [key, value] = this.visitResult(item);
308
- returnList.push(['keyword', key, value]);
349
+ returnList.push(['keyword', key, (0, utils_js_1.unwrapValue)(value)]);
309
350
  });
310
351
  this.setResult(ctx, returnList);
311
352
  };
@@ -315,9 +356,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
315
356
  this.visitFunction_return_expr = (ctx) => {
316
357
  const executor = this.getExecutor();
317
358
  executor.log('return from function');
318
- const ctxDataExpr = ctx.data_expr();
319
- this.visit(ctxDataExpr);
320
- const returnValue = this.getResult(ctxDataExpr);
359
+ const returnValue = this.visitResult(ctx.data_expr());
321
360
  executor.stopFurtherExpressions = true;
322
361
  executor.returnValue = returnValue;
323
362
  this.setResult(ctx, returnValue);
@@ -351,6 +390,25 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
351
390
  this.visitArrayExpr = (ctx) => {
352
391
  this.setResult(ctx, this.visitResult(ctx.array_expr()));
353
392
  };
393
+ this.visitArrayIndexExpr = (ctx) => {
394
+ const ctxArray = ctx.data_expr(0);
395
+ const ctxArrayIndex = ctx.data_expr(1);
396
+ const arrayItem = this.visitResult(ctxArray);
397
+ const indexValue = this.visitResult(ctxArrayIndex);
398
+ if (!Array.isArray(arrayItem)) {
399
+ throw new utils_js_2.RuntimeExecutionError("Invalid array", ctxArray);
400
+ }
401
+ if (!(indexValue instanceof ParamDefinition_js_1.NumericValue)) {
402
+ throw new utils_js_2.RuntimeExecutionError("Invalid index value", ctxArrayIndex);
403
+ }
404
+ const indexValueNumber = indexValue.toNumber();
405
+ if (isNaN(indexValueNumber)) {
406
+ throw new utils_js_2.RuntimeExecutionError("Invalid index value", ctxArrayIndex);
407
+ }
408
+ if (Array.isArray(arrayItem)) {
409
+ this.setResult(ctx, arrayItem[indexValueNumber]);
410
+ }
411
+ };
354
412
  this.visitRoundedBracketsExpr = (ctx) => {
355
413
  const ctxDataExpr = ctx.data_expr();
356
414
  this.visit(ctxDataExpr);
@@ -465,6 +523,11 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
465
523
  currentReference.trailers = [];
466
524
  ctx.trailer_expr().forEach(item => {
467
525
  if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
526
+ if (currentReference.type === globals_js_1.ReferenceTypes.variable) {
527
+ if (currentReference.value instanceof types_js_1.AnyReference && currentReference.value.type === globals_js_1.ReferenceTypes.function) {
528
+ currentReference = currentReference.value;
529
+ }
530
+ }
468
531
  let parameters = [];
469
532
  const ctxParameters = item.parameters();
470
533
  if (ctxParameters) {
@@ -491,10 +554,9 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
491
554
  }
492
555
  }
493
556
  else {
494
- currentReference.trailers.push(item.ID().getText());
495
- currentReference = this.getExecutor().resolveTrailers(currentReference.type, (currentReference.parentValue !== undefined)
496
- ? currentReference.parentValue
497
- : currentReference.value, currentReference.trailers);
557
+ const ctxTrailer = item.trailer_expr2();
558
+ this.setResult(ctxTrailer, currentReference);
559
+ currentReference = this.visitResult(ctxTrailer);
498
560
  }
499
561
  });
500
562
  }
@@ -151,7 +151,7 @@ CircuitScriptLexer.channelNames = [
151
151
  "DEFAULT_TOKEN_CHANNEL", "HIDDEN"
152
152
  ];
153
153
  CircuitScriptLexer.literalNames = [
154
- null, "':'", "','", "'='", "'..'", "'.'", "'['", "']'", "'break'",
154
+ null, "':'", "','", "'='", "'..'", "'['", "']'", "'.'", "'break'",
155
155
  "'branch'", "'create'", "'component'", "'graphic'", "'module'",
156
156
  "'wire'", "'pin'", "'add'", "'at'", "'to'", "'point'", "'join'",
157
157
  "'parallel'", "'return'", "'def'", "'import'", "'for'", "'in'",
@@ -272,8 +272,8 @@ CircuitScriptLexer._serializedATN = [
272
272
  0, 131, 473, 1, 0, 0, 0, 133, 477, 1, 0, 0, 0, 135, 486, 1, 0, 0, 0, 137, 138, 5, 58, 0,
273
273
  0, 138, 2, 1, 0, 0, 0, 139, 140, 5, 44, 0, 0, 140, 4, 1, 0, 0, 0, 141, 142, 5, 61, 0, 0,
274
274
  142, 6, 1, 0, 0, 0, 143, 144, 5, 46, 0, 0, 144, 145, 5, 46, 0, 0, 145, 8, 1, 0, 0, 0, 146,
275
- 147, 5, 46, 0, 0, 147, 10, 1, 0, 0, 0, 148, 149, 5, 91, 0, 0, 149, 12, 1, 0, 0, 0, 150,
276
- 151, 5, 93, 0, 0, 151, 14, 1, 0, 0, 0, 152, 153, 5, 98, 0, 0, 153, 154, 5, 114, 0, 0, 154,
275
+ 147, 5, 91, 0, 0, 147, 10, 1, 0, 0, 0, 148, 149, 5, 93, 0, 0, 149, 12, 1, 0, 0, 0, 150,
276
+ 151, 5, 46, 0, 0, 151, 14, 1, 0, 0, 0, 152, 153, 5, 98, 0, 0, 153, 154, 5, 114, 0, 0, 154,
277
277
  155, 5, 101, 0, 0, 155, 156, 5, 97, 0, 0, 156, 157, 5, 107, 0, 0, 157, 16, 1, 0, 0, 0,
278
278
  158, 159, 5, 98, 0, 0, 159, 160, 5, 114, 0, 0, 160, 161, 5, 97, 0, 0, 161, 162, 5, 110,
279
279
  0, 0, 162, 163, 5, 99, 0, 0, 163, 164, 5, 104, 0, 0, 164, 18, 1, 0, 0, 0, 165, 166, 5,