circuitscript 0.0.28 → 0.0.31

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 (60) hide show
  1. package/dist/cjs/BaseVisitor.js +6 -1
  2. package/dist/cjs/antlr/CircuitScriptLexer.js +204 -200
  3. package/dist/cjs/antlr/CircuitScriptParser.js +1066 -1173
  4. package/dist/cjs/draw_symbols.js +383 -103
  5. package/dist/cjs/execute.js +39 -14
  6. package/dist/cjs/geometry.js +79 -18
  7. package/dist/cjs/globals.js +41 -7
  8. package/dist/cjs/helpers.js +40 -2
  9. package/dist/cjs/layout.js +72 -39
  10. package/dist/cjs/main.js +10 -4
  11. package/dist/cjs/objects/ClassComponent.js +2 -0
  12. package/dist/cjs/objects/ExecutionScope.js +1 -1
  13. package/dist/cjs/objects/Net.js +3 -2
  14. package/dist/cjs/objects/PinTypes.js +7 -1
  15. package/dist/cjs/objects/types.js +11 -1
  16. package/dist/cjs/regenerate-tests.js +84 -14
  17. package/dist/cjs/render.js +22 -15
  18. package/dist/cjs/sizing.js +4 -6
  19. package/dist/cjs/utils.js +29 -5
  20. package/dist/cjs/visitor.js +176 -10
  21. package/dist/esm/BaseVisitor.mjs +6 -1
  22. package/dist/esm/antlr/CircuitScriptLexer.mjs +204 -200
  23. package/dist/esm/antlr/CircuitScriptParser.mjs +1061 -1171
  24. package/dist/esm/antlr/CircuitScriptVisitor.mjs +3 -0
  25. package/dist/esm/draw_symbols.mjs +378 -102
  26. package/dist/esm/execute.mjs +40 -15
  27. package/dist/esm/geometry.mjs +79 -17
  28. package/dist/esm/globals.mjs +40 -6
  29. package/dist/esm/helpers.mjs +38 -1
  30. package/dist/esm/layout.mjs +75 -42
  31. package/dist/esm/main.mjs +11 -5
  32. package/dist/esm/objects/ClassComponent.mjs +6 -0
  33. package/dist/esm/objects/ExecutionScope.mjs +1 -1
  34. package/dist/esm/objects/Net.mjs +3 -2
  35. package/dist/esm/objects/PinTypes.mjs +6 -0
  36. package/dist/esm/objects/types.mjs +14 -0
  37. package/dist/esm/regenerate-tests.mjs +85 -15
  38. package/dist/esm/render.mjs +23 -16
  39. package/dist/esm/sizing.mjs +3 -4
  40. package/dist/esm/utils.mjs +26 -4
  41. package/dist/esm/visitor.mjs +179 -13
  42. package/dist/types/antlr/CircuitScriptLexer.d.ts +42 -41
  43. package/dist/types/antlr/CircuitScriptParser.d.ts +144 -133
  44. package/dist/types/antlr/CircuitScriptVisitor.d.ts +6 -0
  45. package/dist/types/draw_symbols.d.ts +24 -6
  46. package/dist/types/execute.d.ts +5 -4
  47. package/dist/types/geometry.d.ts +5 -3
  48. package/dist/types/globals.d.ts +38 -6
  49. package/dist/types/helpers.d.ts +12 -0
  50. package/dist/types/layout.d.ts +2 -1
  51. package/dist/types/objects/ClassComponent.d.ts +8 -0
  52. package/dist/types/objects/PinTypes.d.ts +1 -0
  53. package/dist/types/objects/Wire.d.ts +4 -2
  54. package/dist/types/objects/types.d.ts +8 -0
  55. package/dist/types/sizing.d.ts +0 -4
  56. package/dist/types/utils.d.ts +3 -0
  57. package/dist/types/visitor.d.ts +8 -1
  58. package/fonts/Arial.ttf +0 -0
  59. package/libs/lib.cst +78 -55
  60. package/package.json +1 -1
package/dist/cjs/main.js CHANGED
@@ -52,16 +52,22 @@ async function main() {
52
52
  let scriptData;
53
53
  if (args.length > 0 && args[0]) {
54
54
  inputFilePath = args[0];
55
- scriptData = (0, fs_1.readFileSync)(inputFilePath, { encoding: 'utf-8' });
56
- if (currentDirectory === null) {
57
- currentDirectory = path_1.default.dirname(inputFilePath);
55
+ if ((0, fs_1.existsSync)(inputFilePath)) {
56
+ scriptData = (0, fs_1.readFileSync)(inputFilePath, { encoding: 'utf-8' });
57
+ if (currentDirectory === null) {
58
+ currentDirectory = path_1.default.dirname(inputFilePath);
59
+ }
60
+ }
61
+ else {
62
+ console.error("Error: File could not be found");
63
+ return;
58
64
  }
59
65
  }
60
66
  else if (options.input) {
61
67
  scriptData = options.input;
62
68
  }
63
69
  else {
64
- console.log("Error: No input provided");
70
+ console.error("Error: No input provided");
65
71
  return;
66
72
  }
67
73
  const scriptOptions = {
@@ -21,8 +21,10 @@ class ClassComponent {
21
21
  this.followWireOrientationProp = true;
22
22
  this.wireOrientationAngle = 0;
23
23
  this.useWireOrientationAngle = true;
24
+ this.didSetWireOrientationAngle = false;
24
25
  this.styles = {};
25
26
  this.assignedRefDes = null;
27
+ this.moduleCounter = 0;
26
28
  this.instanceName = instanceName;
27
29
  this.numPins = numPins;
28
30
  this.className = className;
@@ -90,7 +90,7 @@ class ExecutionScope {
90
90
  }
91
91
  });
92
92
  return sortedNet.map(([component, pin, net]) => {
93
- return [net.namespace + net.name, component.instanceName, pin];
93
+ return [net.toString(), component.instanceName, pin];
94
94
  });
95
95
  }
96
96
  printNets() {
@@ -16,10 +16,11 @@ class Net {
16
16
  this.baseName = name;
17
17
  }
18
18
  toString() {
19
- return this.name;
19
+ return this.namespace + this.name;
20
20
  }
21
21
  static isSame(netA, netB) {
22
- return netA.name === netB.name &&
22
+ return netA.namespace === netB.namespace &&
23
+ netA.name === netB.name &&
23
24
  netA.baseName === netB.baseName &&
24
25
  netA.priority === netB.priority &&
25
26
  netA.type === netB.type;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PinTypes = void 0;
3
+ exports.AllPinTypes = exports.PinTypes = void 0;
4
4
  var PinTypes;
5
5
  (function (PinTypes) {
6
6
  PinTypes["Any"] = "any";
@@ -9,3 +9,9 @@ var PinTypes;
9
9
  PinTypes["IO"] = "io";
10
10
  PinTypes["Power"] = "power";
11
11
  })(PinTypes || (exports.PinTypes = PinTypes = {}));
12
+ exports.AllPinTypes = [
13
+ PinTypes.Any,
14
+ PinTypes.Input,
15
+ PinTypes.Output,
16
+ PinTypes.IO,
17
+ ];
@@ -1,12 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Direction = exports.ParseSymbolType = exports.UndeclaredReference = void 0;
3
+ exports.Direction = exports.ParseSymbolType = exports.DeclaredReference = exports.UndeclaredReference = void 0;
4
4
  class UndeclaredReference {
5
5
  constructor(reference) {
6
6
  this.reference = reference;
7
7
  }
8
8
  }
9
9
  exports.UndeclaredReference = UndeclaredReference;
10
+ class DeclaredReference {
11
+ constructor(refType) {
12
+ this.found = refType.found;
13
+ this.name = refType.name;
14
+ this.trailers = refType.trailers;
15
+ this.type = refType.type;
16
+ this.value = refType.value;
17
+ }
18
+ }
19
+ exports.DeclaredReference = DeclaredReference;
10
20
  var ParseSymbolType;
11
21
  (function (ParseSymbolType) {
12
22
  ParseSymbolType["Variable"] = "variable";
@@ -5,19 +5,89 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const helpers_js_1 = require("./helpers.js");
8
+ const sizing_js_1 = require("./sizing.js");
8
9
  const mainDir = './__tests__/renderData/';
9
- const cstFiles = [];
10
- const files = fs_1.default.readdirSync(mainDir);
11
- files.forEach(file => {
12
- if (file.endsWith('.cst')) {
13
- cstFiles.push(file);
10
+ const fontsPath = (0, helpers_js_1.getFontsPath)();
11
+ const defaultLibsPath = (0, helpers_js_1.getDefaultLibsPath)();
12
+ async function regenerateTests(extra = "") {
13
+ await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
14
+ const cstFiles = [];
15
+ const files = fs_1.default.readdirSync(mainDir);
16
+ files.forEach(file => {
17
+ if (file.endsWith('.cst')) {
18
+ cstFiles.push(file);
19
+ }
20
+ });
21
+ cstFiles.forEach(file => {
22
+ const inputPath = mainDir + file;
23
+ const scriptData = fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' });
24
+ const outputPath = mainDir + 'svgs/' + file + extra + '.svg';
25
+ (0, helpers_js_1.renderScript)(scriptData, outputPath, {
26
+ currentDirectory: mainDir,
27
+ defaultLibsPath,
28
+ });
29
+ console.log('generated ', outputPath);
30
+ });
31
+ return cstFiles;
32
+ }
33
+ (async () => {
34
+ const generateDiff = false;
35
+ const nextExtra = generateDiff ? '.next' : '';
36
+ const cstFiles = await regenerateTests(nextExtra);
37
+ const allFiles = [];
38
+ if (generateDiff) {
39
+ cstFiles.forEach(file => {
40
+ const svg1 = 'svgs/' + file + '.svg';
41
+ const svg2 = 'svgs/' + file + '.next.svg';
42
+ const cleanedName = file.replace('script', '').replace('.cst', '');
43
+ allFiles.push([file, svg1, svg2, cleanedName]);
44
+ });
45
+ const sortedFiles = allFiles.sort((a, b) => {
46
+ const nameA = a[3];
47
+ const nameB = b[3];
48
+ const indexA = Number(nameA);
49
+ const indexB = Number(nameB);
50
+ if (indexA > indexB) {
51
+ return 1;
52
+ }
53
+ else if (indexA < indexB) {
54
+ return -1;
55
+ }
56
+ else {
57
+ return 0;
58
+ }
59
+ });
60
+ const output = [];
61
+ sortedFiles.forEach(group => {
62
+ const [file, svg1, svg2] = group;
63
+ output.push(`<div class='group'>
64
+ <h3>${file}</h3>
65
+ <div class='items'>
66
+ <div style='margin-right: 10px'>
67
+ <h4>${svg1}</h4>
68
+ <img src='${svg1}'/>
69
+ </div>
70
+ <div>
71
+ <h4>${svg2}</h4>
72
+ <img src='${svg2}'/>
73
+ </div>
74
+ </div>
75
+ </div>`);
76
+ });
77
+ const result = `
78
+ <html>
79
+ <header>
80
+ <title>SVG compare</title>
81
+ <style>
82
+ img { border: solid thin #ccc; }
83
+ .items { display: flex; }
84
+ body { font-family: Arial; }
85
+ </style>
86
+ </header>
87
+ <body>
88
+ ${output.join('')}
89
+ </body>
90
+ </html>`;
91
+ fs_1.default.writeFileSync(mainDir + "compiled.html", result);
14
92
  }
15
- });
16
- const useCurrentDir = './examples/';
17
- cstFiles.forEach(file => {
18
- const inputPath = mainDir + file;
19
- const scriptData = fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' });
20
- const outputPath = inputPath + '.svg';
21
- (0, helpers_js_1.renderScript)(scriptData, outputPath, { currentDirectory: useCurrentDir });
22
- console.log('generated ', outputPath);
23
- });
93
+ })();
@@ -7,6 +7,7 @@ const sizing_js_1 = require("./sizing.js");
7
7
  const globals_js_1 = require("./globals.js");
8
8
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
9
9
  const utils_js_1 = require("./utils.js");
10
+ const helpers_js_1 = require("./helpers.js");
10
11
  function generateSVG2(graph) {
11
12
  const window = (0, sizing_js_1.getCreateSVGWindow)()();
12
13
  const document = window.document;
@@ -15,10 +16,10 @@ function generateSVG2(graph) {
15
16
  (0, sizing_js_1.applyFontsToSVG)(canvas);
16
17
  generateSVGChild(canvas, graph.components, graph.wires, graph.junctions, graph.mergedWires, graph.frameObjects, graph.textObjects);
17
18
  const { x, y, width, height } = canvas.bbox();
18
- const margin = 5;
19
+ const margin = (0, helpers_js_1.milsToMM)(10);
19
20
  const widthAndMargin = width + margin * 2;
20
21
  const heightAndMargin = height + margin * 2;
21
- const scale = 1;
22
+ const scale = globals_js_1.MMToPx * globals_js_1.defaultZoomScale;
22
23
  canvas.size(widthAndMargin * scale, heightAndMargin * scale);
23
24
  canvas.viewbox(x - margin, y - margin, widthAndMargin, heightAndMargin);
24
25
  return canvas.svg();
@@ -61,7 +62,7 @@ function generateSVGChild(canvas, components, wires, junctions, mergedWires, fra
61
62
  }
62
63
  else {
63
64
  symbolGroup.rect(width, height)
64
- .fill(globals_js_1.bodyColor)
65
+ .fill(globals_js_1.ColorScheme.BodyColor)
65
66
  .stroke({ width: 1, color: '#333' });
66
67
  }
67
68
  });
@@ -83,14 +84,18 @@ function generateSVGChild(canvas, components, wires, junctions, mergedWires, fra
83
84
  const pt1 = segment[0];
84
85
  const pt2 = segment[1];
85
86
  mergedWireGroup.line([pt1, pt2])
86
- .stroke({ width: 1, color: globals_js_1.wireColor, linecap: 'square' })
87
+ .stroke({
88
+ width: globals_js_1.defaultWireLineWidth,
89
+ color: globals_js_1.ColorScheme.WireColor,
90
+ linecap: 'square'
91
+ })
87
92
  .fill('none');
88
93
  });
89
94
  intersectPoints.forEach(point => {
90
95
  const [x, y, count] = point;
91
96
  mergedWireGroup.circle(globals_js_1.junctionSize)
92
97
  .translate(x - globals_js_1.junctionSize / 2, y - globals_js_1.junctionSize / 2)
93
- .fill(globals_js_1.junctionColor)
98
+ .fill(globals_js_1.ColorScheme.JunctionColor)
94
99
  .stroke('none');
95
100
  });
96
101
  });
@@ -112,7 +117,7 @@ function generateSVGChild(canvas, components, wires, junctions, mergedWires, fra
112
117
  }
113
118
  const tmpRect = frameGroup.rect(width, height)
114
119
  .fill('none')
115
- .stroke({ width: borderWidth, color: strokeColor });
120
+ .stroke({ width: (0, helpers_js_1.milsToMM)(borderWidth), color: strokeColor });
116
121
  tmpRect.translate(item.x, item.y);
117
122
  }
118
123
  });
@@ -123,13 +128,14 @@ function generateSVGChild(canvas, components, wires, junctions, mergedWires, fra
123
128
  symbol.draw(innerGroup);
124
129
  });
125
130
  const drawOrigin = false;
131
+ const originSize = (0, helpers_js_1.milsToMM)(10);
126
132
  drawOrigin && canvas.group().translate(0, 0)
127
- .circle(5)
128
- .translate(-5 / 2, -5 / 2)
133
+ .circle(originSize)
134
+ .translate(-originSize / 2, -originSize / 2)
129
135
  .stroke('none').fill('red');
130
136
  }
131
137
  function drawGrid(group, canvasSize) {
132
- const gridSize = 20;
138
+ const gridSize = globals_js_1.defaultGridSizeUnits;
133
139
  const { x, y, x2, y2 } = canvasSize;
134
140
  const gridStartX = (Math.floor(x / gridSize) - 1) * gridSize;
135
141
  const gridStartY = (Math.floor(y / gridSize) - 1) * gridSize;
@@ -137,19 +143,20 @@ function drawGrid(group, canvasSize) {
137
143
  const gridEndY = (Math.ceil(y2 / gridSize) + 1) * gridSize;
138
144
  const numCols = Math.ceil((gridEndX - gridStartX) / gridSize);
139
145
  const lines = [];
146
+ const smallOffset = (0, helpers_js_1.milsToMM)(3);
147
+ const startY = gridStartY - smallOffset / 2;
148
+ const endY = gridEndY + smallOffset;
140
149
  for (let i = 0; i <= numCols; i++) {
141
150
  const startX = gridStartX + i * gridSize;
142
- const startY = gridStartY - 0.5;
143
- const endY = gridEndY;
144
151
  lines.push(`M ${startX} ${startY} L ${startX} ${endY}`);
145
152
  }
153
+ const strokeSize = (0, helpers_js_1.milsToMM)(3);
146
154
  group.path(lines.join(" "))
147
- .fill('none')
148
155
  .attr({
149
- 'stroke-dasharray': '1,' + (gridSize - 1),
156
+ 'stroke-dasharray': `${strokeSize},${gridSize - strokeSize}`,
150
157
  })
151
158
  .stroke({
152
- width: 1,
153
- color: '#aaa'
159
+ width: strokeSize,
160
+ color: '#000'
154
161
  });
155
162
  }
@@ -23,13 +23,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.measureTextSize2 = exports.measureTextSize = exports.applyFontsToSVG = exports.getCreateSVGWindow = exports.prepareSVGEnvironment = void 0;
26
+ exports.measureTextSize2 = exports.applyFontsToSVG = exports.getCreateSVGWindow = exports.prepareSVGEnvironment = void 0;
27
27
  const svg_js_1 = require("@svgdotjs/svg.js");
28
28
  const geometry_js_1 = require("./geometry.js");
29
29
  const globals_js_1 = require("./globals.js");
30
30
  const helpers_js_1 = require("./helpers.js");
31
31
  let MainCanvas = null;
32
- const supportedFonts = {};
32
+ const supportedFonts = {
33
+ 'Arial': 'Arial.ttf',
34
+ };
33
35
  let globalCreateSVGWindow;
34
36
  async function prepareSVGEnvironment(fontsPath) {
35
37
  const moduleType = (0, helpers_js_1.detectJSModuleType)();
@@ -54,10 +56,6 @@ exports.getCreateSVGWindow = getCreateSVGWindow;
54
56
  function applyFontsToSVG(canvas) {
55
57
  }
56
58
  exports.applyFontsToSVG = applyFontsToSVG;
57
- async function measureTextSize(text, fontFamily, fontSize) {
58
- return measureTextSize2(text, fontFamily, fontSize);
59
- }
60
- exports.measureTextSize = measureTextSize;
61
59
  const measureTextSizeCache = {};
62
60
  const measureTextSizeCacheHits = {};
63
61
  function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', anchor = geometry_js_1.HorizontalAlign.Left, vanchor = geometry_js_1.VerticalAlign.Bottom) {
package/dist/cjs/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
3
+ exports.roundValue = exports.getPortType = exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
4
4
  class SimpleStopwatch {
5
5
  constructor() {
6
6
  this.startTime = new Date();
@@ -28,11 +28,17 @@ function printBounds(bounds) {
28
28
  }
29
29
  }
30
30
  exports.printBounds = printBounds;
31
+ function hasRemainder(value, value2) {
32
+ const tmpValue = Math.abs(value) / value2;
33
+ const flooredValue = Math.floor(tmpValue);
34
+ const diff = tmpValue - flooredValue;
35
+ return diff;
36
+ }
31
37
  function resizeToNearestGrid(bounds, gridSize = 20) {
32
- const addXMin = (bounds.xmin % gridSize) === 0 ? -1 : 0;
33
- const addYMin = (bounds.ymin % gridSize) === 0 ? -1 : 0;
34
- const addXMax = (bounds.xmax % gridSize) === 0 ? 1 : 0;
35
- const addYMax = (bounds.ymax % gridSize) === 0 ? 1 : 0;
38
+ const addXMin = hasRemainder(bounds.xmin, gridSize) === 0 ? -1 : 0;
39
+ const addYMin = hasRemainder(bounds.ymin, gridSize) === 0 ? -1 : 0;
40
+ const addXMax = hasRemainder(bounds.xmax, gridSize) === 0 ? 1 : 0;
41
+ const addYMax = hasRemainder(bounds.ymax, gridSize) === 0 ? 1 : 0;
36
42
  return {
37
43
  xmin: Math.floor((bounds.xmin + addXMin) / gridSize) * gridSize,
38
44
  ymin: Math.floor((bounds.ymin + addYMin) / gridSize) * gridSize,
@@ -52,3 +58,21 @@ function getBoundsSize(bounds) {
52
58
  };
53
59
  }
54
60
  exports.getBoundsSize = getBoundsSize;
61
+ function getPortType(component) {
62
+ const drawingCommands = component.displayProp;
63
+ let foundPinType = null;
64
+ const commands = drawingCommands.getCommands();
65
+ commands.some(item => {
66
+ if (item[0] === 'label' && item[2].has('portType')) {
67
+ foundPinType = item[2].get('portType');
68
+ return true;
69
+ }
70
+ return false;
71
+ });
72
+ return foundPinType;
73
+ }
74
+ exports.getPortType = getPortType;
75
+ function roundValue(value) {
76
+ return +value.toFixed(7);
77
+ }
78
+ exports.roundValue = roundValue;