circuitscript 0.0.22 → 0.0.25

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 (180) hide show
  1. package/dist/cjs/BaseVisitor.js +487 -0
  2. package/dist/cjs/SemanticTokenVisitor.js +218 -0
  3. package/dist/cjs/SymbolValidatorVisitor.js +233 -0
  4. package/dist/cjs/antlr/CircuitScriptLexer.js +302 -0
  5. package/dist/cjs/antlr/CircuitScriptParser.js +5128 -0
  6. package/dist/cjs/antlr/CircuitScriptVisitor.js +7 -0
  7. package/dist/cjs/draw_symbols.js +819 -0
  8. package/dist/cjs/execute.js +778 -0
  9. package/{src/export.ts → dist/cjs/export.js} +34 -56
  10. package/dist/cjs/fonts.js +4 -0
  11. package/dist/cjs/geometry.js +450 -0
  12. package/dist/cjs/globals.js +60 -0
  13. package/dist/cjs/helpers.js +269 -0
  14. package/dist/cjs/index.js +31 -0
  15. package/{src/layout.ts → dist/cjs/layout.js} +421 -1002
  16. package/dist/cjs/lexer.js +111 -0
  17. package/dist/cjs/logger.js +17 -0
  18. package/dist/cjs/main.js +82 -0
  19. package/dist/cjs/objects/ClassComponent.js +145 -0
  20. package/dist/cjs/objects/ExecutionScope.js +135 -0
  21. package/dist/cjs/objects/Frame.js +22 -0
  22. package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
  23. package/dist/cjs/objects/ParamDefinition.js +42 -0
  24. package/dist/cjs/objects/PinDefinition.js +31 -0
  25. package/dist/cjs/objects/PinTypes.js +11 -0
  26. package/dist/cjs/objects/Wire.js +9 -0
  27. package/dist/cjs/objects/types.js +15 -0
  28. package/dist/cjs/parser.js +70 -0
  29. package/dist/cjs/regenerate-tests.js +23 -0
  30. package/dist/cjs/render.js +155 -0
  31. package/{src/server.ts → dist/cjs/server.js} +15 -21
  32. package/dist/cjs/sizing.js +105 -0
  33. package/{src/utils.ts → dist/cjs/utils.js} +25 -35
  34. package/dist/cjs/validate.js +81 -0
  35. package/dist/cjs/visitor.js +844 -0
  36. package/dist/esm/BaseVisitor.mjs +488 -0
  37. package/dist/esm/SemanticTokenVisitor.mjs +215 -0
  38. package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
  39. package/dist/esm/antlr/CircuitScriptLexer.mjs +276 -0
  40. package/dist/esm/antlr/CircuitScriptParser.mjs +5038 -0
  41. package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +8 -3
  42. package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +78 -33
  43. package/{build/src/execute.js → dist/esm/execute.mjs} +59 -60
  44. package/{build/src/export.js → dist/esm/export.mjs} +2 -2
  45. package/{build/src/geometry.js → dist/esm/geometry.mjs} +31 -15
  46. package/dist/esm/helpers.mjs +252 -0
  47. package/dist/esm/index.mjs +15 -0
  48. package/{build/src/layout.js → dist/esm/layout.mjs} +19 -11
  49. package/{build/src/lexer.js → dist/esm/lexer.mjs} +10 -10
  50. package/{build/src/main.js → dist/esm/main.mjs} +9 -14
  51. package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +6 -3
  52. package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +1 -0
  53. package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
  54. package/dist/esm/objects/types.mjs +12 -0
  55. package/dist/esm/parser.mjs +64 -0
  56. package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
  57. package/{build/src/render.js → dist/esm/render.mjs} +7 -24
  58. package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
  59. package/{src/main.ts → dist/esm/validate.mjs} +31 -62
  60. package/dist/esm/visitor.mjs +838 -0
  61. package/dist/types/BaseVisitor.d.ts +69 -0
  62. package/dist/types/SemanticTokenVisitor.d.ts +36 -0
  63. package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
  64. package/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +28 -27
  65. package/dist/types/antlr/CircuitScriptParser.d.ts +719 -0
  66. package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +69 -59
  67. package/{build/src → dist/types}/draw_symbols.d.ts +11 -2
  68. package/{build/src → dist/types}/execute.d.ts +6 -9
  69. package/{build/src → dist/types}/geometry.d.ts +5 -1
  70. package/dist/types/helpers.d.ts +40 -0
  71. package/dist/types/index.d.ts +15 -0
  72. package/{build/src → dist/types}/layout.d.ts +10 -10
  73. package/{build/src → dist/types}/lexer.d.ts +2 -2
  74. package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
  75. package/{build/src → dist/types}/objects/ExecutionScope.d.ts +4 -1
  76. package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
  77. package/{build/src → dist/types}/objects/types.d.ts +5 -0
  78. package/dist/types/parser.d.ts +25 -0
  79. package/{build/src → dist/types}/render.d.ts +1 -1
  80. package/{build/src → dist/types}/sizing.d.ts +3 -1
  81. package/dist/types/validate.d.ts +2 -0
  82. package/dist/types/visitor.d.ts +80 -0
  83. package/libs/lib.cst +0 -2
  84. package/package.json +38 -15
  85. package/.editorconfig +0 -15
  86. package/.eslintignore +0 -1
  87. package/.eslintrc.json +0 -27
  88. package/.gitlab-ci.yml +0 -81
  89. package/.prettierignore +0 -8
  90. package/.prettierrc +0 -16
  91. package/__tests__/expectedResults.ts +0 -657
  92. package/__tests__/helpers.ts +0 -82
  93. package/__tests__/parseScripts.ts +0 -593
  94. package/__tests__/renderData/script1.cst +0 -58
  95. package/__tests__/renderData/script1.cst.svg +0 -1
  96. package/__tests__/renderData/script2.cst +0 -16
  97. package/__tests__/renderData/script2.cst.svg +0 -1
  98. package/__tests__/renderData/script3.cst +0 -30
  99. package/__tests__/renderData/script3.cst.svg +0 -1
  100. package/__tests__/renderData/script4.cst +0 -54
  101. package/__tests__/renderData/script4.cst.svg +0 -1
  102. package/__tests__/renderData/script5.cst +0 -23
  103. package/__tests__/renderData/script5.cst.svg +0 -1
  104. package/__tests__/renderData/script6.cst +0 -28
  105. package/__tests__/renderData/script6.cst.svg +0 -1
  106. package/__tests__/renderData/script7.cst +0 -26
  107. package/__tests__/renderData/script7.cst.svg +0 -1
  108. package/__tests__/renderData/script8.cst +0 -37
  109. package/__tests__/renderData/script8.cst.svg +0 -1
  110. package/__tests__/testCLI.ts +0 -68
  111. package/__tests__/testMathOps.ts +0 -36
  112. package/__tests__/testMergeWires.ts +0 -141
  113. package/__tests__/testParse.ts +0 -263
  114. package/__tests__/testRender.ts +0 -38
  115. package/build/src/antlr/CircuitScriptLexer.js +0 -287
  116. package/build/src/antlr/CircuitScriptParser.d.ts +0 -674
  117. package/build/src/antlr/CircuitScriptParser.js +0 -4841
  118. package/build/src/helpers.d.ts +0 -1
  119. package/build/src/helpers.js +0 -73
  120. package/build/src/objects/types.js +0 -6
  121. package/build/src/parser.js +0 -69
  122. package/build/src/visitor.d.ts +0 -133
  123. package/build/src/visitor.js +0 -1154
  124. package/documentation.md +0 -238
  125. package/examples/example_arduino_uno.cst +0 -1146
  126. package/examples/example_garden_pump.cst +0 -567
  127. package/examples/lib.cst +0 -185
  128. package/jest.config.js +0 -23
  129. package/refresh.html +0 -42
  130. package/server.cjs +0 -50
  131. package/src/antlr/CircuitScript.g4 +0 -209
  132. package/src/antlr/CircuitScriptLexer.ts +0 -317
  133. package/src/antlr/CircuitScriptParser.ts +0 -4979
  134. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  135. package/src/draw_symbols.ts +0 -1085
  136. package/src/execute.ts +0 -1227
  137. package/src/fonts.ts +0 -1
  138. package/src/geometry.ts +0 -638
  139. package/src/globals.ts +0 -67
  140. package/src/helpers.ts +0 -114
  141. package/src/lexer.ts +0 -151
  142. package/src/logger.ts +0 -17
  143. package/src/objects/ClassComponent.ts +0 -223
  144. package/src/objects/ExecutionScope.ts +0 -201
  145. package/src/objects/Frame.ts +0 -20
  146. package/src/objects/ParamDefinition.ts +0 -49
  147. package/src/objects/PinDefinition.ts +0 -49
  148. package/src/objects/PinTypes.ts +0 -7
  149. package/src/objects/Wire.ts +0 -19
  150. package/src/objects/types.ts +0 -66
  151. package/src/parser.ts +0 -106
  152. package/src/regenerate-tests.ts +0 -25
  153. package/src/render.ts +0 -260
  154. package/src/sizing.ts +0 -96
  155. package/src/visitor.ts +0 -1691
  156. package/tsconfig.json +0 -27
  157. package/tsconfig.release.json +0 -8
  158. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  159. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  160. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  161. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  162. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  163. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  164. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  165. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  166. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  167. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
  168. /package/{build/src → dist/types}/export.d.ts +0 -0
  169. /package/{build/src → dist/types}/fonts.d.ts +0 -0
  170. /package/{build/src → dist/types}/globals.d.ts +0 -0
  171. /package/{build/src → dist/types}/logger.d.ts +0 -0
  172. /package/{build/src → dist/types}/main.d.ts +0 -0
  173. /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
  174. /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
  175. /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
  176. /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
  177. /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
  178. /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
  179. /package/{build/src → dist/types}/server.d.ts +0 -0
  180. /package/{build/src → dist/types}/utils.d.ts +0 -0
@@ -0,0 +1,487 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseVisitor = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const CircuitScriptParser_1 = require("./antlr/CircuitScriptParser");
7
+ const CircuitScriptVisitor_1 = require("./antlr/CircuitScriptVisitor");
8
+ const execute_1 = require("./execute");
9
+ const logger_1 = require("./logger");
10
+ const ClassComponent_1 = require("./objects/ClassComponent");
11
+ const ParamDefinition_1 = require("./objects/ParamDefinition");
12
+ const PinTypes_1 = require("./objects/PinTypes");
13
+ const types_1 = require("./objects/types");
14
+ class BaseVisitor extends CircuitScriptVisitor_1.CircuitScriptVisitor {
15
+ constructor(silent = false, onErrorHandler = null, currentDirectory, defaultLibsPath) {
16
+ super();
17
+ this.indentLevel = 0;
18
+ this.silent = false;
19
+ this.printStream = [];
20
+ this.printToConsole = true;
21
+ this.acceptedDirections = ['left', 'right', 'up', 'down'];
22
+ this.acceptedFlip = ['flipX', 'flipY'];
23
+ this.resultData = new Map;
24
+ this.paramData = new Map;
25
+ this.pinTypesList = [
26
+ PinTypes_1.PinTypes.Any,
27
+ PinTypes_1.PinTypes.Input,
28
+ PinTypes_1.PinTypes.Output,
29
+ PinTypes_1.PinTypes.IO,
30
+ PinTypes_1.PinTypes.Power,
31
+ ];
32
+ this.onErrorCallbackHandler = null;
33
+ this.onImportFile = (visitor, filePath) => {
34
+ throw "Import file not implemented";
35
+ };
36
+ this.visitScript = (ctx) => {
37
+ this.log('===', 'start', '===');
38
+ const result = this.visitChildren(ctx);
39
+ this.log('===', 'end', '===');
40
+ return result;
41
+ };
42
+ this.visitAssignment_expr = (ctx) => {
43
+ const atomStr = ctx.atom_expr().getText();
44
+ if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
45
+ throw "Invalid assignment expression!";
46
+ }
47
+ const ctxAtomExpr = ctx.atom_expr();
48
+ this.visit(ctxAtomExpr);
49
+ const reference = this.getResult(ctxAtomExpr);
50
+ const ctxDataExpr = ctx.data_expr();
51
+ this.visit(ctxDataExpr);
52
+ const value = this.getResult(ctxDataExpr);
53
+ if (value instanceof ClassComponent_1.ClassComponent) {
54
+ const instances = this.getExecutor().scope.instances;
55
+ const tmpComponent = value;
56
+ const oldName = tmpComponent.instanceName;
57
+ tmpComponent.instanceName = reference.name;
58
+ instances.delete(oldName);
59
+ instances.set(reference.name, tmpComponent);
60
+ this.getExecutor().log(`assigned '${reference.name}' to ClassComponent`);
61
+ }
62
+ else {
63
+ const trailers = reference.trailers ?? [];
64
+ if (trailers.length === 0) {
65
+ this.getExecutor().scope.variables.set(reference.name, value);
66
+ }
67
+ else if (reference.value instanceof ClassComponent_1.ClassComponent) {
68
+ this.setInstanceParam(reference.value, trailers, value);
69
+ }
70
+ }
71
+ this.setResult(ctx, value);
72
+ };
73
+ this.visitAtom_expr = (ctx) => {
74
+ const executor = this.getExecutor();
75
+ const firstId = ctx.ID(0);
76
+ const atomId = firstId.getText();
77
+ let currentReference;
78
+ if (this.pinTypesList.indexOf(atomId) !== -1) {
79
+ currentReference = {
80
+ found: true,
81
+ value: atomId
82
+ };
83
+ }
84
+ else {
85
+ currentReference = executor.resolveVariable(this.executionStack, atomId);
86
+ }
87
+ if (currentReference.found && currentReference.type === 'instance') {
88
+ const tmpComponent = currentReference.value;
89
+ for (const [pinId, net] of tmpComponent.pinNets) {
90
+ executor.scope.setNet(tmpComponent, pinId, net);
91
+ }
92
+ if (ctx.ID().length > 1) {
93
+ currentReference.trailers = [];
94
+ const idLength = ctx.ID().length;
95
+ for (let i = 1; i < idLength; i++) {
96
+ const tmpCtx = ctx.ID(i);
97
+ currentReference.trailers.push(tmpCtx.getText());
98
+ }
99
+ }
100
+ }
101
+ if (ctx.parent instanceof CircuitScriptParser_1.ExpressionContext && !currentReference.found) {
102
+ throw "Unknown symbol: " + atomId;
103
+ }
104
+ this.setResult(ctx, currentReference);
105
+ };
106
+ this.visitFunctionCallExpr = (ctx) => {
107
+ const tmpCtx = ctx.function_call_expr();
108
+ this.visit(tmpCtx);
109
+ const result = this.getResult(tmpCtx);
110
+ this.setResult(ctx, result);
111
+ };
112
+ this.visitFunction_call_expr = (ctx) => {
113
+ const executor = this.getExecutor();
114
+ const atomId = ctx.ID().getText();
115
+ let passedNetNamespace = null;
116
+ const netNameSpaceExpr = ctx.net_namespace_expr();
117
+ if (netNameSpaceExpr) {
118
+ this.visit(netNameSpaceExpr);
119
+ passedNetNamespace = this.getResult(netNameSpaceExpr);
120
+ }
121
+ let currentReference = executor.resolveVariable(this.executionStack, atomId);
122
+ if (ctx.trailer_expr().length > 0) {
123
+ if (!currentReference.found) {
124
+ throw "Unknown function name: " + atomId;
125
+ }
126
+ currentReference.trailers = [];
127
+ ctx.trailer_expr().forEach(item => {
128
+ const itemValue = item.getText();
129
+ if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
130
+ let parameters = [];
131
+ const ctxParameters = item.parameters();
132
+ if (ctxParameters) {
133
+ this.visit(ctxParameters);
134
+ parameters = this.getResult(ctxParameters);
135
+ }
136
+ const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
137
+ const [, functionResult] = executor.callFunction(currentReference.name, parameters, this.executionStack, useNetNamespace);
138
+ currentReference = {
139
+ found: true,
140
+ value: functionResult,
141
+ type: (functionResult instanceof ClassComponent_1.ClassComponent) ?
142
+ 'instance' : 'value',
143
+ };
144
+ }
145
+ else {
146
+ currentReference.trailers.push(itemValue);
147
+ }
148
+ });
149
+ }
150
+ this.setResult(ctx, currentReference.value);
151
+ };
152
+ this.visitValue_expr = (ctx) => {
153
+ const sign = ctx.Minus() ? -1 : 1;
154
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
155
+ const ctxDecimalValue = ctx.DECIMAL_VALUE();
156
+ const ctxNumericValue = ctx.NUMERIC_VALUE();
157
+ const ctxBooleanValue = ctx.BOOLEAN_VALUE();
158
+ const ctxStringValue = ctx.STRING_VALUE();
159
+ const ctxPercentageValue = ctx.PERCENTAGE_VALUE();
160
+ const ctxBlankExpr = ctx.blank_expr();
161
+ let result = null;
162
+ if (ctxIntegerValue || ctxDecimalValue || ctxNumericValue) {
163
+ if (ctxIntegerValue) {
164
+ result = sign * Number(ctxIntegerValue.getText());
165
+ }
166
+ else if (ctxDecimalValue) {
167
+ result = sign * Number(ctxDecimalValue.getText());
168
+ }
169
+ else if (ctxNumericValue) {
170
+ const textExtra = ctx.Minus() ? '-' : '';
171
+ result = new ParamDefinition_1.NumericValue(textExtra + ctxNumericValue.getText());
172
+ }
173
+ }
174
+ else {
175
+ if (sign === -1) {
176
+ throw "Invalid value!";
177
+ }
178
+ }
179
+ if (ctxBooleanValue) {
180
+ const stringValue = ctxBooleanValue.getText();
181
+ if (stringValue === 'true') {
182
+ result = true;
183
+ }
184
+ else if (stringValue === 'false') {
185
+ result = false;
186
+ }
187
+ }
188
+ else if (ctxStringValue) {
189
+ result = this.prepareStringValue(ctxStringValue.getText());
190
+ }
191
+ else if (ctxPercentageValue) {
192
+ result = new ParamDefinition_1.PercentageValue(ctxPercentageValue.getText());
193
+ }
194
+ else if (ctxBlankExpr) {
195
+ this.visit(ctxBlankExpr);
196
+ result = this.getResult(ctxBlankExpr);
197
+ }
198
+ this.setResult(ctx, result);
199
+ };
200
+ this.visitBlank_expr = (ctx) => {
201
+ this.setResult(ctx, new ParamDefinition_1.PinBlankValue(Number(ctx.INTEGER_VALUE().getText())));
202
+ };
203
+ this.visitValueAtomExpr = (ctx) => {
204
+ let value = null;
205
+ const ctxValueExpr = ctx.value_expr();
206
+ const ctxAtomExpr = ctx.atom_expr();
207
+ if (ctxValueExpr) {
208
+ this.visit(ctxValueExpr);
209
+ value = this.getResult(ctxValueExpr);
210
+ }
211
+ else if (ctxAtomExpr) {
212
+ this.visit(ctxAtomExpr);
213
+ const reference = this.getResult(ctxAtomExpr);
214
+ if (!reference.found) {
215
+ value = new types_1.UndeclaredReference(reference);
216
+ }
217
+ else {
218
+ value = reference.value;
219
+ }
220
+ }
221
+ this.setResult(ctx, value);
222
+ };
223
+ this.visitFunction_args_expr = (ctx) => {
224
+ const defaultValuesProvided = ctx.value_expr();
225
+ const IDs = ctx.ID();
226
+ const boundary = IDs.length - defaultValuesProvided.length;
227
+ const result = IDs.map((id, index) => {
228
+ const idText = id.getText();
229
+ if (index >= boundary) {
230
+ const tmpCtx = defaultValuesProvided[index - boundary];
231
+ this.visit(tmpCtx);
232
+ const defaultValue = this.getResult(tmpCtx);
233
+ return [idText, defaultValue];
234
+ }
235
+ else {
236
+ return [idText];
237
+ }
238
+ });
239
+ this.setResult(ctx, result);
240
+ };
241
+ this.visitParameters = (ctx) => {
242
+ const dataExpressions = ctx.data_expr();
243
+ const keywordAssignmentExpressions = ctx.keyword_assignment_expr();
244
+ const returnList = [];
245
+ dataExpressions.forEach((item, index) => {
246
+ this.visit(item);
247
+ const value = this.getResult(item);
248
+ returnList.push(['position', index, value]);
249
+ });
250
+ keywordAssignmentExpressions.forEach((item) => {
251
+ this.visit(item);
252
+ const [key, value] = this.getResult(item);
253
+ returnList.push(['keyword', key, value]);
254
+ });
255
+ this.setResult(ctx, returnList);
256
+ };
257
+ this.visitImport_expr = (ctx) => {
258
+ const ID = ctx.ID().toString();
259
+ this.log('import', ID);
260
+ this.handleImportFile(ID, true);
261
+ this.log('done import', ID);
262
+ };
263
+ this.visitFunction_return_expr = (ctx) => {
264
+ const executor = this.getExecutor();
265
+ executor.log('return from function');
266
+ const ctxDataExpr = ctx.data_expr();
267
+ this.visit(ctxDataExpr);
268
+ const returnValue = this.getResult(ctxDataExpr);
269
+ executor.stopFurtherExpressions = true;
270
+ executor.returnValue = returnValue;
271
+ this.setResult(ctx, returnValue);
272
+ };
273
+ this.visitBreak_keyword = (ctx) => {
274
+ this.getExecutor().breakBranch();
275
+ this.setResult(ctx, -1);
276
+ };
277
+ this.visitRoundedBracketsExpr = (ctx) => {
278
+ const ctxDataExpr = ctx.data_expr();
279
+ this.visit(ctxDataExpr);
280
+ const innerResult = this.getResult(ctxDataExpr);
281
+ this.setResult(ctx, innerResult);
282
+ };
283
+ this.logger = new logger_1.Logger();
284
+ this.onErrorCallbackHandler = onErrorHandler;
285
+ this.startingContext = new execute_1.ExecutionContext('__', '__.', '/', 0, 0, silent, this.logger, null);
286
+ this.setupPrintFunction(this.startingContext);
287
+ this.executionStack = [this.startingContext];
288
+ this.startingContext.resolveNet =
289
+ this.createNetResolver(this.executionStack);
290
+ this.silent = silent;
291
+ this.currentDirectory = currentDirectory;
292
+ this.defaultLibsPath = defaultLibsPath;
293
+ }
294
+ getExecutor() {
295
+ return this.executionStack[this.executionStack.length - 1];
296
+ }
297
+ setupPrintFunction(context) {
298
+ context.createFunction('print', (params) => {
299
+ const items = params.map(([, , value]) => {
300
+ return value;
301
+ });
302
+ if (this.printToConsole) {
303
+ console.log('::', ...items);
304
+ }
305
+ this.printStream.push(...items);
306
+ return [this, null];
307
+ });
308
+ }
309
+ createNetResolver(executionStack) {
310
+ const resolveNet = (netName, netNamespace) => {
311
+ this.log('find net', netNamespace, netName);
312
+ const reversed = [...executionStack].reverse();
313
+ for (let i = 0; i < reversed.length; i++) {
314
+ const context = reversed[i];
315
+ const net = context.scope.getNetWithName(netName);
316
+ if (net !== null && net.namespace === netNamespace) {
317
+ return {
318
+ found: true,
319
+ net,
320
+ };
321
+ }
322
+ }
323
+ return {
324
+ found: false
325
+ };
326
+ };
327
+ return resolveNet;
328
+ }
329
+ log(...params) {
330
+ const indentOutput = ''.padStart(this.indentLevel * 4, ' ');
331
+ const indentLevelText = this.indentLevel.toString().padStart(3, ' ');
332
+ const args = ['[' + indentLevelText + ']', indentOutput, ...params];
333
+ this.logger.add(args.join(' '));
334
+ if (!this.silent) {
335
+ console.log.apply(null, args);
336
+ }
337
+ }
338
+ log2(message) {
339
+ this.getExecutor().log(message);
340
+ }
341
+ setResult(ctx, value) {
342
+ this.resultData.set(ctx, value);
343
+ }
344
+ getResult(ctx) {
345
+ return this.resultData.get(ctx);
346
+ }
347
+ setParam(ctx, value) {
348
+ this.paramData.set(ctx, value);
349
+ }
350
+ getParam(ctx) {
351
+ return this.paramData.get(ctx);
352
+ }
353
+ hasParam(ctx) {
354
+ return this.paramData.has(ctx);
355
+ }
356
+ handleImportFile(name, throwErrors = true) {
357
+ let hasError = false;
358
+ let hasParseError = false;
359
+ let pathExists = false;
360
+ const tmpFilePath = (0, path_1.join)(this.currentDirectory, name + ".cst");
361
+ this.log('importing path:', tmpFilePath);
362
+ let fileData = null;
363
+ try {
364
+ fileData = (0, fs_1.readFileSync)(tmpFilePath, { encoding: 'utf8' });
365
+ pathExists = true;
366
+ }
367
+ catch (err) {
368
+ pathExists = false;
369
+ }
370
+ if (!pathExists) {
371
+ try {
372
+ const tmpFilePath2 = (0, path_1.join)(this.defaultLibsPath, name + ".cst");
373
+ fileData = (0, fs_1.readFileSync)(tmpFilePath2, { encoding: 'utf8' });
374
+ pathExists = true;
375
+ }
376
+ catch (err) {
377
+ pathExists = false;
378
+ }
379
+ }
380
+ try {
381
+ if (pathExists) {
382
+ this.log('done reading imported file data');
383
+ const importResult = this.onImportFile(this, fileData, this.onErrorCallbackHandler);
384
+ hasError = importResult.hasError;
385
+ hasParseError = importResult.hasParseError;
386
+ }
387
+ }
388
+ catch (err) {
389
+ this.log('Failed to import file: ', err.message);
390
+ }
391
+ if (throwErrors && (hasError || hasParseError || !pathExists)) {
392
+ if (!pathExists) {
393
+ throw `File does not exist: ${name}`;
394
+ }
395
+ else {
396
+ throw `Failed to import: ${name}`;
397
+ }
398
+ }
399
+ return {
400
+ hasError,
401
+ hasParseError,
402
+ pathExists,
403
+ };
404
+ }
405
+ setupDefinedParameters(funcDefinedParameters, passedInParameters, executor) {
406
+ for (let i = 0; i < funcDefinedParameters.length; i++) {
407
+ const tmpFuncArg = funcDefinedParameters[i];
408
+ if (i < passedInParameters.length) {
409
+ const tmpPassedInArgs = passedInParameters[i];
410
+ if (tmpPassedInArgs[0] === 'position') {
411
+ const variableName = tmpFuncArg[0];
412
+ executor.log('set variable in scope, var name: ', variableName);
413
+ executor.scope.variables.set(variableName, tmpPassedInArgs[2]);
414
+ if (tmpPassedInArgs[2] instanceof ClassComponent_1.ClassComponent) {
415
+ const component = tmpPassedInArgs[2];
416
+ for (const [pinNumber, net] of component.pinNets) {
417
+ executor.scope.setNet(component, pinNumber, net);
418
+ }
419
+ }
420
+ }
421
+ }
422
+ else if (tmpFuncArg.length === 2) {
423
+ const variableName = tmpFuncArg[0];
424
+ const defaultValue = tmpFuncArg[1];
425
+ executor.log('set variable in scope, var name: ', variableName);
426
+ executor.scope.variables.set(variableName, defaultValue);
427
+ }
428
+ else {
429
+ throw `Invalid arguments got: ` + passedInParameters;
430
+ }
431
+ }
432
+ }
433
+ runExpressions(executor, expressions) {
434
+ let returnValue = null;
435
+ for (let i = 0; i < expressions.length; i++) {
436
+ const expr = expressions[i];
437
+ this.visit(expr);
438
+ if (executor.stopFurtherExpressions) {
439
+ returnValue = executor.returnValue;
440
+ break;
441
+ }
442
+ }
443
+ return returnValue;
444
+ }
445
+ getNetNamespace(executorNetNamespace, passedNetNamespace) {
446
+ let result = executorNetNamespace;
447
+ if (passedNetNamespace !== null && passedNetNamespace.length > 0) {
448
+ if (passedNetNamespace === '/' || passedNetNamespace === '_') {
449
+ result = '';
450
+ }
451
+ else if (passedNetNamespace[0] === '+') {
452
+ if (executorNetNamespace === '/') {
453
+ result = passedNetNamespace.slice(1);
454
+ }
455
+ else {
456
+ result = executorNetNamespace + passedNetNamespace.slice(2);
457
+ }
458
+ }
459
+ else {
460
+ result = passedNetNamespace;
461
+ }
462
+ result = result + '/';
463
+ }
464
+ return result;
465
+ }
466
+ setInstanceParam(object, trailers, value) {
467
+ const paramName = trailers[0];
468
+ object.setParam(paramName, value);
469
+ this.getExecutor().log(`set instance ${object.instanceName} param ${paramName} to ${value}`);
470
+ }
471
+ enterNewChildContext(executionStack, parentContext, executionContextName, options, funcDefinedParameters, passedInParameters) {
472
+ const { netNamespace = "" } = options;
473
+ const currentExecutionContext = executionStack[executionStack.length - 1];
474
+ const executionLevel = currentExecutionContext.executionLevel;
475
+ const executionContextNamespace = currentExecutionContext.namespace
476
+ + executionContextName + ".";
477
+ const newExecutor = new execute_1.ExecutionContext(executionContextName, executionContextNamespace, netNamespace, executionLevel + 1, this.getExecutor().scope.indentLevel + 1, currentExecutionContext.silent, currentExecutionContext.logger, parentContext);
478
+ this.setupPrintFunction(newExecutor);
479
+ executionStack.push(newExecutor);
480
+ this.setupDefinedParameters(funcDefinedParameters, passedInParameters, newExecutor);
481
+ return newExecutor;
482
+ }
483
+ prepareStringValue(value) {
484
+ return value.slice(1, value.length - 1);
485
+ }
486
+ }
487
+ exports.BaseVisitor = BaseVisitor;