circuitscript 0.1.15 → 0.1.16

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.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.linkBuiltInMethods = exports.buildInMethodNamesList = void 0;
7
7
  const big_js_1 = __importDefault(require("big.js"));
8
8
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
9
+ const types_js_1 = require("./objects/types.js");
9
10
  const utils_js_1 = require("./utils.js");
10
11
  const builtInMethods = [
11
12
  ['enumerate', enumerate],
@@ -151,8 +152,17 @@ function toString(obj) {
151
152
  else if (obj instanceof ParamDefinition_js_1.NumericValue) {
152
153
  return obj.toBigNumber().toString();
153
154
  }
155
+ else if (obj instanceof types_js_1.CFunctionEntry) {
156
+ return obj.toString();
157
+ }
154
158
  else {
155
- if (obj.toDisplayString) {
159
+ if (obj === undefined) {
160
+ return 'undefined';
161
+ }
162
+ else if (obj === null) {
163
+ return 'null';
164
+ }
165
+ else if (obj.toDisplayString) {
156
166
  return obj.toDisplayString();
157
167
  }
158
168
  else if (obj.toString) {
@@ -21,7 +21,7 @@ class ExecutionContext {
21
21
  this.stopFurtherExpressions = false;
22
22
  this.returnValue = null;
23
23
  this.silent = false;
24
- this.__functionCache = {};
24
+ this.__functionCache = new Map();
25
25
  this.componentAngleFollowsWire = true;
26
26
  this.warnings = [];
27
27
  this.name = name;
@@ -533,9 +533,9 @@ class ExecutionContext {
533
533
  getBreakContext() {
534
534
  return this.scope.breakStack[this.scope.breakStack.length - 1];
535
535
  }
536
- createFunction(functionName, __runFunc) {
537
- this.scope.functions.set(functionName, __runFunc);
538
- this.__functionCache[functionName] = __runFunc;
536
+ createFunction(functionName, __runFunc, source, uniqueId) {
537
+ this.scope.functions.set(functionName, new types_js_1.CFunctionEntry(functionName, __runFunc, source, uniqueId));
538
+ this.__functionCache.set(functionName, __runFunc);
539
539
  this.log(`defined new function '${functionName}'`);
540
540
  }
541
541
  hasFunction(functionName) {
@@ -604,12 +604,17 @@ class ExecutionContext {
604
604
  }
605
605
  }
606
606
  else {
607
- useValue = parentValue.parameters.get(trailersPath);
607
+ useValue = parentValue
608
+ .parameters.get(trailersPath);
608
609
  }
609
610
  }
610
611
  }
612
+ let found = false;
613
+ if (parentValue !== undefined && useValue !== undefined) {
614
+ found = true;
615
+ }
611
616
  return new types_js_1.AnyReference({
612
- found: true,
617
+ found,
613
618
  type: type,
614
619
  parentValue,
615
620
  trailers,
@@ -618,26 +623,28 @@ class ExecutionContext {
618
623
  }
619
624
  callFunction(functionName, functionParams, executionStack, netNamespace) {
620
625
  let __runFunc = null;
621
- if (this.__functionCache[functionName] === undefined) {
626
+ if (!this.__functionCache.has(functionName)) {
622
627
  if (this.hasFunction(functionName)) {
623
- __runFunc = this.getFunction(functionName);
628
+ const entry = this.getFunction(functionName);
629
+ __runFunc = entry.execute;
624
630
  }
625
631
  if (__runFunc === null) {
626
632
  this.log(`searching for function ${functionName} in upper context`);
627
633
  const tmpResolveResult = this.resolveVariable(executionStack, functionName);
628
634
  if (tmpResolveResult.found) {
629
- __runFunc = tmpResolveResult.value;
635
+ const entry = tmpResolveResult.value;
636
+ __runFunc = entry.execute;
630
637
  }
631
638
  else {
632
639
  throw `Invalid function ${functionName}`;
633
640
  }
634
641
  }
635
642
  this.log('save function to cache:', functionName);
636
- this.__functionCache[functionName] = __runFunc;
643
+ this.__functionCache.set(functionName, __runFunc);
637
644
  }
638
645
  else {
639
646
  this.log('found function in cache:', functionName);
640
- __runFunc = this.__functionCache[functionName];
647
+ __runFunc = this.__functionCache.get(functionName);
641
648
  }
642
649
  if (__runFunc !== null) {
643
650
  this.log(`call function '${functionName}'`);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SymbolValidatorContext = exports.RenderFlags = exports.GlobalDocumentName = exports.ModuleContainsKeyword = exports.FrameType = exports.NetGraphicsParams = exports.BlockTypes = exports.ReferenceTypes = exports.ComponentTypes = exports.ColorScheme = exports.PortPaddingVertical = exports.PortPaddingHorizontal = exports.PortArrowSize = exports.junctionSize = exports.defaultFontSize = exports.defaultFontBold = exports.defaultFont = exports.displayUnits = exports.defaultFrameTitleTextSize = exports.CustomSymbolParamTextSize = exports.CustomSymbolRefDesSize = exports.CustomSymbolPinIdSize = exports.CustomSymbolPinTextSize = exports.defaultPageSpacingMM = exports.defaultPageMarginMM = exports.defaultPinIdTextSize = exports.defaultPinNameTextSize = exports.defaultWireLineWidth = exports.defaultSymbolLineWidth = exports.fontDisplayScale = exports.defaultZoomScale = exports.defaultGridSizeUnits = exports.portHeight = exports.portWidth = exports.PxToMM = exports.MMToPt = exports.MMToPx = exports.MilsToMM = exports.WireAutoDirection = exports.LengthUnit = exports.ValidPinSides = exports.SymbolPinSide = exports.LayoutDirection = exports.ParamKeys = exports.NoNetText = exports.GlobalNames = exports.DoubleDelimiter1 = exports.Delimiter1 = exports.TOOL_VERSION = void 0;
3
+ exports.TrailerArrayIndex = exports.SymbolValidatorContext = exports.RenderFlags = exports.GlobalDocumentName = exports.ModuleContainsKeyword = exports.FrameType = exports.NetGraphicsParams = exports.BlockTypes = exports.ReferenceTypes = exports.ComponentTypes = exports.ColorScheme = exports.PortPaddingVertical = exports.PortPaddingHorizontal = exports.PortArrowSize = exports.junctionSize = exports.defaultFontSize = exports.defaultFontBold = exports.defaultFont = exports.displayUnits = exports.defaultFrameTitleTextSize = exports.CustomSymbolParamTextSize = exports.CustomSymbolRefDesSize = exports.CustomSymbolPinIdSize = exports.CustomSymbolPinTextSize = exports.defaultPageSpacingMM = exports.defaultPageMarginMM = exports.defaultPinIdTextSize = exports.defaultPinNameTextSize = exports.defaultWireLineWidth = exports.defaultSymbolLineWidth = exports.fontDisplayScale = exports.defaultZoomScale = exports.defaultGridSizeUnits = exports.portHeight = exports.portWidth = exports.PxToMM = exports.MMToPt = exports.MMToPx = exports.MilsToMM = exports.WireAutoDirection = exports.LengthUnit = exports.ValidPinSides = exports.SymbolPinSide = exports.LayoutDirection = exports.ParamKeys = exports.NoNetText = exports.GlobalNames = exports.DoubleDelimiter1 = exports.Delimiter1 = exports.TOOL_VERSION = void 0;
4
4
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
5
5
  exports.TOOL_VERSION = '0.1.5';
6
6
  exports.Delimiter1 = '-';
@@ -95,6 +95,7 @@ var ReferenceTypes;
95
95
  ReferenceTypes["variable"] = "variable";
96
96
  ReferenceTypes["instance"] = "instance";
97
97
  ReferenceTypes["pinType"] = "pinType";
98
+ ReferenceTypes["unknown"] = "unknown";
98
99
  })(ReferenceTypes || (exports.ReferenceTypes = ReferenceTypes = {}));
99
100
  var BlockTypes;
100
101
  (function (BlockTypes) {
@@ -126,3 +127,4 @@ exports.RenderFlags = {
126
127
  ShowLabelOrigin: false,
127
128
  };
128
129
  exports.SymbolValidatorContext = '_sym';
130
+ exports.TrailerArrayIndex = 'index';
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RenderItemType = exports.generateLayoutPinDefinition = exports.getWireName = exports.NetGraph = void 0;
4
+ const graphlib_1 = require("@dagrejs/graphlib");
5
+ const draw_symbols_js_1 = require("./draw_symbols.js");
6
+ const globals_js_1 = require("./globals.js");
7
+ const helpers_js_1 = require("./helpers.js");
8
+ const layout_js_1 = require("./layout.js");
9
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
10
+ const Frame_js_1 = require("./objects/Frame.js");
11
+ const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
12
+ class NetGraph {
13
+ constructor(logger) {
14
+ this.logger = logger;
15
+ }
16
+ generateLayoutGraph(sequence, nets) {
17
+ this.print('===== creating graph and populating with nodes =====');
18
+ let previousNode = null;
19
+ let previousPin = null;
20
+ const graph = new graphlib_1.Graph({
21
+ directed: true,
22
+ compound: true,
23
+ });
24
+ this.print('sequence length:', sequence.length);
25
+ const baseFrame = new layout_js_1.RenderFrame(new Frame_js_1.Frame(Frame_js_1.FixedFrameIds.BaseFrame));
26
+ const frameStack = [baseFrame];
27
+ const containerFrames = [baseFrame];
28
+ sequence.forEach((sequenceStep, index) => {
29
+ const action = sequenceStep[0];
30
+ let tmpComponent;
31
+ switch (action) {
32
+ case ExecutionScope_js_1.SequenceAction.To:
33
+ case ExecutionScope_js_1.SequenceAction.At: {
34
+ this.print(...sequenceStep);
35
+ const [, component, pin] = sequenceStep;
36
+ const tmpInstanceName = component.instanceName;
37
+ if (!graph.hasNode(tmpInstanceName)) {
38
+ this.print('create instance', tmpInstanceName);
39
+ const { displayProp = null } = component;
40
+ let tmpSymbol;
41
+ if (displayProp instanceof draw_symbols_js_1.SymbolDrawing) {
42
+ tmpSymbol = new draw_symbols_js_1.SymbolPlaceholder(displayProp);
43
+ tmpSymbol.drawing.logger = this.logger;
44
+ }
45
+ else {
46
+ const symbolPinDefinitions = generateLayoutPinDefinition(component);
47
+ if (component.typeProp === globals_js_1.ComponentTypes.module) {
48
+ tmpSymbol = new draw_symbols_js_1.SymbolCustomModule(symbolPinDefinitions, component.pinsMaxPositions);
49
+ }
50
+ else {
51
+ tmpSymbol = new draw_symbols_js_1.SymbolCustom(symbolPinDefinitions, component.pinsMaxPositions);
52
+ }
53
+ }
54
+ (0, layout_js_1.applyComponentParamsToSymbol)(component, tmpSymbol);
55
+ tmpSymbol.refreshDrawing();
56
+ const { width: useWidth, height: useHeight } = tmpSymbol.size();
57
+ tmpComponent = new layout_js_1.RenderComponent(component, useWidth, useHeight);
58
+ tmpComponent.symbol = tmpSymbol;
59
+ graph.setNode(tmpInstanceName, [RenderItemType.Component, tmpComponent, index]);
60
+ const currentFrame = frameStack[frameStack.length - 1];
61
+ currentFrame && currentFrame.innerItems.push(tmpComponent);
62
+ }
63
+ if (action === ExecutionScope_js_1.SequenceAction.To && previousNode && previousPin) {
64
+ this.setGraphEdge(graph, previousNode, tmpInstanceName, makeEdgeValue(previousNode, previousPin, tmpInstanceName, pin, index));
65
+ }
66
+ previousNode = tmpInstanceName;
67
+ previousPin = pin;
68
+ break;
69
+ }
70
+ case ExecutionScope_js_1.SequenceAction.Wire: {
71
+ const [, wireId, wireSegments] = sequenceStep;
72
+ let useNet;
73
+ if (previousNode !== null) {
74
+ const [prevNodeType, prevNodeItem] = graph.node(previousNode);
75
+ if (prevNodeType === RenderItemType.Component) {
76
+ const matchingItem = nets.find(([comp, pin]) => {
77
+ return comp.instanceName === previousNode
78
+ && pin === previousPin;
79
+ });
80
+ if (matchingItem !== undefined) {
81
+ useNet = matchingItem[2];
82
+ }
83
+ }
84
+ else if (prevNodeType === RenderItemType.Wire) {
85
+ useNet = prevNodeItem.net;
86
+ }
87
+ }
88
+ const wire = new layout_js_1.RenderWire(useNet, (0, ParamDefinition_js_1.numeric)(0), (0, ParamDefinition_js_1.numeric)(0), wireSegments);
89
+ wire.id = wireId;
90
+ wire.netName = useNet.toString();
91
+ const wireName = getWireName(wire.id);
92
+ graph.setNode(wireName, [RenderItemType.Wire, wire, index]);
93
+ this.setGraphEdge(graph, previousNode, wireName, makeEdgeValue(previousNode, previousPin, wireName, 0, index));
94
+ previousNode = wireName;
95
+ previousPin = 1;
96
+ const wireSegmentsInfo = wireSegments.map(item => {
97
+ const tmp = {
98
+ direction: item.direction,
99
+ value: item.value,
100
+ };
101
+ if (item.valueXY) {
102
+ tmp.valueXY = item.valueXY;
103
+ }
104
+ if (item.until) {
105
+ tmp.until = [item.until[0].toString(), item.until[1]];
106
+ }
107
+ return tmp;
108
+ });
109
+ this.print(ExecutionScope_js_1.SequenceAction.Wire, wireId, JSON.stringify(wireSegmentsInfo));
110
+ break;
111
+ }
112
+ case ExecutionScope_js_1.SequenceAction.WireJump: {
113
+ this.print(...sequenceStep);
114
+ const wireId = sequenceStep[1];
115
+ const wireName = getWireName(wireId);
116
+ let wirePin = 1;
117
+ if (sequenceStep.length === 3) {
118
+ wirePin = sequenceStep[2];
119
+ }
120
+ previousNode = wireName;
121
+ previousPin = wirePin;
122
+ break;
123
+ }
124
+ case ExecutionScope_js_1.SequenceAction.Frame: {
125
+ const [, frameObject, frameAction] = sequenceStep;
126
+ if (frameAction === ExecutionScope_js_1.FrameAction.Enter) {
127
+ const prevFrame = frameStack[frameStack.length - 1];
128
+ const newFrame = new layout_js_1.RenderFrame(frameObject);
129
+ if (frameObject.parameters.has(Frame_js_1.FrameParamKeys.Direction)) {
130
+ newFrame.direction =
131
+ frameObject.parameters.get(Frame_js_1.FrameParamKeys.Direction);
132
+ }
133
+ if (frameObject.parameters.has(Frame_js_1.FrameParamKeys.Padding)) {
134
+ newFrame.padding = (0, helpers_js_1.milsToMM)(frameObject.parameters.get(Frame_js_1.FrameParamKeys.Padding));
135
+ }
136
+ if (frameObject.parameters.has(Frame_js_1.FrameParamKeys.Border)) {
137
+ newFrame.borderWidth =
138
+ frameObject.parameters.get(Frame_js_1.FrameParamKeys.Border);
139
+ }
140
+ if (frameObject.parameters.has(Frame_js_1.FrameParamKeys.Width)) {
141
+ newFrame.width = (0, helpers_js_1.milsToMM)(frameObject.parameters.get(Frame_js_1.FrameParamKeys.Width));
142
+ }
143
+ if (frameObject.parameters.has(Frame_js_1.FrameParamKeys.Height)) {
144
+ newFrame.height = (0, helpers_js_1.milsToMM)(frameObject.parameters.get(Frame_js_1.FrameParamKeys.Height));
145
+ }
146
+ containerFrames.push(newFrame);
147
+ frameStack.push(newFrame);
148
+ prevFrame && prevFrame.innerItems.push(newFrame);
149
+ }
150
+ else if (frameAction === ExecutionScope_js_1.FrameAction.Exit) {
151
+ frameStack.pop();
152
+ }
153
+ break;
154
+ }
155
+ }
156
+ });
157
+ this.print('===== done populating graph =====');
158
+ this.print('');
159
+ const logNodesAndEdges = true;
160
+ if (logNodesAndEdges) {
161
+ this.print('===== graph edges =====');
162
+ const allEdges = graph.edges();
163
+ allEdges.forEach(edge => {
164
+ const [nodeId1, pin1, nodeId2, pin2] = graph.edge(edge);
165
+ this.print(nodeId1, 'pin', pin1, '-----', nodeId2, 'pin', pin2);
166
+ });
167
+ this.print('===== end edges =====');
168
+ this.print();
169
+ this.print('===== graph nodes =====');
170
+ const nodes = graph.nodes();
171
+ nodes.forEach(node => {
172
+ this.print(`name:${node}, value:${graph.node(node)}`);
173
+ });
174
+ this.print('===== end nodes =====');
175
+ this.print('');
176
+ }
177
+ return {
178
+ graph,
179
+ containerFrames,
180
+ };
181
+ }
182
+ setGraphEdge(graph, node1, node2, edgeValue) {
183
+ if (!graph.isDirected && graph.hasEdge(node1, node2)) {
184
+ this.print(`Warning: edge already exists ${node1} ${node2}`);
185
+ }
186
+ graph.setEdge(node1, node2, edgeValue);
187
+ this.print(`created edge: node1:${node1} node2:${node2} edgeValue:${edgeValue}`);
188
+ }
189
+ print(...params) {
190
+ this.logger.add(params.join(' '));
191
+ }
192
+ generateNetGraph(nets) {
193
+ const graph = new graphlib_1.Graph({
194
+ directed: false
195
+ });
196
+ nets.forEach(item => {
197
+ const [component, pin, net] = item;
198
+ const netNodeName = this.getNetNodeName(net);
199
+ if (!graph.hasNode(netNodeName)) {
200
+ graph.setNode(netNodeName, net);
201
+ }
202
+ const componentNodeName = this.getComponentName(component);
203
+ if (!graph.hasNode(componentNodeName)) {
204
+ graph.setNode(componentNodeName, component);
205
+ }
206
+ graph.setEdge(netNodeName, componentNodeName, [component, pin, net]);
207
+ });
208
+ }
209
+ findNodePaths(graph, startNode, endNode, seenNodes = []) {
210
+ const edges = graph.nodeEdges(startNode);
211
+ const paths = [];
212
+ for (let i = 0; i < edges.length; i++) {
213
+ const edge = edges[i];
214
+ const node1 = edge.v;
215
+ const node2 = edge.w;
216
+ let nextNode = "";
217
+ if (node1 === startNode) {
218
+ nextNode = node2;
219
+ }
220
+ else {
221
+ nextNode = node1;
222
+ }
223
+ if (nextNode === endNode) {
224
+ paths.push([startNode, endNode]);
225
+ continue;
226
+ }
227
+ else if (seenNodes.indexOf(nextNode) !== -1) {
228
+ continue;
229
+ }
230
+ seenNodes.push(startNode);
231
+ const routes = this.findNodePaths(graph, nextNode, endNode, seenNodes);
232
+ for (let j = 0; j < routes.length; j++) {
233
+ paths.push([startNode, ...routes[j]]);
234
+ }
235
+ }
236
+ return paths;
237
+ }
238
+ getNetNodeName(net) {
239
+ return 'net:' + net.toString();
240
+ }
241
+ getComponentName(component) {
242
+ return 'component:' + component.instanceName;
243
+ }
244
+ }
245
+ exports.NetGraph = NetGraph;
246
+ function makeEdgeValue(instanceName1, instancePin1, instanceName2, instancePin2, priority) {
247
+ return [instanceName1, instancePin1, instanceName2, instancePin2, priority];
248
+ }
249
+ function getWireName(wireId) {
250
+ return 'wire:' + wireId;
251
+ }
252
+ exports.getWireName = getWireName;
253
+ function generateLayoutPinDefinition(component) {
254
+ const pins = component.pins;
255
+ const symbolPinDefinitions = [];
256
+ const existingPinIds = Array.from(pins.keys());
257
+ const arrangeProps = component.arrangeProps ?? [];
258
+ const addedPins = [];
259
+ for (const [key, items] of arrangeProps) {
260
+ let useItems;
261
+ if (!Array.isArray(items)) {
262
+ useItems = [items];
263
+ }
264
+ else {
265
+ useItems = [...items];
266
+ }
267
+ useItems.forEach(pinId => {
268
+ if (pinId instanceof ParamDefinition_js_1.NumericValue) {
269
+ const pinIdValue = pinId.toNumber();
270
+ if (existingPinIds.indexOf(pinIdValue) !== -1) {
271
+ const pin = pins.get(pinIdValue);
272
+ symbolPinDefinitions.push({
273
+ side: key,
274
+ pinId: pinIdValue,
275
+ text: pin.name,
276
+ position: pin.position,
277
+ pinType: pin.pinType,
278
+ });
279
+ addedPins.push(pinIdValue);
280
+ }
281
+ }
282
+ });
283
+ }
284
+ const unplacedPins = existingPinIds.filter(pinId => {
285
+ return addedPins.indexOf(pinId) === -1;
286
+ });
287
+ if (unplacedPins.length > 0) {
288
+ component._unplacedPins = unplacedPins;
289
+ console.warn("Warning: There are unplaced pins: " + unplacedPins);
290
+ }
291
+ return symbolPinDefinitions;
292
+ }
293
+ exports.generateLayoutPinDefinition = generateLayoutPinDefinition;
294
+ var RenderItemType;
295
+ (function (RenderItemType) {
296
+ RenderItemType["Wire"] = "wire";
297
+ RenderItemType["Component"] = "component";
298
+ })(RenderItemType || (exports.RenderItemType = RenderItemType = {}));
@@ -23,6 +23,7 @@ const globals_js_1 = require("./globals.js");
23
23
  const Frame_js_1 = require("./objects/Frame.js");
24
24
  const big_js_1 = __importDefault(require("big.js"));
25
25
  const logger_js_1 = require("./logger.js");
26
+ const graph_js_1 = require("./graph.js");
26
27
  var JSModuleType;
27
28
  (function (JSModuleType) {
28
29
  JSModuleType["CommonJs"] = "cjs";
@@ -247,11 +248,14 @@ async function renderScript(scriptData, outputPath, options) {
247
248
  errors,
248
249
  };
249
250
  }
250
- const layoutEngine = new layout_js_1.LayoutEngine();
251
+ const logger = new logger_js_1.Logger();
252
+ const graphEngine = new graph_js_1.NetGraph(logger);
253
+ const layoutEngine = new layout_js_1.LayoutEngine(logger);
251
254
  const layoutTimer = new utils_js_1.SimpleStopwatch();
252
255
  let sheetFrames;
253
256
  try {
254
- sheetFrames = layoutEngine.runLayout(sequence, nets);
257
+ const { graph, containerFrames } = graphEngine.generateLayoutGraph(sequence, nets);
258
+ sheetFrames = layoutEngine.runLayout(graph, containerFrames, nets);
255
259
  }
256
260
  catch (err) {
257
261
  throw new utils_js_1.RenderError(`Error during layout generation: ${err}`, 'layout');