circuitscript 0.0.32 → 0.0.34

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 (51) hide show
  1. package/dist/cjs/BaseVisitor.js +187 -39
  2. package/dist/cjs/antlr/CircuitScriptLexer.js +226 -185
  3. package/dist/cjs/antlr/CircuitScriptParser.js +1439 -902
  4. package/dist/cjs/draw_symbols.js +1 -0
  5. package/dist/cjs/execute.js +14 -12
  6. package/dist/cjs/globals.js +14 -2
  7. package/dist/cjs/helpers.js +62 -19
  8. package/dist/cjs/layout.js +88 -36
  9. package/dist/cjs/objects/ClassComponent.js +3 -0
  10. package/dist/cjs/objects/ExecutionScope.js +1 -0
  11. package/dist/cjs/objects/Frame.js +4 -1
  12. package/dist/cjs/objects/ParamDefinition.js +1 -7
  13. package/dist/cjs/objects/types.js +6 -0
  14. package/dist/cjs/regenerate-tests.js +2 -1
  15. package/dist/cjs/render.js +238 -42
  16. package/dist/cjs/visitor.js +162 -27
  17. package/dist/esm/BaseVisitor.mjs +189 -41
  18. package/dist/esm/antlr/CircuitScriptLexer.mjs +226 -185
  19. package/dist/esm/antlr/CircuitScriptParser.mjs +1428 -899
  20. package/dist/esm/antlr/CircuitScriptVisitor.mjs +9 -2
  21. package/dist/esm/draw_symbols.mjs +1 -0
  22. package/dist/esm/execute.mjs +14 -12
  23. package/dist/esm/globals.mjs +13 -1
  24. package/dist/esm/helpers.mjs +61 -20
  25. package/dist/esm/layout.mjs +88 -37
  26. package/dist/esm/objects/ClassComponent.mjs +3 -0
  27. package/dist/esm/objects/ExecutionScope.mjs +1 -0
  28. package/dist/esm/objects/Frame.mjs +5 -1
  29. package/dist/esm/objects/ParamDefinition.mjs +0 -6
  30. package/dist/esm/objects/types.mjs +6 -0
  31. package/dist/esm/regenerate-tests.mjs +2 -1
  32. package/dist/esm/render.mjs +234 -43
  33. package/dist/esm/visitor.mjs +164 -29
  34. package/dist/types/BaseVisitor.d.ts +8 -2
  35. package/dist/types/antlr/CircuitScriptLexer.d.ts +41 -30
  36. package/dist/types/antlr/CircuitScriptParser.d.ts +169 -81
  37. package/dist/types/antlr/CircuitScriptVisitor.d.ts +18 -4
  38. package/dist/types/draw_symbols.d.ts +2 -1
  39. package/dist/types/execute.d.ts +6 -3
  40. package/dist/types/globals.d.ts +11 -0
  41. package/dist/types/helpers.d.ts +12 -0
  42. package/dist/types/layout.d.ts +17 -9
  43. package/dist/types/objects/ClassComponent.d.ts +2 -1
  44. package/dist/types/objects/ExecutionScope.d.ts +2 -0
  45. package/dist/types/objects/Frame.d.ts +6 -2
  46. package/dist/types/objects/ParamDefinition.d.ts +0 -4
  47. package/dist/types/objects/types.d.ts +4 -2
  48. package/dist/types/render.d.ts +6 -14
  49. package/dist/types/visitor.d.ts +10 -2
  50. package/libs/lib.cst +283 -0
  51. package/package.json +1 -1
@@ -42,36 +42,91 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
42
42
  return result;
43
43
  };
44
44
  this.visitAssignment_expr = (ctx) => {
45
- const atomStr = ctx.atom_expr().getText();
46
- if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
47
- throw "Invalid assignment expression!";
48
- }
49
- const ctxAtomExpr = ctx.atom_expr();
50
- this.visit(ctxAtomExpr);
51
- const reference = this.getResult(ctxAtomExpr);
45
+ const reference = this.getReference(ctx.atom_expr());
52
46
  const ctxDataExpr = ctx.data_expr();
53
47
  this.visit(ctxDataExpr);
54
48
  const value = this.getResult(ctxDataExpr);
55
- if (value instanceof ClassComponent_1.ClassComponent) {
56
- const instances = this.getExecutor().scope.instances;
57
- const tmpComponent = value;
58
- const oldName = tmpComponent.instanceName;
59
- tmpComponent.instanceName = reference.name;
60
- instances.delete(oldName);
61
- instances.set(reference.name, tmpComponent);
62
- this.getExecutor().log(`assigned '${reference.name}' to ClassComponent`);
63
- }
64
- else {
65
- const trailers = reference.trailers ?? [];
66
- if (trailers.length === 0) {
49
+ const trailers = reference.trailers ?? [];
50
+ if (trailers.length === 0) {
51
+ if (value instanceof ClassComponent_1.ClassComponent) {
52
+ const instances = this.getExecutor().scope.instances;
53
+ const tmpComponent = value;
54
+ const oldName = tmpComponent.instanceName;
55
+ tmpComponent.instanceName = reference.name;
56
+ instances.delete(oldName);
57
+ instances.set(reference.name, tmpComponent);
58
+ this.getExecutor().log(`assigned '${reference.name}' to ClassComponent`);
59
+ }
60
+ else {
67
61
  this.getExecutor().scope.variables.set(reference.name, value);
68
62
  }
69
- else if (reference.value instanceof ClassComponent_1.ClassComponent) {
63
+ }
64
+ else {
65
+ if (reference.value instanceof ClassComponent_1.ClassComponent) {
70
66
  this.setInstanceParam(reference.value, trailers, value);
71
67
  }
68
+ else if (reference.value instanceof Object) {
69
+ reference.value[trailers.join('.')] = value;
70
+ }
72
71
  }
73
72
  this.setResult(ctx, value);
74
73
  };
74
+ this.visitOperator_assignment_expr = (ctx) => {
75
+ const reference = this.getReference(ctx.atom_expr());
76
+ const ctxDataExpr = ctx.data_expr();
77
+ this.visit(ctxDataExpr);
78
+ const value = this.getResult(ctxDataExpr);
79
+ if (!reference.found) {
80
+ throw 'Undefined reference: ' + reference.name;
81
+ }
82
+ const trailers = reference.trailers ?? [];
83
+ let currentValue = null;
84
+ if (trailers.length === 0) {
85
+ currentValue = this.getExecutor().scope.variables.get(reference.name);
86
+ }
87
+ else {
88
+ if (reference.value instanceof ClassComponent_1.ClassComponent) {
89
+ currentValue = this.getInstanceParam(reference.value, trailers);
90
+ }
91
+ else if (reference.value instanceof Object) {
92
+ currentValue = reference.value[trailers.join('.')];
93
+ }
94
+ }
95
+ if (currentValue === null) {
96
+ throw 'Operator assignment failed: could not get value';
97
+ }
98
+ let newValue = 0;
99
+ if (ctx.AdditionAssign()) {
100
+ newValue = currentValue + value;
101
+ }
102
+ else if (ctx.MinusAssign()) {
103
+ newValue = currentValue - value;
104
+ }
105
+ else if (ctx.MultiplyAssign()) {
106
+ newValue = currentValue * value;
107
+ }
108
+ else if (ctx.DivideAssign()) {
109
+ newValue = currentValue / value;
110
+ }
111
+ else if (ctx.ModulusAssign()) {
112
+ newValue = currentValue % value;
113
+ }
114
+ else {
115
+ throw 'Operator assignment failed: could not perform operator';
116
+ }
117
+ if (trailers.length === 0) {
118
+ this.getExecutor().scope.variables.set(reference.name, newValue);
119
+ }
120
+ else {
121
+ if (reference.value instanceof ClassComponent_1.ClassComponent) {
122
+ this.setInstanceParam(reference.value, trailers, newValue);
123
+ }
124
+ else if (reference.value instanceof Object) {
125
+ reference.value[trailers.join('.')] = newValue;
126
+ }
127
+ }
128
+ this.setResult(ctx, newValue);
129
+ };
75
130
  this.visitAtom_expr = (ctx) => {
76
131
  const executor = this.getExecutor();
77
132
  const firstId = ctx.ID(0);
@@ -87,20 +142,22 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
87
142
  else {
88
143
  currentReference = executor.resolveVariable(this.executionStack, atomId);
89
144
  }
145
+ const idTrailers = [];
146
+ if (ctx.ID().length > 1) {
147
+ const idLength = ctx.ID().length;
148
+ for (let i = 1; i < idLength; i++) {
149
+ const tmpCtx = ctx.ID(i);
150
+ idTrailers.push(tmpCtx.getText());
151
+ }
152
+ }
153
+ currentReference.trailers = idTrailers;
90
154
  if (currentReference.found && currentReference.type === 'instance') {
91
155
  const tmpComponent = currentReference.value;
92
156
  for (const [pinId, net] of tmpComponent.pinNets) {
93
157
  executor.scope.setNet(tmpComponent, pinId, net);
94
158
  }
95
- if (ctx.ID().length > 1) {
96
- currentReference.trailers = [];
97
- const idLength = ctx.ID().length;
98
- for (let i = 1; i < idLength; i++) {
99
- const tmpCtx = ctx.ID(i);
100
- currentReference.trailers.push(tmpCtx.getText());
101
- }
102
- }
103
159
  }
160
+ this.getExecutor().log('atomId:', atomId, currentReference);
104
161
  if (ctx.parent instanceof CircuitScriptParser_1.ExpressionContext && !currentReference.found) {
105
162
  throw "Unknown symbol: " + atomId;
106
163
  }
@@ -160,7 +217,6 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
160
217
  const ctxBooleanValue = ctx.BOOLEAN_VALUE();
161
218
  const ctxStringValue = ctx.STRING_VALUE();
162
219
  const ctxPercentageValue = ctx.PERCENTAGE_VALUE();
163
- const ctxBlankExpr = ctx.blank_expr();
164
220
  let result = null;
165
221
  if (ctxIntegerValue || ctxDecimalValue || ctxNumericValue) {
166
222
  if (ctxIntegerValue) {
@@ -194,15 +250,8 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
194
250
  else if (ctxPercentageValue) {
195
251
  result = new ParamDefinition_1.PercentageValue(ctxPercentageValue.getText());
196
252
  }
197
- else if (ctxBlankExpr) {
198
- this.visit(ctxBlankExpr);
199
- result = this.getResult(ctxBlankExpr);
200
- }
201
253
  this.setResult(ctx, result);
202
254
  };
203
- this.visitBlank_expr = (ctx) => {
204
- this.setResult(ctx, new ParamDefinition_1.PinBlankValue(Number(ctx.INTEGER_VALUE().getText())));
205
- };
206
255
  this.visitValueAtomExpr = (ctx) => {
207
256
  let value = null;
208
257
  const ctxValueExpr = ctx.value_expr();
@@ -284,8 +333,32 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
284
333
  this.setResult(ctx, returnValue);
285
334
  };
286
335
  this.visitBreak_keyword = (ctx) => {
287
- this.getExecutor().breakBranch();
288
- this.setResult(ctx, -1);
336
+ const breakContext = this.getExecutor().getBreakContext();
337
+ const currentResult = this.getResult(breakContext) ?? {};
338
+ this.setResult(breakContext, {
339
+ ...currentResult,
340
+ breakSignal: true
341
+ });
342
+ };
343
+ this.visitContinue_keyword = (ctx) => {
344
+ const breakContext = this.getExecutor().getBreakContext();
345
+ const currentResult = this.getResult(breakContext) ?? {};
346
+ this.setResult(breakContext, {
347
+ ...currentResult,
348
+ breakSignal: true,
349
+ continueSignal: true,
350
+ });
351
+ };
352
+ this.visitArray_expr = (ctx) => {
353
+ const array = ctx.data_expr().map(item => {
354
+ this.visit(item);
355
+ return this.getResult(item);
356
+ });
357
+ this.setResult(ctx, array);
358
+ };
359
+ this.visitArrayExpr = (ctx) => {
360
+ this.visit(ctx.array_expr());
361
+ this.setResult(ctx, this.getResult(ctx.array_expr()));
289
362
  };
290
363
  this.visitRoundedBracketsExpr = (ctx) => {
291
364
  const ctxDataExpr = ctx.data_expr();
@@ -296,6 +369,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
296
369
  this.logger = new logger_1.Logger();
297
370
  this.onErrorCallbackHandler = onErrorHandler;
298
371
  this.startingContext = new execute_1.ExecutionContext('__', '__.', '/', 0, 0, silent, this.logger, null);
372
+ this.startingContext.scope.variables.set(globals_1.GlobalDocumentName, {});
299
373
  this.setupPrintFunction(this.startingContext);
300
374
  this.executionStack = [this.startingContext];
301
375
  this.startingContext.resolveNet =
@@ -307,10 +381,30 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
307
381
  getExecutor() {
308
382
  return this.executionStack[this.executionStack.length - 1];
309
383
  }
384
+ toString(obj) {
385
+ if (typeof obj === 'string') {
386
+ return `"${obj}"`;
387
+ }
388
+ else if (typeof obj === 'number') {
389
+ return obj.toString();
390
+ }
391
+ else if (Array.isArray(obj)) {
392
+ const inner = obj.map(item => this.toString(item)).join(", ");
393
+ return "[" + inner + "]";
394
+ }
395
+ else {
396
+ if (obj.toString) {
397
+ return obj.toString();
398
+ }
399
+ else {
400
+ throw "Could not create string from object: " + obj;
401
+ }
402
+ }
403
+ }
310
404
  setupPrintFunction(context) {
311
405
  context.createFunction('print', (params) => {
312
406
  const items = params.map(([, , value]) => {
313
- return value;
407
+ return this.toString(value);
314
408
  });
315
409
  if (this.printToConsole) {
316
410
  console.log('::', ...items);
@@ -318,6 +412,38 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
318
412
  this.printStream.push(...items);
319
413
  return [this, null];
320
414
  });
415
+ context.createFunction('range', (params) => {
416
+ const items = params.map(([, , value]) => {
417
+ if (isNaN(value)) {
418
+ throw 'Invalid value: ' + value;
419
+ }
420
+ return value;
421
+ });
422
+ let startValue = 0;
423
+ let endValue = 0;
424
+ if (items.length === 1) {
425
+ endValue = items[0];
426
+ }
427
+ else if (items.length === 2) {
428
+ startValue = items[0];
429
+ endValue = items[1];
430
+ }
431
+ const returnArray = [];
432
+ for (let i = startValue; i < endValue; i++) {
433
+ returnArray.push(i);
434
+ }
435
+ return [this, returnArray];
436
+ });
437
+ context.createFunction('enumerate', (params) => {
438
+ const [, , array] = params[0];
439
+ if (!Array.isArray(array)) {
440
+ throw "Invalid parameter for enumerate function!";
441
+ }
442
+ const output = array.map((item, index) => {
443
+ return [index, item];
444
+ });
445
+ return [this, output];
446
+ });
321
447
  }
322
448
  createNetResolver(executionStack) {
323
449
  const resolveNet = (netName, netNamespace) => {
@@ -351,6 +477,18 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
351
477
  log2(message) {
352
478
  this.getExecutor().log(message);
353
479
  }
480
+ getReference(ctx) {
481
+ const atomStr = ctx.getText();
482
+ if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
483
+ throw "Invalid assignment expression!";
484
+ }
485
+ this.visit(ctx);
486
+ const reference = this.getResult(ctx);
487
+ if (!reference.found && reference.trailers && reference.trailers.length > 0) {
488
+ throw 'Undefined reference: ' + reference.name + '.' + reference.trailers.join('.');
489
+ }
490
+ return reference;
491
+ }
354
492
  setResult(ctx, value) {
355
493
  this.resultData.set(ctx, value);
356
494
  }
@@ -441,9 +579,15 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
441
579
  }
442
580
  runExpressions(executor, expressions) {
443
581
  let returnValue = null;
582
+ const breakContext = executor.getBreakContext();
444
583
  for (let i = 0; i < expressions.length; i++) {
445
584
  const expr = expressions[i];
446
585
  this.visit(expr);
586
+ const { breakSignal = false } = this.getResult(breakContext) ?? {};
587
+ if (breakSignal) {
588
+ returnValue = executor.returnValue;
589
+ break;
590
+ }
447
591
  if (executor.stopFurtherExpressions) {
448
592
  returnValue = executor.returnValue;
449
593
  break;
@@ -477,6 +621,10 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
477
621
  object.setParam(paramName, value);
478
622
  this.getExecutor().log(`set instance ${object.instanceName} param ${paramName} to ${value}`);
479
623
  }
624
+ getInstanceParam(object, trailers) {
625
+ const paramName = trailers[0];
626
+ return object.getParam(paramName);
627
+ }
480
628
  enterNewChildContext(executionStack, parentContext, executionContextName, options, funcDefinedParameters, passedInParameters) {
481
629
  const { netNamespace = "" } = options;
482
630
  const currentExecutionContext = executionStack[executionStack.length - 1];