circuitscript 0.3.2 → 0.4.0

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 (147) hide show
  1. package/dist/cjs/BaseVisitor.js +394 -262
  2. package/dist/cjs/LexerDiagnosticListener.js +375 -0
  3. package/dist/cjs/{ComponentAnnotater.js → annotate/ComponentAnnotater.js} +29 -15
  4. package/dist/cjs/annotate/DefaultPostAnnotationCallback.js +126 -0
  5. package/dist/cjs/{RefdesAnnotationVisitor.js → annotate/RefdesAnnotationVisitor.js} +8 -82
  6. package/dist/cjs/annotate/utils.js +70 -0
  7. package/dist/cjs/antlr/CircuitScriptLexer.js +279 -286
  8. package/dist/cjs/antlr/CircuitScriptParser.js +1954 -3535
  9. package/dist/cjs/antlr/CircuitScriptParserVisitor.js +7 -0
  10. package/dist/cjs/cache/deserializer.js +34 -0
  11. package/dist/cjs/cache/hash.js +8 -0
  12. package/dist/cjs/cache/serializer.js +122 -0
  13. package/dist/cjs/cache/storage.js +45 -0
  14. package/dist/cjs/cache/types.js +4 -0
  15. package/dist/cjs/{environment.js → environment/environment.js} +18 -6
  16. package/dist/cjs/environment/esm-environment.js +21 -0
  17. package/dist/cjs/environment/helpers.js +8 -0
  18. package/dist/cjs/execute.js +49 -15
  19. package/dist/cjs/globals.js +9 -1
  20. package/dist/cjs/helpers.js +3 -485
  21. package/dist/cjs/importResolver.js +102 -0
  22. package/dist/cjs/index.js +7 -6
  23. package/dist/cjs/lexer.js +48 -12
  24. package/dist/cjs/main.js +14 -4
  25. package/dist/cjs/objects/ClassComponent.js +1 -1
  26. package/dist/cjs/objects/ExecutionScope.js +0 -1
  27. package/dist/cjs/objects/types.js +17 -1
  28. package/dist/cjs/parser.js +18 -4
  29. package/dist/cjs/pipeline.js +284 -0
  30. package/dist/cjs/regenerate-tests.js +4 -3
  31. package/dist/cjs/render/KiCadNetListOutputHandler.js +30 -0
  32. package/dist/cjs/render/PaperSizes.js +46 -0
  33. package/dist/cjs/{draw_symbols.js → render/draw_symbols.js} +58 -36
  34. package/dist/cjs/{export.js → render/export.js} +2 -2
  35. package/dist/cjs/{geometry.js → render/geometry.js} +5 -5
  36. package/dist/cjs/{graph.js → render/graph.js} +7 -7
  37. package/dist/cjs/{layout.js → render/layout.js} +8 -8
  38. package/dist/cjs/{render.js → render/render.js} +9 -8
  39. package/dist/cjs/rules-check/no-connect-on-connected-pin.js +1 -1
  40. package/dist/cjs/rules-check/unconnected-pins.js +1 -1
  41. package/dist/cjs/{SemanticTokenVisitor.js → semantic-tokens/SemanticTokenVisitor.js} +12 -14
  42. package/dist/cjs/semantic-tokens/getSemanticTokens.js +55 -0
  43. package/dist/cjs/sizing.js +2 -2
  44. package/dist/cjs/utils.js +2 -2
  45. package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +6 -0
  46. package/dist/cjs/validate/SymbolValidatorVisitor.js +34 -39
  47. package/dist/cjs/validate/validateScript.js +54 -0
  48. package/dist/cjs/validate.js +5 -4
  49. package/dist/cjs/visitor.js +140 -204
  50. package/dist/esm/BaseVisitor.js +396 -264
  51. package/dist/esm/LexerDiagnosticListener.js +371 -0
  52. package/dist/esm/{ComponentAnnotater.js → annotate/ComponentAnnotater.js} +29 -15
  53. package/dist/esm/annotate/DefaultPostAnnotationCallback.js +122 -0
  54. package/dist/esm/{RefdesAnnotationVisitor.js → annotate/RefdesAnnotationVisitor.js} +8 -82
  55. package/dist/esm/annotate/utils.js +66 -0
  56. package/dist/esm/antlr/CircuitScriptLexer.js +279 -286
  57. package/dist/esm/antlr/CircuitScriptParser.js +1962 -3522
  58. package/dist/esm/antlr/{CircuitScriptVisitor.js → CircuitScriptParserVisitor.js} +14 -35
  59. package/dist/esm/cache/deserializer.js +30 -0
  60. package/dist/esm/cache/hash.js +4 -0
  61. package/dist/esm/cache/serializer.js +118 -0
  62. package/dist/esm/cache/storage.js +39 -0
  63. package/dist/esm/cache/types.js +1 -0
  64. package/dist/esm/{environment.js → environment/environment.js} +18 -6
  65. package/dist/esm/environment/esm-environment.js +17 -0
  66. package/dist/esm/environment/helpers.js +4 -0
  67. package/dist/esm/execute.js +49 -15
  68. package/dist/esm/globals.js +8 -0
  69. package/dist/esm/helpers.js +5 -474
  70. package/dist/esm/importResolver.js +96 -0
  71. package/dist/esm/index.js +7 -6
  72. package/dist/esm/lexer.js +51 -12
  73. package/dist/esm/main.js +13 -3
  74. package/dist/esm/objects/ClassComponent.js +1 -1
  75. package/dist/esm/objects/ExecutionScope.js +0 -1
  76. package/dist/esm/objects/types.js +21 -1
  77. package/dist/esm/parser.js +19 -5
  78. package/dist/esm/pipeline.js +276 -0
  79. package/dist/esm/regenerate-tests.js +3 -2
  80. package/dist/esm/render/KiCadNetListOutputHandler.js +20 -0
  81. package/dist/esm/render/PaperSizes.js +41 -0
  82. package/dist/esm/{draw_symbols.js → render/draw_symbols.js} +58 -36
  83. package/dist/esm/{export.js → render/export.js} +2 -2
  84. package/dist/esm/{geometry.js → render/geometry.js} +5 -5
  85. package/dist/esm/{graph.js → render/graph.js} +7 -7
  86. package/dist/esm/{layout.js → render/layout.js} +8 -8
  87. package/dist/esm/{render.js → render/render.js} +8 -7
  88. package/dist/esm/rules-check/no-connect-on-connected-pin.js +1 -1
  89. package/dist/esm/rules-check/unconnected-pins.js +1 -1
  90. package/dist/esm/{SemanticTokenVisitor.js → semantic-tokens/SemanticTokenVisitor.js} +12 -14
  91. package/dist/esm/semantic-tokens/getSemanticTokens.js +51 -0
  92. package/dist/esm/sizing.js +2 -2
  93. package/dist/esm/utils.js +2 -2
  94. package/dist/esm/validate/SymbolValidatorResolveVisitor.js +3 -0
  95. package/dist/esm/validate/SymbolValidatorVisitor.js +36 -41
  96. package/dist/esm/validate/validateScript.js +50 -0
  97. package/dist/esm/validate.js +4 -3
  98. package/dist/esm/visitor.js +142 -206
  99. package/dist/libs/std.cst +15 -19
  100. package/dist/types/BaseVisitor.d.ts +25 -18
  101. package/dist/types/BomGeneration.d.ts +1 -1
  102. package/dist/types/LexerDiagnosticListener.d.ts +85 -0
  103. package/dist/types/{ComponentAnnotater.d.ts → annotate/ComponentAnnotater.d.ts} +1 -1
  104. package/dist/types/annotate/DefaultPostAnnotationCallback.d.ts +7 -0
  105. package/dist/types/{RefdesAnnotationVisitor.d.ts → annotate/RefdesAnnotationVisitor.d.ts} +6 -8
  106. package/dist/types/annotate/utils.d.ts +6 -0
  107. package/dist/types/antlr/CircuitScriptLexer.d.ts +71 -70
  108. package/dist/types/antlr/CircuitScriptParser.d.ts +357 -515
  109. package/dist/types/antlr/{CircuitScriptVisitor.d.ts → CircuitScriptParserVisitor.d.ts} +27 -69
  110. package/dist/types/cache/deserializer.d.ts +5 -0
  111. package/dist/types/cache/hash.d.ts +1 -0
  112. package/dist/types/cache/serializer.d.ts +3 -0
  113. package/dist/types/cache/storage.d.ts +4 -0
  114. package/dist/types/cache/types.d.ts +20 -0
  115. package/dist/types/{environment.d.ts → environment/environment.d.ts} +5 -4
  116. package/dist/types/environment/esm-environment.d.ts +4 -0
  117. package/dist/types/environment/helpers.d.ts +2 -0
  118. package/dist/types/execute.d.ts +3 -2
  119. package/dist/types/globals.d.ts +1 -0
  120. package/dist/types/helpers.d.ts +31 -36
  121. package/dist/types/importResolver.d.ts +4 -0
  122. package/dist/types/index.d.ts +7 -6
  123. package/dist/types/lexer.d.ts +9 -5
  124. package/dist/types/objects/ClassComponent.d.ts +1 -1
  125. package/dist/types/objects/ExecutionScope.d.ts +1 -4
  126. package/dist/types/objects/types.d.ts +16 -2
  127. package/dist/types/parser.d.ts +9 -2
  128. package/dist/types/pipeline.d.ts +9 -0
  129. package/dist/types/render/KiCadNetListOutputHandler.d.ts +10 -0
  130. package/dist/types/render/PaperSizes.d.ts +12 -0
  131. package/dist/types/{draw_symbols.d.ts → render/draw_symbols.d.ts} +4 -4
  132. package/dist/types/{export.d.ts → render/export.d.ts} +1 -1
  133. package/dist/types/{geometry.d.ts → render/geometry.d.ts} +2 -2
  134. package/dist/types/{graph.d.ts → render/graph.d.ts} +6 -6
  135. package/dist/types/{layout.d.ts → render/layout.d.ts} +10 -10
  136. package/dist/types/{render.d.ts → render/render.d.ts} +1 -1
  137. package/dist/types/{SemanticTokenVisitor.d.ts → semantic-tokens/SemanticTokenVisitor.d.ts} +6 -6
  138. package/dist/types/semantic-tokens/getSemanticTokens.d.ts +6 -0
  139. package/dist/types/sizing.d.ts +1 -1
  140. package/dist/types/utils.d.ts +1 -1
  141. package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +3 -0
  142. package/dist/types/validate/SymbolValidatorVisitor.d.ts +8 -8
  143. package/dist/types/validate/validateScript.d.ts +3 -0
  144. package/dist/types/visitor.d.ts +8 -14
  145. package/libs/std.cst +15 -19
  146. package/package.json +3 -6
  147. package/dist/cjs/antlr/CircuitScriptVisitor.js +0 -7
@@ -3,12 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseVisitor = void 0;
4
4
  const big_js_1 = require("big.js");
5
5
  const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
6
- const CircuitScriptVisitor_js_1 = require("./antlr/CircuitScriptVisitor.js");
6
+ const CircuitScriptParserVisitor_js_1 = require("./antlr/CircuitScriptParserVisitor.js");
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
10
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
11
- const PinTypes_js_1 = require("./objects/PinTypes.js");
12
11
  const types_js_1 = require("./objects/types.js");
13
12
  const globals_js_1 = require("./globals.js");
14
13
  const utils_js_1 = require("./utils.js");
@@ -16,7 +15,12 @@ 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");
18
17
  const PinDefinition_js_1 = require("./objects/PinDefinition.js");
19
- class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
18
+ const hash_js_1 = require("./cache/hash.js");
19
+ const storage_js_1 = require("./cache/storage.js");
20
+ const serializer_js_1 = require("./cache/serializer.js");
21
+ const deserializer_js_1 = require("./cache/deserializer.js");
22
+ const importResolver_js_1 = require("./importResolver.js");
23
+ class BaseVisitor extends CircuitScriptParserVisitor_js_1.CircuitScriptParserVisitor {
20
24
  constructor(silent = false, onErrorHandler = null, environment) {
21
25
  super();
22
26
  this.filePathStack = [];
@@ -28,25 +32,22 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
28
32
  types_js_1.Direction.Right, types_js_1.Direction.Left];
29
33
  this.resultData = new Map;
30
34
  this.componentCtxLinks = new Map;
31
- this.pinTypesList = [
32
- PinTypes_js_1.PinTypes.Any,
33
- PinTypes_js_1.PinTypes.Input,
34
- PinTypes_js_1.PinTypes.Output,
35
- PinTypes_js_1.PinTypes.IO,
36
- PinTypes_js_1.PinTypes.Power,
37
- ];
38
35
  this.onErrorHandler = null;
39
36
  this.importedFiles = [];
40
37
  this.warnings = [];
41
- this.onImportFile = async (visitor, filePath, fileData, onErrorHandler) => {
38
+ this.loadedFiles = new Map();
39
+ this.enableCacheImports = true;
40
+ this.enableCachedImportsRead = false || this.enableCacheImports;
41
+ this.enableCachedImportsWrite = false || this.enableCacheImports;
42
+ this.onImportFile = (visitor, filePath, fileData, onErrorHandler, fileLineOffset) => {
42
43
  throw "Import file not implemented";
43
44
  };
44
45
  this.refdesFileAnnotations = new Map();
45
- this.visitScript = async (ctx) => {
46
+ this.visitScript = (ctx) => {
46
47
  this.log('===', 'start', '===');
47
48
  this.allowParseImports = true;
48
49
  for (const ctxImport of ctx.import_expr()) {
49
- await this.visit(ctxImport);
50
+ this.visit(ctxImport);
50
51
  }
51
52
  this.allowParseImports = false;
52
53
  const result = this.runExpressions(this.getExecutor(), ctx.expression());
@@ -54,148 +55,225 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
54
55
  this.getExecutor().closeOpenPathBlocks();
55
56
  this.log('===', 'end', '===');
56
57
  };
57
- this.visitImport_simple = async (ctx) => {
58
- await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllWithNamespace);
58
+ this.visitImport_simple = (ctx) => {
59
+ this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllWithNamespace);
59
60
  };
60
- this.visitImport_all_simple = async (ctx) => {
61
- await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllMergeIntoNamespace);
61
+ this.visitImport_specific_or_all = (ctx) => {
62
+ let importType = types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace;
63
+ if (ctx._all) {
64
+ importType = types_js_1.ImportFunctionHandling.AllMergeIntoNamespace;
65
+ }
66
+ this.importCommon(ctx, importType);
62
67
  };
63
- this.visitImport_specific = async (ctx) => {
64
- await this.importCommon(ctx, types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace);
68
+ this.visitCallableExpr = (ctx) => {
69
+ this.passResult(ctx, ctx.callable_expr());
70
+ };
71
+ this.visitCallable_expr = (ctx) => {
72
+ const ctxParams = this.getResult(ctx);
73
+ const { keepReference = false } = ctxParams ?? {};
74
+ const executor = this.getExecutor();
75
+ let passedNetNamespace = null;
76
+ const netNameSpaceExpr = ctx.net_namespace_expr();
77
+ if (netNameSpaceExpr) {
78
+ passedNetNamespace = this.visitResult(netNameSpaceExpr);
79
+ }
80
+ const firstId = ctx.ID(0);
81
+ const atomId = firstId.getText();
82
+ let currentReference;
83
+ if (globals_js_1.PinTypesList.indexOf(atomId) !== -1 && ctx.trailer().length === 0) {
84
+ currentReference = new types_js_1.AnyReference({
85
+ found: true,
86
+ value: atomId,
87
+ type: globals_js_1.ReferenceTypes.pinType,
88
+ });
89
+ }
90
+ else {
91
+ this.log('resolve variable ctx: ' + ctx.getText(), 'atomId', atomId);
92
+ currentReference = executor.resolveVariable(this.executionStack, atomId);
93
+ this.log('reference:', currentReference.name, 'found:', currentReference.found);
94
+ }
95
+ if (currentReference !== undefined && currentReference.found) {
96
+ ctx.trailer().forEach(ctxTrailer => {
97
+ this.setResult(ctxTrailer, {
98
+ reference: currentReference,
99
+ netNamespace: passedNetNamespace
100
+ });
101
+ currentReference = this.visitResult(ctxTrailer);
102
+ });
103
+ }
104
+ let resultValue = currentReference;
105
+ if (!keepReference) {
106
+ if (currentReference.type !== globals_js_1.ReferenceTypes.pinType) {
107
+ resultValue = (0, utils_js_1.unwrapValue)(resultValue);
108
+ }
109
+ }
110
+ this.setResult(ctx, resultValue);
111
+ this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
112
+ };
113
+ this.visitTrailer = (ctx) => {
114
+ const { reference, netNamespace } = this.getResult(ctx);
115
+ const ctxLParam = ctx.LParen();
116
+ const ctxID = ctx.ID();
117
+ const ctxDataExpr = ctx.data_expr();
118
+ const useValue = reference.value;
119
+ let nextReference;
120
+ if (ctxID) {
121
+ reference.trailers.push(ctxID.getText());
122
+ const useRootValue = reference.rootValue ?? reference.value;
123
+ const useTrailerIndex = reference.trailerIndex ?? 0;
124
+ nextReference = this.getExecutor().resolveTrailers(reference.type, useRootValue, reference.trailers);
125
+ nextReference.name =
126
+ [reference.name,
127
+ ...reference.trailers.slice(useTrailerIndex)].join('.');
128
+ }
129
+ else if (ctxDataExpr) {
130
+ const arrayIndex = this.visitResult(ctxDataExpr);
131
+ if (arrayIndex instanceof ParamDefinition_js_1.NumericValue) {
132
+ const arrayIndexValue = arrayIndex.toNumber();
133
+ const foundValue = useValue[arrayIndexValue];
134
+ const refType = foundValue instanceof ClassComponent_js_1.ClassComponent
135
+ ? globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.variable;
136
+ nextReference = new types_js_1.AnyReference({
137
+ found: true,
138
+ type: refType,
139
+ value: foundValue,
140
+ trailers: [[globals_js_1.TrailerArrayIndex, arrayIndexValue]],
141
+ rootValue: useValue
142
+ });
143
+ }
144
+ }
145
+ else if (ctxLParam) {
146
+ nextReference = this.handleFunctionCall(reference, netNamespace, ctx);
147
+ }
148
+ this.setResult(ctx, nextReference);
65
149
  };
66
150
  this.visitAssignment_expr = (ctx) => {
67
- const ctxAtom = ctx.atom_expr();
68
- const ctxFuncCallRef = ctx.function_call_expr();
69
- let leftSideReference;
70
- let lhsCtx;
71
- if (ctxAtom) {
72
- leftSideReference = this.getReference(ctx.atom_expr());
73
- lhsCtx = ctxAtom;
74
- }
75
- else if (ctxFuncCallRef) {
76
- this.setResult(ctxFuncCallRef, { keepReference: true });
77
- leftSideReference = this.visitResult(ctxFuncCallRef);
78
- lhsCtx = ctxFuncCallRef;
79
- }
80
- const rhsCtx = ctx.data_expr();
81
- const rhsCtxResult = this.visitResult(rhsCtx);
82
- if ((0, utils_js_1.isReference)(rhsCtxResult) && !rhsCtxResult.found) {
83
- this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
84
- }
85
- const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
86
- const trailers = leftSideReference.trailers ?? [];
87
- const sequenceParts = [];
88
- if (trailers.length === 0) {
89
- this.getScope().setVariable(leftSideReference.name, rhsValue);
90
- let itemType = '';
91
- if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
92
- itemType = globals_js_1.ReferenceTypes.instance;
93
- this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
151
+ if (ctx.Assign()) {
152
+ const lhsCtx = ctx.callable_expr();
153
+ this.setResult(lhsCtx, { keepReference: true });
154
+ const leftSideReference = this.visitResult(lhsCtx);
155
+ const rhsCtx = ctx.data_expr();
156
+ const rhsCtxResult = this.visitResult(rhsCtx);
157
+ if ((0, utils_js_1.isReference)(rhsCtxResult) && !rhsCtxResult.found) {
158
+ this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
94
159
  }
95
- else {
96
- itemType = globals_js_1.ReferenceTypes.variable;
160
+ const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
161
+ const { trailers = [] } = leftSideReference;
162
+ const sequenceParts = [];
163
+ if (trailers.length === 0) {
97
164
  this.getScope().setVariable(leftSideReference.name, rhsValue);
98
- this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
165
+ let itemType = '';
166
+ if (rhsValue instanceof ClassComponent_js_1.ClassComponent) {
167
+ itemType = globals_js_1.ReferenceTypes.instance;
168
+ this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
169
+ }
170
+ else {
171
+ itemType = globals_js_1.ReferenceTypes.variable;
172
+ this.getScope().setVariable(leftSideReference.name, rhsValue);
173
+ this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
174
+ }
175
+ sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
99
176
  }
100
- sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
101
- }
102
- else {
103
- if (leftSideReference.rootValue instanceof ClassComponent_js_1.ClassComponent) {
104
- this.setInstanceParam(leftSideReference.rootValue, trailers, rhsValue);
105
- this.log2(`assigned component param ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
106
- sequenceParts.push(...['instance', [leftSideReference.rootValue, trailers], rhsValue]);
107
- if (leftSideReference.rootValue.typeProp === globals_js_1.ComponentTypes.net) {
108
- const net = this.getScope().getNet(leftSideReference.rootValue, new PinDefinition_js_1.PinId(1));
109
- if (net) {
110
- const trailerValue = trailers.join(".");
111
- net.params.set(trailerValue, rhsValue);
177
+ else {
178
+ if (leftSideReference.rootValue instanceof ClassComponent_js_1.ClassComponent) {
179
+ this.setInstanceParam(leftSideReference.rootValue, trailers, rhsValue);
180
+ this.log2(`assigned component param ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
181
+ sequenceParts.push(...['instance', [leftSideReference.rootValue, trailers], rhsValue]);
182
+ if (leftSideReference.rootValue.typeProp === globals_js_1.ComponentTypes.net) {
183
+ const net = this.getScope().getNet(leftSideReference.rootValue, new PinDefinition_js_1.PinId(1));
184
+ if (net) {
185
+ const trailerValue = trailers.join(".");
186
+ net.params.set(trailerValue, rhsValue);
187
+ }
112
188
  }
113
189
  }
114
- }
115
- else if (leftSideReference.rootValue instanceof Object) {
116
- if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
117
- if (Array.isArray(leftSideReference.rootValue)) {
118
- const arrayIndexValue = trailers[0][1];
119
- leftSideReference.rootValue[arrayIndexValue] = rhsValue;
120
- this.log2(`assigned array index ${leftSideReference.rootValue} index: ${arrayIndexValue} value: ${rhsValue}`);
190
+ else if (leftSideReference.rootValue instanceof Object) {
191
+ if (Array.isArray(trailers[0]) && trailers[0][0] === globals_js_1.TrailerArrayIndex) {
192
+ if (Array.isArray(leftSideReference.rootValue)) {
193
+ const arrayIndexValue = trailers[0][1];
194
+ leftSideReference.rootValue[arrayIndexValue] = rhsValue;
195
+ this.log2(`assigned array index ${leftSideReference.rootValue} index: ${arrayIndexValue} value: ${rhsValue}`);
196
+ }
197
+ else {
198
+ this.throwWithContext(lhsCtx, "Invalid array");
199
+ }
121
200
  }
122
201
  else {
123
- this.throwWithContext(lhsCtx, "Invalid array");
202
+ let expandedValue = leftSideReference.rootValue;
203
+ trailers.slice(0, -1).forEach(trailer => {
204
+ expandedValue = expandedValue[trailer];
205
+ });
206
+ const lastTrailer = trailers.slice(-1)[0];
207
+ expandedValue[lastTrailer] = rhsValue;
208
+ this.log2(`assigned object ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
124
209
  }
210
+ sequenceParts.push(...['variable', [leftSideReference.rootValue, trailers], rhsValue]);
125
211
  }
126
- else {
127
- let expandedValue = leftSideReference.rootValue;
128
- trailers.slice(0, -1).forEach(trailer => {
129
- expandedValue = expandedValue[trailer];
130
- });
131
- const lastTrailer = trailers.slice(-1)[0];
132
- expandedValue[lastTrailer] = rhsValue;
133
- this.log2(`assigned object ${leftSideReference.rootValue} trailers: ${trailers} value: ${rhsValue}`);
134
- }
135
- sequenceParts.push(...['variable', [leftSideReference.rootValue, trailers], rhsValue]);
136
212
  }
137
- }
138
- if (sequenceParts.length > 0) {
139
- this.getScope().sequence.push([
140
- ExecutionScope_js_1.SequenceAction.Assign, ...sequenceParts
141
- ]);
142
- }
143
- this.setResult(ctx, rhsValue);
144
- };
145
- this.visitOperator_assignment_expr = (ctx) => {
146
- const reference = this.getReference(ctx.atom_expr());
147
- const value = this.visitResult(ctx.data_expr());
148
- if (!reference.found) {
149
- this.throwWithContext(ctx, 'Undefined reference: ' + reference.name);
150
- }
151
- const trailers = reference.trailers ?? [];
152
- let currentValue = null;
153
- if (trailers.length === 0) {
154
- currentValue = this.getExecutor().scope.variables.get(reference.name);
213
+ if (sequenceParts.length > 0) {
214
+ this.getScope().sequence.push([
215
+ ExecutionScope_js_1.SequenceAction.Assign, ...sequenceParts
216
+ ]);
217
+ }
218
+ this.setResult(ctx, rhsValue);
155
219
  }
156
220
  else {
157
- if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
158
- currentValue = this.getInstanceParam(reference.value, trailers);
221
+ const ctxCallable = ctx.callable_expr();
222
+ this.setResult(ctxCallable, { keepReference: true });
223
+ const reference = this.visitResult(ctx.callable_expr());
224
+ const value = this.visitResult(ctx.data_expr());
225
+ if (!reference.found) {
226
+ this.throwWithContext(ctx, 'Undefined reference: ' + reference.name);
159
227
  }
160
- else if (reference.value instanceof Object) {
161
- currentValue = reference.value[trailers.join('.')];
228
+ const trailers = reference.trailers ?? [];
229
+ let currentValue = null;
230
+ if (trailers.length === 0) {
231
+ currentValue = this.getExecutor().scope.variables.get(reference.name);
162
232
  }
163
- }
164
- if (currentValue === null) {
165
- this.throwWithContext(ctx, 'Operator assignment failed: could not get value');
166
- }
167
- let newValue = 0;
168
- const operator = new ParamDefinition_js_1.NumberOperator();
169
- if (ctx.AdditionAssign()) {
170
- newValue = operator.addition(currentValue, value);
171
- }
172
- else if (ctx.MinusAssign()) {
173
- newValue = operator.subtraction(currentValue, value);
174
- }
175
- else if (ctx.MultiplyAssign()) {
176
- newValue = operator.multiply(currentValue, value);
177
- }
178
- else if (ctx.DivideAssign()) {
179
- newValue = operator.divide(currentValue, value);
180
- }
181
- else if (ctx.ModulusAssign()) {
182
- newValue = operator.modulus(currentValue, value);
183
- }
184
- else {
185
- this.throwWithContext(ctx, 'Operator assignment failed: could not perform operator');
186
- }
187
- if (trailers.length === 0) {
188
- this.getExecutor().scope.setVariable(reference.name, newValue);
189
- }
190
- else {
191
- if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
192
- this.setInstanceParam(reference.value, trailers, newValue);
233
+ else {
234
+ if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
235
+ currentValue = this.getInstanceParam(reference.value, trailers);
236
+ }
237
+ else if (reference.value instanceof Object) {
238
+ currentValue = reference.value[trailers.join('.')];
239
+ }
240
+ }
241
+ if (currentValue === null) {
242
+ this.throwWithContext(ctx, 'Operator assignment failed: could not get value');
193
243
  }
194
- else if (reference.value instanceof Object) {
195
- reference.value[trailers.join('.')] = newValue;
244
+ let newValue = 0;
245
+ const operator = new ParamDefinition_js_1.NumberOperator();
246
+ if (ctx.AdditionAssign()) {
247
+ newValue = operator.addition(currentValue, value);
248
+ }
249
+ else if (ctx.MinusAssign()) {
250
+ newValue = operator.subtraction(currentValue, value);
251
+ }
252
+ else if (ctx.MultiplyAssign()) {
253
+ newValue = operator.multiply(currentValue, value);
254
+ }
255
+ else if (ctx.DivideAssign()) {
256
+ newValue = operator.divide(currentValue, value);
257
+ }
258
+ else if (ctx.ModulusAssign()) {
259
+ newValue = operator.modulus(currentValue, value);
260
+ }
261
+ else {
262
+ this.throwWithContext(ctx, 'Operator assignment failed: could not perform operator');
196
263
  }
264
+ if (trailers.length === 0) {
265
+ this.getExecutor().scope.setVariable(reference.name, newValue);
266
+ }
267
+ else {
268
+ if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
269
+ this.setInstanceParam(reference.value, trailers, newValue);
270
+ }
271
+ else if (reference.value instanceof Object) {
272
+ reference.value[trailers.join('.')] = newValue;
273
+ }
274
+ }
275
+ this.setResult(ctx, newValue);
197
276
  }
198
- this.setResult(ctx, newValue);
199
277
  };
200
278
  this.visitTrailer_expr2 = (ctx) => {
201
279
  const reference = this.getResult(ctx);
@@ -235,7 +313,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
235
313
  const firstId = ctx.ID(0);
236
314
  const atomId = firstId.getText();
237
315
  let currentReference;
238
- if (this.pinTypesList.indexOf(atomId) !== -1) {
316
+ if (globals_js_1.PinTypesList.indexOf(atomId) !== -1) {
239
317
  currentReference = new types_js_1.AnyReference({
240
318
  found: true,
241
319
  value: atomId,
@@ -256,19 +334,6 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
256
334
  this.setResult(ctx, currentReference);
257
335
  this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
258
336
  };
259
- this.visitFunctionCallExpr = (ctx) => {
260
- const result = this.visitResult(ctx.function_call_expr());
261
- this.setResult(ctx, result);
262
- };
263
- this.visitFunction_call_expr = (ctx) => {
264
- const ctxParams = this.getResult(ctx);
265
- const { keepReference = false } = ctxParams ?? {};
266
- this.handleFunctionCall(ctx);
267
- if (!keepReference) {
268
- const functionResultReference = this.getResult(ctx);
269
- this.setResult(ctx, functionResultReference.value);
270
- }
271
- };
272
337
  this.visitValue_expr = (ctx) => {
273
338
  const sign = ctx.Minus() ? -1 : 1;
274
339
  const ctxIntegerValue = ctx.INTEGER_VALUE();
@@ -312,30 +377,26 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
312
377
  }
313
378
  this.setResult(ctx, result);
314
379
  };
315
- this.visitValueAtomExpr = (ctx) => {
380
+ this.visitValueExpr = (ctx) => {
381
+ this.passResult(ctx, ctx.value_expr());
382
+ };
383
+ this.visitAtomExpr = (ctx) => {
316
384
  let value = null;
317
- const ctxValueExpr = ctx.value_expr();
318
- const ctxAtomExpr = ctx.atom_expr();
319
- if (ctxValueExpr) {
320
- value = this.visitResult(ctxValueExpr);
385
+ const reference = this.visitResult(ctx.atom_expr());
386
+ if (!reference.found) {
387
+ value = new types_js_1.UndeclaredReference(reference);
321
388
  }
322
- else if (ctxAtomExpr) {
323
- const reference = this.visitResult(ctxAtomExpr);
324
- if (!reference.found) {
325
- value = new types_js_1.UndeclaredReference(reference);
389
+ else {
390
+ if (reference.type && reference.type === globals_js_1.ReferenceTypes.pinType) {
391
+ value = reference;
326
392
  }
327
393
  else {
328
- if (reference.type && reference.type === globals_js_1.ReferenceTypes.pinType) {
394
+ if ((reference.trailers && reference.trailers.length > 0)
395
+ || reference.type === globals_js_1.ReferenceTypes.function) {
329
396
  value = reference;
330
397
  }
331
398
  else {
332
- if ((reference.trailers && reference.trailers.length > 0)
333
- || reference.type === globals_js_1.ReferenceTypes.function) {
334
- value = reference;
335
- }
336
- else {
337
- value = reference.value;
338
- }
399
+ value = reference.value;
339
400
  }
340
401
  }
341
402
  }
@@ -359,6 +420,11 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
359
420
  });
360
421
  this.setResult(ctx, result);
361
422
  };
423
+ this.visitKeyword_assignment_expr = (ctx) => {
424
+ const id = ctx.ID().getText();
425
+ const value = this.visitResult(ctx.data_expr());
426
+ this.setResult(ctx, [id, value]);
427
+ };
362
428
  this.visitParameters = (ctx) => {
363
429
  const dataExpressions = ctx.data_expr();
364
430
  const keywordAssignmentExpressions = ctx.keyword_assignment_expr();
@@ -411,7 +477,10 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
411
477
  this.setResult(ctx, array);
412
478
  };
413
479
  this.visitArrayExpr = (ctx) => {
414
- this.setResult(ctx, this.visitResult(ctx.array_expr()));
480
+ const array = ctx.data_expr().map(item => {
481
+ return this.visitResult(item);
482
+ });
483
+ this.setResult(ctx, array);
415
484
  };
416
485
  this.visitArrayIndexExpr = (ctx) => {
417
486
  const ctxArray = ctx.data_expr(0);
@@ -516,21 +585,38 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
516
585
  log2(message) {
517
586
  this.getExecutor().log(message);
518
587
  }
519
- async visitAsync(ctx) {
520
- const result = await ctx.accept(this);
521
- return result;
588
+ async resolveImportsAndLoad(inputPath, scriptData) {
589
+ this.log('resolve and load imports');
590
+ const importedFiles = await (0, importResolver_js_1.resolveAllImportFilepaths)(inputPath, scriptData, this.environment);
591
+ this.log('resolved all referenced imports');
592
+ const loadedFiles = new Map();
593
+ for (const importFilePath of importedFiles) {
594
+ this.log(`reading file: ${importFilePath}`);
595
+ const importFileData = await this.environment.readFile(importFilePath);
596
+ loadedFiles.set(importFilePath, importFileData);
597
+ }
598
+ if (this.filePathStack.length > 0) {
599
+ const mainRefdesFile = this.getPathRefdesFile(this.filePathStack[0]);
600
+ const fileExists = await this.environment.exists(mainRefdesFile);
601
+ if (fileExists) {
602
+ this.log('.refdes.json file exists, reading it');
603
+ const mainRefdesFileData = await this.environment.readFile(mainRefdesFile);
604
+ loadedFiles.set(mainRefdesFile, mainRefdesFileData);
605
+ }
606
+ }
607
+ this.loadedFiles = loadedFiles;
522
608
  }
523
- async importCommon(ctx, handling) {
609
+ importCommon(ctx, handling) {
524
610
  const specificImports = [];
525
- if (ctx instanceof CircuitScriptParser_js_1.Import_specificContext) {
611
+ if (ctx instanceof CircuitScriptParser_js_1.Import_specific_or_allContext) {
526
612
  const tmpSpecificImports = ctx._funcNames.map(item => {
527
613
  return item.text;
528
614
  });
529
615
  specificImports.push(...tmpSpecificImports);
530
616
  }
531
- const id = ctx._libraryName.text;
532
- const importedFile = await this.handleImportFile(id, handling, true, ctx, specificImports);
533
- const ctxImportAnnotation = ctx.import_annotation_expr();
617
+ const id = ctx._libraryName.text.slice(1, -1);
618
+ const importedFile = this.handleImportFile(id, handling, true, ctx, specificImports);
619
+ const ctxImportAnnotation = ctx.annotation_comment_expr();
534
620
  if (ctxImportAnnotation) {
535
621
  const textValue = ctxImportAnnotation.getText().replace('#=', '');
536
622
  const { importedLibrary } = importedFile;
@@ -556,80 +642,52 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
556
642
  }
557
643
  return reference;
558
644
  }
559
- handleFunctionCall(ctx) {
645
+ handleFunctionCall(currentReference, passedNetNamespace, ctx) {
560
646
  const executor = this.getExecutor();
561
- const atomId = ctx.ID().getText();
562
- let passedNetNamespace = null;
563
- const netNameSpaceExpr = ctx.net_namespace_expr();
564
- if (netNameSpaceExpr) {
565
- passedNetNamespace = this.visitResult(netNameSpaceExpr);
647
+ let parameters = [];
648
+ const ctxParameters = ctx.parameters();
649
+ if (ctxParameters) {
650
+ parameters = this.visitResult(ctxParameters);
566
651
  }
567
- let currentReference = executor.resolveVariable(this.executionStack, atomId);
568
- if (ctx.trailer_expr().length > 0) {
569
- if (!currentReference.found) {
570
- this.log(`could not resolve function: ${atomId}`);
571
- this.throwWithContext(ctx, "could not resolve function: " + atomId);
572
- }
573
- currentReference.trailers = [];
574
- ctx.trailer_expr().forEach(item => {
575
- if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
576
- if (currentReference.type === globals_js_1.ReferenceTypes.variable) {
577
- if (currentReference.value instanceof types_js_1.AnyReference && currentReference.value.type === globals_js_1.ReferenceTypes.function) {
578
- currentReference = currentReference.value;
579
- }
580
- }
581
- let parameters = [];
582
- const ctxParameters = item.parameters();
583
- if (ctxParameters) {
584
- parameters = this.visitResult(ctxParameters);
585
- }
586
- const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
587
- try {
588
- const isLibraryFunction = currentReference.rootValue
589
- && currentReference.rootValue instanceof types_js_1.ImportedLibrary;
590
- if (isLibraryFunction) {
591
- this.log('create new library context');
592
- const importedLibrary = currentReference.rootValue;
593
- const { context: importedLibraryContext } = importedLibrary;
594
- this.enterFile(importedLibrary.libraryFilePath);
595
- const newExecutor = this.handleEnterContext(this.getExecutor(), this.executionStack, importedLibraryContext.name, ctx, {
596
- netNamespace: executor.netNamespace,
597
- namespace: importedLibrary.libraryNamespace
598
- }, [], [], false);
599
- this.log('copy library context scope');
600
- importedLibraryContext.scope.copyTo(newExecutor.scope);
601
- }
602
- const [, functionResult] = executor.callFunction(currentReference, parameters, this.executionStack, useNetNamespace);
603
- if (isLibraryFunction) {
604
- this.log('pop library context scope');
605
- this.handlePopContext(this.getExecutor(), this.executionStack, "", false);
606
- this.exitFile();
607
- }
608
- if ((0, utils_js_1.isReference)(functionResult)) {
609
- currentReference = functionResult;
610
- }
611
- else {
612
- currentReference = new types_js_1.AnyReference({
613
- found: true,
614
- value: functionResult,
615
- trailers: [],
616
- type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
617
- globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.value,
618
- });
619
- }
620
- }
621
- catch (err) {
622
- this.throwWithContext(ctx, err);
623
- }
624
- }
625
- else {
626
- const ctxTrailer = item.trailer_expr2();
627
- this.setResult(ctxTrailer, currentReference);
628
- currentReference = this.visitResult(ctxTrailer);
629
- }
630
- });
652
+ const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
653
+ try {
654
+ const isLibraryFunction = currentReference.rootValue
655
+ && currentReference.rootValue instanceof types_js_1.ImportedLibrary;
656
+ if (isLibraryFunction) {
657
+ this.log('create new library context');
658
+ const importedLibrary = currentReference.rootValue;
659
+ const { context: importedLibraryContext } = importedLibrary;
660
+ this.enterFile(importedLibrary.libraryFilePath);
661
+ const newExecutor = this.handleEnterContext(this.getExecutor(), this.executionStack, importedLibraryContext.name, ctx, {
662
+ netNamespace: executor.netNamespace,
663
+ namespace: importedLibrary.libraryNamespace
664
+ }, [], [], false);
665
+ this.log('copy library context scope');
666
+ importedLibraryContext.scope.copyTo(newExecutor.scope);
667
+ }
668
+ const [, functionResult] = executor.callFunction(currentReference, parameters, this.executionStack, useNetNamespace);
669
+ if (isLibraryFunction) {
670
+ this.log('pop library context scope');
671
+ this.handlePopContext(this.getExecutor(), this.executionStack, "", false);
672
+ this.exitFile();
673
+ }
674
+ if ((0, utils_js_1.isReference)(functionResult)) {
675
+ currentReference = functionResult;
676
+ }
677
+ else {
678
+ currentReference = new types_js_1.AnyReference({
679
+ found: true,
680
+ value: functionResult,
681
+ trailers: [],
682
+ type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
683
+ globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.value,
684
+ });
685
+ }
631
686
  }
632
- this.setResult(ctx, currentReference);
687
+ catch (err) {
688
+ this.throwWithContext(ctx, err);
689
+ }
690
+ return currentReference;
633
691
  }
634
692
  handleEnterContext(executor, executionStack, contextName, ctx, options, funcDefinedParameters, passedInParameters, isBreakContext = true) {
635
693
  if (isBreakContext) {
@@ -711,11 +769,27 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
711
769
  this.visit(ctx);
712
770
  return this.getResult(ctx);
713
771
  }
714
- async handleImportFile(name, importHandling, throwErrors = true, ctx = null, specificImports = []) {
772
+ passResult(ctx, innerCtx) {
773
+ const params = this.getResult(ctx);
774
+ if (params) {
775
+ this.setResult(innerCtx, params);
776
+ }
777
+ this.setResult(ctx, this.visitResult(innerCtx));
778
+ }
779
+ handleImportFile(name, importHandling, throwErrors = true, ctx = null, specificImports = []) {
715
780
  name = name.trim();
716
781
  const importAlready = this.importedFiles.find(item => {
717
782
  return item.id === name;
718
783
  });
784
+ let importStatement;
785
+ if (ctx !== null) {
786
+ const start = ctx.start;
787
+ const inputStream = start.inputStream;
788
+ importStatement = [
789
+ start.line, start.column,
790
+ inputStream.getTextFromRange(start.start, ctx.stop.stop)
791
+ ];
792
+ }
719
793
  if (importAlready) {
720
794
  const { importedLibrary: tmpImportedLibrary } = importAlready;
721
795
  const alreadyImportedFlag = tmpImportedLibrary.importHandlingFlag;
@@ -744,14 +818,17 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
744
818
  let hasError = false;
745
819
  let hasParseError = false;
746
820
  let pathExists = false;
747
- const tmpFilePath = this.environment.getRelativeToCurrentFolder(name + ".cst");
821
+ const tmpFilePath = this.environment.getAbsPathRelativeToCurrentFolder(name + ".cst");
822
+ const relativeFilePath = this.environment.relative(this.environment.getCurrentDirectory(), tmpFilePath);
748
823
  this.log('importing path:', tmpFilePath);
749
824
  let fileData = null;
750
825
  let filePathUsed = null;
751
826
  try {
752
- filePathUsed = tmpFilePath;
753
- fileData = await this.environment.readFile(tmpFilePath, { encoding: 'utf8' });
754
- pathExists = true;
827
+ filePathUsed = relativeFilePath;
828
+ if (this.loadedFiles.has(relativeFilePath)) {
829
+ fileData = this.loadedFiles.get(relativeFilePath);
830
+ pathExists = true;
831
+ }
755
832
  }
756
833
  catch (err) {
757
834
  this.log('failed to read file');
@@ -763,8 +840,10 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
763
840
  const tmpFilePath2 = this.environment.getRelativeToDefaultLibs(name + ".cst");
764
841
  this.log('checking default libs: ' + tmpFilePath2);
765
842
  filePathUsed = tmpFilePath2;
766
- fileData = await this.environment.readFile(tmpFilePath2, { encoding: 'utf8' });
767
- pathExists = true;
843
+ if (this.loadedFiles.has(tmpFilePath2)) {
844
+ fileData = this.loadedFiles.get(tmpFilePath2);
845
+ pathExists = true;
846
+ }
768
847
  }
769
848
  catch (err) {
770
849
  this.log('failed to read file');
@@ -779,16 +858,46 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
779
858
  const executionContextName = name;
780
859
  const netNamespace = executor.netNamespace;
781
860
  const libraryNamespace = `${globals_js_1.BaseNamespace}${name}.`;
782
- this.enterNewChildContext(executionStack, executor, executionContextName, {
861
+ const importContext = this.enterNewChildContext(executionStack, executor, executionContextName, {
783
862
  netNamespace,
784
863
  namespace: libraryNamespace,
785
864
  }, [], []);
786
- const importResult = await this.onImportFile(this, filePathUsed, fileData, this.onErrorHandler);
787
- hasError = importResult.hasError;
788
- hasParseError = importResult.hasParseError;
789
- const importContext = executionStack.pop();
790
- this.log(`import handling flag: ${importHandling}`);
791
- importedLibrary = new types_js_1.ImportedLibrary(name, libraryNamespace, filePathUsed, importResult.tree, importResult.tokens, importContext, importHandling, specificImports);
865
+ let loadedFromCache = false;
866
+ let contentHash = '';
867
+ if (this.enableCachedImportsRead) {
868
+ try {
869
+ contentHash = (0, hash_js_1.computeContentHash)(fileData);
870
+ const cachedIR = (0, storage_js_1.readCache)(filePathUsed, contentHash);
871
+ if (cachedIR !== null) {
872
+ this.log('cache hit for:', filePathUsed);
873
+ const parseAndVisit = (miniScript, lineOffset) => {
874
+ return this.onImportFile(this, filePathUsed, miniScript, this.onErrorHandler, lineOffset);
875
+ };
876
+ importedLibrary = (0, deserializer_js_1.deserializeLibraryScope)(cachedIR, name, libraryNamespace, filePathUsed, importHandling, specificImports, importContext, parseAndVisit, () => {
877
+ executionStack.push(importContext);
878
+ return importContext;
879
+ }, () => {
880
+ executionStack.pop();
881
+ }, contentHash);
882
+ loadedFromCache = true;
883
+ this.log('Done reading from import cache');
884
+ }
885
+ }
886
+ catch (err) {
887
+ this.log('Error while reading cache: ', err);
888
+ loadedFromCache = false;
889
+ }
890
+ }
891
+ if (!loadedFromCache) {
892
+ const importResult = this.onImportFile(this, filePathUsed, fileData, this.onErrorHandler);
893
+ hasError = importResult.hasError;
894
+ hasParseError = importResult.hasParseError;
895
+ this.log(`import handling flag: ${importHandling}`);
896
+ importedLibrary = new types_js_1.ImportedLibrary(name, libraryNamespace, filePathUsed, importResult.tree, importResult.tokens, importContext, importHandling, specificImports, contentHash, importStatement);
897
+ importedLibrary.parseError = hasError || hasParseError;
898
+ importedLibrary.writeToCache = true;
899
+ }
900
+ executionStack.pop();
792
901
  if (specificImports.length > 0) {
793
902
  this.log('specific import: ' + specificImports.join(', '));
794
903
  }
@@ -797,7 +906,8 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
797
906
  importedLibrary.context.scope.libraries.forEach((lib, key) => {
798
907
  scope.libraries.set(key, lib);
799
908
  });
800
- await this.checkLibraryHasRefdesFile(filePathUsed);
909
+ executor.mergeScope(importedLibrary.context.scope, importedLibrary.libraryNamespace);
910
+ this.checkLibraryInRefdesFile(filePathUsed);
801
911
  }
802
912
  }
803
913
  catch (err) {
@@ -830,10 +940,32 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
830
940
  this.importedFiles.push(newImportedFile);
831
941
  return newImportedFile;
832
942
  }
833
- async checkLibraryHasRefdesFile(filePath) {
943
+ cacheLibraries() {
944
+ this.log('caching libraries');
945
+ const libraries = this.getScope().libraries;
946
+ for (const [libName, library] of libraries) {
947
+ this.log(`checking write flag for library ${library.libraryName}: ${library.writeToCache}`);
948
+ if (library.writeToCache && !library.parseError) {
949
+ try {
950
+ const { fileHash } = library;
951
+ const ir = (0, serializer_js_1.serializeLibraryScope)(library, fileHash);
952
+ (0, storage_js_1.writeCache)(library.libraryFilePath, fileHash, ir);
953
+ this.log(`created cache for library: ${libName}`);
954
+ }
955
+ catch (err) {
956
+ this.log(`failed to cache library: ${libName}`);
957
+ throw new utils_js_2.RuntimeExecutionError(`Failed to cache library: ${libName}`);
958
+ }
959
+ }
960
+ }
961
+ }
962
+ checkLibraryInRefdesFile(filePath) {
834
963
  return;
835
964
  }
836
- getRefdesFileAnnotation(filePath, startLine, startColumn, stopLine, stopColumn) {
965
+ getPathRefdesFile(filePath) {
966
+ return '';
967
+ }
968
+ getRefdesFileAnnotationKey(filePath, startLine, startColumn, stopLine, stopColumn) {
837
969
  return `${filePath}:${startLine}:${startColumn}:${stopLine}:${stopColumn}`;
838
970
  }
839
971
  setupDefinedParameters(funcDefinedParameters, passedInParameters, executor) {