circuitscript 0.1.29 → 0.1.32

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 (71) hide show
  1. package/dist/cjs/BaseVisitor.js +185 -22
  2. package/dist/cjs/RefdesAnnotationVisitor.js +27 -10
  3. package/dist/cjs/antlr/CircuitScriptLexer.js +241 -236
  4. package/dist/cjs/antlr/CircuitScriptParser.js +1197 -901
  5. package/dist/cjs/builtinMethods.js +6 -2
  6. package/dist/cjs/draw_symbols.js +38 -34
  7. package/dist/cjs/environment.js +28 -4
  8. package/dist/cjs/execute.js +195 -125
  9. package/dist/cjs/globals.js +6 -1
  10. package/dist/cjs/graph.js +14 -12
  11. package/dist/cjs/helpers.js +90 -17
  12. package/dist/cjs/layout.js +50 -25
  13. package/dist/cjs/main.js +16 -14
  14. package/dist/cjs/objects/ClassComponent.js +199 -30
  15. package/dist/cjs/objects/ExecutionScope.js +9 -0
  16. package/dist/cjs/objects/types.js +25 -2
  17. package/dist/cjs/parser.js +6 -2
  18. package/dist/cjs/regenerate-tests.js +3 -3
  19. package/dist/cjs/render.js +5 -3
  20. package/dist/cjs/rules-check/no-connect-on-connected-pin.js +9 -8
  21. package/dist/cjs/rules-check/rules.js +7 -2
  22. package/dist/cjs/rules-check/unconnected-pins.js +10 -8
  23. package/dist/cjs/utils.js +2 -1
  24. package/dist/cjs/validate/SymbolTable.js +7 -1
  25. package/dist/cjs/validate/SymbolValidatorVisitor.js +54 -17
  26. package/dist/cjs/visitor.js +299 -238
  27. package/dist/esm/BaseVisitor.js +187 -24
  28. package/dist/esm/RefdesAnnotationVisitor.js +27 -10
  29. package/dist/esm/antlr/CircuitScriptLexer.js +241 -236
  30. package/dist/esm/antlr/CircuitScriptParser.js +1196 -899
  31. package/dist/esm/antlr/CircuitScriptVisitor.js +4 -1
  32. package/dist/esm/builtinMethods.js +7 -3
  33. package/dist/esm/draw_symbols.js +38 -34
  34. package/dist/esm/environment.js +25 -1
  35. package/dist/esm/execute.js +197 -127
  36. package/dist/esm/globals.js +4 -0
  37. package/dist/esm/graph.js +14 -12
  38. package/dist/esm/helpers.js +91 -18
  39. package/dist/esm/layout.js +51 -26
  40. package/dist/esm/main.js +16 -14
  41. package/dist/esm/objects/ClassComponent.js +201 -30
  42. package/dist/esm/objects/ExecutionScope.js +9 -0
  43. package/dist/esm/objects/types.js +33 -1
  44. package/dist/esm/parser.js +6 -2
  45. package/dist/esm/regenerate-tests.js +3 -3
  46. package/dist/esm/render.js +5 -3
  47. package/dist/esm/rules-check/no-connect-on-connected-pin.js +9 -8
  48. package/dist/esm/rules-check/rules.js +7 -2
  49. package/dist/esm/rules-check/unconnected-pins.js +10 -8
  50. package/dist/esm/utils.js +2 -1
  51. package/dist/esm/validate/SymbolTable.js +5 -0
  52. package/dist/esm/validate/SymbolValidatorVisitor.js +53 -16
  53. package/dist/esm/visitor.js +201 -137
  54. package/dist/types/BaseVisitor.d.ts +27 -10
  55. package/dist/types/RefdesAnnotationVisitor.d.ts +2 -0
  56. package/dist/types/antlr/CircuitScriptLexer.d.ts +43 -42
  57. package/dist/types/antlr/CircuitScriptParser.d.ts +102 -58
  58. package/dist/types/antlr/CircuitScriptVisitor.d.ts +8 -2
  59. package/dist/types/environment.d.ts +8 -1
  60. package/dist/types/execute.d.ts +6 -3
  61. package/dist/types/globals.d.ts +4 -0
  62. package/dist/types/graph.d.ts +2 -2
  63. package/dist/types/helpers.d.ts +2 -1
  64. package/dist/types/layout.d.ts +5 -4
  65. package/dist/types/objects/ClassComponent.d.ts +34 -9
  66. package/dist/types/objects/ExecutionScope.d.ts +3 -1
  67. package/dist/types/objects/types.d.ts +40 -3
  68. package/dist/types/validate/SymbolTable.d.ts +1 -0
  69. package/dist/types/validate/SymbolValidatorVisitor.d.ts +6 -6
  70. package/dist/types/visitor.d.ts +10 -2
  71. package/package.json +4 -1
@@ -72,7 +72,10 @@ export class CircuitScriptVisitor extends AbstractParseTreeVisitor {
72
72
  visitWire_expr;
73
73
  visitArray_expr;
74
74
  visitPoint_expr;
75
- visitImport_expr;
75
+ visitImport_simple;
76
+ visitImport_all_simple;
77
+ visitImport_specific;
78
+ visitImport_annotation_expr;
76
79
  visitFrame_expr;
77
80
  visitIf_expr;
78
81
  visitIf_inner_expr;
@@ -1,7 +1,8 @@
1
1
  import Big from "big.js";
2
2
  import { numeric, NumericValue } from "./objects/ParamDefinition.js";
3
- import { CFunctionEntry } from "./objects/types.js";
3
+ import { CFunctionEntry, ImportedModule } from "./objects/types.js";
4
4
  import { unwrapValue, resolveToNumericValue, RuntimeExecutionError } from "./utils.js";
5
+ import { BaseNamespace } from "./globals.js";
5
6
  const builtInMethods = [
6
7
  ['enumerate', enumerate],
7
8
  ['toMils', toMils],
@@ -14,7 +15,7 @@ const builtInMethods = [
14
15
  ];
15
16
  export const buildInMethodNamesList = builtInMethods.map(item => item[0]);
16
17
  export function linkBuiltInMethods(context, visitor) {
17
- context.createFunction('print', (params) => {
18
+ context.createFunction(BaseNamespace, 'print', (params) => {
18
19
  const args = getPositionParams(params);
19
20
  const items = args.map(item => {
20
21
  return toString(unwrapValue(item));
@@ -28,7 +29,7 @@ export function linkBuiltInMethods(context, visitor) {
28
29
  });
29
30
  builtInMethods.forEach(([functionName, functionImpl]) => {
30
31
  if (functionImpl !== null) {
31
- context.createFunction(functionName, params => {
32
+ context.createFunction(BaseNamespace, functionName, params => {
32
33
  const args = getPositionParams(params);
33
34
  const functionReturn = functionImpl(...args);
34
35
  return [visitor, functionReturn];
@@ -148,6 +149,9 @@ function toString(obj) {
148
149
  else if (obj instanceof CFunctionEntry) {
149
150
  return obj.toString();
150
151
  }
152
+ else if (obj instanceof ImportedModule) {
153
+ return `[module: ${obj.moduleName}]`;
154
+ }
151
155
  else {
152
156
  if (obj === undefined) {
153
157
  return 'undefined';
@@ -133,7 +133,7 @@ export class SymbolGraphic {
133
133
  }
134
134
  drawLabels(group) {
135
135
  const labels = this.drawing.getLabels();
136
- labels.forEach(label => {
136
+ for (const label of labels) {
137
137
  const tmpLabel = label;
138
138
  const { fontSize = numeric(50), anchor = HorizontalAlign.Left, vanchor = VerticalAlign.Bottom, fontWeight = 'regular', angle: tmpLabelAngle = numeric(0), textColor = "#333", } = tmpLabel.style ?? {};
139
139
  const labelAngle = tmpLabelAngle.toNumber();
@@ -146,31 +146,35 @@ export class SymbolGraphic {
146
146
  useAnchor = this.flipTextAnchor(anchor);
147
147
  useDominantBaseline = this.flipDominantBaseline(vanchor);
148
148
  }
149
- switch (useAnchor) {
150
- case HorizontalAlign.Left:
151
- anchorStyle = (this.flipX === 0)
152
- ? HorizontalAlignProp.Start : HorizontalAlignProp.End;
153
- break;
154
- case HorizontalAlign.Middle:
155
- anchorStyle = HorizontalAlignProp.Middle;
156
- break;
157
- case HorizontalAlign.Right:
158
- anchorStyle = (this.flipX === 0)
159
- ? HorizontalAlignProp.End : HorizontalAlignProp.Start;
160
- break;
149
+ const isHorizontalLabel = labelAngle === 0 || labelAngle === 180;
150
+ const isVerticalLabel = labelAngle === 90 || labelAngle === -90;
151
+ if (useAnchor === HorizontalAlign.Middle) {
152
+ anchorStyle = HorizontalAlignProp.Middle;
161
153
  }
162
- switch (useDominantBaseline) {
163
- case VerticalAlign.Top:
164
- dominantBaseline = (this.flipY === 0)
165
- ? VerticalAlignProp.Hanging : VerticalAlignProp.TextTop;
166
- break;
167
- case VerticalAlign.Middle:
168
- dominantBaseline = VerticalAlignProp.Central;
169
- break;
170
- case VerticalAlign.Bottom:
171
- dominantBaseline = (this.flipY === 0)
172
- ? VerticalAlignProp.TextTop : VerticalAlignProp.Hanging;
173
- break;
154
+ else if (useAnchor === HorizontalAlign.Left) {
155
+ anchorStyle = HorizontalAlignProp.Start;
156
+ }
157
+ else if (useAnchor === HorizontalAlign.Right) {
158
+ anchorStyle = HorizontalAlignProp.End;
159
+ }
160
+ if (useDominantBaseline === VerticalAlign.Middle) {
161
+ dominantBaseline = VerticalAlignProp.Central;
162
+ }
163
+ else if (useDominantBaseline === VerticalAlign.Top) {
164
+ dominantBaseline = VerticalAlignProp.Hanging;
165
+ }
166
+ else if (useDominantBaseline === VerticalAlign.Bottom) {
167
+ dominantBaseline = VerticalAlignProp.TextTop;
168
+ }
169
+ if (anchorStyle !== HorizontalAlignProp.Middle &&
170
+ ((isHorizontalLabel && this.flipX === 1) || (isVerticalLabel && this.flipY === 1))) {
171
+ anchorStyle = (anchorStyle === HorizontalAlignProp.Start)
172
+ ? HorizontalAlignProp.End : HorizontalAlignProp.Start;
173
+ }
174
+ if (dominantBaseline !== VerticalAlignProp.Central &&
175
+ ((isHorizontalLabel && this.flipY === 1) || (isVerticalLabel && this.flipX === 1))) {
176
+ dominantBaseline = (dominantBaseline === VerticalAlignProp.Hanging)
177
+ ? VerticalAlignProp.TextTop : VerticalAlignProp.Hanging;
174
178
  }
175
179
  const position = tmpLabel.getLabelPosition();
176
180
  if (this.flipX !== 0) {
@@ -314,7 +318,7 @@ export class SymbolGraphic {
314
318
  .translate(-originSize / 2, -originSize / 2)
315
319
  .fill('green');
316
320
  }
317
- });
321
+ }
318
322
  }
319
323
  flipTextAnchor(value) {
320
324
  if (value === HorizontalAlign.Left) {
@@ -709,7 +713,7 @@ export class SymbolCustom extends SymbolGraphic {
709
713
  const pinStartY = bodyHeightMM.neg().half();
710
714
  const pinStartX = bodyWidthMM.neg().half();
711
715
  const tmpPinSpacing = this.pinSpacing.toNumber();
712
- leftPins.forEach(pin => {
716
+ for (const pin of leftPins) {
713
717
  const position = pin.position;
714
718
  const pinY = pinStartY.add((position + 1) * tmpPinSpacing);
715
719
  drawing.addPinMM(pin.pinId, leftPinStart.sub(this.pinLength), pinY, leftPinStart, pinY, defaultLineColor);
@@ -725,8 +729,8 @@ export class SymbolCustom extends SymbolGraphic {
725
729
  vanchor: VerticalAlign.Bottom,
726
730
  textColor: defaultLineColor
727
731
  });
728
- });
729
- rightPins.forEach(pin => {
732
+ }
733
+ for (const pin of rightPins) {
730
734
  const position = pin.position;
731
735
  const pinY = pinStartY.add((position + 1) * tmpPinSpacing);
732
736
  drawing.addPinMM(pin.pinId, rightPinStart.add(this.pinLength), pinY, rightPinStart, pinY, defaultLineColor);
@@ -742,8 +746,8 @@ export class SymbolCustom extends SymbolGraphic {
742
746
  vanchor: VerticalAlign.Bottom,
743
747
  textColor: defaultLineColor
744
748
  });
745
- });
746
- topPins.forEach(pin => {
749
+ }
750
+ for (const pin of topPins) {
747
751
  const position = pin.position;
748
752
  const pinX = pinStartX.add((position + 1) * tmpPinSpacing);
749
753
  drawing.addPinMM(pin.pinId, pinX, topPinStart.sub(this.pinLength), pinX, topPinStart, defaultLineColor);
@@ -761,8 +765,8 @@ export class SymbolCustom extends SymbolGraphic {
761
765
  textColor: defaultLineColor,
762
766
  angle: numeric(-90),
763
767
  });
764
- });
765
- bottomPins.forEach(pin => {
768
+ }
769
+ for (const pin of bottomPins) {
766
770
  const position = pin.position;
767
771
  const pinX = pinStartX.add((position + 1) * tmpPinSpacing);
768
772
  drawing.addPinMM(pin.pinId, pinX, bottomPinStart.add(this.pinLength), pinX, bottomPinStart, defaultLineColor);
@@ -780,7 +784,7 @@ export class SymbolCustom extends SymbolGraphic {
780
784
  textColor: defaultLineColor,
781
785
  angle: numeric(-90)
782
786
  });
783
- });
787
+ }
784
788
  const instanceName = drawing.variables.get('refdes');
785
789
  instanceName && drawing.addLabel(bodyWidthMM.neg().half(), bodyHeightMM.neg().half().sub(milsToMM(20)), instanceName, {
786
790
  fontSize: numeric(CustomSymbolRefDesSize),
@@ -1,6 +1,8 @@
1
1
  import { registerWindow, SVG } from "@svgdotjs/svg.js";
2
+ import { writeFileSync } from "fs";
2
3
  import fs from 'fs';
3
4
  import path from "path";
5
+ import CryptoJs from "crypto-js";
4
6
  import { TOOL_VERSION } from "./globals.js";
5
7
  import { RuntimeExecutionError } from "./utils.js";
6
8
  export class NodeScriptEnvironment {
@@ -111,8 +113,12 @@ export class NodeScriptEnvironment {
111
113
  return this.prepareSVGEnvironmentInternal(this.getFontsPath());
112
114
  }
113
115
  async readFile(path, options) {
116
+ options = options ?? { encoding: 'utf8' };
114
117
  return fs.promises.readFile(path, options);
115
118
  }
119
+ writeFileSync(path, data) {
120
+ return writeFileSync(path, data);
121
+ }
116
122
  getAbsolutePath(filePath) {
117
123
  return path.resolve(filePath);
118
124
  }
@@ -131,11 +137,29 @@ export class NodeScriptEnvironment {
131
137
  }
132
138
  async exists(path) {
133
139
  try {
134
- fs.promises.access(path, fs.constants.F_OK);
140
+ await fs.promises.access(path, fs.constants.F_OK);
135
141
  return true;
136
142
  }
137
143
  catch (err) {
138
144
  return false;
139
145
  }
140
146
  }
147
+ hashStringSHA256(value) {
148
+ return CryptoJs.SHA256(value).toString();
149
+ }
150
+ dirname(filePath) {
151
+ return path.dirname(filePath);
152
+ }
153
+ extname(filePath) {
154
+ return path.extname(filePath);
155
+ }
156
+ basename(filePath, ext) {
157
+ return path.basename(filePath, ext);
158
+ }
159
+ join(...paths) {
160
+ return path.join(...paths);
161
+ }
162
+ relative(from, to) {
163
+ return path.relative(from, to);
164
+ }
141
165
  }