circuitscript 0.1.5 → 0.1.8

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 (97) hide show
  1. package/dist/cjs/BaseVisitor.js +127 -73
  2. package/dist/cjs/SemanticTokenVisitor.js +19 -13
  3. package/dist/cjs/antlr/CircuitScriptParser.js +711 -671
  4. package/dist/cjs/builtinMethods.js +29 -25
  5. package/dist/cjs/environment.js +118 -0
  6. package/dist/cjs/execute.js +53 -12
  7. package/dist/cjs/export.js +0 -5
  8. package/dist/cjs/geometry.js +1 -0
  9. package/dist/cjs/globals.js +11 -6
  10. package/dist/cjs/helpers.js +152 -127
  11. package/dist/cjs/index.js +5 -0
  12. package/dist/cjs/layout.js +86 -44
  13. package/dist/cjs/main.js +31 -19
  14. package/dist/cjs/objects/ExecutionScope.js +33 -0
  15. package/dist/cjs/objects/ParamDefinition.js +15 -15
  16. package/dist/cjs/parser.js +27 -21
  17. package/dist/cjs/regenerate-tests.js +14 -10
  18. package/dist/cjs/render.js +3 -1
  19. package/dist/cjs/sizing.js +5 -58
  20. package/dist/cjs/utils.js +85 -30
  21. package/dist/cjs/validate/SymbolTable.js +96 -0
  22. package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +14 -0
  23. package/dist/cjs/validate/SymbolValidatorVisitor.js +170 -0
  24. package/dist/cjs/validate.js +71 -44
  25. package/dist/cjs/visitor.js +140 -24
  26. package/dist/esm/{BaseVisitor.mjs → BaseVisitor.js} +98 -45
  27. package/dist/esm/{SemanticTokenVisitor.mjs → SemanticTokenVisitor.js} +17 -11
  28. package/dist/esm/antlr/{CircuitScriptParser.mjs → CircuitScriptParser.js} +711 -671
  29. package/dist/esm/{builtinMethods.mjs → builtinMethods.js} +20 -16
  30. package/dist/esm/{draw_symbols.mjs → draw_symbols.js} +7 -7
  31. package/dist/esm/environment.js +110 -0
  32. package/dist/esm/{execute.mjs → execute.js} +66 -25
  33. package/dist/esm/{export.mjs → export.js} +2 -7
  34. package/dist/esm/{geometry.mjs → geometry.js} +6 -5
  35. package/dist/esm/{globals.mjs → globals.js} +6 -1
  36. package/dist/esm/helpers.js +394 -0
  37. package/dist/esm/index.js +20 -0
  38. package/dist/esm/{layout.mjs → layout.js} +72 -53
  39. package/dist/esm/{lexer.mjs → lexer.js} +2 -2
  40. package/dist/esm/{main.mjs → main.js} +33 -21
  41. package/dist/esm/objects/{ClassComponent.mjs → ClassComponent.js} +5 -4
  42. package/dist/esm/objects/{ExecutionScope.mjs → ExecutionScope.js} +33 -0
  43. package/dist/esm/objects/{Frame.mjs → Frame.js} +1 -1
  44. package/dist/esm/objects/{ParamDefinition.mjs → ParamDefinition.js} +1 -1
  45. package/dist/esm/objects/{PinDefinition.mjs → PinDefinition.js} +1 -1
  46. package/dist/esm/parser.js +71 -0
  47. package/dist/esm/{regenerate-tests.mjs → regenerate-tests.js} +15 -11
  48. package/dist/esm/{render.mjs → render.js} +11 -9
  49. package/dist/esm/{sizing.mjs → sizing.js} +6 -34
  50. package/dist/esm/{utils.mjs → utils.js} +61 -17
  51. package/dist/esm/validate/SymbolTable.js +90 -0
  52. package/dist/esm/validate/SymbolValidatorResolveVisitor.js +10 -0
  53. package/dist/esm/validate/SymbolValidatorVisitor.js +163 -0
  54. package/dist/esm/validate.js +105 -0
  55. package/dist/esm/{visitor.mjs → visitor.js} +151 -35
  56. package/dist/fonts/Arial.ttf +0 -0
  57. package/dist/fonts/Inter-Bold.ttf +0 -0
  58. package/dist/fonts/Inter-Regular.ttf +0 -0
  59. package/dist/fonts/OpenSans-Regular.ttf +0 -0
  60. package/dist/fonts/Roboto-Regular.ttf +0 -0
  61. package/dist/libs/lib.cst +423 -0
  62. package/dist/types/BaseVisitor.d.ts +34 -21
  63. package/dist/types/SemanticTokenVisitor.d.ts +6 -5
  64. package/dist/types/antlr/CircuitScriptParser.d.ts +4 -2
  65. package/dist/types/builtinMethods.d.ts +3 -2
  66. package/dist/types/environment.d.ts +31 -0
  67. package/dist/types/globals.d.ts +4 -1
  68. package/dist/types/helpers.d.ts +12 -14
  69. package/dist/types/index.d.ts +5 -0
  70. package/dist/types/layout.d.ts +2 -2
  71. package/dist/types/objects/ClassComponent.d.ts +1 -0
  72. package/dist/types/objects/ExecutionScope.d.ts +11 -0
  73. package/dist/types/objects/types.d.ts +6 -1
  74. package/dist/types/parser.d.ts +7 -11
  75. package/dist/types/sizing.d.ts +0 -3
  76. package/dist/types/utils.d.ts +30 -6
  77. package/dist/types/validate/SymbolTable.d.ts +40 -0
  78. package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +7 -0
  79. package/dist/types/validate/SymbolValidatorVisitor.d.ts +32 -0
  80. package/dist/types/validate.d.ts +1 -1
  81. package/package.json +15 -14
  82. package/dist/cjs/SymbolValidatorVisitor.js +0 -233
  83. package/dist/esm/SymbolValidatorVisitor.mjs +0 -222
  84. package/dist/esm/helpers.mjs +0 -364
  85. package/dist/esm/index.mjs +0 -15
  86. package/dist/esm/parser.mjs +0 -64
  87. package/dist/esm/validate.mjs +0 -74
  88. package/dist/types/SymbolValidatorVisitor.d.ts +0 -61
  89. /package/dist/esm/antlr/{CircuitScriptLexer.mjs → CircuitScriptLexer.js} +0 -0
  90. /package/dist/esm/antlr/{CircuitScriptVisitor.mjs → CircuitScriptVisitor.js} +0 -0
  91. /package/dist/esm/{fonts.mjs → fonts.js} +0 -0
  92. /package/dist/esm/{logger.mjs → logger.js} +0 -0
  93. /package/dist/esm/objects/{Net.mjs → Net.js} +0 -0
  94. /package/dist/esm/objects/{PinTypes.mjs → PinTypes.js} +0 -0
  95. /package/dist/esm/objects/{Wire.mjs → Wire.js} +0 -0
  96. /package/dist/esm/objects/{types.mjs → types.js} +0 -0
  97. /package/dist/esm/{server.mjs → server.js} +0 -0
@@ -1,48 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseVisitor = void 0;
4
- const fs_1 = require("fs");
5
- const path_1 = require("path");
6
4
  const big_js_1 = require("big.js");
7
- const CircuitScriptParser_1 = require("./antlr/CircuitScriptParser");
8
- const CircuitScriptVisitor_1 = require("./antlr/CircuitScriptVisitor");
9
- const execute_1 = require("./execute");
10
- const logger_1 = require("./logger");
11
- const ClassComponent_1 = require("./objects/ClassComponent");
12
- const ParamDefinition_1 = require("./objects/ParamDefinition");
13
- const PinTypes_1 = require("./objects/PinTypes");
14
- const types_1 = require("./objects/types");
15
- const globals_1 = require("./globals");
16
- const builtinMethods_1 = require("./builtinMethods");
17
- const utils_1 = require("./utils");
18
- const ExecutionScope_1 = require("./objects/ExecutionScope");
19
- class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
20
- constructor(silent = false, onErrorHandler = null, currentDirectory, defaultLibsPath) {
5
+ const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
6
+ const CircuitScriptVisitor_js_1 = require("./antlr/CircuitScriptVisitor.js");
7
+ const execute_js_1 = require("./execute.js");
8
+ const logger_js_1 = require("./logger.js");
9
+ const ClassComponent_js_1 = require("./objects/ClassComponent.js");
10
+ const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
11
+ const PinTypes_js_1 = require("./objects/PinTypes.js");
12
+ const types_js_1 = require("./objects/types.js");
13
+ const globals_js_1 = require("./globals.js");
14
+ const builtinMethods_js_1 = require("./builtinMethods.js");
15
+ const utils_js_1 = require("./utils.js");
16
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
17
+ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
18
+ constructor(silent = false, onErrorHandler = null, environment) {
21
19
  super();
22
20
  this.indentLevel = 0;
23
21
  this.silent = false;
24
22
  this.printStream = [];
25
23
  this.printToConsole = true;
26
- this.acceptedDirections = [types_1.Direction.Up, types_1.Direction.Down,
27
- types_1.Direction.Right, types_1.Direction.Left];
24
+ this.acceptedDirections = [types_js_1.Direction.Up, types_js_1.Direction.Down,
25
+ types_js_1.Direction.Right, types_js_1.Direction.Left];
28
26
  this.resultData = new Map;
29
27
  this.paramData = new Map;
30
28
  this.pinTypesList = [
31
- PinTypes_1.PinTypes.Any,
32
- PinTypes_1.PinTypes.Input,
33
- PinTypes_1.PinTypes.Output,
34
- PinTypes_1.PinTypes.IO,
35
- PinTypes_1.PinTypes.Power,
29
+ PinTypes_js_1.PinTypes.Any,
30
+ PinTypes_js_1.PinTypes.Input,
31
+ PinTypes_js_1.PinTypes.Output,
32
+ PinTypes_js_1.PinTypes.IO,
33
+ PinTypes_js_1.PinTypes.Power,
36
34
  ];
37
- this.onErrorCallbackHandler = null;
38
- this.onImportFile = (visitor, filePath) => {
35
+ this.onErrorHandler = null;
36
+ this.importedFiles = [];
37
+ this.onImportFile = async (visitor, filePath, fileData, onErrorHandler) => {
39
38
  throw "Import file not implemented";
40
39
  };
41
- this.visitScript = (ctx) => {
40
+ this.visitScript = async (ctx) => {
42
41
  this.log('===', 'start', '===');
43
- const result = this.visitChildren(ctx);
42
+ const imports = ctx.import_expr();
43
+ for (let i = 0; i < imports.length; i++) {
44
+ const ctxImport = imports[i];
45
+ const ID = ctxImport.ID().toString();
46
+ await this.handleImportFile(ID, true, ctxImport);
47
+ }
48
+ const result = this.runExpressions(this.getExecutor(), ctx.expression());
49
+ this.setResult(ctx, result);
44
50
  this.log('===', 'end', '===');
45
- return result;
46
51
  };
47
52
  this.visitAssignment_expr = (ctx) => {
48
53
  const reference = this.getReference(ctx.atom_expr());
@@ -51,10 +56,10 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
51
56
  const value = this.getResult(ctxDataExpr);
52
57
  const trailers = reference.trailers ?? [];
53
58
  if (trailers.length === 0) {
54
- if (value instanceof ClassComponent_1.ClassComponent) {
59
+ if (value instanceof ClassComponent_js_1.ClassComponent) {
55
60
  const variables = this.getExecutor().scope.variables;
56
61
  variables.set(reference.name, value);
57
- this.getExecutor().scope.sequence.push([ExecutionScope_1.SequenceAction.Assign, reference.name, value]);
62
+ this.getExecutor().scope.sequence.push([ExecutionScope_js_1.SequenceAction.Assign, reference.name, value]);
58
63
  this.log2(`assigned '${reference.name}' to ClassComponent`);
59
64
  }
60
65
  else {
@@ -63,7 +68,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
63
68
  }
64
69
  }
65
70
  else {
66
- if (reference.parentValue instanceof ClassComponent_1.ClassComponent) {
71
+ if (reference.parentValue instanceof ClassComponent_js_1.ClassComponent) {
67
72
  this.setInstanceParam(reference.parentValue, trailers, value);
68
73
  this.log2(`assigned component param ${reference.parentValue} trailers: ${trailers} value: ${value}`);
69
74
  }
@@ -88,7 +93,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
88
93
  currentValue = this.getExecutor().scope.variables.get(reference.name);
89
94
  }
90
95
  else {
91
- if (reference.value instanceof ClassComponent_1.ClassComponent) {
96
+ if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
92
97
  currentValue = this.getInstanceParam(reference.value, trailers);
93
98
  }
94
99
  else if (reference.value instanceof Object) {
@@ -99,7 +104,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
99
104
  this.throwWithContext(ctx, 'Operator assignment failed: could not get value');
100
105
  }
101
106
  let newValue = 0;
102
- const operator = new ParamDefinition_1.NumberOperator();
107
+ const operator = new ParamDefinition_js_1.NumberOperator();
103
108
  if (ctx.AdditionAssign()) {
104
109
  newValue = operator.addition(currentValue, value);
105
110
  }
@@ -122,7 +127,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
122
127
  this.getExecutor().scope.variables.set(reference.name, newValue);
123
128
  }
124
129
  else {
125
- if (reference.value instanceof ClassComponent_1.ClassComponent) {
130
+ if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
126
131
  this.setInstanceParam(reference.value, trailers, newValue);
127
132
  }
128
133
  else if (reference.value instanceof Object) {
@@ -148,7 +153,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
148
153
  currentReference = {
149
154
  found: true,
150
155
  value: atomId,
151
- type: globals_1.ReferenceTypes.pinType,
156
+ type: globals_js_1.ReferenceTypes.pinType,
152
157
  };
153
158
  }
154
159
  else {
@@ -160,7 +165,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
160
165
  executor.scope.setNet(tmpComponent, pinId, net);
161
166
  }
162
167
  }
163
- if (ctx.parent instanceof CircuitScriptParser_1.ExpressionContext && !currentReference.found) {
168
+ if (ctx.parent instanceof CircuitScriptParser_js_1.ExpressionContext && !currentReference.found) {
164
169
  this.throwWithContext(ctx, "Unknown symbol: " + atomId);
165
170
  }
166
171
  this.setResult(ctx, currentReference);
@@ -201,7 +206,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
201
206
  currentReference = {
202
207
  found: true,
203
208
  value: functionResult,
204
- type: (functionResult instanceof ClassComponent_1.ClassComponent) ?
209
+ type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
205
210
  'instance' : 'value',
206
211
  };
207
212
  }
@@ -227,14 +232,14 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
227
232
  let result = null;
228
233
  if (ctxIntegerValue || ctxDecimalValue || ctxNumericValue) {
229
234
  if (ctxIntegerValue) {
230
- result = (0, utils_1.resolveToNumericValue)((new big_js_1.Big(ctxIntegerValue.getText())).mul(new big_js_1.Big(sign)));
235
+ result = (0, utils_js_1.resolveToNumericValue)((new big_js_1.Big(ctxIntegerValue.getText())).mul(new big_js_1.Big(sign)));
231
236
  }
232
237
  else if (ctxDecimalValue) {
233
- result = (0, utils_1.resolveToNumericValue)((new big_js_1.Big(ctxDecimalValue.getText())).mul(new big_js_1.Big(sign)));
238
+ result = (0, utils_js_1.resolveToNumericValue)((new big_js_1.Big(ctxDecimalValue.getText())).mul(new big_js_1.Big(sign)));
234
239
  }
235
240
  else if (ctxNumericValue) {
236
241
  const textExtra = ctx.Minus() ? '-' : '';
237
- result = new ParamDefinition_1.NumericValue(textExtra + ctxNumericValue.getText());
242
+ result = new ParamDefinition_js_1.NumericValue(textExtra + ctxNumericValue.getText());
238
243
  }
239
244
  }
240
245
  else {
@@ -255,7 +260,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
255
260
  result = this.prepareStringValue(ctxStringValue.getText());
256
261
  }
257
262
  else if (ctxPercentageValue) {
258
- result = new ParamDefinition_1.PercentageValue(ctxPercentageValue.getText());
263
+ result = new ParamDefinition_js_1.PercentageValue(ctxPercentageValue.getText());
259
264
  }
260
265
  this.setResult(ctx, result);
261
266
  };
@@ -271,10 +276,10 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
271
276
  this.visit(ctxAtomExpr);
272
277
  const reference = this.getResult(ctxAtomExpr);
273
278
  if (!reference.found) {
274
- value = new types_1.UndeclaredReference(reference);
279
+ value = new types_js_1.UndeclaredReference(reference);
275
280
  }
276
281
  else {
277
- if (reference.type && reference.type === globals_1.ReferenceTypes.pinType) {
282
+ if (reference.type && reference.type === globals_js_1.ReferenceTypes.pinType) {
278
283
  value = reference;
279
284
  }
280
285
  else {
@@ -299,10 +304,10 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
299
304
  const tmpCtx = defaultValuesProvided[index - boundary];
300
305
  this.visit(tmpCtx);
301
306
  const defaultValue = this.getResult(tmpCtx);
302
- return [idText, defaultValue];
307
+ return [idText, tmpCtx.start, defaultValue];
303
308
  }
304
309
  else {
305
- return [idText];
310
+ return [idText, id.getSymbol()];
306
311
  }
307
312
  });
308
313
  this.setResult(ctx, result);
@@ -324,10 +329,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
324
329
  this.setResult(ctx, returnList);
325
330
  };
326
331
  this.visitImport_expr = (ctx) => {
327
- const ID = ctx.ID().toString();
328
- this.log('import', ID);
329
- this.handleImportFile(ID, true, ctx);
330
- this.log('done import', ID);
332
+ throw new utils_js_1.RuntimeExecutionError("Cannot parse imports here", ctx.start, ctx.stop);
331
333
  };
332
334
  this.visitFunction_return_expr = (ctx) => {
333
335
  const executor = this.getExecutor();
@@ -373,14 +375,15 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
373
375
  const innerResult = this.getResult(ctxDataExpr);
374
376
  this.setResult(ctx, innerResult);
375
377
  };
376
- this.logger = new logger_1.Logger();
377
- this.onErrorCallbackHandler = onErrorHandler;
378
- this.startingContext = new execute_1.ExecutionContext(globals_1.DoubleDelimiter1, `${globals_1.DoubleDelimiter1}.`, '/', 0, 0, silent, this.logger, null);
378
+ this.logger = new logger_js_1.Logger();
379
+ this.onErrorHandler = onErrorHandler;
380
+ this.environment = environment;
381
+ this.startingContext = new execute_js_1.ExecutionContext(globals_js_1.DoubleDelimiter1, `${globals_js_1.DoubleDelimiter1}.`, '/', 0, 0, silent, this.logger, null);
379
382
  const scope = this.startingContext.scope;
380
383
  scope.sequence.push([
381
- ExecutionScope_1.SequenceAction.At, scope.componentRoot, scope.currentPin
384
+ ExecutionScope_js_1.SequenceAction.At, scope.componentRoot, scope.currentPin
382
385
  ]);
383
- scope.variables.set(globals_1.GlobalDocumentName, {});
386
+ scope.variables.set(globals_js_1.GlobalDocumentName, {});
384
387
  this.setupBuiltInFunctions(this.startingContext);
385
388
  this.executionStack = [this.startingContext];
386
389
  this.startingContext.resolveNet =
@@ -388,14 +391,18 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
388
391
  this.startingContext.resolveComponentPinNet =
389
392
  this.createComponentPinNetResolver(this.executionStack);
390
393
  this.silent = silent;
391
- this.currentDirectory = currentDirectory;
392
- this.defaultLibsPath = defaultLibsPath;
393
394
  }
394
395
  getExecutor() {
395
396
  return this.executionStack[this.executionStack.length - 1];
396
397
  }
398
+ getScope() {
399
+ return this.getExecutor().scope;
400
+ }
401
+ getRootExecutor() {
402
+ return this.executionStack[0];
403
+ }
397
404
  setupBuiltInFunctions(context) {
398
- (0, builtinMethods_1.linkBuiltInMethods)(context, this);
405
+ (0, builtinMethods_js_1.linkBuiltInMethods)(context, this);
399
406
  }
400
407
  createNetResolver(executionStack) {
401
408
  const resolveNet = (netName, netNamespace) => {
@@ -442,6 +449,10 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
442
449
  log2(message) {
443
450
  this.getExecutor().log(message);
444
451
  }
452
+ async visitAsync(ctx) {
453
+ const result = await ctx.accept(this);
454
+ return result;
455
+ }
445
456
  getReference(ctx) {
446
457
  const atomStr = ctx.getText();
447
458
  if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
@@ -467,15 +478,24 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
467
478
  this.visit(ctx);
468
479
  return this.getResult(ctx);
469
480
  }
470
- handleImportFile(name, throwErrors = true, ctx = null) {
481
+ async handleImportFile(name, throwErrors = true, ctx = null) {
482
+ name = name.trim();
483
+ const importAlready = this.importedFiles.find(item => {
484
+ return item.id === name;
485
+ });
486
+ if (importAlready) {
487
+ return importAlready;
488
+ }
471
489
  let hasError = false;
472
490
  let hasParseError = false;
473
491
  let pathExists = false;
474
- const tmpFilePath = (0, path_1.join)(this.currentDirectory, name + ".cst");
492
+ const tmpFilePath = this.environment.getRelativeToModule(name + ".cst");
475
493
  this.log('importing path:', tmpFilePath);
476
494
  let fileData = null;
495
+ let filePathUsed = null;
477
496
  try {
478
- fileData = (0, fs_1.readFileSync)(tmpFilePath, { encoding: 'utf8' });
497
+ filePathUsed = tmpFilePath;
498
+ fileData = await this.environment.readFile(tmpFilePath, { encoding: 'utf8' });
479
499
  pathExists = true;
480
500
  }
481
501
  catch (err) {
@@ -483,8 +503,9 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
483
503
  }
484
504
  if (!pathExists) {
485
505
  try {
486
- const tmpFilePath2 = (0, path_1.join)(this.defaultLibsPath, name + ".cst");
487
- fileData = (0, fs_1.readFileSync)(tmpFilePath2, { encoding: 'utf8' });
506
+ const tmpFilePath2 = this.environment.getRelativeToDefaultLibs(name + ".cst");
507
+ filePathUsed = tmpFilePath2;
508
+ fileData = await this.environment.readFile(tmpFilePath2, { encoding: 'utf8' });
488
509
  pathExists = true;
489
510
  }
490
511
  catch (err) {
@@ -494,31 +515,39 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
494
515
  try {
495
516
  if (pathExists) {
496
517
  this.log('done reading imported file data');
497
- const importResult = this.onImportFile(this, fileData, this.onErrorCallbackHandler);
518
+ const importResult = await this.onImportFile(this, filePathUsed, fileData, this.onErrorHandler);
498
519
  hasError = importResult.hasError;
499
520
  hasParseError = importResult.hasParseError;
500
521
  }
501
522
  }
502
523
  catch (err) {
503
- this.log('Failed to import file: ', err.message);
524
+ if (ctx != null) {
525
+ throw new utils_js_1.RuntimeExecutionError("An error occurred while importing file", ctx.start, ctx.stop);
526
+ }
527
+ else {
528
+ this.log('An error occurred while importing file:', err.message);
529
+ }
504
530
  }
505
531
  let errorMessage = null;
506
532
  if (throwErrors && (hasError || hasParseError || !pathExists)) {
507
533
  if (!pathExists) {
508
- errorMessage = `File does not exist: ${name}`;
534
+ errorMessage = `File does not exist: ${name} (${filePathUsed})`;
509
535
  }
510
536
  else {
511
537
  errorMessage = `Failed to import: ${name}`;
512
538
  }
513
539
  }
514
540
  if (errorMessage !== null && ctx) {
515
- this.throwWithContext(ctx, errorMessage);
541
+ throw new utils_js_1.RuntimeExecutionError(errorMessage, ctx.start, ctx.end);
516
542
  }
517
- return {
543
+ const newImportedFile = {
544
+ id: name.trim(),
518
545
  hasError,
519
546
  hasParseError,
520
547
  pathExists,
521
548
  };
549
+ this.importedFiles.push(newImportedFile);
550
+ return newImportedFile;
522
551
  }
523
552
  setupDefinedParameters(funcDefinedParameters, passedInParameters, executor) {
524
553
  for (let i = 0; i < funcDefinedParameters.length; i++) {
@@ -529,7 +558,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
529
558
  const variableName = tmpFuncArg[0];
530
559
  executor.log('set variable in scope, var name: ', variableName);
531
560
  executor.scope.variables.set(variableName, tmpPassedInArgs[2]);
532
- if (tmpPassedInArgs[2] instanceof ClassComponent_1.ClassComponent) {
561
+ if (tmpPassedInArgs[2] instanceof ClassComponent_js_1.ClassComponent) {
533
562
  const component = tmpPassedInArgs[2];
534
563
  for (const [pinNumber, net] of component.pinNets) {
535
564
  executor.scope.setNet(component, pinNumber, net);
@@ -542,9 +571,9 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
542
571
  executor.scope.variables.set(variableName, tmpPassedInArgs[2]);
543
572
  }
544
573
  }
545
- else if (tmpFuncArg.length === 2) {
574
+ else if (tmpFuncArg.length === 3) {
546
575
  const variableName = tmpFuncArg[0];
547
- const defaultValue = tmpFuncArg[1];
576
+ const defaultValue = tmpFuncArg[2];
548
577
  executor.log('set variable in scope, var name: ', variableName);
549
578
  executor.scope.variables.set(variableName, defaultValue);
550
579
  }
@@ -607,7 +636,7 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
607
636
  const executionLevel = currentExecutionContext.executionLevel;
608
637
  const executionContextNamespace = currentExecutionContext.namespace
609
638
  + executionContextName + ".";
610
- const newExecutor = new execute_1.ExecutionContext(executionContextName, executionContextNamespace, netNamespace, executionLevel + 1, this.getExecutor().scope.indentLevel + 1, currentExecutionContext.silent, currentExecutionContext.logger, parentContext);
639
+ const newExecutor = new execute_js_1.ExecutionContext(executionContextName, executionContextNamespace, netNamespace, executionLevel + 1, this.getExecutor().scope.indentLevel + 1, currentExecutionContext.silent, currentExecutionContext.logger, parentContext);
611
640
  executionStack.push(newExecutor);
612
641
  this.setupDefinedParameters(funcDefinedParameters, passedInParameters, newExecutor);
613
642
  return newExecutor;
@@ -615,8 +644,33 @@ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
615
644
  prepareStringValue(value) {
616
645
  return value.slice(1, value.length - 1);
617
646
  }
618
- throwWithContext(context, message) {
619
- (0, utils_1.throwWithContext)(context, message);
647
+ throwWithContext(context, messageOrError) {
648
+ (0, utils_js_1.throwWithContext)(context, messageOrError);
649
+ }
650
+ validateType(value, context, validateFunction, expectedType) {
651
+ if (value === undefined) {
652
+ return false;
653
+ }
654
+ const result = validateFunction(value);
655
+ if (!result) {
656
+ throw new utils_js_1.RuntimeExecutionError(`Invalid ${expectedType}`, context.start, context.stop);
657
+ }
658
+ return result;
659
+ }
660
+ validateString(value, context) {
661
+ this.validateType(value, context, (val) => {
662
+ return typeof val === 'string';
663
+ }, 'string');
664
+ }
665
+ validateBoolean(value, context) {
666
+ this.validateType(value, context, (val) => {
667
+ return typeof val === 'boolean';
668
+ }, 'boolean');
669
+ }
670
+ validateNumeric(value, context) {
671
+ this.validateType(value, context, (val) => {
672
+ return (val instanceof ParamDefinition_js_1.NumericValue);
673
+ }, 'numeric value');
620
674
  }
621
675
  }
622
676
  exports.BaseVisitor = BaseVisitor;
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.prepareTokens = exports.SemanticTokensVisitor = void 0;
4
- const CircuitScriptParser_1 = require("./antlr/CircuitScriptParser");
5
- const BaseVisitor_1 = require("./BaseVisitor");
6
- class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
7
- constructor(silent = false, onErrorHandler = null, currentDirectory, defaultsLibsPath, lexer, script) {
8
- super(silent, onErrorHandler, currentDirectory, defaultsLibsPath);
4
+ const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
5
+ const BaseVisitor_js_1 = require("./BaseVisitor.js");
6
+ const builtinMethods_js_1 = require("./builtinMethods.js");
7
+ const globals_js_1 = require("./globals.js");
8
+ class SemanticTokensVisitor extends BaseVisitor_js_1.BaseVisitor {
9
+ constructor(silent = false, onErrorHandler = null, environment, lexer, script) {
10
+ super(silent, onErrorHandler, environment);
9
11
  this.parsedTokens = [];
10
12
  this.semanticTokens = new Map();
11
13
  this.visitFunction_args_expr = (ctx) => {
@@ -15,7 +17,11 @@ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
15
17
  });
16
18
  };
17
19
  this.visitFunction_call_expr = (ctx) => {
18
- this.addSemanticToken(ctx.ID(), [], 'function');
20
+ const modifiers = [];
21
+ if (builtinMethods_js_1.buildInMethodNamesList.indexOf(ctx.ID().getText()) !== -1) {
22
+ modifiers.push('defaultLibrary');
23
+ }
24
+ this.addSemanticToken(ctx.ID(), modifiers, 'function');
19
25
  };
20
26
  this.visitFunction_def_expr = (ctx) => {
21
27
  const functionName = ctx.ID().getText();
@@ -24,7 +30,7 @@ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
24
30
  if (ctxFunctionArgsExpr) {
25
31
  this.visit(ctxFunctionArgsExpr);
26
32
  }
27
- const executionContextName = functionName + '_validate';
33
+ const executionContextName = functionName + globals_js_1.SymbolValidatorContext;
28
34
  const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, [], []);
29
35
  this.runExpressions(newExecutor, ctx.function_expr());
30
36
  this.executionStack.pop();
@@ -37,9 +43,8 @@ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
37
43
  };
38
44
  this.visitCreate_graphic_expr = (ctx) => {
39
45
  this.addSemanticToken(ctx.Create(), ['defaultLibrary'], 'function');
40
- ctx.graphic_expr().forEach(graphic_expr => {
41
- this.visit(graphic_expr);
42
- });
46
+ const graphicsExpressionsCtx = ctx.graphic_expressions_block();
47
+ this.visitResult(graphicsExpressionsCtx);
43
48
  };
44
49
  this.visitProperty_key_expr = (ctx) => {
45
50
  let useValue = null;
@@ -59,7 +64,7 @@ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
59
64
  this.addSemanticToken(useValue, [], 'property');
60
65
  }
61
66
  };
62
- this.visitGraphic_expr = (ctx) => {
67
+ this.visitGraphicCommandExpr = (ctx) => {
63
68
  let useValue = null;
64
69
  const ctxId = ctx.ID();
65
70
  const ctxPin = ctx.Pin();
@@ -88,7 +93,7 @@ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
88
93
  this.visit(ctx.data_expr());
89
94
  };
90
95
  this.visitAtom_expr = (ctx) => {
91
- if (ctx.parent instanceof CircuitScriptParser_1.Assignment_exprContext && ctx.ID(0)) {
96
+ if (ctx.parent instanceof CircuitScriptParser_js_1.Assignment_exprContext && ctx.ID(0)) {
92
97
  this.addSemanticToken(ctx.ID(0), [], 'variable');
93
98
  }
94
99
  };
@@ -182,7 +187,7 @@ const languageKeywords = [
182
187
  'break', 'branch', 'create', 'component',
183
188
  'graphic', 'wire', 'pin', 'add', 'at', 'to',
184
189
  'point', 'join', 'parallel', 'return', 'def', 'import',
185
- 'true', 'false', 'nc', 'frame',
190
+ 'true', 'false', 'nc', 'sheet', 'frame', 'if', 'for',
186
191
  ];
187
192
  const operatorKeywords = [
188
193
  'at', 'to', 'wire', 'add', 'frame', 'join', 'parallel', 'point'
@@ -206,6 +211,7 @@ function resolveTokenType(tokenType) {
206
211
  case 'ID':
207
212
  return 'variable';
208
213
  case 'Define':
214
+ case 'BOOLEAN_VALUE':
209
215
  return 'keyword';
210
216
  case 'COMMENT':
211
217
  return 'comment';