circuitscript 0.1.5 → 0.1.8

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 (97) hide show
  1. package/dist/cjs/BaseVisitor.js +127 -73
  2. package/dist/cjs/SemanticTokenVisitor.js +19 -13
  3. package/dist/cjs/antlr/CircuitScriptParser.js +711 -671
  4. package/dist/cjs/builtinMethods.js +29 -25
  5. package/dist/cjs/environment.js +118 -0
  6. package/dist/cjs/execute.js +53 -12
  7. package/dist/cjs/export.js +0 -5
  8. package/dist/cjs/geometry.js +1 -0
  9. package/dist/cjs/globals.js +11 -6
  10. package/dist/cjs/helpers.js +152 -127
  11. package/dist/cjs/index.js +5 -0
  12. package/dist/cjs/layout.js +86 -44
  13. package/dist/cjs/main.js +31 -19
  14. package/dist/cjs/objects/ExecutionScope.js +33 -0
  15. package/dist/cjs/objects/ParamDefinition.js +15 -15
  16. package/dist/cjs/parser.js +27 -21
  17. package/dist/cjs/regenerate-tests.js +14 -10
  18. package/dist/cjs/render.js +3 -1
  19. package/dist/cjs/sizing.js +5 -58
  20. package/dist/cjs/utils.js +85 -30
  21. package/dist/cjs/validate/SymbolTable.js +96 -0
  22. package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +14 -0
  23. package/dist/cjs/validate/SymbolValidatorVisitor.js +170 -0
  24. package/dist/cjs/validate.js +71 -44
  25. package/dist/cjs/visitor.js +140 -24
  26. package/dist/esm/{BaseVisitor.mjs → BaseVisitor.js} +98 -45
  27. package/dist/esm/{SemanticTokenVisitor.mjs → SemanticTokenVisitor.js} +17 -11
  28. package/dist/esm/antlr/{CircuitScriptParser.mjs → CircuitScriptParser.js} +711 -671
  29. package/dist/esm/{builtinMethods.mjs → builtinMethods.js} +20 -16
  30. package/dist/esm/{draw_symbols.mjs → draw_symbols.js} +7 -7
  31. package/dist/esm/environment.js +110 -0
  32. package/dist/esm/{execute.mjs → execute.js} +66 -25
  33. package/dist/esm/{export.mjs → export.js} +2 -7
  34. package/dist/esm/{geometry.mjs → geometry.js} +6 -5
  35. package/dist/esm/{globals.mjs → globals.js} +6 -1
  36. package/dist/esm/helpers.js +394 -0
  37. package/dist/esm/index.js +20 -0
  38. package/dist/esm/{layout.mjs → layout.js} +72 -53
  39. package/dist/esm/{lexer.mjs → lexer.js} +2 -2
  40. package/dist/esm/{main.mjs → main.js} +33 -21
  41. package/dist/esm/objects/{ClassComponent.mjs → ClassComponent.js} +5 -4
  42. package/dist/esm/objects/{ExecutionScope.mjs → ExecutionScope.js} +33 -0
  43. package/dist/esm/objects/{Frame.mjs → Frame.js} +1 -1
  44. package/dist/esm/objects/{ParamDefinition.mjs → ParamDefinition.js} +1 -1
  45. package/dist/esm/objects/{PinDefinition.mjs → PinDefinition.js} +1 -1
  46. package/dist/esm/parser.js +71 -0
  47. package/dist/esm/{regenerate-tests.mjs → regenerate-tests.js} +15 -11
  48. package/dist/esm/{render.mjs → render.js} +11 -9
  49. package/dist/esm/{sizing.mjs → sizing.js} +6 -34
  50. package/dist/esm/{utils.mjs → utils.js} +61 -17
  51. package/dist/esm/validate/SymbolTable.js +90 -0
  52. package/dist/esm/validate/SymbolValidatorResolveVisitor.js +10 -0
  53. package/dist/esm/validate/SymbolValidatorVisitor.js +163 -0
  54. package/dist/esm/validate.js +105 -0
  55. package/dist/esm/{visitor.mjs → visitor.js} +151 -35
  56. package/dist/fonts/Arial.ttf +0 -0
  57. package/dist/fonts/Inter-Bold.ttf +0 -0
  58. package/dist/fonts/Inter-Regular.ttf +0 -0
  59. package/dist/fonts/OpenSans-Regular.ttf +0 -0
  60. package/dist/fonts/Roboto-Regular.ttf +0 -0
  61. package/dist/libs/lib.cst +423 -0
  62. package/dist/types/BaseVisitor.d.ts +34 -21
  63. package/dist/types/SemanticTokenVisitor.d.ts +6 -5
  64. package/dist/types/antlr/CircuitScriptParser.d.ts +4 -2
  65. package/dist/types/builtinMethods.d.ts +3 -2
  66. package/dist/types/environment.d.ts +31 -0
  67. package/dist/types/globals.d.ts +4 -1
  68. package/dist/types/helpers.d.ts +12 -14
  69. package/dist/types/index.d.ts +5 -0
  70. package/dist/types/layout.d.ts +2 -2
  71. package/dist/types/objects/ClassComponent.d.ts +1 -0
  72. package/dist/types/objects/ExecutionScope.d.ts +11 -0
  73. package/dist/types/objects/types.d.ts +6 -1
  74. package/dist/types/parser.d.ts +7 -11
  75. package/dist/types/sizing.d.ts +0 -3
  76. package/dist/types/utils.d.ts +30 -6
  77. package/dist/types/validate/SymbolTable.d.ts +40 -0
  78. package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +7 -0
  79. package/dist/types/validate/SymbolValidatorVisitor.d.ts +32 -0
  80. package/dist/types/validate.d.ts +1 -1
  81. package/package.json +15 -14
  82. package/dist/cjs/SymbolValidatorVisitor.js +0 -233
  83. package/dist/esm/SymbolValidatorVisitor.mjs +0 -222
  84. package/dist/esm/helpers.mjs +0 -364
  85. package/dist/esm/index.mjs +0 -15
  86. package/dist/esm/parser.mjs +0 -64
  87. package/dist/esm/validate.mjs +0 -74
  88. package/dist/types/SymbolValidatorVisitor.d.ts +0 -61
  89. /package/dist/esm/antlr/{CircuitScriptLexer.mjs → CircuitScriptLexer.js} +0 -0
  90. /package/dist/esm/antlr/{CircuitScriptVisitor.mjs → CircuitScriptVisitor.js} +0 -0
  91. /package/dist/esm/{fonts.mjs → fonts.js} +0 -0
  92. /package/dist/esm/{logger.mjs → logger.js} +0 -0
  93. /package/dist/esm/objects/{Net.mjs → Net.js} +0 -0
  94. /package/dist/esm/objects/{PinTypes.mjs → PinTypes.js} +0 -0
  95. /package/dist/esm/objects/{Wire.mjs → Wire.js} +0 -0
  96. /package/dist/esm/objects/{types.mjs → types.js} +0 -0
  97. /package/dist/esm/{server.mjs → server.js} +0 -0
package/dist/cjs/utils.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBlockTypeString = exports.generateDebugSequenceAction = exports.sequenceActionString = exports.areasOverlap = exports.isPointWithinArea = exports.resolveToNumericValue = exports.getNumberExponentialText = exports.getNumberExponential = exports.combineMaps = exports.throwWithContext = exports.roundValue = exports.getPortType = exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
3
+ exports.RenderError = exports.RuntimeExecutionError = exports.ParseError = exports.ParseSyntaxError = exports.BaseError = exports.getBlockTypeString = exports.generateDebugSequenceAction = exports.sequenceActionString = exports.areasOverlap = exports.isPointWithinArea = exports.resolveToNumericValue = exports.getNumberExponentialText = exports.getNumberExponential = exports.combineMaps = exports.throwWithTokenRange = exports.throwWithToken = exports.throwWithContext = exports.roundValue = exports.getPortType = exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
4
4
  const big_js_1 = require("big.js");
5
- const ClassComponent_1 = require("./objects/ClassComponent");
6
- const ParamDefinition_1 = require("./objects/ParamDefinition");
7
- const ExecutionScope_1 = require("./objects/ExecutionScope");
8
- const globals_1 = require("./globals");
5
+ const ClassComponent_js_1 = require("./objects/ClassComponent.js");
6
+ const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
7
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
8
+ const globals_js_1 = require("./globals.js");
9
9
  class SimpleStopwatch {
10
10
  constructor() {
11
11
  this.startTime = new Date();
@@ -81,22 +81,21 @@ function roundValue(value) {
81
81
  return resolveToNumericValue(new big_js_1.Big(value.toBigNumber().toFixed(7)));
82
82
  }
83
83
  exports.roundValue = roundValue;
84
- function throwWithContext(context, message) {
85
- const startLine = context.start?.line;
86
- const startColumn = context.start?.column;
87
- const startString = startLine + ":" + startColumn;
88
- const stopLine = context.stop?.line;
89
- const stopColumn = context.stop?.column;
90
- let stopString = "";
91
- if (startLine === stopLine) {
92
- stopString = stopColumn?.toString();
84
+ function throwWithContext(context, messageOrError) {
85
+ if (messageOrError instanceof BaseError) {
86
+ throw messageOrError;
93
87
  }
94
- else {
95
- stopString = stopLine + ":" + stopString;
96
- }
97
- throw `Parse exception at [${startString} - ${stopString}] : ${message}`;
88
+ throwWithTokenRange(messageOrError, context.start, context.stop);
98
89
  }
99
90
  exports.throwWithContext = throwWithContext;
91
+ function throwWithToken(message, token) {
92
+ throw new ParseError(message, token);
93
+ }
94
+ exports.throwWithToken = throwWithToken;
95
+ function throwWithTokenRange(message, startToken, endToken) {
96
+ throw new ParseError(message, startToken, endToken);
97
+ }
98
+ exports.throwWithTokenRange = throwWithTokenRange;
100
99
  function combineMaps(map1, map2) {
101
100
  const newMap = new Map(map1);
102
101
  map2.forEach((value, key) => {
@@ -156,7 +155,7 @@ function getNumberExponentialText(value) {
156
155
  exports.getNumberExponentialText = getNumberExponentialText;
157
156
  function resolveToNumericValue(value) {
158
157
  if (value.toNumber() === 0) {
159
- return new ParamDefinition_1.NumericValue(0);
158
+ return new ParamDefinition_js_1.NumericValue(0);
160
159
  }
161
160
  const isNeg = value.lt(0);
162
161
  const positiveValue = isNeg ? value.neg() : value;
@@ -166,7 +165,7 @@ function resolveToNumericValue(value) {
166
165
  const tmpValue1 = positiveValue.div(Math.pow(10, prefixPart * 3));
167
166
  useValue = isNeg ? tmpValue1.neg() : tmpValue1;
168
167
  }
169
- return new ParamDefinition_1.NumericValue(useValue, prefixPart * 3);
168
+ return new ParamDefinition_js_1.NumericValue(useValue, prefixPart * 3);
170
169
  }
171
170
  exports.resolveToNumericValue = resolveToNumericValue;
172
171
  function isPointWithinArea(point, bounds) {
@@ -190,7 +189,7 @@ exports.areasOverlap = areasOverlap;
190
189
  function sequenceActionString(sequenceAction) {
191
190
  const tmp = [...sequenceAction];
192
191
  const action = tmp[0];
193
- if (action === ExecutionScope_1.SequenceAction.Wire) {
192
+ if (action === ExecutionScope_js_1.SequenceAction.Wire) {
194
193
  tmp[2] = tmp[2].map(item2 => {
195
194
  const lengthValue = item2.value;
196
195
  const useValue = [item2.direction];
@@ -201,12 +200,12 @@ function sequenceActionString(sequenceAction) {
201
200
  return useValue.join(",");
202
201
  }).join(" ");
203
202
  }
204
- else if (action === ExecutionScope_1.SequenceAction.Frame) {
203
+ else if (action === ExecutionScope_js_1.SequenceAction.Frame) {
205
204
  tmp[1] = sequenceAction[1].frameId;
206
205
  }
207
- else if (action !== ExecutionScope_1.SequenceAction.WireJump) {
206
+ else if (action !== ExecutionScope_js_1.SequenceAction.WireJump) {
208
207
  const [, component] = sequenceAction;
209
- if (component instanceof ClassComponent_1.ClassComponent) {
208
+ if (component instanceof ClassComponent_js_1.ClassComponent) {
210
209
  tmp[1] = sequenceAction[1].instanceName;
211
210
  }
212
211
  }
@@ -218,12 +217,12 @@ function generateDebugSequenceAction(sequence) {
218
217
  return sequence.map(item => {
219
218
  const returnResult = [...item];
220
219
  const [action,] = item;
221
- if (action === ExecutionScope_1.SequenceAction.Assign) {
220
+ if (action === ExecutionScope_js_1.SequenceAction.Assign) {
222
221
  const [, name, component] = item;
223
222
  variableMapping.set(name, component);
224
223
  }
225
224
  else {
226
- if (action === ExecutionScope_1.SequenceAction.At || action === ExecutionScope_1.SequenceAction.To) {
225
+ if (action === ExecutionScope_js_1.SequenceAction.At || action === ExecutionScope_js_1.SequenceAction.To) {
227
226
  const [, component,] = item;
228
227
  const foundIndex = Array.from(variableMapping.values()).findIndex(item2 => {
229
228
  if (component._copyFrom !== null) {
@@ -244,19 +243,75 @@ exports.generateDebugSequenceAction = generateDebugSequenceAction;
244
243
  function getBlockTypeString(type) {
245
244
  let returnValue = 'branch';
246
245
  switch (type) {
247
- case globals_1.BlockTypes.Branch:
246
+ case globals_js_1.BlockTypes.Branch:
248
247
  returnValue = 'branch';
249
248
  break;
250
- case globals_1.BlockTypes.Join:
249
+ case globals_js_1.BlockTypes.Join:
251
250
  returnValue = 'join';
252
251
  break;
253
- case globals_1.BlockTypes.Parallel:
252
+ case globals_js_1.BlockTypes.Parallel:
254
253
  returnValue = 'parallel';
255
254
  break;
256
- case globals_1.BlockTypes.Point:
255
+ case globals_js_1.BlockTypes.Point:
257
256
  returnValue = 'point';
258
257
  break;
259
258
  }
260
259
  return returnValue;
261
260
  }
262
261
  exports.getBlockTypeString = getBlockTypeString;
262
+ class BaseError extends Error {
263
+ constructor(message, startToken, endToken, filePath) {
264
+ super(message);
265
+ this.name = 'BaseError';
266
+ this.message = message;
267
+ this.startToken = startToken;
268
+ this.endToken = endToken;
269
+ this.filePath = filePath;
270
+ }
271
+ toString() {
272
+ const parts = [this.name];
273
+ if (this.startToken) {
274
+ const { line, column } = this.startToken;
275
+ if (this.endToken && (this.endToken.line !== this.startToken.line || this.endToken.column !== this.startToken.column)) {
276
+ const endLine = this.endToken.line;
277
+ const endColumn = this.endToken.column + (this.endToken.stop - this.endToken.start);
278
+ parts.push(` at ${line}:${column}-${endLine}:${endColumn}`);
279
+ }
280
+ else {
281
+ parts.push(` at ${line}:${column}`);
282
+ }
283
+ }
284
+ parts.push(`: ${this.message}`);
285
+ return parts.join('');
286
+ }
287
+ }
288
+ exports.BaseError = BaseError;
289
+ class ParseSyntaxError extends BaseError {
290
+ constructor() {
291
+ super(...arguments);
292
+ this.name = 'ParseSyntaxError';
293
+ }
294
+ }
295
+ exports.ParseSyntaxError = ParseSyntaxError;
296
+ class ParseError extends ParseSyntaxError {
297
+ constructor() {
298
+ super(...arguments);
299
+ this.name = 'ParseError';
300
+ }
301
+ }
302
+ exports.ParseError = ParseError;
303
+ class RuntimeExecutionError extends BaseError {
304
+ constructor() {
305
+ super(...arguments);
306
+ this.name = 'RuntimeExecutionError';
307
+ }
308
+ }
309
+ exports.RuntimeExecutionError = RuntimeExecutionError;
310
+ class RenderError extends Error {
311
+ constructor(message, stage) {
312
+ super(message);
313
+ this.name = 'RenderError';
314
+ this.stage = stage;
315
+ }
316
+ }
317
+ exports.RenderError = RenderError;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolTable = void 0;
4
+ const types_js_1 = require("../objects/types.js");
5
+ class SymbolTable {
6
+ constructor() {
7
+ this.symbols = new Map();
8
+ this.executonContextsNamespaces = [];
9
+ }
10
+ getSymbols() {
11
+ return this.symbols;
12
+ }
13
+ addFunction(token, fileName, executionContext, id, funcDefinedParameters) {
14
+ return this.add(fileName, executionContext, id, types_js_1.ParseSymbolType.Function, {
15
+ funcDefinedParameters
16
+ }, token);
17
+ }
18
+ addVariable(token, fileName, executionContext, id, variableValue) {
19
+ return this.add(fileName, executionContext, id, types_js_1.ParseSymbolType.Variable, {
20
+ variableValue
21
+ }, token);
22
+ }
23
+ addUndefined(filePath, executionContext, id, token) {
24
+ return this.add(filePath, executionContext, id, types_js_1.ParseSymbolType.Undefined, {}, token);
25
+ }
26
+ add(fileName, executionContext, id, type, extra, token) {
27
+ if (this.executonContextsNamespaces.indexOf(executionContext.namespace) === -1) {
28
+ this.executonContextsNamespaces.push(executionContext.namespace);
29
+ }
30
+ const item = {
31
+ id,
32
+ type,
33
+ context: executionContext,
34
+ fileName,
35
+ extra,
36
+ token,
37
+ instances: [],
38
+ };
39
+ if (token !== null) {
40
+ item.instances.push(token);
41
+ }
42
+ this.symbols.set(this.idName(executionContext, id), item);
43
+ return item;
44
+ }
45
+ idName(executionContext, id) {
46
+ return executionContext.namespace + id;
47
+ }
48
+ dumpSymbols() {
49
+ for (const [key, value] of this.symbols) {
50
+ console.log(value.type.padEnd(10, " "), key);
51
+ }
52
+ }
53
+ exists(executionContext, id) {
54
+ const name = this.idName(executionContext, id);
55
+ return this.symbols.has(name);
56
+ }
57
+ existsAny(executionContext, id) {
58
+ if (this.exists(executionContext, id)) {
59
+ return true;
60
+ }
61
+ else {
62
+ return this.searchParentContext(executionContext, id) !== null;
63
+ }
64
+ }
65
+ get(executionContext, id) {
66
+ const name = this.idName(executionContext, id);
67
+ return this.symbols.get(name);
68
+ }
69
+ getParentContexts(executionContext, contextsNamespace) {
70
+ if (executionContext.parentContext !== null) {
71
+ contextsNamespace.push(executionContext.parentContext.namespace);
72
+ this.getParentContexts(executionContext.parentContext, contextsNamespace);
73
+ }
74
+ return contextsNamespace;
75
+ }
76
+ searchParentContext(executionContext, id) {
77
+ const contextNames = this.getParentContexts(executionContext, []);
78
+ for (const [key,] of this.symbols) {
79
+ if (key.endsWith(`.${id}`)) {
80
+ const { context } = this.symbols.get(key);
81
+ if (contextNames.indexOf(context.namespace) !== -1) {
82
+ return context;
83
+ }
84
+ }
85
+ }
86
+ return null;
87
+ }
88
+ clearUndefined() {
89
+ for (const [key, value] of this.symbols) {
90
+ if (value.type === types_js_1.ParseSymbolType.Undefined) {
91
+ this.symbols.delete(key);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ exports.SymbolTable = SymbolTable;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolValidatorResolveVisitor = void 0;
4
+ const SymbolValidatorVisitor_js_1 = require("./SymbolValidatorVisitor.js");
5
+ class SymbolValidatorResolveVisitor extends SymbolValidatorVisitor_js_1.SymbolValidatorVisitor {
6
+ addSymbolVariable(token, name, value) {
7
+ }
8
+ addSymbolFunction(token, functionName, funcDefinedParameters) {
9
+ if (this.symbolTable.exists(this.getExecutor(), functionName)) {
10
+ this.symbolTable.addFunction(token, this.getCurrentFile(), this.getExecutor(), functionName, funcDefinedParameters);
11
+ }
12
+ }
13
+ }
14
+ exports.SymbolValidatorResolveVisitor = SymbolValidatorResolveVisitor;
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolValidatorVisitor = void 0;
4
+ const builtinMethods_js_1 = require("../builtinMethods.js");
5
+ const types_js_1 = require("../objects/types.js");
6
+ const SymbolTable_js_1 = require("./SymbolTable.js");
7
+ const BaseVisitor_js_1 = require("../BaseVisitor.js");
8
+ const globals_js_1 = require("../globals.js");
9
+ class SymbolValidatorVisitor extends BaseVisitor_js_1.BaseVisitor {
10
+ constructor() {
11
+ super(...arguments);
12
+ this.symbolTable = new SymbolTable_js_1.SymbolTable();
13
+ this.filePathStack = [];
14
+ this.visitImport_expr = (ctx) => {
15
+ const ID = ctx.ID().toString();
16
+ const { pathExists } = this.handleImportFile(ID, false, ctx);
17
+ if (!pathExists) {
18
+ this.symbolTable.addUndefined(this.getCurrentFile(), this.getExecutor(), ID, ctx.ID().getSymbol());
19
+ }
20
+ };
21
+ this.visitAssignment_expr = (ctx) => {
22
+ const ctxDataExpr = ctx.data_expr();
23
+ this.visit(ctxDataExpr);
24
+ const value = this.getResult(ctxDataExpr);
25
+ const atomId = ctx.atom_expr().ID(0);
26
+ const atomText = atomId.getText();
27
+ const executor = this.getExecutor();
28
+ if (!this.symbolTable.exists(executor, atomText)) {
29
+ this.addSymbolVariable(atomId.getSymbol(), atomId.getText(), value);
30
+ }
31
+ else {
32
+ const tmpSymbol = this.symbolTable.get(executor, atomText);
33
+ this.addSymbolInstance(tmpSymbol, atomId.getSymbol());
34
+ }
35
+ return null;
36
+ };
37
+ this.visitAtom_expr = (ctx) => {
38
+ const tmpSymbol = this.handleAtomSymbol(ctx.ID(0));
39
+ this.setResult(ctx, tmpSymbol);
40
+ };
41
+ this.visitFunction_call_expr = (ctx) => {
42
+ this.handleAtomSymbol(ctx.ID());
43
+ if (ctx.trailer_expr().length > 0) {
44
+ ctx.trailer_expr().forEach(item => {
45
+ if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
46
+ const params = item.parameters();
47
+ if (params) {
48
+ this.visit(params);
49
+ }
50
+ }
51
+ });
52
+ }
53
+ };
54
+ this.visitValueAtomExpr = (ctx) => {
55
+ let value = null;
56
+ const ctxValueExpr = ctx.value_expr();
57
+ const cxtAtomExpr = ctx.atom_expr();
58
+ if (ctxValueExpr) {
59
+ this.visit(ctxValueExpr);
60
+ value = this.getResult(ctxValueExpr);
61
+ }
62
+ else if (cxtAtomExpr) {
63
+ this.visit(cxtAtomExpr);
64
+ value = this.getResult(cxtAtomExpr);
65
+ }
66
+ this.setResult(ctx, value);
67
+ };
68
+ this.visitUnaryOperatorExpr = (ctx) => {
69
+ this.visit(ctx.data_expr());
70
+ };
71
+ this.visitMultiplyExpr = (ctx) => {
72
+ this.visit(ctx.data_expr(0));
73
+ this.visit(ctx.data_expr(1));
74
+ };
75
+ this.visitAdditionExpr = (ctx) => {
76
+ this.visit(ctx.data_expr(0));
77
+ this.visit(ctx.data_expr(1));
78
+ };
79
+ this.visitBinaryOperatorExpr = (ctx) => {
80
+ this.visit(ctx.data_expr(0));
81
+ this.visit(ctx.data_expr(1));
82
+ };
83
+ this.visitDataExpr = (ctx) => {
84
+ return;
85
+ };
86
+ this.visitFunction_def_expr = (ctx) => {
87
+ const functionName = ctx.ID().getText();
88
+ let funcDefinedParameters = [];
89
+ const ctxFunctionArgsExpr = ctx.function_args_expr();
90
+ if (ctxFunctionArgsExpr) {
91
+ this.visit(ctxFunctionArgsExpr);
92
+ funcDefinedParameters = this.getResult(ctxFunctionArgsExpr);
93
+ }
94
+ this.addSymbolFunction(ctx.ID().getSymbol(), functionName, funcDefinedParameters);
95
+ const executionContextName = functionName + globals_js_1.SymbolValidatorContext;
96
+ const passedInParamsNull = funcDefinedParameters.map((param, index) => {
97
+ return ['position', index, null];
98
+ });
99
+ const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, funcDefinedParameters, passedInParamsNull);
100
+ funcDefinedParameters.forEach(param => {
101
+ this.addSymbolVariable(param[1], param[0], null, newExecutor);
102
+ });
103
+ this.runExpressions(newExecutor, ctx.function_expr());
104
+ this.executionStack.pop();
105
+ };
106
+ }
107
+ enterFile(filePath) {
108
+ this.filePathStack.push(filePath);
109
+ }
110
+ exitFile() {
111
+ this.filePathStack.pop();
112
+ }
113
+ getCurrentFile() {
114
+ return this.filePathStack[this.filePathStack.length - 1];
115
+ }
116
+ addSymbolVariable(token, name, value, executor = null) {
117
+ const useExecutor = executor === null ? this.getExecutor() : executor;
118
+ this.symbolTable.addVariable(token, this.getCurrentFile(), useExecutor, name, value);
119
+ this.log2('add symbol variable: ' + name);
120
+ }
121
+ addSymbolFunction(token, functionName, funcDefinedParameters) {
122
+ if (!this.symbolTable.exists(this.getExecutor(), functionName)) {
123
+ this.symbolTable.addFunction(token, this.getCurrentFile(), this.getExecutor(), functionName, funcDefinedParameters);
124
+ this.log2('add symbol function: ' + functionName);
125
+ }
126
+ }
127
+ handleAtomSymbol(atom) {
128
+ const atomId = atom.getText();
129
+ const executor = this.getExecutor();
130
+ let tmpSymbol;
131
+ if (this.symbolTable.exists(executor, atomId)) {
132
+ tmpSymbol = this.symbolTable.get(executor, atomId);
133
+ }
134
+ else {
135
+ if (builtinMethods_js_1.buildInMethodNamesList.indexOf(atomId) !== -1) {
136
+ tmpSymbol = this.symbolTable.addFunction(null, "<builtIn>", this.getRootExecutor(), atomId, {});
137
+ }
138
+ else {
139
+ const foundContext = this.symbolTable.searchParentContext(executor, atomId);
140
+ if (foundContext === null) {
141
+ tmpSymbol = this.symbolTable.addUndefined(this.getCurrentFile(), executor, atomId, atom.getSymbol());
142
+ this.log2('symbol not found: ' + atomId);
143
+ }
144
+ else {
145
+ tmpSymbol = this.symbolTable.get(foundContext, atomId);
146
+ }
147
+ }
148
+ }
149
+ if (tmpSymbol.type !== types_js_1.ParseSymbolType.Undefined) {
150
+ this.addSymbolInstance(tmpSymbol, atom.getSymbol());
151
+ }
152
+ return tmpSymbol;
153
+ }
154
+ addSymbolInstance(symbol, token) {
155
+ symbol = symbol;
156
+ if (symbol.instances.indexOf(token) == -1) {
157
+ symbol.instances.push(token);
158
+ }
159
+ }
160
+ setSymbols(symbolTable) {
161
+ this.symbolTable = symbolTable;
162
+ }
163
+ getSymbols() {
164
+ return this.symbolTable;
165
+ }
166
+ dumpSymbols() {
167
+ this.symbolTable.dumpSymbols();
168
+ }
169
+ }
170
+ exports.SymbolValidatorVisitor = SymbolValidatorVisitor;
@@ -1,81 +1,108 @@
1
1
  #! /usr/bin/env node
2
2
  "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
3
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.validate = void 0;
8
4
  const commander_1 = require("commander");
9
- const figlet_1 = __importDefault(require("figlet"));
10
- const path_1 = __importDefault(require("path"));
11
5
  const fs_1 = require("fs");
12
- const sizing_js_1 = require("./sizing.js");
13
6
  const helpers_js_1 = require("./helpers.js");
7
+ const environment_js_1 = require("./environment.js");
8
+ const types_js_1 = require("./objects/types.js");
14
9
  async function validate() {
15
- const { filePath } = (0, helpers_js_1.getCurrentPath)();
16
- const toolSrcPath = filePath;
17
- const toolDirectory = path_1.default.dirname(toolSrcPath) + '/../../';
18
- const fontsPath = toolDirectory + '/fonts';
19
- const defaultLibsPath = toolDirectory + '/libs';
20
- const packageJson = JSON.parse((0, fs_1.readFileSync)(toolDirectory + 'package.json').toString());
21
- ;
22
- const { version } = packageJson;
10
+ const env = new environment_js_1.NodeScriptEnvironment();
11
+ environment_js_1.NodeScriptEnvironment.setInstance(env);
12
+ const version = env.getPackageVersion();
23
13
  commander_1.program
24
- .description('generate graphical output from circuitscript files')
14
+ .description('generate validation output circuitscript files')
25
15
  .version(version)
16
+ .argument('[input path]', 'Input path')
17
+ .argument('[output path]', 'Output path')
26
18
  .option('-i, --input text <input text>', 'Input text directly')
27
- .option('-f, --input-file <path>', 'Input file')
28
- .option('-o, --output <path>', 'Output path')
29
19
  .option('-c, --current-directory <path>', 'Set current directory')
30
- .option('-k, --kicad-netlist <filename>', 'Create KiCad netlist')
31
20
  .option('-w, --watch', 'Watch for file changes')
32
21
  .option('-n, --dump-nets', 'Dump out net information')
33
22
  .option('-d, --dump-data', 'Dump data during parsing')
34
- .option('-s, --stats', 'Show stats during generation');
35
- commander_1.program.addHelpText('before', figlet_1.default.textSync('circuitscript', {
36
- font: 'Small Slant'
37
- }));
23
+ .option('-s, --stats', 'Show stats during generation')
24
+ .option('-x, --skip-output', 'Skip output generation');
38
25
  if (process.argv.length < 3) {
39
26
  commander_1.program.help();
40
27
  }
41
28
  commander_1.program.parse();
42
29
  const options = commander_1.program.opts();
30
+ const args = commander_1.program.args;
43
31
  const watchFileChanges = options.watch;
44
- const outputPath = options.output ?? null;
45
32
  const dumpNets = options.dumpNets;
46
33
  const dumpData = options.dumpData;
47
- const kicadNetlist = options.kicadNetlist;
48
- let currentDirectory = options.currentDirectory ?? null;
34
+ if (options.currentDirectory) {
35
+ env.setModuleDirectory(options.currentDirectory);
36
+ }
49
37
  if (watchFileChanges) {
50
38
  console.log('watching for file changes...');
51
39
  }
52
- await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
53
- let inputFilePath = null;
40
+ await env.prepareSVGEnvironment();
41
+ let inputFilePath = "";
42
+ if (args.length > 2) {
43
+ console.log("Error: Extra arguments passed");
44
+ return;
45
+ }
54
46
  let scriptData;
55
- if (options.input) {
47
+ if (args.length > 0 && args[0]) {
48
+ inputFilePath = args[0];
49
+ if ((0, fs_1.existsSync)(inputFilePath)) {
50
+ scriptData = (0, fs_1.readFileSync)(inputFilePath, { encoding: 'utf-8' });
51
+ }
52
+ else {
53
+ console.error("Error: File could not be found");
54
+ return;
55
+ }
56
+ }
57
+ else if (options.input) {
56
58
  scriptData = options.input;
57
59
  }
58
60
  else {
59
- inputFilePath = options.inputFile;
60
- const tmpScriptData = (0, helpers_js_1.getScriptText)(inputFilePath);
61
- if (tmpScriptData === null) {
62
- throw "File does not exists";
63
- }
64
- scriptData = tmpScriptData;
65
- if (currentDirectory === null) {
66
- currentDirectory = path_1.default.dirname(inputFilePath);
67
- }
61
+ console.error("Error: No input provided");
62
+ return;
68
63
  }
69
64
  const scriptOptions = {
70
- currentDirectory,
71
- defaultLibsPath,
72
65
  dumpNets,
73
66
  dumpData,
74
- kicadNetlistPath: kicadNetlist,
75
67
  showStats: options.stats,
68
+ environment: env,
76
69
  };
77
- const visitor = (0, helpers_js_1.validateScript)(scriptData, scriptOptions);
78
- const semanticTokensVisitor = (0, helpers_js_1.getSemanticTokens)(scriptData, scriptOptions);
70
+ const visitor = await (0, helpers_js_1.validateScript)(inputFilePath, scriptData, scriptOptions);
71
+ const symbols = visitor.getSymbols().getSymbols();
72
+ const undefinedSymbols = [];
73
+ symbols.forEach((value, key) => {
74
+ if (value.type !== types_js_1.ParseSymbolType.Undefined) {
75
+ value = value;
76
+ const token = value.token;
77
+ console.log(key, value.fileName, token !== null ? (token.line + ":" + token.column) : "");
78
+ value.instances.forEach(instance => {
79
+ console.log(" " + instance.line + ":" + instance.column + " " + instance.start);
80
+ });
81
+ }
82
+ else {
83
+ undefinedSymbols.push(value);
84
+ }
85
+ });
86
+ const { parsedTokens } = await (0, helpers_js_1.getSemanticTokens)(scriptData, scriptOptions);
87
+ parsedTokens.forEach(item => {
88
+ const { line, column, tokenType, tokenModifiers, textValue } = item;
89
+ console.log(`${line}:${column} - ${textValue} - ${tokenType} | ${tokenModifiers.join(',')}`);
90
+ });
91
+ console.log('--- undefined ---');
92
+ undefinedSymbols.forEach(symbol => {
93
+ const { token } = symbol;
94
+ const info = {
95
+ start: {
96
+ line: (token?.line || 1),
97
+ character: token?.column || 0
98
+ },
99
+ end: {
100
+ line: (token?.line || 1),
101
+ character: (token?.column || 0) + ((token?.stop || 0) - (token?.start || 0) + 1)
102
+ }
103
+ };
104
+ console.log(token.text, info);
105
+ });
79
106
  }
80
- exports.validate = validate;
107
+ exports.default = validate;
81
108
  validate();