circuitscript 0.1.15 → 0.1.17

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/dist/cjs/BaseVisitor.js +98 -35
  2. package/dist/cjs/antlr/CircuitScriptLexer.js +3 -3
  3. package/dist/cjs/antlr/CircuitScriptParser.js +868 -757
  4. package/dist/cjs/builtinMethods.js +11 -1
  5. package/dist/cjs/draw_symbols.js +18 -17
  6. package/dist/cjs/execute.js +58 -31
  7. package/dist/cjs/globals.js +4 -1
  8. package/dist/cjs/graph.js +372 -0
  9. package/dist/cjs/helpers.js +6 -2
  10. package/dist/cjs/layout.js +18 -259
  11. package/dist/cjs/objects/ClassComponent.js +27 -20
  12. package/dist/cjs/objects/ExecutionScope.js +7 -2
  13. package/dist/cjs/objects/Net.js +1 -1
  14. package/dist/cjs/objects/PinDefinition.js +55 -3
  15. package/dist/cjs/objects/types.js +42 -6
  16. package/dist/cjs/visitor.js +88 -48
  17. package/dist/esm/BaseVisitor.js +98 -35
  18. package/dist/esm/antlr/CircuitScriptLexer.js +3 -3
  19. package/dist/esm/antlr/CircuitScriptParser.js +864 -755
  20. package/dist/esm/antlr/CircuitScriptVisitor.js +2 -0
  21. package/dist/esm/builtinMethods.js +11 -1
  22. package/dist/esm/draw_symbols.js +18 -17
  23. package/dist/esm/execute.js +60 -33
  24. package/dist/esm/globals.js +3 -0
  25. package/dist/esm/graph.js +344 -0
  26. package/dist/esm/helpers.js +6 -2
  27. package/dist/esm/layout.js +14 -235
  28. package/dist/esm/objects/ClassComponent.js +28 -21
  29. package/dist/esm/objects/ExecutionScope.js +7 -2
  30. package/dist/esm/objects/Net.js +1 -1
  31. package/dist/esm/objects/PinDefinition.js +53 -2
  32. package/dist/esm/objects/types.js +42 -6
  33. package/dist/esm/visitor.js +90 -50
  34. package/dist/libs/std.cst +3 -2
  35. package/dist/types/BaseVisitor.d.ts +5 -2
  36. package/dist/types/antlr/CircuitScriptParser.d.ts +42 -26
  37. package/dist/types/antlr/CircuitScriptVisitor.d.ts +4 -0
  38. package/dist/types/draw_symbols.d.ts +13 -7
  39. package/dist/types/execute.d.ts +12 -12
  40. package/dist/types/globals.d.ts +4 -1
  41. package/dist/types/graph.d.ts +29 -0
  42. package/dist/types/layout.d.ts +4 -9
  43. package/dist/types/objects/ClassComponent.d.ts +8 -8
  44. package/dist/types/objects/ExecutionScope.d.ts +8 -7
  45. package/dist/types/objects/Net.d.ts +2 -2
  46. package/dist/types/objects/PinDefinition.d.ts +17 -2
  47. package/dist/types/objects/types.d.ts +31 -7
  48. package/libs/std.cst +3 -2
  49. package/package.json +2 -1
@@ -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");
@@ -15,6 +14,7 @@ const utils_js_1 = require("./utils.js");
15
14
  const builtinMethods_js_1 = require("./builtinMethods.js");
16
15
  const utils_js_2 = require("./utils.js");
17
16
  const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
17
+ const PinDefinition_js_1 = require("./objects/PinDefinition.js");
18
18
  class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
19
19
  constructor(silent = false, onErrorHandler = null, environment) {
20
20
  super();
@@ -55,28 +55,37 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
55
55
  const ctxAtom = ctx.atom_expr();
56
56
  const ctxFuncCallRef = ctx.function_call_expr();
57
57
  let leftSideReference;
58
+ let lhsCtx;
58
59
  if (ctxAtom) {
59
60
  leftSideReference = this.getReference(ctx.atom_expr());
61
+ lhsCtx = ctxAtom;
60
62
  }
61
63
  else if (ctxFuncCallRef) {
62
64
  this.setResult(ctxFuncCallRef, { keepReference: true });
63
65
  leftSideReference = this.visitResult(ctxFuncCallRef);
66
+ lhsCtx = ctxFuncCallRef;
67
+ }
68
+ const rhsCtx = ctx.data_expr();
69
+ const rhsCtxResult = this.visitResult(rhsCtx);
70
+ if ((0, utils_js_1.isReference)(rhsCtxResult) && !rhsCtxResult.found) {
71
+ this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
64
72
  }
65
- const rhsCtxResult = this.visitResult(ctx.data_expr());
66
73
  const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
67
74
  const trailers = leftSideReference.trailers ?? [];
68
75
  const sequenceParts = [];
69
76
  if (trailers.length === 0) {
77
+ this.getScope().setVariable(leftSideReference.name, rhsValue);
78
+ let itemType = '';
70
79
  if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
71
- this.getScope().setVariable(leftSideReference.name, rhsValue);
72
- sequenceParts.push(...['instance', leftSideReference.name, rhsValue]);
80
+ itemType = globals_js_1.ReferenceTypes.instance;
73
81
  this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
74
82
  }
75
83
  else {
84
+ itemType = globals_js_1.ReferenceTypes.variable;
76
85
  this.getScope().setVariable(leftSideReference.name, rhsValue);
77
86
  this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
78
- sequenceParts.push(...['variable', leftSideReference.name, rhsValue]);
79
87
  }
88
+ sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
80
89
  }
81
90
  else {
82
91
  if (leftSideReference.parentValue instanceof ClassComponent_js_1.ClassComponent) {
@@ -84,7 +93,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
84
93
  this.log2(`assigned component param ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
85
94
  sequenceParts.push(...['instance', [leftSideReference.parentValue, trailers], rhsValue]);
86
95
  if (leftSideReference.parentValue.typeProp === globals_js_1.ComponentTypes.net) {
87
- const net = this.getScope().getNet(leftSideReference.parentValue, 1);
96
+ const net = this.getScope().getNet(leftSideReference.parentValue, new PinDefinition_js_1.PinId(1));
88
97
  if (net) {
89
98
  const trailerValue = trailers.join(".");
90
99
  net.params.set(trailerValue, rhsValue);
@@ -92,8 +101,20 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
92
101
  }
93
102
  }
94
103
  else if (leftSideReference.parentValue instanceof Object) {
95
- leftSideReference.parentValue[trailers.join('.')] = rhsValue;
96
- this.log2(`assigned object ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
104
+ if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
105
+ if (Array.isArray(leftSideReference.parentValue)) {
106
+ const arrayIndexValue = trailers[0][1];
107
+ leftSideReference.parentValue[arrayIndexValue] = rhsValue;
108
+ this.log2(`assigned array index ${leftSideReference.parentValue} index: ${arrayIndexValue} value: ${rhsValue}`);
109
+ }
110
+ else {
111
+ this.throwWithContext(lhsCtx, "Invalid array");
112
+ }
113
+ }
114
+ else {
115
+ leftSideReference.parentValue[trailers.join('.')] = rhsValue;
116
+ this.log2(`assigned object ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
117
+ }
97
118
  sequenceParts.push(...['variable', [leftSideReference.parentValue, trailers], rhsValue]);
98
119
  }
99
120
  }
@@ -159,19 +180,39 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
159
180
  }
160
181
  this.setResult(ctx, newValue);
161
182
  };
183
+ this.visitTrailer_expr2 = (ctx) => {
184
+ const reference = this.getResult(ctx);
185
+ const ctxID = ctx.ID();
186
+ const ctxDataExpr = ctx.data_expr();
187
+ const useValue = reference.value;
188
+ let nextReference;
189
+ if (ctxID) {
190
+ reference.trailers.push(ctxID.getText());
191
+ nextReference = this.getExecutor().resolveTrailers(reference.type, useValue, reference.trailers);
192
+ }
193
+ else if (ctxDataExpr) {
194
+ const arrayIndex = this.visitResult(ctxDataExpr);
195
+ if (arrayIndex instanceof ParamDefinition_js_1.NumericValue) {
196
+ const arrayIndexValue = arrayIndex.toNumber();
197
+ const foundValue = useValue[arrayIndexValue];
198
+ const refType = foundValue instanceof ClassComponent_js_1.ClassComponent
199
+ ? globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.variable;
200
+ nextReference = new types_js_1.AnyReference({
201
+ found: true,
202
+ type: refType,
203
+ value: foundValue,
204
+ trailers: [[globals_js_1.TrailerArrayIndex, arrayIndexValue]],
205
+ parentValue: useValue
206
+ });
207
+ }
208
+ }
209
+ this.setResult(ctx, nextReference);
210
+ };
162
211
  this.visitAtom_expr = (ctx) => {
163
212
  const executor = this.getExecutor();
164
213
  const firstId = ctx.ID(0);
165
214
  const atomId = firstId.getText();
166
215
  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
216
  if (this.pinTypesList.indexOf(atomId) !== -1) {
176
217
  currentReference = new types_js_1.AnyReference({
177
218
  found: true,
@@ -180,18 +221,18 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
180
221
  });
181
222
  }
182
223
  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);
224
+ currentReference = executor.resolveVariable(this.executionStack, atomId);
225
+ }
226
+ if (currentReference !== undefined && currentReference.found) {
227
+ const trailersLength = ctx.trailer_expr2().length;
228
+ for (let i = 0; i < trailersLength; i++) {
229
+ const trailerCtx = ctx.trailer_expr2(i);
230
+ this.setResult(trailerCtx, currentReference);
231
+ currentReference = this.visitResult(trailerCtx);
189
232
  }
190
233
  }
191
- if (ctx.parent instanceof CircuitScriptParser_js_1.ExpressionContext && !currentReference.found) {
192
- this.throwWithContext(ctx, "Unknown symbol: " + atomId);
193
- }
194
234
  this.setResult(ctx, currentReference);
235
+ this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
195
236
  };
196
237
  this.visitFunctionCallExpr = (ctx) => {
197
238
  const result = this.visitResult(ctx.function_call_expr());
@@ -266,7 +307,8 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
266
307
  value = reference;
267
308
  }
268
309
  else {
269
- if (reference.trailers && reference.trailers.length > 0) {
310
+ if ((reference.trailers && reference.trailers.length > 0)
311
+ || reference.type === globals_js_1.ReferenceTypes.function) {
270
312
  value = reference;
271
313
  }
272
314
  else {
@@ -301,11 +343,11 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
301
343
  const returnList = [];
302
344
  dataExpressions.forEach((item, index) => {
303
345
  const value = this.visitResult(item);
304
- returnList.push(['position', index, value]);
346
+ returnList.push(['position', index, (0, utils_js_1.unwrapValue)(value)]);
305
347
  });
306
348
  keywordAssignmentExpressions.forEach((item) => {
307
349
  const [key, value] = this.visitResult(item);
308
- returnList.push(['keyword', key, value]);
350
+ returnList.push(['keyword', key, (0, utils_js_1.unwrapValue)(value)]);
309
351
  });
310
352
  this.setResult(ctx, returnList);
311
353
  };
@@ -315,9 +357,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
315
357
  this.visitFunction_return_expr = (ctx) => {
316
358
  const executor = this.getExecutor();
317
359
  executor.log('return from function');
318
- const ctxDataExpr = ctx.data_expr();
319
- this.visit(ctxDataExpr);
320
- const returnValue = this.getResult(ctxDataExpr);
360
+ const returnValue = this.visitResult(ctx.data_expr());
321
361
  executor.stopFurtherExpressions = true;
322
362
  executor.returnValue = returnValue;
323
363
  this.setResult(ctx, returnValue);
@@ -351,6 +391,25 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
351
391
  this.visitArrayExpr = (ctx) => {
352
392
  this.setResult(ctx, this.visitResult(ctx.array_expr()));
353
393
  };
394
+ this.visitArrayIndexExpr = (ctx) => {
395
+ const ctxArray = ctx.data_expr(0);
396
+ const ctxArrayIndex = ctx.data_expr(1);
397
+ const arrayItem = this.visitResult(ctxArray);
398
+ const indexValue = this.visitResult(ctxArrayIndex);
399
+ if (!Array.isArray(arrayItem)) {
400
+ throw new utils_js_2.RuntimeExecutionError("Invalid array", ctxArray);
401
+ }
402
+ if (!(indexValue instanceof ParamDefinition_js_1.NumericValue)) {
403
+ throw new utils_js_2.RuntimeExecutionError("Invalid index value", ctxArrayIndex);
404
+ }
405
+ const indexValueNumber = indexValue.toNumber();
406
+ if (isNaN(indexValueNumber)) {
407
+ throw new utils_js_2.RuntimeExecutionError("Invalid index value", ctxArrayIndex);
408
+ }
409
+ if (Array.isArray(arrayItem)) {
410
+ this.setResult(ctx, arrayItem[indexValueNumber]);
411
+ }
412
+ };
354
413
  this.visitRoundedBracketsExpr = (ctx) => {
355
414
  const ctxDataExpr = ctx.data_expr();
356
415
  this.visit(ctxDataExpr);
@@ -465,6 +524,11 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
465
524
  currentReference.trailers = [];
466
525
  ctx.trailer_expr().forEach(item => {
467
526
  if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
527
+ if (currentReference.type === globals_js_1.ReferenceTypes.variable) {
528
+ if (currentReference.value instanceof types_js_1.AnyReference && currentReference.value.type === globals_js_1.ReferenceTypes.function) {
529
+ currentReference = currentReference.value;
530
+ }
531
+ }
468
532
  let parameters = [];
469
533
  const ctxParameters = item.parameters();
470
534
  if (ctxParameters) {
@@ -491,10 +555,9 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
491
555
  }
492
556
  }
493
557
  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);
558
+ const ctxTrailer = item.trailer_expr2();
559
+ this.setResult(ctxTrailer, currentReference);
560
+ currentReference = this.visitResult(ctxTrailer);
498
561
  }
499
562
  });
500
563
  }
@@ -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,