circuitscript 0.0.21 → 0.0.24

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 (163) hide show
  1. package/dist/cjs/antlr/CircuitScriptLexer.js +288 -0
  2. package/dist/cjs/antlr/CircuitScriptParser.js +4905 -0
  3. package/dist/cjs/antlr/CircuitScriptVisitor.js +6 -0
  4. package/{src/draw_symbols.ts → dist/cjs/draw_symbols.js} +303 -614
  5. package/dist/cjs/execute.js +780 -0
  6. package/{src/export.ts → dist/cjs/export.js} +34 -56
  7. package/dist/cjs/fonts.js +4 -0
  8. package/dist/cjs/geometry.js +430 -0
  9. package/dist/cjs/globals.js +60 -0
  10. package/dist/cjs/helpers.js +99 -0
  11. package/dist/cjs/index.js +29 -0
  12. package/{src/layout.ts → dist/cjs/layout.js} +413 -1002
  13. package/dist/cjs/lexer.js +114 -0
  14. package/dist/cjs/logger.js +17 -0
  15. package/dist/cjs/main.js +87 -0
  16. package/dist/cjs/objects/ClassComponent.js +142 -0
  17. package/dist/cjs/objects/ExecutionScope.js +134 -0
  18. package/dist/cjs/objects/Frame.js +22 -0
  19. package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
  20. package/dist/cjs/objects/ParamDefinition.js +42 -0
  21. package/dist/cjs/objects/PinDefinition.js +31 -0
  22. package/dist/cjs/objects/PinTypes.js +11 -0
  23. package/dist/cjs/objects/Wire.js +9 -0
  24. package/dist/cjs/objects/types.js +9 -0
  25. package/dist/cjs/parser.js +299 -0
  26. package/dist/cjs/regenerate-tests.js +23 -0
  27. package/dist/cjs/render.js +155 -0
  28. package/{src/server.ts → dist/cjs/server.js} +15 -21
  29. package/dist/cjs/sizing.js +105 -0
  30. package/{src/utils.ts → dist/cjs/utils.js} +25 -35
  31. package/{src/visitor.ts → dist/cjs/visitor.js} +392 -948
  32. package/{build/src/antlr/CircuitScriptLexer.js → dist/esm/antlr/CircuitScriptLexer.mjs} +90 -91
  33. package/{build/src/antlr/CircuitScriptParser.js → dist/esm/antlr/CircuitScriptParser.mjs} +138 -136
  34. package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +11 -11
  35. package/{build/src/execute.js → dist/esm/execute.mjs} +9 -8
  36. package/{build/src/export.js → dist/esm/export.mjs} +2 -2
  37. package/{build/src/geometry.js → dist/esm/geometry.mjs} +3 -7
  38. package/{build/src/helpers.js → dist/esm/helpers.mjs} +27 -7
  39. package/dist/esm/index.mjs +13 -0
  40. package/{build/src/layout.js → dist/esm/layout.mjs} +11 -11
  41. package/{build/src/lexer.js → dist/esm/lexer.mjs} +2 -2
  42. package/{build/src/main.js → dist/esm/main.mjs} +5 -5
  43. package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +3 -3
  44. package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
  45. package/dist/esm/parser.mjs +269 -0
  46. package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
  47. package/{build/src/render.js → dist/esm/render.mjs} +7 -24
  48. package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
  49. package/{build/src/visitor.js → dist/esm/visitor.mjs} +10 -29
  50. package/dist/types/antlr/CircuitScriptLexer.d.ts +71 -0
  51. package/dist/types/antlr/CircuitScriptParser.d.ts +675 -0
  52. package/dist/types/antlr/CircuitScriptVisitor.d.ts +115 -0
  53. package/dist/types/draw_symbols.d.ts +162 -0
  54. package/dist/types/execute.d.ts +85 -0
  55. package/dist/types/export.d.ts +2 -0
  56. package/dist/types/fonts.d.ts +1 -0
  57. package/dist/types/geometry.d.ts +84 -0
  58. package/dist/types/globals.d.ts +50 -0
  59. package/dist/types/helpers.d.ts +9 -0
  60. package/dist/types/index.d.ts +13 -0
  61. package/dist/types/layout.d.ts +147 -0
  62. package/dist/types/lexer.d.ts +19 -0
  63. package/dist/types/logger.d.ts +6 -0
  64. package/dist/types/main.d.ts +2 -0
  65. package/dist/types/objects/ClassComponent.d.ts +40 -0
  66. package/dist/types/objects/ExecutionScope.d.ts +64 -0
  67. package/dist/types/objects/Frame.d.ts +15 -0
  68. package/dist/types/objects/Net.d.ts +10 -0
  69. package/dist/types/objects/ParamDefinition.d.ts +20 -0
  70. package/dist/types/objects/PinDefinition.d.ts +24 -0
  71. package/dist/types/objects/PinTypes.d.ts +7 -0
  72. package/dist/types/objects/Wire.d.ts +11 -0
  73. package/dist/types/objects/types.d.ts +49 -0
  74. package/dist/types/parser.d.ts +38 -0
  75. package/dist/types/regenerate-tests.d.ts +1 -0
  76. package/dist/types/render.d.ts +10 -0
  77. package/dist/types/server.d.ts +1 -0
  78. package/dist/types/sizing.d.ts +15 -0
  79. package/dist/types/utils.d.ts +19 -0
  80. package/dist/types/visitor.d.ts +135 -0
  81. package/package.json +30 -12
  82. package/.editorconfig +0 -15
  83. package/.eslintignore +0 -1
  84. package/.eslintrc.json +0 -27
  85. package/.gitlab-ci.yml +0 -81
  86. package/.prettierignore +0 -8
  87. package/.prettierrc +0 -16
  88. package/__tests__/expectedResults.ts +0 -657
  89. package/__tests__/helpers.ts +0 -82
  90. package/__tests__/parseScripts.ts +0 -593
  91. package/__tests__/renderData/script1.cst +0 -58
  92. package/__tests__/renderData/script1.cst.svg +0 -1
  93. package/__tests__/renderData/script2.cst +0 -16
  94. package/__tests__/renderData/script2.cst.svg +0 -1
  95. package/__tests__/renderData/script3.cst +0 -30
  96. package/__tests__/renderData/script3.cst.svg +0 -1
  97. package/__tests__/renderData/script4.cst +0 -54
  98. package/__tests__/renderData/script4.cst.svg +0 -1
  99. package/__tests__/renderData/script5.cst +0 -23
  100. package/__tests__/renderData/script5.cst.svg +0 -1
  101. package/__tests__/renderData/script6.cst +0 -28
  102. package/__tests__/renderData/script6.cst.svg +0 -1
  103. package/__tests__/renderData/script7.cst +0 -26
  104. package/__tests__/renderData/script7.cst.svg +0 -1
  105. package/__tests__/renderData/script8.cst +0 -37
  106. package/__tests__/renderData/script8.cst.svg +0 -1
  107. package/__tests__/testCLI.ts +0 -68
  108. package/__tests__/testMathOps.ts +0 -36
  109. package/__tests__/testMergeWires.ts +0 -141
  110. package/__tests__/testParse.ts +0 -263
  111. package/__tests__/testRender.ts +0 -38
  112. package/build/src/parser.js +0 -69
  113. package/documentation.md +0 -238
  114. package/examples/example_arduino_uno.cst +0 -1146
  115. package/examples/example_garden_pump.cst +0 -567
  116. package/examples/lib.cst +0 -185
  117. package/fonts/Inter-Bold.ttf +0 -0
  118. package/fonts/Inter-Regular.ttf +0 -0
  119. package/fonts/OpenSans-Regular.ttf +0 -0
  120. package/fonts/Roboto-Regular.ttf +0 -0
  121. package/jest.config.js +0 -23
  122. package/libs/lib.cst +0 -185
  123. package/refresh.html +0 -42
  124. package/server.cjs +0 -50
  125. package/src/antlr/CircuitScript.g4 +0 -209
  126. package/src/antlr/CircuitScriptLexer.ts +0 -317
  127. package/src/antlr/CircuitScriptParser.ts +0 -4979
  128. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  129. package/src/execute.ts +0 -1227
  130. package/src/fonts.ts +0 -1
  131. package/src/geometry.ts +0 -638
  132. package/src/globals.ts +0 -67
  133. package/src/helpers.ts +0 -114
  134. package/src/lexer.ts +0 -151
  135. package/src/logger.ts +0 -17
  136. package/src/main.ts +0 -105
  137. package/src/objects/ClassComponent.ts +0 -223
  138. package/src/objects/ExecutionScope.ts +0 -201
  139. package/src/objects/Frame.ts +0 -20
  140. package/src/objects/ParamDefinition.ts +0 -49
  141. package/src/objects/PinDefinition.ts +0 -49
  142. package/src/objects/PinTypes.ts +0 -7
  143. package/src/objects/Wire.ts +0 -19
  144. package/src/objects/types.ts +0 -66
  145. package/src/parser.ts +0 -106
  146. package/src/regenerate-tests.ts +0 -25
  147. package/src/render.ts +0 -260
  148. package/src/sizing.ts +0 -96
  149. package/tsconfig.json +0 -26
  150. package/tsconfig.release.json +0 -8
  151. /package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +0 -0
  152. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  153. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  154. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  155. /package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +0 -0
  156. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  157. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  158. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  159. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  160. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  161. /package/{build/src/objects/types.js → dist/esm/objects/types.mjs} +0 -0
  162. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  163. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
@@ -0,0 +1,780 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPortSide = exports.isLabelComponent = exports.isNetComponent = exports.ExecutionContext = void 0;
4
+ const globals_js_1 = require("./globals.js");
5
+ const ClassComponent_js_1 = require("./objects/ClassComponent.js");
6
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
7
+ const Net_js_1 = require("./objects/Net.js");
8
+ const PinDefinition_js_1 = require("./objects/PinDefinition.js");
9
+ const Wire_js_1 = require("./objects/Wire.js");
10
+ const Frame_js_1 = require("./objects/Frame.js");
11
+ class ExecutionContext {
12
+ constructor(name, namespace, netNamespace, executionLevel = 0, indentLevel = 0, silent = false, logger) {
13
+ this.tmpPointId = 0;
14
+ this.resolveNet = null;
15
+ this.stopFurtherExpressions = false;
16
+ this.returnValue = null;
17
+ this.silent = false;
18
+ this.__functionCache = {};
19
+ this.name = name;
20
+ this.namespace = namespace;
21
+ this.netNamespace = netNamespace;
22
+ this.executionLevel = executionLevel;
23
+ this.logger = logger;
24
+ this.scope = ExecutionScope_js_1.ExecutionScope.create();
25
+ this.scope.indentLevel = indentLevel;
26
+ this.setupRoot();
27
+ this.silent = silent;
28
+ this.print('create new execution context', this.namespace, this.name, this.scope.indentLevel);
29
+ }
30
+ print(...params) {
31
+ const indentOutput = ''.padStart(this.scope.indentLevel * 4, ' ');
32
+ const indentLevelText = this.scope.indentLevel
33
+ .toString()
34
+ .padStart(3, ' ');
35
+ const args = ['[' + indentLevelText + ']', indentOutput, ...params];
36
+ this.logger.add(args.join(' '));
37
+ if (this.silent) {
38
+ return;
39
+ }
40
+ console.log.apply(null, args);
41
+ }
42
+ setupRoot() {
43
+ const componentRoot = ClassComponent_js_1.ClassComponent.simple(globals_js_1.GlobalNames.__root, 1, '__root');
44
+ componentRoot.typeProp = globals_js_1.ComponentTypes.net;
45
+ this.scope.instances.set(globals_js_1.GlobalNames.__root, componentRoot);
46
+ this.scope.currentComponent = componentRoot;
47
+ this.scope.currentPin = componentRoot.getDefaultPin();
48
+ this.scope.componentRoot = componentRoot;
49
+ }
50
+ instanceExists(instanceName) {
51
+ return this.scope.instances.has(instanceName);
52
+ }
53
+ getComponent(instanceName) {
54
+ return this.scope.instances.get(instanceName);
55
+ }
56
+ getUniqueInstanceName(className) {
57
+ let extraPrefix = '';
58
+ switch (className) {
59
+ case globals_js_1.GlobalNames.DefaultResistor:
60
+ extraPrefix = 'R_';
61
+ break;
62
+ case globals_js_1.GlobalNames.DefaultCapacitor:
63
+ extraPrefix = 'C_';
64
+ break;
65
+ case globals_js_1.GlobalNames.DefaultInductor:
66
+ extraPrefix = 'L_';
67
+ break;
68
+ }
69
+ const tmpName = extraPrefix + 'COMP_' + this.scope.unnamedCounter;
70
+ this.scope.unnamedCounter += 1;
71
+ return tmpName;
72
+ }
73
+ getUniqueNetName() {
74
+ const tmpName = 'NET_' + this.scope.netCounter;
75
+ this.scope.netCounter++;
76
+ return tmpName;
77
+ }
78
+ getCurrentPoint() {
79
+ return [this.scope.currentComponent, this.scope.currentPin];
80
+ }
81
+ linkComponentPinNet(component1, component1Pin, component2, component2Pin) {
82
+ const net1_exists = this.scope.hasNet(component1, component1Pin);
83
+ const net2_exists = this.scope.hasNet(component2, component2Pin);
84
+ const net1 = net1_exists
85
+ ? this.scope.getNet(component1, component1Pin)
86
+ : null;
87
+ const net2 = net2_exists
88
+ ? this.scope.getNet(component2, component2Pin)
89
+ : null;
90
+ this.print('link nets', component1, component1Pin, net1, 'to', component2, component2Pin, net2);
91
+ let returnNet;
92
+ if (net1 === null && net2 === null) {
93
+ const tmpNet = new Net_js_1.Net(this.netNamespace, this.getUniqueNetName());
94
+ this.scope.setNet(component1, component1Pin, tmpNet);
95
+ this.scope.setNet(component2, component2Pin, tmpNet);
96
+ returnNet = tmpNet;
97
+ }
98
+ else if (net1 === null && net2 !== null) {
99
+ this.scope.setNet(component1, component1Pin, net2);
100
+ returnNet = net2;
101
+ }
102
+ else if (net1 !== null && net2 === null) {
103
+ this.scope.setNet(component2, component2Pin, net1);
104
+ returnNet = net1;
105
+ }
106
+ else {
107
+ if (net1 !== net2) {
108
+ returnNet = this.mergeNets(net1, net2);
109
+ }
110
+ else {
111
+ return net1;
112
+ }
113
+ }
114
+ return returnNet;
115
+ }
116
+ mergeNets(net1, net2) {
117
+ if (net1 === net2) {
118
+ return net1;
119
+ }
120
+ let tmpNet;
121
+ if (net2.priority > net1.priority) {
122
+ tmpNet = net1;
123
+ net1 = net2;
124
+ net2 = tmpNet;
125
+ }
126
+ const scopeNets = this.scope.getNets();
127
+ scopeNets.forEach(([component, pin, net]) => {
128
+ if (Net_js_1.Net.isSame(net, net2)) {
129
+ this.scope.setNet(component, pin, net1);
130
+ }
131
+ });
132
+ return net1;
133
+ }
134
+ createComponent(instanceName, pins, params, props) {
135
+ const numPins = pins.length;
136
+ const component = new ClassComponent_js_1.ClassComponent(instanceName, numPins, globals_js_1.GlobalNames.symbol);
137
+ pins.forEach((pin) => {
138
+ component.pins.set(pin.id, pin);
139
+ });
140
+ const paramsMap = new Map();
141
+ params.forEach((param) => {
142
+ component.parameters.set(param.paramName, param.paramValue);
143
+ paramsMap.set(param.paramName, param.paramValue);
144
+ });
145
+ if (paramsMap.has(globals_js_1.ParamKeys.__is_net)) {
146
+ const netName = paramsMap.get(globals_js_1.ParamKeys.net_name);
147
+ const priority = paramsMap.get(globals_js_1.ParamKeys.priority);
148
+ const result = this.resolveNet(netName, this.netNamespace);
149
+ let tmpNet;
150
+ if (result.found) {
151
+ tmpNet = result.net;
152
+ this.print('net found', tmpNet.namespace, tmpNet.name);
153
+ }
154
+ else {
155
+ tmpNet = new Net_js_1.Net(this.netNamespace, netName, priority);
156
+ this.print('net not found, added net instance', tmpNet.namespace, tmpNet.name);
157
+ }
158
+ this.scope.setNet(component, 1, tmpNet);
159
+ this.print('set net', netName, 'component', component);
160
+ }
161
+ const { arrange = null } = props;
162
+ component.arrangeProps = arrange;
163
+ component.displayProp = props.display ?? null;
164
+ component.widthProp = props.width ?? null;
165
+ component.typeProp = props.type ?? null;
166
+ const portSides = getPortSide(component.pins, arrange);
167
+ portSides.forEach(({ pinId, side, position }) => {
168
+ if (component.pins.has(pinId)) {
169
+ const tmpPin = component.pins.get(pinId);
170
+ tmpPin.side = side;
171
+ tmpPin.position = position;
172
+ }
173
+ });
174
+ this.scope.instances.set(instanceName, component);
175
+ const pinsOutput = pins.map((pin) => {
176
+ return pin.id + ':' + pin.name;
177
+ });
178
+ this.print('add symbol', instanceName, '[' + pinsOutput.join(', ') + ']');
179
+ return component;
180
+ }
181
+ printPoint(extra = '') {
182
+ let netName = globals_js_1.NoNetText;
183
+ if (this.scope.hasNet(this.scope.currentComponent, this.scope.currentPin)) {
184
+ netName = this.scope
185
+ .getNet(this.scope.currentComponent, this.scope.currentPin)
186
+ .toString();
187
+ }
188
+ this.print((extra !== '' ? (extra + ' ') : '') + 'point: ' +
189
+ this.scope.currentComponent.instanceName +
190
+ ' ' +
191
+ this.scope.currentPin + ' ' + netName);
192
+ }
193
+ addComponentExisting(component, pin) {
194
+ const startPin = pin;
195
+ const nextPin = component.getNextPinAfter(startPin);
196
+ this.toComponent(component, startPin, { addSequence: true });
197
+ this.print('move to next pin: ' + nextPin);
198
+ this.atComponent(component, nextPin, {
199
+ addSequence: true
200
+ });
201
+ this.printPoint();
202
+ return this.getCurrentPoint();
203
+ }
204
+ toComponent(component, pinId, options) {
205
+ this.print('to component');
206
+ const { addSequence = false, cloneNetComponent = false } = options ?? {};
207
+ if (cloneNetComponent && this.isNetOnlyComponent(component)) {
208
+ component = this.cloneComponent(component);
209
+ }
210
+ if (!(component instanceof ClassComponent_js_1.ClassComponent)) {
211
+ throw "Not a valid component!";
212
+ }
213
+ if (pinId === null) {
214
+ pinId = component.getDefaultPin();
215
+ }
216
+ else {
217
+ if (component.hasPin(pinId)) {
218
+ pinId = component.getPin(pinId);
219
+ }
220
+ else {
221
+ console.trace();
222
+ throw ('Invalid pin number ' +
223
+ pinId +
224
+ ' in ' +
225
+ component.instanceName);
226
+ }
227
+ }
228
+ if (this.scope.hasNet(this.scope.currentComponent, this.scope.currentPin)) {
229
+ this.print('net: ', this.scope
230
+ .getNet(this.scope.currentComponent, this.scope.currentPin)
231
+ .toString());
232
+ }
233
+ const linkedNet = this.linkComponentPinNet(this.scope.currentComponent, this.scope.currentPin, component, pinId);
234
+ this.scope.currentComponent = component;
235
+ this.scope.currentPin = pinId;
236
+ this.scope.clearActive();
237
+ if (addSequence) {
238
+ if (this.scope.sequence.length > 0) {
239
+ if (component.pinWires.has(pinId) && component.typeProp !== globals_js_1.ComponentTypes.point) {
240
+ }
241
+ const [entryType, , segments] = this.scope.sequence[this.scope.sequence.length - 1];
242
+ if (entryType === ExecutionScope_js_1.SequenceAction.Wire && isWireSegmentsEndAuto(segments)) {
243
+ segments[segments.length - 1].until = [
244
+ component, pinId
245
+ ];
246
+ }
247
+ component.pinWires.set(pinId, segments);
248
+ }
249
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.To, component,
250
+ pinId, linkedNet]);
251
+ }
252
+ this.printPoint();
253
+ return this.getCurrentPoint();
254
+ }
255
+ atComponent(component, pinId, options) {
256
+ this.print('at component');
257
+ const { addSequence = false, cloneNetComponent = false } = options ?? {};
258
+ if (cloneNetComponent && this.isNetOnlyComponent(component)) {
259
+ component = this.cloneComponent(component);
260
+ }
261
+ this.scope.currentComponent = component;
262
+ let usePinId;
263
+ if (pinId === null) {
264
+ usePinId = component.getDefaultPin();
265
+ }
266
+ else {
267
+ if (component.hasPin(pinId)) {
268
+ usePinId = component.getPin(pinId);
269
+ }
270
+ else {
271
+ throw 'Invalid pin number ' + pinId + ' in ' + component;
272
+ }
273
+ }
274
+ if (usePinId) {
275
+ this.scope.currentPin = usePinId;
276
+ }
277
+ this.scope.clearActive();
278
+ if (addSequence) {
279
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.At,
280
+ component, usePinId]);
281
+ }
282
+ this.printPoint();
283
+ return this.getCurrentPoint();
284
+ }
285
+ isNetOnlyComponent(component) {
286
+ return isNetComponent(component) && !isLabelComponent(component);
287
+ }
288
+ cloneComponent(component) {
289
+ let clonedComponent = null;
290
+ if (!this.scope.copyIDs.has(component.instanceName)) {
291
+ this.scope.copyIDs.set(component.instanceName, 0);
292
+ }
293
+ const idNum = this.scope.copyIDs.get(component.instanceName);
294
+ clonedComponent = component.clone();
295
+ clonedComponent._copyID = idNum;
296
+ clonedComponent._copyFrom = component;
297
+ this.scope.copyIDs.set(component.instanceName, idNum + 1);
298
+ const cloneInstanceName = component.instanceName + ':' + idNum;
299
+ this.scope.instances.set(cloneInstanceName, clonedComponent);
300
+ clonedComponent.instanceName = cloneInstanceName;
301
+ this.linkComponentPinNet(component, 1, clonedComponent, 1);
302
+ this.print('created clone of net component:', cloneInstanceName);
303
+ return clonedComponent;
304
+ }
305
+ enterBlocks(blockType) {
306
+ if (blockType === globals_js_1.BlockTypes.Point) {
307
+ this.addPoint(`_point.${this.name}.${this.tmpPointId}`, false);
308
+ this.tmpPointId += 1;
309
+ }
310
+ else if (blockType === globals_js_1.BlockTypes.Parallel) {
311
+ this.addPoint(`_parallel.${this.name}.${this.tmpPointId}`, false);
312
+ this.tmpPointId += 1;
313
+ }
314
+ this.scope.blockStack.set(this.scope.indentLevel, {
315
+ entered_at: [
316
+ this.scope.currentComponent,
317
+ this.scope.currentPin,
318
+ this.scope.currentWireId
319
+ ],
320
+ inner_blocks: new Map(),
321
+ current_index: null,
322
+ type: blockType,
323
+ });
324
+ this.print('enter blocks');
325
+ }
326
+ exitBlocks() {
327
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel);
328
+ const { type: blockType } = stackRef;
329
+ if (blockType === globals_js_1.BlockTypes.Join || blockType === globals_js_1.BlockTypes.Parallel) {
330
+ const { final_point: finalPoint } = stackRef;
331
+ const [component, pin, wireId] = finalPoint;
332
+ this.scope.currentComponent = component;
333
+ this.scope.currentPin = pin;
334
+ this.scope.currentWireId = wireId;
335
+ if (wireId !== -1) {
336
+ this.scope.sequence.push([
337
+ ExecutionScope_js_1.SequenceAction.WireJump, wireId, 1
338
+ ]);
339
+ }
340
+ }
341
+ else if (blockType === globals_js_1.BlockTypes.Point) {
342
+ const { entered_at: [component, pin,] } = stackRef;
343
+ this.atComponent(component, pin, { addSequence: true });
344
+ }
345
+ this.print('exit blocks');
346
+ }
347
+ enterBlock(blockIndex) {
348
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel);
349
+ stackRef['block_index'] = blockIndex;
350
+ const { type: blockType } = stackRef;
351
+ stackRef['inner_blocks'].set(blockIndex, {
352
+ last_net: null,
353
+ ignore_last_net: false,
354
+ });
355
+ if (blockType === globals_js_1.BlockTypes.Join || blockType === globals_js_1.BlockTypes.Point) {
356
+ this.scope.currentComponent = null;
357
+ this.scope.currentPin = null;
358
+ this.scope.currentWireId = -1;
359
+ }
360
+ else if (blockType === globals_js_1.BlockTypes.Parallel) {
361
+ const { entered_at: [component, pin,] } = stackRef;
362
+ this.atComponent(component, pin, { addSequence: true });
363
+ }
364
+ this.print(`enter inner block of type (${blockType}) >>>`);
365
+ this.scope.indentLevel += 1;
366
+ }
367
+ exitBlock(blockIndex) {
368
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel - 1);
369
+ const { type: blockType } = stackRef;
370
+ const blockIndexRef = stackRef['inner_blocks'].get(blockIndex);
371
+ blockIndexRef['last_net'] = [
372
+ this.scope.currentComponent,
373
+ this.scope.currentPin,
374
+ this.scope.currentWireId
375
+ ];
376
+ stackRef['block_index'] = null;
377
+ this.scope.indentLevel -= 1;
378
+ this.print('exit inner block <<<');
379
+ if (blockType === globals_js_1.BlockTypes.Branch) {
380
+ const { entered_at: [component, pin, wireId] } = stackRef;
381
+ this.atComponent(component, pin, { addSequence: true });
382
+ if (wireId !== -1) {
383
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.WireJump, wireId, 1]);
384
+ }
385
+ }
386
+ else if (blockType === globals_js_1.BlockTypes.Join || blockType === globals_js_1.BlockTypes.Parallel) {
387
+ if (blockIndex === 0) {
388
+ const pointIdName = (blockType === globals_js_1.BlockTypes.Join) ? '_join' : '_parallel';
389
+ this.addPoint(`${pointIdName}.${this.name}.${this.tmpPointId}`, false);
390
+ this.tmpPointId += 1;
391
+ stackRef['final_point'] = [
392
+ this.scope.currentComponent,
393
+ this.scope.currentPin,
394
+ this.scope.currentWireId
395
+ ];
396
+ }
397
+ else {
398
+ const { final_point: finalPoint } = stackRef;
399
+ const [component, pin,] = finalPoint;
400
+ this.toComponent(component, pin, { addSequence: true });
401
+ }
402
+ }
403
+ }
404
+ atPointBlock() {
405
+ const [component, pin,] = this.getPointBlockLocation();
406
+ this.atComponent(component, pin, {
407
+ addSequence: true
408
+ });
409
+ }
410
+ toPointBlock() {
411
+ const [component, pin,] = this.getPointBlockLocation();
412
+ this.toComponent(component, pin, {
413
+ addSequence: true
414
+ });
415
+ }
416
+ getPointBlockLocation() {
417
+ this.print('get block point');
418
+ for (let i = 0; i < this.scope.indentLevel; i++) {
419
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel - 1 - i);
420
+ const { entered_at } = stackRef;
421
+ const component = entered_at[0];
422
+ if (component.instanceName.startsWith('_point.')) {
423
+ return entered_at;
424
+ }
425
+ }
426
+ this.print('did not find block point');
427
+ return null;
428
+ }
429
+ breakBranch() {
430
+ this.print('break branch');
431
+ const branchesInfo = this.scope.blockStack.get(this.scope.indentLevel - 1);
432
+ const branchIndex = branchesInfo['block_index'];
433
+ const branchIndexRef = branchesInfo['inner_blocks'].get(branchIndex);
434
+ branchIndexRef['ignore_last_net'] = true;
435
+ }
436
+ createFunction(functionName, __runFunc) {
437
+ this.scope.functions.set(functionName, __runFunc);
438
+ this.__functionCache[functionName] = __runFunc;
439
+ this.print(`defined new function '${functionName}'`);
440
+ }
441
+ hasFunction(functionName) {
442
+ return this.scope.functions.has(functionName);
443
+ }
444
+ getFunction(functionName) {
445
+ return this.scope.functions.get(functionName);
446
+ }
447
+ resolveVariable(executionStack, idName) {
448
+ const reversed = [...executionStack].reverse();
449
+ for (let i = 0; i < reversed.length; i++) {
450
+ const context = reversed[i];
451
+ if (context.hasFunction(idName)) {
452
+ return {
453
+ found: true,
454
+ value: context.getFunction(idName),
455
+ type: globals_js_1.ReferenceTypes.function,
456
+ name: idName,
457
+ };
458
+ }
459
+ else if (context.scope.variables.has(idName)) {
460
+ return {
461
+ found: true,
462
+ value: context.scope.variables.get(idName),
463
+ type: globals_js_1.ReferenceTypes.variable,
464
+ name: idName,
465
+ };
466
+ }
467
+ else if (context.scope.instances.has(idName)) {
468
+ return {
469
+ found: true,
470
+ value: context.scope.instances.get(idName),
471
+ type: globals_js_1.ReferenceTypes.instance,
472
+ name: idName,
473
+ };
474
+ }
475
+ }
476
+ return {
477
+ found: false,
478
+ name: idName,
479
+ };
480
+ }
481
+ callFunction(functionName, functionParams, executionStack, netNamespace) {
482
+ let __runFunc = null;
483
+ if (this.__functionCache[functionName] === undefined) {
484
+ if (this.hasFunction(functionName)) {
485
+ __runFunc = this.getFunction(functionName);
486
+ }
487
+ if (__runFunc === null) {
488
+ this.print(`searching for function ${functionName} in upper context`);
489
+ const tmpResolveResult = this.resolveVariable(executionStack, functionName);
490
+ if (tmpResolveResult.found) {
491
+ __runFunc = tmpResolveResult.value;
492
+ }
493
+ else {
494
+ throw `Invalid function ${functionName}`;
495
+ }
496
+ }
497
+ this.print('save function to cache:', functionName);
498
+ this.__functionCache[functionName] = __runFunc;
499
+ }
500
+ else {
501
+ this.print('found function in cache:', functionName);
502
+ __runFunc = this.__functionCache[functionName];
503
+ }
504
+ if (__runFunc !== null) {
505
+ this.print(`call function '${functionName}'`);
506
+ const functionResult = __runFunc(functionParams, { netNamespace });
507
+ this.print(`done call function '${functionName}'`);
508
+ return functionResult;
509
+ }
510
+ else {
511
+ throw `Invalid function '${functionName}'`;
512
+ }
513
+ }
514
+ mergeScope(childScope, namespace) {
515
+ this.print('-- merging scope to parent --');
516
+ const currentComponent = this.scope.currentComponent;
517
+ const currentPin = this.scope.currentPin;
518
+ const currentWireId = this.scope.currentWireId;
519
+ const gndCopyIdOffset = 0;
520
+ const tmpInstances = childScope.instances;
521
+ const tmpNets = childScope.getNets();
522
+ for (const [instanceName, component] of tmpInstances) {
523
+ const newInstanceName = `${namespace}.${instanceName}`;
524
+ component.instanceName = newInstanceName;
525
+ if (component === childScope.componentGnd ||
526
+ component === childScope.componentRoot) {
527
+ continue;
528
+ }
529
+ if (!this.scope.instances.has(newInstanceName)) {
530
+ this.scope.instances.set(newInstanceName, component);
531
+ }
532
+ else {
533
+ throw "Invalid instance name to merge into parent scope!";
534
+ }
535
+ }
536
+ tmpNets.forEach(([component, pin, net]) => {
537
+ this.scope.setNet(component, pin, net);
538
+ });
539
+ const linkRootComponent = true;
540
+ const tmpRoot = childScope.componentRoot;
541
+ if (linkRootComponent) {
542
+ const netConnectedToRoot = childScope.getNet(tmpRoot, 1);
543
+ if (netConnectedToRoot !== null) {
544
+ let currentNet = this.scope.getNet(currentComponent, currentPin);
545
+ if (currentNet === null) {
546
+ const tmpNet = new Net_js_1.Net(this.netNamespace, this.getUniqueNetName());
547
+ this.scope.setNet(currentComponent, currentPin, netConnectedToRoot);
548
+ currentNet = tmpNet;
549
+ }
550
+ netConnectedToRoot.priority = currentNet.priority - 1;
551
+ this.toComponent(tmpRoot, 1);
552
+ }
553
+ }
554
+ const wireIdOffset = this.scope.wires.length;
555
+ const frameIdOffset = this.scope.frames.length;
556
+ let incrementGndLinkId = 0;
557
+ childScope.sequence.forEach(sequenceAction => {
558
+ const [action] = sequenceAction;
559
+ if (action === ExecutionScope_js_1.SequenceAction.Wire) {
560
+ const [, innerWireId, segments] = sequenceAction;
561
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.Wire, wireIdOffset + innerWireId, segments]);
562
+ this.scope.wires.push(new Wire_js_1.Wire(segments));
563
+ }
564
+ else if (action === ExecutionScope_js_1.SequenceAction.WireJump) {
565
+ const jumpWireId = wireIdOffset + sequenceAction[1];
566
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.WireJump, jumpWireId, 1]);
567
+ }
568
+ else if (action === ExecutionScope_js_1.SequenceAction.At || action === ExecutionScope_js_1.SequenceAction.To) {
569
+ const tmpComponent = sequenceAction[1];
570
+ if (isNetComponent(tmpComponent) && tmpComponent.parameters.get(globals_js_1.ParamKeys.net_name) === 'gnd') {
571
+ tmpComponent._copyID = gndCopyIdOffset + incrementGndLinkId;
572
+ incrementGndLinkId += 1;
573
+ }
574
+ else if (tmpComponent === tmpRoot) {
575
+ if (currentWireId !== -1) {
576
+ sequenceAction = [ExecutionScope_js_1.SequenceAction.WireJump, currentWireId];
577
+ }
578
+ else {
579
+ sequenceAction = [action, currentComponent, currentPin];
580
+ }
581
+ }
582
+ this.scope.sequence.push(sequenceAction);
583
+ }
584
+ else if (action === ExecutionScope_js_1.SequenceAction.Frame) {
585
+ const frame = sequenceAction[1];
586
+ const frameAction = sequenceAction[2];
587
+ if (frameAction === ExecutionScope_js_1.FrameAction.Enter) {
588
+ frame.frameId += frameIdOffset;
589
+ this.scope.frames.push(frame);
590
+ }
591
+ this.scope.sequence.push(sequenceAction);
592
+ }
593
+ });
594
+ if (childScope.currentComponent === childScope.componentRoot) {
595
+ this.scope.currentComponent = currentComponent;
596
+ this.scope.currentPin = currentPin;
597
+ this.scope.currentWireId = currentWireId;
598
+ }
599
+ else {
600
+ this.scope.currentComponent = childScope.currentComponent;
601
+ this.scope.currentPin = childScope.currentPin;
602
+ this.scope.currentWireId = childScope.currentWireId + wireIdOffset;
603
+ }
604
+ this.printPoint('resume at');
605
+ this.print('-- nets --');
606
+ const currentNets = this.scope.getNets();
607
+ currentNets.reduce((accum, [, , net]) => {
608
+ if (accum.indexOf(net) === -1) {
609
+ accum.push(net);
610
+ this.print(`${net.namespace}${net.name} ${net.priority}`);
611
+ }
612
+ return accum;
613
+ }, []);
614
+ this.print('-- done merging scope --');
615
+ }
616
+ addWire(segments) {
617
+ if (this.scope.currentComponent === null) {
618
+ throw "No current component";
619
+ }
620
+ const tmp = segments.map(item => {
621
+ const [direction, value = null] = item;
622
+ return {
623
+ direction,
624
+ value
625
+ };
626
+ });
627
+ const wireId = this.scope.wires.length;
628
+ this.scope.wires.push(new Wire_js_1.Wire(tmp));
629
+ const output = [];
630
+ segments.forEach(item => {
631
+ output.push(item.join(","));
632
+ });
633
+ this.print('add wire: ', output.join("|"));
634
+ this.scope.setActive(ExecutionScope_js_1.ActiveObject.Wire, wireId);
635
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.Wire, wireId, tmp]);
636
+ this.scope.currentComponent.pinWires.set(this.scope.currentPin, tmp);
637
+ }
638
+ addPoint(pointId, userDefined = true) {
639
+ if (this.scope.instances.has(pointId)) {
640
+ this.print('Warning: ' + pointId + ' is being redefined');
641
+ }
642
+ const useName = userDefined ? 'point.' + pointId : pointId;
643
+ const componentPoint = ClassComponent_js_1.ClassComponent.simple(useName, 1, "point");
644
+ componentPoint.displayProp = "point";
645
+ componentPoint.typeProp = globals_js_1.ComponentTypes.point;
646
+ this.scope.instances.set(pointId, componentPoint);
647
+ this.toComponent(componentPoint, 1, { addSequence: true });
648
+ return this.getCurrentPoint();
649
+ }
650
+ setProperty(nameWithProp, value) {
651
+ this.print('set property', nameWithProp, 'value', value);
652
+ let idName;
653
+ let paramName;
654
+ let useActive = false;
655
+ if (nameWithProp.startsWith('..')) {
656
+ useActive = true;
657
+ paramName = nameWithProp.substring(2);
658
+ }
659
+ else {
660
+ const parts = nameWithProp.split(".");
661
+ idName = parts[0];
662
+ paramName = parts[1];
663
+ }
664
+ if (useActive && this.scope.currentFrameId !== -1) {
665
+ this.scope.frames[this.scope.currentFrameId - 1]
666
+ .parameters.set(paramName, value);
667
+ }
668
+ else {
669
+ idName = this.scope.currentComponent.instanceName;
670
+ if (this.scope.instances.has(idName)) {
671
+ const component = this.scope.instances.get(idName);
672
+ component.parameters.set(paramName, value);
673
+ }
674
+ else if (this.scope.variables.has(idName)) {
675
+ throw "Not implemented yet!";
676
+ }
677
+ else {
678
+ throw "Unknown identifier: " + idName;
679
+ }
680
+ }
681
+ }
682
+ setCurrentComponentStyle(styles) {
683
+ for (const key in styles) {
684
+ this.scope.currentComponent.styles[key] = styles[key];
685
+ }
686
+ }
687
+ enterFrame() {
688
+ const frameId = this.scope.frames.length + 1;
689
+ const frameObject = new Frame_js_1.Frame(frameId);
690
+ this.scope.frames.push(frameObject);
691
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.Frame,
692
+ frameObject, ExecutionScope_js_1.FrameAction.Enter]);
693
+ this.scope.currentFrameId = frameId;
694
+ this.scope.setActive(ExecutionScope_js_1.ActiveObject.Frame, frameId);
695
+ return frameId;
696
+ }
697
+ exitFrame(frameId) {
698
+ const frame = this.scope.frames[frameId - 1];
699
+ this.scope.sequence.push([ExecutionScope_js_1.SequenceAction.Frame,
700
+ frame, ExecutionScope_js_1.FrameAction.Exit]);
701
+ }
702
+ }
703
+ exports.ExecutionContext = ExecutionContext;
704
+ function isWireSegmentsEndAuto(segments) {
705
+ if (segments.length > 0) {
706
+ if (segments[segments.length - 1].value === null) {
707
+ return true;
708
+ }
709
+ }
710
+ return false;
711
+ }
712
+ function isNetComponent(component) {
713
+ return component.parameters.has(globals_js_1.ParamKeys.__is_net);
714
+ }
715
+ exports.isNetComponent = isNetComponent;
716
+ function isLabelComponent(component) {
717
+ return component.parameters.has(globals_js_1.ParamKeys.__is_label);
718
+ }
719
+ exports.isLabelComponent = isLabelComponent;
720
+ function getPortSide(pins, arrangeProps) {
721
+ const result = [];
722
+ if (arrangeProps === null) {
723
+ let counter = 0;
724
+ for (const [pinId] of pins) {
725
+ result.push({
726
+ pinId,
727
+ side: counter % 2 === 0 ? PinDefinition_js_1.PortSide.WEST : PinDefinition_js_1.PortSide.EAST,
728
+ order: counter,
729
+ position: Math.floor(counter / 2),
730
+ });
731
+ counter++;
732
+ }
733
+ }
734
+ else {
735
+ let counter = pins.size;
736
+ const existingPinIds = Array.from(pins.keys());
737
+ for (const [key, items] of arrangeProps) {
738
+ let useItems;
739
+ if (!Array.isArray(items)) {
740
+ useItems = [items];
741
+ }
742
+ else {
743
+ useItems = [...items];
744
+ }
745
+ let useSide = PinDefinition_js_1.PortSide.WEST;
746
+ if (key === 'left') {
747
+ useSide = PinDefinition_js_1.PortSide.WEST;
748
+ }
749
+ else if (key === 'right') {
750
+ useSide = PinDefinition_js_1.PortSide.EAST;
751
+ }
752
+ else if (key === 'top') {
753
+ useSide = PinDefinition_js_1.PortSide.NORTH;
754
+ }
755
+ else if (key === 'bottom') {
756
+ useSide = PinDefinition_js_1.PortSide.SOUTH;
757
+ }
758
+ let position = 0;
759
+ useItems.forEach(item => {
760
+ if (typeof item === 'object') {
761
+ if (item.blank) {
762
+ position += item.blank;
763
+ }
764
+ }
765
+ if (existingPinIds.indexOf(item) !== -1) {
766
+ result.push({
767
+ pinId: item,
768
+ side: useSide,
769
+ position,
770
+ order: counter
771
+ });
772
+ counter--;
773
+ position += 1;
774
+ }
775
+ });
776
+ }
777
+ }
778
+ return result;
779
+ }
780
+ exports.getPortSide = getPortSide;