circuitscript 0.0.24 → 0.0.26

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 (73) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/BaseVisitor.js +485 -0
  3. package/dist/cjs/SemanticTokenVisitor.js +218 -0
  4. package/dist/cjs/SymbolValidatorVisitor.js +233 -0
  5. package/dist/cjs/antlr/CircuitScriptLexer.js +256 -219
  6. package/dist/cjs/antlr/CircuitScriptParser.js +2891 -2151
  7. package/dist/cjs/antlr/CircuitScriptVisitor.js +4 -3
  8. package/dist/cjs/draw_symbols.js +73 -22
  9. package/dist/cjs/execute.js +70 -78
  10. package/dist/cjs/export.js +91 -5
  11. package/dist/cjs/geometry.js +28 -8
  12. package/dist/cjs/globals.js +1 -2
  13. package/dist/cjs/helpers.js +180 -7
  14. package/dist/cjs/index.js +2 -0
  15. package/dist/cjs/layout.js +8 -0
  16. package/dist/cjs/lexer.js +19 -22
  17. package/dist/cjs/main.js +27 -20
  18. package/dist/cjs/objects/ClassComponent.js +4 -0
  19. package/dist/cjs/objects/ExecutionScope.js +1 -0
  20. package/dist/cjs/objects/types.js +7 -1
  21. package/dist/cjs/parser.js +29 -258
  22. package/dist/cjs/render.js +1 -1
  23. package/dist/cjs/validate.js +81 -0
  24. package/dist/cjs/visitor.js +601 -823
  25. package/dist/esm/BaseVisitor.mjs +486 -0
  26. package/dist/esm/SemanticTokenVisitor.mjs +215 -0
  27. package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
  28. package/dist/esm/antlr/CircuitScriptLexer.mjs +231 -218
  29. package/dist/esm/antlr/CircuitScriptParser.mjs +2852 -2144
  30. package/dist/esm/antlr/CircuitScriptVisitor.mjs +13 -4
  31. package/dist/esm/draw_symbols.mjs +74 -23
  32. package/dist/esm/execute.mjs +70 -75
  33. package/dist/esm/export.mjs +89 -6
  34. package/dist/esm/geometry.mjs +28 -8
  35. package/dist/esm/globals.mjs +1 -2
  36. package/dist/esm/helpers.mjs +171 -9
  37. package/dist/esm/index.mjs +2 -0
  38. package/dist/esm/layout.mjs +8 -0
  39. package/dist/esm/lexer.mjs +10 -10
  40. package/dist/esm/main.mjs +28 -21
  41. package/dist/esm/objects/ClassComponent.mjs +4 -0
  42. package/dist/esm/objects/ExecutionScope.mjs +1 -0
  43. package/dist/esm/objects/types.mjs +6 -0
  44. package/dist/esm/parser.mjs +25 -230
  45. package/dist/esm/render.mjs +2 -2
  46. package/dist/esm/validate.mjs +74 -0
  47. package/dist/esm/visitor.mjs +415 -643
  48. package/dist/types/BaseVisitor.d.ts +66 -0
  49. package/dist/types/SemanticTokenVisitor.d.ts +36 -0
  50. package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
  51. package/dist/types/antlr/CircuitScriptLexer.d.ts +37 -29
  52. package/dist/types/antlr/CircuitScriptParser.d.ts +606 -494
  53. package/dist/types/antlr/CircuitScriptVisitor.d.ts +78 -60
  54. package/dist/types/draw_symbols.d.ts +12 -3
  55. package/dist/types/execute.d.ts +5 -10
  56. package/dist/types/export.d.ts +27 -1
  57. package/dist/types/geometry.d.ts +4 -0
  58. package/dist/types/globals.d.ts +2 -3
  59. package/dist/types/helpers.d.ts +32 -1
  60. package/dist/types/index.d.ts +2 -0
  61. package/dist/types/lexer.d.ts +2 -2
  62. package/dist/types/objects/ClassComponent.d.ts +1 -0
  63. package/dist/types/objects/ExecutionScope.d.ts +4 -1
  64. package/dist/types/objects/types.d.ts +5 -0
  65. package/dist/types/parser.d.ts +15 -28
  66. package/dist/types/validate.d.ts +2 -0
  67. package/dist/types/visitor.d.ts +43 -95
  68. package/fonts/Inter-Bold.ttf +0 -0
  69. package/fonts/Inter-Regular.ttf +0 -0
  70. package/fonts/OpenSans-Regular.ttf +0 -0
  71. package/fonts/Roboto-Regular.ttf +0 -0
  72. package/libs/lib.cst +184 -0
  73. package/package.json +11 -6
@@ -1,815 +1,639 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VisitorExecutionException = exports.MainVisitor = void 0;
4
- const antlr4_1 = require("antlr4");
5
- const fs_1 = require("fs");
6
- const path_1 = require("path");
7
- const execute_js_1 = require("./execute.js");
3
+ exports.VisitorExecutionException = exports.ParserVisitor = void 0;
8
4
  const ClassComponent_js_1 = require("./objects/ClassComponent.js");
9
5
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
10
6
  const PinDefinition_js_1 = require("./objects/PinDefinition.js");
11
7
  const PinTypes_js_1 = require("./objects/PinTypes.js");
12
8
  const types_js_1 = require("./objects/types.js");
13
- const logger_js_1 = require("./logger.js");
14
9
  const globals_js_1 = require("./globals.js");
15
10
  const draw_symbols_js_1 = require("./draw_symbols.js");
16
- const parser_js_1 = require("./parser.js");
17
- class MainVisitor extends antlr4_1.ParseTreeVisitor {
18
- constructor(silent = false) {
19
- super();
20
- this.indentLevel = 0;
21
- this.silent = false;
22
- this.printStream = [];
23
- this.printToConsole = true;
24
- this.acceptedDirections = ['left', 'right', 'up', 'down'];
25
- this.pinTypesList = [
26
- PinTypes_js_1.PinTypes.Any,
27
- PinTypes_js_1.PinTypes.Input,
28
- PinTypes_js_1.PinTypes.Output,
29
- PinTypes_js_1.PinTypes.IO,
30
- PinTypes_js_1.PinTypes.Power,
31
- ];
32
- this.onImportFile = (visitor, filePath) => {
33
- throw "Import file not implemented";
11
+ const BaseVisitor_js_1 = require("./BaseVisitor.js");
12
+ class ParserVisitor extends BaseVisitor_js_1.BaseVisitor {
13
+ constructor() {
14
+ super(...arguments);
15
+ this.visitKeyword_assignment_expr = (ctx) => {
16
+ const id = ctx.ID().getText();
17
+ const ctxDataExpr = ctx.data_expr();
18
+ this.visit(ctxDataExpr);
19
+ const value = this.getResult(ctxDataExpr);
20
+ this.setResult(ctx, [id, value]);
34
21
  };
35
- this.pinTypes = [
36
- PinTypes_js_1.PinTypes.Any,
37
- PinTypes_js_1.PinTypes.IO,
38
- PinTypes_js_1.PinTypes.Input,
39
- PinTypes_js_1.PinTypes.Output,
40
- PinTypes_js_1.PinTypes.Power,
41
- ];
42
- this.logger = new logger_js_1.Logger();
43
- this.startingContext = new execute_js_1.ExecutionContext('__', '__.', '/', 0, 0, silent, this.logger);
44
- this.setupPrintFunction(this.startingContext);
45
- this.executionStack = [this.startingContext];
46
- this.startingContext.resolveNet =
47
- this.createNetResolver(this.executionStack);
48
- this.silent = silent;
49
- }
50
- getExecutor() {
51
- return this.executionStack[this.executionStack.length - 1];
52
- }
53
- visit(ctx) {
54
- if (Array.isArray(ctx)) {
55
- return ctx.map(function (child) {
56
- try {
57
- return child.accept(this);
58
- }
59
- catch (err) {
60
- this.handleError(child, err);
61
- }
62
- }, this);
63
- }
64
- else {
65
- try {
66
- return ctx.accept(this);
67
- }
68
- catch (err) {
69
- this.handleError(ctx, err);
70
- }
71
- }
72
- }
73
- handleError(ctx, err) {
74
- if (!(err instanceof VisitorExecutionException)) {
75
- throw new VisitorExecutionException(ctx, err);
76
- }
77
- else {
78
- throw err;
79
- }
80
- }
81
- visitScript(ctx) {
82
- this.print('===', 'start', '===');
83
- const result = this.visitChildren(ctx);
84
- this.print('===', 'end', '===');
85
- return result;
86
- }
87
- visitParameters(ctx) {
88
- const dataExpressions = ctx.data_expr_list();
89
- const keywordAssignmentExpressions = ctx.keyword_assignment_expr_list();
90
- const returnList = [];
91
- dataExpressions.forEach((item, index) => {
92
- const value = this.visit(item);
93
- returnList.push(['position', index, value]);
94
- });
95
- keywordAssignmentExpressions.forEach((item) => {
96
- const [key, value] = this.visit(item);
97
- returnList.push(['keyword', key, value]);
98
- });
99
- return returnList;
100
- }
101
- visitKeyword_assignment_expr(ctx) {
102
- const id = ctx.ID().getText();
103
- const value = this.visit(ctx.data_expr());
104
- return [id, value];
105
- }
106
- visitAssignment_expr(ctx) {
107
- const atomStr = ctx.atom_expr().getText();
108
- if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
109
- throw "Invalid assignment expression!";
110
- }
111
- const reference = this.visit(ctx.atom_expr());
112
- const value = this.visit(ctx.data_expr());
113
- if (value instanceof ClassComponent_js_1.ClassComponent) {
114
- const instances = this.getExecutor().scope.instances;
115
- const tmpComponent = value;
116
- const oldName = tmpComponent.instanceName;
117
- tmpComponent.instanceName = reference.name;
118
- instances.delete(oldName);
119
- instances.set(reference.name, tmpComponent);
120
- this.getExecutor().print(`assigned '${reference.name}' to ClassComponent`);
121
- }
122
- else {
123
- const trailers = reference.trailers ?? [];
124
- if (trailers.length === 0) {
125
- this.getExecutor().scope.variables.set(reference.name, value);
126
- }
127
- else if (reference.value instanceof ClassComponent_js_1.ClassComponent) {
128
- this.setInstanceParam(reference.value, trailers, value);
22
+ this.visitPin_select_expr = (ctx) => {
23
+ let value = null;
24
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
25
+ const ctxStringValue = ctx.STRING_VALUE();
26
+ if (ctxIntegerValue) {
27
+ value = Number(ctxIntegerValue.getText());
129
28
  }
130
- }
131
- return value;
132
- }
133
- setInstanceParam(object, trailers, value) {
134
- const paramName = trailers[0].slice(1);
135
- object.setParam(paramName, value);
136
- this.getExecutor().print(`set instance ${object.instanceName} param ${paramName} to ${value}`);
137
- }
138
- visitValue_expr(ctx) {
139
- const sign = ctx.Minus() ? -1 : 1;
140
- if (ctx.INTEGER_VALUE() || ctx.DECIMAL_VALUE() || ctx.NUMERIC_VALUE()) {
141
- if (ctx.INTEGER_VALUE()) {
142
- return sign * Number(ctx.INTEGER_VALUE().getText());
143
- }
144
- else if (ctx.DECIMAL_VALUE()) {
145
- return sign * Number(ctx.DECIMAL_VALUE().getText());
146
- }
147
- else if (ctx.NUMERIC_VALUE()) {
148
- const textExtra = ctx.Minus() ? '-' : '';
149
- return new ParamDefinition_js_1.NumericValue(textExtra + ctx.NUMERIC_VALUE().getText());
29
+ else if (ctxStringValue) {
30
+ value = this.prepareStringValue(ctxStringValue.getText());
150
31
  }
151
- }
152
- else {
153
- if (sign === -1) {
154
- throw "Invalid value!";
155
- }
156
- }
157
- if (ctx.BOOLEAN_VALUE()) {
158
- const stringValue = ctx.BOOLEAN_VALUE().getText();
159
- if (stringValue === 'true') {
160
- return true;
32
+ this.setResult(ctx, value);
33
+ };
34
+ this.visitAdd_component_expr = (ctx) => {
35
+ const ctxDataWithAssignmentExpr = ctx.data_expr_with_assignment();
36
+ this.visit(ctxDataWithAssignmentExpr);
37
+ const [component, pinValue] = this.getResult(ctxDataWithAssignmentExpr);
38
+ return this.getExecutor().addComponentExisting(component, pinValue);
39
+ };
40
+ this.visitAt_component_expr = (ctx) => {
41
+ if (ctx.Point()) {
42
+ this.getExecutor().atPointBlock();
161
43
  }
162
- else if (stringValue === 'false') {
163
- return false;
44
+ else {
45
+ const ctxComponentSelectExpr = ctx.component_select_expr();
46
+ this.visit(ctxComponentSelectExpr);
47
+ const [component, pin] = this.getResult(ctxComponentSelectExpr);
48
+ this.getExecutor().atComponent(component, pin, {
49
+ addSequence: true
50
+ });
164
51
  }
165
- }
166
- else if (ctx.STRING_VALUE()) {
167
- return this.prepareStringValue(ctx.STRING_VALUE().getText());
168
- }
169
- else if (ctx.PERCENTAGE_VALUE()) {
170
- return new ParamDefinition_js_1.PercentageValue(ctx.PERCENTAGE_VALUE().getText());
171
- }
172
- else if (ctx.blank_expr()) {
173
- return this.visit(ctx.blank_expr());
174
- }
175
- }
176
- visitBlank_expr(ctx) {
177
- return new ParamDefinition_js_1.PinBlankValue(Number(ctx.INTEGER_VALUE().getText()));
178
- }
179
- visitPin_select_expr(ctx) {
180
- if (ctx.INTEGER_VALUE()) {
181
- return Number(ctx.INTEGER_VALUE().getText());
182
- }
183
- else if (ctx.STRING_VALUE()) {
184
- return this.prepareStringValue(ctx.STRING_VALUE().getText());
185
- }
186
- return null;
187
- }
188
- visitAdd_component_expr(ctx) {
189
- const [component, pinValue] = this.visit(ctx.data_expr_with_assignment());
190
- if (ctx.ID()) {
191
- this.setComponentOrientation(component, pinValue, ctx.ID().getText());
192
- }
193
- return this.getExecutor().addComponentExisting(component, pinValue);
194
- }
195
- visitAt_component_expr(ctx) {
196
- if (ctx.Point()) {
197
- this.getExecutor().atPointBlock();
198
- }
199
- else {
200
- const [component, pin] = this.visit(ctx.component_select_expr());
201
- const currentPoint = this.getExecutor().atComponent(component, pin, {
202
- addSequence: true,
203
- cloneNetComponent: true
204
- });
205
- if (ctx.ID()) {
206
- this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
52
+ return this.getExecutor().getCurrentPoint();
53
+ };
54
+ this.visitTo_component_expr = (ctx) => {
55
+ if (ctx.Point()) {
56
+ this.getExecutor().toPointBlock();
207
57
  }
208
- }
209
- return this.getExecutor().getCurrentPoint();
210
- }
211
- visitTo_component_expr(ctx) {
212
- let currentPoint;
213
- if (ctx.Point()) {
214
- this.getExecutor().toPointBlock();
215
- }
216
- else {
217
- ctx.component_select_expr_list().forEach((item) => {
218
- const [component, pin] = this.visit(item);
219
- currentPoint = this.getExecutor().toComponent(component, pin, {
220
- addSequence: true, cloneNetComponent: true
58
+ else {
59
+ ctx.component_select_expr().forEach(item => {
60
+ this.visit(item);
61
+ const [component, pin] = this.getResult(item);
62
+ this.getExecutor().toComponent(component, pin, {
63
+ addSequence: true
64
+ });
221
65
  });
222
- });
223
- if (ctx.ID()) {
224
- this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
225
66
  }
226
- }
227
- return this.getExecutor().getCurrentPoint();
228
- }
229
- visitComponent_select_expr(ctx) {
230
- if (ctx.data_expr_with_assignment()) {
231
- return this.visit(ctx.data_expr_with_assignment());
232
- }
233
- else {
234
- const component = this.getExecutor().scope.currentComponent;
235
- let pinId = null;
236
- if (ctx.pin_select_expr()) {
237
- pinId = this.visit(ctx.pin_select_expr());
67
+ return this.getExecutor().getCurrentPoint();
68
+ };
69
+ this.visitComponent_select_expr = (ctx) => {
70
+ const ctxDataExprWithAssigment = ctx.data_expr_with_assignment();
71
+ if (ctxDataExprWithAssigment) {
72
+ this.visit(ctxDataExprWithAssigment);
73
+ this.setResult(ctx, this.getResult(ctxDataExprWithAssigment));
238
74
  }
239
- return [component, pinId];
240
- }
241
- }
242
- visitPath_blocks(ctx) {
243
- const blocks = ctx.path_block_inner_list();
244
- let blockIndex = 0;
245
- let blockType = globals_js_1.BlockTypes.Branch;
246
- let prevBlockType = null;
247
- blocks.forEach((block, index) => {
248
- if (block.Branch()) {
249
- blockType = globals_js_1.BlockTypes.Branch;
250
- }
251
- else if (block.Join()) {
252
- blockType = globals_js_1.BlockTypes.Join;
253
- }
254
- else if (block.Parallel()) {
255
- blockType = globals_js_1.BlockTypes.Parallel;
256
- }
257
- else if (block.Point()) {
258
- blockType = globals_js_1.BlockTypes.Point;
259
- }
260
- if (prevBlockType !== blockType) {
261
- if (index > 0) {
262
- this.getExecutor().exitBlocks();
75
+ else {
76
+ const component = this.getExecutor().scope.currentComponent;
77
+ let pinId = null;
78
+ const ctxPinSelectExpr = ctx.pin_select_expr();
79
+ if (ctxPinSelectExpr) {
80
+ this.visit(ctxPinSelectExpr);
81
+ pinId = this.getResult(ctxPinSelectExpr);
263
82
  }
264
- this.getExecutor().enterBlocks(blockType);
265
- blockIndex = 0;
266
- }
267
- this.getExecutor().enterBlock(blockIndex);
268
- this.visit(block);
269
- this.getExecutor().exitBlock(blockIndex);
270
- blockIndex += 1;
271
- prevBlockType = blockType;
272
- });
273
- this.getExecutor().exitBlocks();
274
- return this.getExecutor().getCurrentPoint();
275
- }
276
- visitBreak_keyword() {
277
- this.getExecutor().breakBranch();
278
- return -1;
279
- }
280
- visitCreate_component_expr(ctx) {
281
- const properties = this.getPropertyExprList(ctx.property_expr_list());
282
- const pins = this.parseCreateComponentPins(properties.get('pins'));
283
- let instanceName = this.getExecutor().getUniqueInstanceName('');
284
- const propParams = properties.get('params');
285
- const params = this.parseCreateComponentParams(propParams);
286
- if (params.length > 0) {
287
- const firstParam = params[0];
288
- const paramValue = firstParam.paramValue;
289
- let appendValue = paramValue.toString();
290
- if (paramValue instanceof ParamDefinition_js_1.NumericValue) {
291
- appendValue = paramValue.value;
292
- }
293
- instanceName += '_' + appendValue;
294
- }
295
- const arrange = properties.has('arrange') ?
296
- properties.get('arrange') : null;
297
- const display = properties.has('display') ?
298
- properties.get('display') : null;
299
- const type = properties.has('type') ?
300
- properties.get('type') : null;
301
- const width = properties.has('width') ?
302
- properties.get('width') : null;
303
- const props = {
304
- arrange,
305
- display,
306
- type,
307
- width,
83
+ this.setResult(ctx, [component, pinId]);
84
+ }
308
85
  };
309
- return this.getExecutor().createComponent(instanceName, pins, params, props);
310
- }
311
- visitCreate_graphic_expr(ctx) {
312
- const commands = ctx.sub_expr_list().reduce((accum, item) => {
313
- const [commandName, parameters] = this.visit(item);
314
- const keywordParams = new Map();
315
- const positionParams = parameters.reduce((accum, [argType, name, value]) => {
316
- if (argType === 'position') {
317
- accum.push(value);
86
+ this.visitPath_blocks = (ctx) => {
87
+ const blocks = ctx.path_block_inner();
88
+ let blockIndex = 0;
89
+ let blockType = globals_js_1.BlockTypes.Branch;
90
+ let prevBlockType = null;
91
+ blocks.forEach((block, index) => {
92
+ if (block.Branch()) {
93
+ blockType = globals_js_1.BlockTypes.Branch;
318
94
  }
319
- else {
320
- keywordParams.set(name, value);
95
+ else if (block.Join()) {
96
+ blockType = globals_js_1.BlockTypes.Join;
97
+ }
98
+ else if (block.Parallel()) {
99
+ blockType = globals_js_1.BlockTypes.Parallel;
100
+ }
101
+ else if (block.Point()) {
102
+ blockType = globals_js_1.BlockTypes.Point;
103
+ }
104
+ if (prevBlockType !== blockType) {
105
+ if (index > 0) {
106
+ this.getExecutor().exitBlocks();
107
+ }
108
+ this.getExecutor().enterBlocks(blockType);
109
+ blockIndex = 0;
321
110
  }
111
+ this.getExecutor().enterBlock(blockIndex);
112
+ this.visit(block);
113
+ this.getExecutor().exitBlock(blockIndex);
114
+ blockIndex += 1;
115
+ prevBlockType = blockType;
116
+ });
117
+ this.getExecutor().exitBlocks();
118
+ return this.getExecutor().getCurrentPoint();
119
+ };
120
+ this.visitCreate_component_expr = (ctx) => {
121
+ const properties = this.getPropertyExprList(ctx.property_expr());
122
+ const pins = this.parseCreateComponentPins(properties.get('pins'));
123
+ let instanceName = this.getExecutor().getUniqueInstanceName('');
124
+ const propParams = properties.get('params');
125
+ const params = this.parseCreateComponentParams(propParams);
126
+ if (params.length > 0) {
127
+ const firstParam = params[0];
128
+ const paramValue = firstParam.paramValue;
129
+ let appendValue = paramValue.toString();
130
+ if (paramValue instanceof ParamDefinition_js_1.NumericValue) {
131
+ appendValue = paramValue.value;
132
+ }
133
+ instanceName += '_' + appendValue;
134
+ }
135
+ const arrange = properties.has('arrange') ?
136
+ properties.get('arrange') : null;
137
+ const display = properties.has('display') ?
138
+ properties.get('display') : null;
139
+ const type = properties.has('type') ?
140
+ properties.get('type') : null;
141
+ const copy = properties.has('copy') ?
142
+ properties.get('copy') : false;
143
+ const width = properties.has('width') ?
144
+ properties.get('width') : null;
145
+ const props = {
146
+ arrange,
147
+ display,
148
+ type,
149
+ width,
150
+ copy
151
+ };
152
+ this.setResult(ctx, this.getExecutor().createComponent(instanceName, pins, params, props));
153
+ };
154
+ this.visitCreate_graphic_expr = (ctx) => {
155
+ const commands = ctx.graphic_expr().reduce((accum, item) => {
156
+ this.visit(item);
157
+ const [commandName, parameters] = this.getResult(item);
158
+ const keywordParams = new Map();
159
+ const positionParams = parameters.reduce((accum, [argType, name, value]) => {
160
+ if (argType === 'position') {
161
+ accum.push(value);
162
+ }
163
+ else {
164
+ keywordParams.set(name, value);
165
+ }
166
+ return accum;
167
+ }, []);
168
+ accum.push([commandName, positionParams, keywordParams]);
322
169
  return accum;
323
170
  }, []);
324
- accum.push([commandName, positionParams, keywordParams]);
325
- return accum;
326
- }, []);
327
- return new draw_symbols_js_1.SymbolDrawingCommands(commands);
328
- }
329
- visitSub_expr(ctx) {
330
- let commandName = null;
331
- if (ctx.ID()) {
332
- commandName = ctx.ID().getText();
333
- }
334
- else if (ctx.Pin()) {
335
- commandName = ctx.Pin().getText();
336
- }
337
- else {
338
- throw "Invalid command!";
339
- }
340
- const parameters = this.visit(ctx.parameters());
341
- return [commandName, parameters];
342
- }
343
- visitProperty_expr(ctx) {
344
- const keyName = this.visit(ctx.property_key_expr());
345
- const value = this.visit(ctx.property_value_expr());
346
- const map = new Map();
347
- map.set(keyName, value);
348
- return map;
349
- }
350
- visitSingle_line_property(ctx) {
351
- let value;
352
- if (ctx.data_expr_list().length === 1) {
353
- value = this.visit(ctx.data_expr(0));
354
- }
355
- else {
356
- value = ctx.data_expr_list().map(item => {
357
- return this.visit(item);
358
- });
359
- }
360
- return value;
361
- }
362
- visitNested_properties(ctx) {
363
- const result = new Map();
364
- ctx.property_expr_list().forEach((item) => {
365
- const property = this.visit(item);
366
- for (const [key, value] of property) {
367
- result.set(key, value);
171
+ const drawing = new draw_symbols_js_1.SymbolDrawingCommands(commands);
172
+ drawing.source = ctx.getText();
173
+ this.setResult(ctx, drawing);
174
+ };
175
+ this.visitGraphic_expr = (ctx) => {
176
+ let commandName = null;
177
+ const command = ctx._command;
178
+ if (command) {
179
+ commandName = command.text;
368
180
  }
369
- });
370
- return result;
371
- }
372
- visitProperty_key_expr(ctx) {
373
- if (ctx.ID()) {
374
- return ctx.ID().getText();
375
- }
376
- else if (ctx.INTEGER_VALUE()) {
377
- return Number(ctx.INTEGER_VALUE().getText());
378
- }
379
- else if (ctx.STRING_VALUE()) {
380
- return this.prepareStringValue(ctx.STRING_VALUE().getText());
381
- }
382
- }
383
- visitData_expr_with_assignment(ctx) {
384
- let component;
385
- if (ctx.data_expr()) {
386
- component = this.visit(ctx.data_expr());
387
- if (component === null || component === undefined) {
388
- throw "Could not find component: " + ctx.data_expr().getText();
181
+ else {
182
+ throw "Invalid command!";
389
183
  }
390
- }
391
- else if (ctx.assignment_expr()) {
392
- component = this.visit(ctx.assignment_expr());
393
- }
394
- let pinValue = null;
395
- if (ctx.pin_select_expr()) {
396
- pinValue = this.visit(ctx.pin_select_expr());
397
- }
398
- else {
399
- pinValue = component.getDefaultPin();
400
- }
401
- return [component, pinValue];
402
- }
403
- visitValueAtomExpr(ctx) {
404
- let value;
405
- if (ctx.value_expr()) {
406
- value = this.visit(ctx.value_expr());
407
- }
408
- else if (ctx.atom_expr()) {
409
- const reference = this.visit(ctx.atom_expr());
410
- if (!reference.found) {
411
- value = new types_js_1.UndeclaredReference(reference);
184
+ const ctxParameters = ctx.parameters();
185
+ this.visit(ctxParameters);
186
+ const parameters = this.getResult(ctxParameters);
187
+ this.setResult(ctx, [commandName, parameters]);
188
+ };
189
+ this.visitProperty_expr = (ctx) => {
190
+ const ctxPropertyKeyExpr = ctx.property_key_expr();
191
+ const ctxPropertyValueExpr = ctx.property_value_expr();
192
+ this.visit(ctxPropertyKeyExpr);
193
+ this.visit(ctxPropertyValueExpr);
194
+ const keyName = this.getResult(ctxPropertyKeyExpr);
195
+ const value = this.getResult(ctxPropertyValueExpr);
196
+ const map = new Map();
197
+ map.set(keyName, value);
198
+ this.setResult(ctx, map);
199
+ };
200
+ this.visitSingle_line_property = (ctx) => {
201
+ let value;
202
+ if (ctx.data_expr().length === 1) {
203
+ const ctxFirst = ctx.data_expr(0);
204
+ this.visit(ctxFirst);
205
+ value = this.getResult(ctxFirst);
412
206
  }
413
207
  else {
414
- value = reference.value;
208
+ value = ctx.data_expr().map(item => {
209
+ this.visit(item);
210
+ return this.getResult(item);
211
+ });
415
212
  }
416
- }
417
- return value;
418
- }
419
- visitUnaryOperatorExpr(ctx) {
420
- const value = this.visit(ctx.data_expr());
421
- const unaryOp = ctx.unary_operator();
422
- if (unaryOp) {
423
- if (unaryOp.Not()) {
424
- if (typeof value === "boolean") {
425
- value = !value;
426
- }
427
- else {
428
- throw "Failed to do Not operator";
213
+ this.setResult(ctx, value);
214
+ };
215
+ this.visitNested_properties = (ctx) => {
216
+ const result = new Map();
217
+ ctx.property_expr().forEach((item) => {
218
+ this.visit(item);
219
+ const property = this.getResult(item);
220
+ for (const [key, value] of property) {
221
+ result.set(key, value);
429
222
  }
223
+ });
224
+ this.setResult(ctx, result);
225
+ };
226
+ this.visitProperty_key_expr = (ctx) => {
227
+ const ctxID = ctx.ID();
228
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
229
+ const ctxStringValue = ctx.STRING_VALUE();
230
+ let result = null;
231
+ if (ctxID) {
232
+ result = ctxID.getText();
430
233
  }
431
- else if (unaryOp.Minus()) {
432
- if (typeof value === 'number') {
433
- return -value;
434
- }
435
- else {
436
- throw "Failed to do Negation operator";
234
+ else if (ctxIntegerValue) {
235
+ result = Number(ctxIntegerValue.getText());
236
+ }
237
+ else if (ctxStringValue) {
238
+ result = this.prepareStringValue(ctxStringValue.getText());
239
+ }
240
+ this.setResult(ctx, result);
241
+ };
242
+ this.visitData_expr_with_assignment = (ctx) => {
243
+ let component = null;
244
+ const ctxDataExpr = ctx.data_expr();
245
+ const ctxAssignmentExpr = ctx.assignment_expr();
246
+ if (ctxDataExpr) {
247
+ this.visit(ctxDataExpr);
248
+ component = this.getResult(ctxDataExpr);
249
+ if (component === null || component === undefined) {
250
+ throw "Could not find component: " + ctxDataExpr.getText();
437
251
  }
438
252
  }
439
- }
440
- return value;
441
- }
442
- visitDataExpr(ctx) {
443
- let value;
444
- if (ctx.create_component_expr()) {
445
- value = this.visit(ctx.create_component_expr());
446
- }
447
- else if (ctx.create_graphic_expr()) {
448
- value = this.visit(ctx.create_graphic_expr());
449
- }
450
- return value;
451
- }
452
- visitBinaryOperatorExpr(ctx) {
453
- const value1 = this.visit(ctx.data_expr(0));
454
- const value2 = this.visit(ctx.data_expr(1));
455
- const binaryOperatorType = ctx.binary_operator();
456
- if (binaryOperatorType.Equals()) {
457
- return value1 == value2;
458
- }
459
- else if (binaryOperatorType.NotEquals()) {
460
- return value1 != value2;
461
- }
462
- }
463
- visitMultiplyExpr(ctx) {
464
- const value1 = this.visit(ctx.data_expr(0));
465
- const value2 = this.visit(ctx.data_expr(1));
466
- if (ctx.Multiply()) {
467
- return value1 * value2;
468
- }
469
- else if (ctx.Divide()) {
470
- return value1 / value2;
471
- }
472
- }
473
- visitAdditionExpr(ctx) {
474
- const value1 = this.visit(ctx.data_expr(0));
475
- const value2 = this.visit(ctx.data_expr(1));
476
- if (ctx.Addition()) {
477
- return value1 + value2;
478
- }
479
- else if (ctx.Minus()) {
480
- return value1 - value2;
481
- }
482
- }
483
- visitFunction_args_expr(ctx) {
484
- const defaultValuesProvided = ctx.value_expr_list();
485
- const IDs = ctx.ID_list();
486
- const boundary = IDs.length - defaultValuesProvided.length;
487
- return IDs.map((id, index) => {
488
- if (index >= boundary) {
489
- const defaultValue = this.visit(defaultValuesProvided[index - boundary]);
490
- return [id.getText(), defaultValue];
253
+ else if (ctxAssignmentExpr) {
254
+ this.visit(ctxAssignmentExpr);
255
+ component = this.getResult(ctxAssignmentExpr);
256
+ }
257
+ if (component instanceof ClassComponent_js_1.ClassComponent
258
+ && component.copyProp) {
259
+ component = this.getExecutor().copyComponent(component);
260
+ }
261
+ if (component && component instanceof ClassComponent_js_1.ClassComponent) {
262
+ const modifiers = ctx.component_modifier_expr();
263
+ modifiers.forEach(modifier => {
264
+ const modifierText = modifier.ID(0).getText();
265
+ const ctxValueExpr = modifier.value_expr();
266
+ const ctxID2 = modifier.ID(1);
267
+ let result = null;
268
+ if (ctxValueExpr) {
269
+ this.visit(ctxValueExpr);
270
+ result = this.getResult(ctxValueExpr);
271
+ }
272
+ else if (ctxID2) {
273
+ result = ctxID2.getText();
274
+ }
275
+ if (modifierText === 'flip') {
276
+ const flipValue = result;
277
+ if (flipValue.indexOf('x') !== -1) {
278
+ component.setParam('flipX', 1);
279
+ }
280
+ if (flipValue.indexOf('y') !== -1) {
281
+ component.setParam('flipY', 1);
282
+ }
283
+ }
284
+ else if (modifierText === 'angle') {
285
+ const angleValue = Number(result);
286
+ component.setParam('angle', angleValue);
287
+ }
288
+ });
491
289
  }
492
- else {
493
- return [id.getText()];
290
+ let pinValue = null;
291
+ const ctxPinSelectExpr = ctx.pin_select_expr();
292
+ if (ctxPinSelectExpr) {
293
+ this.visit(ctxPinSelectExpr);
294
+ pinValue = this.getResult(ctxPinSelectExpr);
494
295
  }
495
- });
496
- }
497
- createNetResolver(executionStack) {
498
- const resolveNet = (netName, netNamespace) => {
499
- this.print('find net', netNamespace, netName);
500
- const reversed = [...executionStack].reverse();
501
- for (let i = 0; i < reversed.length; i++) {
502
- const context = reversed[i];
503
- const net = context.scope.getNetWithName(netName);
504
- if (net !== null && net.namespace === netNamespace) {
505
- return {
506
- found: true,
507
- net,
508
- };
509
- }
296
+ else {
297
+ pinValue = component.getDefaultPin();
510
298
  }
511
- return {
512
- found: false
513
- };
299
+ this.setResult(ctx, [component, pinValue]);
514
300
  };
515
- return resolveNet;
516
- }
517
- visitFunction_def_expr(ctx) {
518
- const functionName = ctx.ID().getText();
519
- let funcDefinedParameters = [];
520
- if (ctx.function_args_expr()) {
521
- funcDefinedParameters = this.visit(ctx.function_args_expr());
522
- }
523
- const executionStack = this.executionStack;
524
- const functionCounter = { counter: 0 };
525
- const resolveNet = this.createNetResolver(this.executionStack);
526
- const __runFunc = (passedInParameters, options) => {
527
- const { netNamespace = "" } = options;
528
- const currentExecutionContext = executionStack[executionStack.length - 1];
529
- const executionLevel = currentExecutionContext.executionLevel;
530
- const executionContextName = functionName + '_' + functionCounter['counter'];
531
- const executionContextNamespace = currentExecutionContext.namespace
532
- + executionContextName + ".";
533
- functionCounter['counter'] += 1;
534
- const newExecutor = new execute_js_1.ExecutionContext(executionContextName, executionContextNamespace, netNamespace, executionLevel + 1, this.getExecutor().scope.indentLevel + 1, currentExecutionContext.silent, currentExecutionContext.logger);
535
- this.setupPrintFunction(newExecutor);
536
- newExecutor.resolveNet = resolveNet;
537
- executionStack.push(newExecutor);
538
- this.setupDefinedParameters(functionName, funcDefinedParameters, passedInParameters, newExecutor);
539
- const returnValue = this.runExpressions(newExecutor, ctx.function_expr_list());
540
- const lastExecution = executionStack.pop();
541
- const nextLastExecution = executionStack[executionStack.length - 1];
542
- nextLastExecution.mergeScope(lastExecution.scope, executionContextName);
543
- return [lastExecution, returnValue];
544
- };
545
- this.getExecutor().createFunction(functionName, __runFunc);
546
- }
547
- setupDefinedParameters(functionName, funcDefinedParameters, passedInParameters, executor) {
548
- for (let i = 0; i < funcDefinedParameters.length; i++) {
549
- const tmpFuncArg = funcDefinedParameters[i];
550
- if (i < passedInParameters.length) {
551
- const tmpPassedInArgs = passedInParameters[i];
552
- if (tmpPassedInArgs[0] === 'position') {
553
- const variableName = tmpFuncArg[0];
554
- executor.print('set variable in scope, var name: ', variableName);
555
- executor.scope.variables.set(variableName, tmpPassedInArgs[2]);
556
- if (tmpPassedInArgs[2] instanceof ClassComponent_js_1.ClassComponent) {
557
- const component = tmpPassedInArgs[2];
558
- for (const [pinNumber, net] of component.pinNets) {
559
- executor.scope.setNet(component, pinNumber, net);
560
- }
301
+ this.visitUnaryOperatorExpr = (ctx) => {
302
+ this.visit(ctx.data_expr());
303
+ let value = this.getResult(ctx.data_expr());
304
+ const unaryOp = ctx.unary_operator();
305
+ if (unaryOp) {
306
+ if (unaryOp.Not()) {
307
+ if (typeof value === "boolean") {
308
+ value = !value;
309
+ }
310
+ else {
311
+ throw "Failed to do Not operator";
312
+ }
313
+ }
314
+ else if (unaryOp.Minus()) {
315
+ if (typeof value === 'number') {
316
+ value = -value;
317
+ }
318
+ else {
319
+ throw "Failed to do Negation operator";
561
320
  }
562
321
  }
563
322
  }
564
- else if (tmpFuncArg.length === 2) {
565
- const variableName = tmpFuncArg[0];
566
- const defaultValue = tmpFuncArg[1];
567
- executor.print('set variable in scope, var name: ', variableName);
568
- executor.scope.variables.set(variableName, defaultValue);
323
+ this.setResult(ctx, value);
324
+ };
325
+ this.visitDataExpr = (ctx) => {
326
+ let value;
327
+ const ctxCreateComponentExpr = ctx.create_component_expr();
328
+ const ctxCreateGraphicExpr = ctx.create_graphic_expr();
329
+ if (ctxCreateComponentExpr) {
330
+ this.visit(ctxCreateComponentExpr);
331
+ value = this.getResult(ctxCreateComponentExpr);
332
+ }
333
+ else if (ctxCreateGraphicExpr) {
334
+ this.visit(ctxCreateGraphicExpr);
335
+ value = this.getResult(ctxCreateGraphicExpr);
569
336
  }
570
337
  else {
571
- throw `Invalid arguments for function '${functionName}', got: `
572
- + passedInParameters;
338
+ throw "Invalid data expression";
573
339
  }
574
- }
575
- }
576
- visitFunction_return_expr(ctx) {
577
- const executor = this.getExecutor();
578
- executor.print('return from function');
579
- const returnValue = this.visit(ctx.data_expr());
580
- executor.stopFurtherExpressions = true;
581
- executor.returnValue = returnValue;
582
- return returnValue;
583
- }
584
- visitAtom_expr(ctx) {
585
- const executor = this.getExecutor();
586
- const atomId = ctx.ID().getText();
587
- let passedNetNamespace = null;
588
- if (ctx.net_namespace_expr()) {
589
- passedNetNamespace = this.visit(ctx.net_namespace_expr());
590
- }
591
- let currentReference;
592
- if (this.pinTypesList.indexOf(atomId) !== -1) {
593
- currentReference = {
594
- found: true,
595
- value: atomId
340
+ this.setResult(ctx, value);
341
+ };
342
+ this.visitBinaryOperatorExpr = (ctx) => {
343
+ const ctx0 = ctx.data_expr(0);
344
+ const ctx1 = ctx.data_expr(1);
345
+ this.visit(ctx0);
346
+ this.visit(ctx1);
347
+ const value1 = this.getResult(ctx0);
348
+ const value2 = this.getResult(ctx1);
349
+ const binaryOperatorType = ctx.binary_operator();
350
+ let result = null;
351
+ if (binaryOperatorType.Equals()) {
352
+ result = value1 == value2;
353
+ }
354
+ else if (binaryOperatorType.NotEquals()) {
355
+ result = value1 != value2;
356
+ }
357
+ else if (binaryOperatorType.GreaterThan()) {
358
+ result = value1 > value2;
359
+ }
360
+ else if (binaryOperatorType.GreatOrEqualThan()) {
361
+ result = value1 >= value2;
362
+ }
363
+ else if (binaryOperatorType.LessThan()) {
364
+ result = value1 < value2;
365
+ }
366
+ else if (binaryOperatorType.LessOrEqualThan()) {
367
+ result = value1 <= value2;
368
+ }
369
+ this.setResult(ctx, result);
370
+ };
371
+ this.visitLogicalOperatorExpr = (ctx) => {
372
+ const ctx0 = ctx.data_expr(0);
373
+ const ctx1 = ctx.data_expr(1);
374
+ this.visit(ctx0);
375
+ const value1 = this.getResult(ctx0);
376
+ let value2 = false;
377
+ let skipNext = false;
378
+ if (ctx.LogicalOr() && value1) {
379
+ skipNext = true;
380
+ }
381
+ if (!skipNext) {
382
+ this.visit(ctx1);
383
+ value2 = this.getResult(ctx1);
384
+ }
385
+ let result = null;
386
+ if (ctx.LogicalAnd()) {
387
+ result = value1 && value2;
388
+ }
389
+ else if (ctx.LogicalOr()) {
390
+ result = value1 || value2;
391
+ }
392
+ this.setResult(ctx, result);
393
+ };
394
+ this.visitMultiplyExpr = (ctx) => {
395
+ this.visit(ctx.data_expr(0));
396
+ this.visit(ctx.data_expr(1));
397
+ const value1 = this.getResult(ctx.data_expr(0));
398
+ const value2 = this.getResult(ctx.data_expr(1));
399
+ let result = null;
400
+ if (ctx.Multiply()) {
401
+ result = value1 * value2;
402
+ }
403
+ else if (ctx.Divide()) {
404
+ result = value1 / value2;
405
+ }
406
+ this.setResult(ctx, result);
407
+ };
408
+ this.visitAdditionExpr = (ctx) => {
409
+ this.visit(ctx.data_expr(0));
410
+ this.visit(ctx.data_expr(1));
411
+ const value1 = this.getResult(ctx.data_expr(0));
412
+ const value2 = this.getResult(ctx.data_expr(1));
413
+ let result = null;
414
+ if (ctx.Addition()) {
415
+ result = value1 + value2;
416
+ }
417
+ else if (ctx.Minus()) {
418
+ result = value1 - value2;
419
+ }
420
+ this.setResult(ctx, result);
421
+ };
422
+ this.visitFunction_def_expr = (ctx) => {
423
+ const functionName = ctx.ID().getText();
424
+ let funcDefinedParameters = [];
425
+ const ctxFunctionArgsExpr = ctx.function_args_expr();
426
+ if (ctxFunctionArgsExpr) {
427
+ this.visit(ctxFunctionArgsExpr);
428
+ funcDefinedParameters = this.getResult(ctxFunctionArgsExpr);
429
+ }
430
+ const executionStack = this.executionStack;
431
+ const functionCounter = { counter: 0 };
432
+ const resolveNet = this.createNetResolver(this.executionStack);
433
+ const __runFunc = (passedInParameters, options) => {
434
+ const executionContextName = functionName + '_' + functionCounter['counter'];
435
+ const newExecutor = this.enterNewChildContext(executionStack, this.getExecutor(), executionContextName, options, funcDefinedParameters, passedInParameters);
436
+ functionCounter['counter'] += 1;
437
+ newExecutor.resolveNet = resolveNet;
438
+ const returnValue = this.runExpressions(newExecutor, ctx.function_expr());
439
+ const lastExecution = executionStack.pop();
440
+ const nextLastExecution = executionStack[executionStack.length - 1];
441
+ nextLastExecution.mergeScope(lastExecution.scope, executionContextName);
442
+ return [lastExecution, returnValue];
596
443
  };
597
- }
598
- else {
599
- currentReference = executor.resolveVariable(this.executionStack, atomId);
600
- }
601
- if (currentReference.found && currentReference.type === 'instance') {
602
- const tmpComponent = currentReference.value;
603
- for (const [pinId, net] of tmpComponent.pinNets) {
604
- executor.scope.setNet(tmpComponent, pinId, net);
444
+ this.getExecutor().createFunction(functionName, __runFunc);
445
+ };
446
+ this.visitPin_select_expr2 = (ctx) => {
447
+ const ctxStringValue = ctx.STRING_VALUE();
448
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
449
+ let result = null;
450
+ if (ctxStringValue) {
451
+ result = this.prepareStringValue(ctxStringValue.getText());
605
452
  }
606
- }
607
- if (ctx.trailer_expr_list().length > 0) {
608
- if (!currentReference.found) {
609
- throw "Could not find reference! " + atomId;
610
- }
611
- currentReference.trailers = [];
612
- ctx.trailer_expr_list().forEach(item => {
613
- const itemValue = item.getText();
614
- if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
615
- let parameters = [];
616
- if (item.parameters()) {
617
- parameters = this.visit(item.parameters());
618
- }
619
- const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
620
- const [, functionResult] = executor.callFunction(currentReference.name, parameters, this.executionStack, useNetNamespace);
621
- currentReference = {
622
- found: true,
623
- value: functionResult,
624
- type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
625
- 'instance' : 'value',
626
- };
453
+ else if (ctxIntegerValue) {
454
+ result = Number(ctxIntegerValue.getText());
455
+ }
456
+ this.setResult(ctx, result);
457
+ };
458
+ this.visitAt_block_pin_expr = (ctx) => {
459
+ const ctxPinSelectExpr2 = ctx.pin_select_expr2();
460
+ this.visit(ctxPinSelectExpr2);
461
+ const atPin = this.getResult(ctxPinSelectExpr2);
462
+ const executor = this.getExecutor();
463
+ const currentComponent = executor.scope.currentComponent;
464
+ const currentPin = executor.scope.currentPin;
465
+ executor.atComponent(currentComponent, atPin, {
466
+ addSequence: true
467
+ });
468
+ executor.log('at block pin expressions');
469
+ const ctxAtBlockSimple = ctx.at_block_pin_expression_simple();
470
+ const ctxAtBlockComplex = ctx.at_block_pin_expression_complex();
471
+ if (ctxAtBlockSimple) {
472
+ this.visit(ctxAtBlockSimple);
473
+ }
474
+ else if (ctxAtBlockComplex) {
475
+ this.visit(ctxAtBlockComplex);
476
+ }
477
+ executor.log('end at block pin expressions');
478
+ executor.atComponent(currentComponent, currentPin);
479
+ };
480
+ this.visitAt_block = (ctx) => {
481
+ const executor = this.getExecutor();
482
+ executor.log('entering at block');
483
+ this.visit(ctx.at_component_expr());
484
+ const currentComponent = executor.scope.currentComponent;
485
+ const currentPin = executor.scope.currentPin;
486
+ executor.scope.indentLevel += 1;
487
+ ctx.at_block_expressions().forEach(expression => {
488
+ this.visit(expression);
489
+ });
490
+ executor.scope.indentLevel -= 1;
491
+ executor.scope.currentComponent = currentComponent;
492
+ executor.scope.currentPin = currentPin;
493
+ executor.log('leaving at block');
494
+ };
495
+ this.visitAt_block_pin_expression_simple = (ctx) => {
496
+ const ctxExpression = ctx.expression();
497
+ if (ctxExpression) {
498
+ this.visit(ctxExpression);
499
+ }
500
+ else if (ctx.NOT_CONNECTED()) {
501
+ return;
502
+ }
503
+ };
504
+ this.visitAt_block_pin_expression_complex = (ctx) => {
505
+ ctx.expression().forEach(item => {
506
+ this.visit(item);
507
+ });
508
+ };
509
+ this.visitWire_expr_direction_only = (ctx) => {
510
+ const value = ctx.ID().getText();
511
+ if (value === 'auto' || value === 'auto_') {
512
+ this.setResult(ctx, [value]);
513
+ }
514
+ else {
515
+ throw 'Invalid direction for wire';
516
+ }
517
+ };
518
+ this.visitWire_expr_direction_value = (ctx) => {
519
+ const direction = ctx.ID().getText();
520
+ if (this.acceptedDirections.indexOf(direction) !== -1) {
521
+ let useValue = null;
522
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
523
+ const ctxDataExpr = ctx.data_expr();
524
+ if (ctxIntegerValue) {
525
+ useValue = Number(ctxIntegerValue);
627
526
  }
628
- else {
629
- currentReference.trailers.push(itemValue);
527
+ else if (ctxDataExpr) {
528
+ this.visit(ctxDataExpr);
529
+ useValue = this.getResult(ctxDataExpr);
530
+ }
531
+ if (useValue !== null) {
532
+ this.setResult(ctx, [direction, useValue]);
533
+ return;
630
534
  }
535
+ }
536
+ throw "Invalid direction or value for wire";
537
+ };
538
+ this.visitWire_expr = (ctx) => {
539
+ const wireAtomExpr = ctx.wire_atom_expr();
540
+ const segments = wireAtomExpr.map(wireSegment => {
541
+ this.visit(wireSegment);
542
+ return this.getResult(wireSegment);
631
543
  });
632
- }
633
- return currentReference;
634
- }
635
- visitPin_select_expr2(ctx) {
636
- if (ctx.STRING_VALUE()) {
637
- return this.prepareStringValue(ctx.STRING_VALUE().getText());
638
- }
639
- else if (ctx.INTEGER_VALUE()) {
640
- return Number(ctx.INTEGER_VALUE().getText());
641
- }
642
- }
643
- visitAt_block_pin_expr(ctx) {
644
- const atPin = this.visit(ctx.pin_select_expr2());
645
- const executor = this.getExecutor();
646
- const currentComponent = executor.scope.currentComponent;
647
- const currentPin = executor.scope.currentPin;
648
- executor.atComponent(currentComponent, atPin, {
649
- addSequence: true
650
- });
651
- executor.print('at block pin expressions');
652
- if (ctx.at_block_pin_expression_simple()) {
653
- this.visit(ctx.at_block_pin_expression_simple());
654
- }
655
- else if (ctx.at_block_pin_expression_complex()) {
656
- this.visit(ctx.at_block_pin_expression_complex());
657
- }
658
- executor.print('end at block pin expressions');
659
- return executor.atComponent(currentComponent, currentPin);
660
- }
661
- visitAt_block(ctx) {
662
- const executor = this.getExecutor();
663
- executor.print('entering at block');
664
- this.visit(ctx.at_component_expr());
665
- const currentComponent = executor.scope.currentComponent;
666
- const currentPin = executor.scope.currentPin;
667
- executor.scope.indentLevel += 1;
668
- ctx.at_block_expressions_list().forEach(expression => {
669
- this.visit(expression);
670
- });
671
- executor.scope.indentLevel -= 1;
672
- executor.scope.currentComponent = currentComponent;
673
- executor.scope.currentPin = currentPin;
674
- executor.print('leaving at block');
675
- return executor.getCurrentPoint();
676
- }
677
- visitAt_block_pin_expression_simple(ctx) {
678
- if (ctx.expression()) {
679
- this.visit(ctx.expression());
680
- }
681
- else if (ctx.NOT_CONNECTED()) {
682
- return;
683
- }
684
- }
685
- visitAt_block_pin_expression_complex(ctx) {
686
- ctx.expression_list().forEach(item => {
687
- this.visit(item);
688
- });
689
- return this.getExecutor().getCurrentPoint();
690
- }
691
- visitWire_expr(ctx) {
692
- const segments = [];
693
- const parts = ctx.children.slice(1);
694
- for (let i = 0; i < parts.length; i++) {
695
- const textValue = parts[i].getText();
696
- if (this.acceptedDirections.indexOf(textValue) !== -1) {
697
- const segment = [textValue];
698
- let skipNext = false;
699
- let invalidValue = true;
700
- if (i + 1 < parts.length) {
701
- const nextValue = parts[i + 1].getText();
702
- const nextValueNumber = Number(nextValue);
703
- if (!isNaN(nextValueNumber)) {
704
- invalidValue = false;
705
- segment.push(nextValueNumber);
706
- skipNext = true;
707
- }
544
+ this.getExecutor().addWire(segments);
545
+ };
546
+ this.visitPoint_expr = (ctx) => {
547
+ const ID = ctx.ID();
548
+ return this.getExecutor().addPoint(ID.getText());
549
+ };
550
+ this.visitProperty_set_expr = (ctx) => {
551
+ const ctxDataExpr = ctx.data_expr();
552
+ this.visit(ctxDataExpr);
553
+ const result = this.getResult(ctxDataExpr);
554
+ const ctxAtomExpr = ctx.atom_expr();
555
+ this.visit(ctxAtomExpr);
556
+ const resolvedProperty = this.getResult(ctxAtomExpr);
557
+ this.getExecutor().setProperty(resolvedProperty, result);
558
+ };
559
+ this.visitDouble_dot_property_set_expr = (ctx) => {
560
+ const ctxDataExpr = ctx.data_expr();
561
+ this.visit(ctxDataExpr);
562
+ const result = this.getResult(ctxDataExpr);
563
+ const propertyName = ctx.ID().getText();
564
+ this.getExecutor().setProperty('..' + propertyName, result);
565
+ };
566
+ this.visitFrame_expr = (ctx) => {
567
+ const frameId = this.getExecutor().enterFrame();
568
+ this.runExpressions(this.getExecutor(), ctx.expression());
569
+ this.getExecutor().exitFrame(frameId);
570
+ };
571
+ this.visitNet_namespace_expr = (ctx) => {
572
+ let dataValue = null;
573
+ let netNamespace = null;
574
+ const hasPlus = ctx.Addition();
575
+ const ctxDataExpr = ctx.data_expr();
576
+ if (ctxDataExpr) {
577
+ this.visit(ctxDataExpr);
578
+ dataValue = this.getResult(ctxDataExpr);
579
+ if (dataValue instanceof types_js_1.UndeclaredReference) {
580
+ netNamespace = "/" + dataValue.reference.name;
708
581
  }
709
- if (invalidValue) {
710
- throw `Invalid value provided for direction ${textValue} in wire`;
582
+ else if (typeof dataValue === "string") {
583
+ netNamespace = "/" + dataValue;
711
584
  }
712
- segments.push(segment);
713
- if (skipNext) {
714
- i += 1;
585
+ else {
586
+ throw "Failed to resolve net namespace value";
715
587
  }
716
588
  }
717
- else if (textValue === "auto" || textValue === "auto_") {
718
- segments.push([textValue]);
589
+ else {
590
+ netNamespace = "/";
719
591
  }
720
- }
721
- this.getExecutor().addWire(segments);
722
- }
723
- visitPoint_expr(ctx) {
724
- const ID = ctx.ID();
725
- return this.getExecutor().addPoint(ID.getText());
726
- }
727
- visitImport_expr(ctx) {
728
- const ID = ctx.ID().toString();
729
- this.print('import', ID);
730
- const { hasError, hasParseError } = this.onImportFile(this, ID);
731
- if (hasError || hasParseError) {
732
- this.print('import', ID, 'failed');
733
- throw `import ${ID} failed`;
734
- }
735
- this.print('done import', ID);
736
- }
737
- visitProperty_set_expr(ctx) {
738
- const result = this.visit(ctx.data_expr());
739
- const resolvedProperty = this.visit(ctx.atom_expr());
740
- this.getExecutor().setProperty(resolvedProperty, result);
741
- }
742
- visitDouble_dot_property_set_expr(ctx) {
743
- const result = this.visit(ctx.data_expr());
744
- const propertyName = ctx.ID().getText();
745
- this.getExecutor().setProperty('..' + propertyName, result);
746
- }
747
- visitRoundedBracketsExpr(ctx) {
748
- return this.visit(ctx.data_expr());
749
- }
750
- visitFrame_expr(ctx) {
751
- const frameId = this.getExecutor().enterFrame();
752
- this.runExpressions(this.getExecutor(), ctx.expression_list());
753
- this.getExecutor().exitFrame(frameId);
754
- }
755
- visitNet_namespace_expr(ctx) {
756
- let dataValue = null;
757
- let netNamespace = null;
758
- const hasPlus = ctx.Addition();
759
- if (ctx.data_expr()) {
760
- dataValue = this.visit(ctx.data_expr());
761
- if (dataValue instanceof types_js_1.UndeclaredReference) {
762
- netNamespace = "/" + dataValue.reference.name;
763
- }
764
- else if (typeof dataValue === "string") {
765
- netNamespace = "/" + dataValue;
592
+ this.setResult(ctx, (hasPlus ? "+" : "") + netNamespace);
593
+ };
594
+ this.visitIf_expr = (ctx) => {
595
+ const ctxDataExpr = ctx.data_expr();
596
+ this.visit(ctxDataExpr);
597
+ const result = this.getResult(ctxDataExpr);
598
+ if (result) {
599
+ this.runExpressions(this.getExecutor(), ctx.expression());
766
600
  }
767
601
  else {
768
- throw "Failed to resolve net namespace value";
602
+ const ctxInnerIfExprs = ctx.if_inner_expr();
603
+ let innerIfWasTrue = false;
604
+ for (let i = 0; i < ctxInnerIfExprs.length; i++) {
605
+ const tmpCtx = ctxInnerIfExprs[i];
606
+ this.visit(tmpCtx);
607
+ const innerResult = this.getResult(tmpCtx);
608
+ if (innerResult) {
609
+ innerIfWasTrue = true;
610
+ break;
611
+ }
612
+ }
613
+ if (!innerIfWasTrue) {
614
+ const elseCtx = ctx.else_expr();
615
+ if (elseCtx) {
616
+ this.visit(elseCtx);
617
+ }
618
+ }
769
619
  }
770
- }
771
- else {
772
- netNamespace = "/";
773
- }
774
- return (hasPlus ? "+" : "") + netNamespace;
775
- }
776
- createImportFileHandler(directory, defaultLibsPath) {
777
- return (visitor, importPath) => {
778
- let importResult;
779
- importResult = this.importLib(visitor, directory, importPath);
780
- if (!importResult.pathExists && importPath == 'lib') {
781
- importResult = this.importLib(visitor, defaultLibsPath, importPath);
782
- }
783
- return importResult;
784
620
  };
785
- }
786
- importLib(visitor, directory, filename) {
787
- const tmpFilePath = (0, path_1.join)(directory, filename + ".cst");
788
- visitor.print('importing path:', tmpFilePath);
789
- let pathExists = false;
790
- let fileData = null;
791
- try {
792
- fileData = (0, fs_1.readFileSync)(tmpFilePath, { encoding: 'utf8' });
793
- pathExists = true;
794
- }
795
- catch (err) {
796
- pathExists = false;
797
- }
798
- try {
799
- if (pathExists) {
800
- visitor.print('done reading imported file data');
801
- const { hasError, hasParseError } = (0, parser_js_1.parseFileWithVisitor)(visitor, fileData);
802
- return { hasError, hasParseError, pathExists };
803
- }
804
- }
805
- catch (err) {
806
- visitor.print('Failed to import file: ', err.message);
807
- }
808
- return {
809
- hasError: true,
810
- hasParseError: true,
811
- pathExists,
621
+ this.visitIf_inner_expr = (ctx) => {
622
+ const ctxDataExpr = ctx.data_expr();
623
+ this.visit(ctxDataExpr);
624
+ const result = this.getResult(ctxDataExpr);
625
+ if (result) {
626
+ this.runExpressions(this.getExecutor(), ctx.expression());
627
+ }
628
+ this.setResult(ctx, result);
812
629
  };
630
+ this.pinTypes = [
631
+ PinTypes_js_1.PinTypes.Any,
632
+ PinTypes_js_1.PinTypes.IO,
633
+ PinTypes_js_1.PinTypes.Input,
634
+ PinTypes_js_1.PinTypes.Output,
635
+ PinTypes_js_1.PinTypes.Power,
636
+ ];
813
637
  }
814
638
  parseCreateComponentPins(pinData) {
815
639
  const pins = [];
@@ -831,8 +655,10 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
831
655
  }
832
656
  if (Array.isArray(pinDef)) {
833
657
  const firstValue = pinDef[0];
834
- if (this.pinTypes.indexOf(firstValue) !== -1) {
835
- pinType = firstValue;
658
+ if (firstValue.type
659
+ && firstValue.type === globals_js_1.ReferenceTypes.pinType
660
+ && this.pinTypes.indexOf(firstValue.value) !== -1) {
661
+ pinType = firstValue.value;
836
662
  pinName = pinDef[1];
837
663
  if (pinDef.length > 2) {
838
664
  altPinNames = pinDef.slice(2);
@@ -862,18 +688,6 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
862
688
  }
863
689
  return result;
864
690
  }
865
- prepareStringValue(value) {
866
- return value.slice(1, value.length - 1);
867
- }
868
- print(...params) {
869
- const indentOutput = ''.padStart(this.indentLevel * 4, ' ');
870
- const indentLevelText = this.indentLevel.toString().padStart(3, ' ');
871
- const args = ['[' + indentLevelText + ']', indentOutput, ...params];
872
- this.logger.add(args.join(' '));
873
- if (!this.silent) {
874
- console.log.apply(null, args);
875
- }
876
- }
877
691
  printNets() {
878
692
  this.getExecutor().scope.printNets();
879
693
  }
@@ -935,7 +749,10 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
935
749
  }
936
750
  getGraph() {
937
751
  const executor = this.getExecutor();
938
- const sequence = executor.scope.sequence;
752
+ const fullSequence = executor.scope.sequence;
753
+ const tmpNet = executor.scope.getNet(executor.scope.componentRoot, 1);
754
+ const sequence = (tmpNet === null)
755
+ ? fullSequence.slice(1) : fullSequence;
939
756
  const nets = executor.scope.getNets();
940
757
  return {
941
758
  sequence,
@@ -944,7 +761,7 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
944
761
  };
945
762
  }
946
763
  annotateComponents() {
947
- this.print('===== annotate components =====');
764
+ this.log('===== annotate components =====');
948
765
  const annotater = new ComponentAnnotater();
949
766
  const instances = this.getExecutor().scope.instances;
950
767
  const toAnnotate = [];
@@ -956,7 +773,7 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
956
773
  continue;
957
774
  }
958
775
  if (instance.typeProp === null) {
959
- this.print('Instance has no type:', instance.instanceName, ' assuming connector');
776
+ this.log('Instance has no type:', instance.instanceName, ' assuming connector');
960
777
  instance.typeProp = 'conn';
961
778
  }
962
779
  if (instance.parameters.has('refdes')) {
@@ -964,7 +781,7 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
964
781
  if (refdes) {
965
782
  instance.assignedRefDes = refdes;
966
783
  annotater.trackRefDes(refdes);
967
- this.print(refdes, '-', instance.instanceName);
784
+ this.log(refdes, '-', instance.instanceName);
968
785
  continue;
969
786
  }
970
787
  }
@@ -975,14 +792,14 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
975
792
  const newRefDes = annotater.getAnnotation(instance.typeProp);
976
793
  if (newRefDes !== null) {
977
794
  instance.assignedRefDes = newRefDes;
978
- this.print(newRefDes, '-', instance.instanceName);
795
+ this.log(newRefDes, '-', instance.instanceName);
979
796
  }
980
797
  else {
981
- this.print('Failed to annotate:', instance.instanceName);
798
+ this.log('Failed to annotate:', instance.instanceName);
982
799
  }
983
800
  });
984
- this.print('===== annotate done =====');
985
- this.print('');
801
+ this.log('===== annotate done =====');
802
+ this.log('');
986
803
  }
987
804
  resolveNets(scope, instance) {
988
805
  const result = [];
@@ -1011,63 +828,24 @@ class MainVisitor extends antlr4_1.ParseTreeVisitor {
1011
828
  throw "Invalid modifier for orientation";
1012
829
  }
1013
830
  }
831
+ setComponentFlip(component, flipValue) {
832
+ if (this.acceptedFlip.indexOf(flipValue) !== -1) {
833
+ component.setParam(flipValue, 1);
834
+ }
835
+ }
1014
836
  getPropertyExprList(items) {
1015
837
  const properties = new Map();
1016
838
  items.forEach((item) => {
1017
- const result = this.visit(item);
839
+ this.visit(item);
840
+ const result = this.getResult(item);
1018
841
  for (const [key, value] of result) {
1019
842
  properties.set(key, value);
1020
843
  }
1021
844
  });
1022
845
  return properties;
1023
846
  }
1024
- runExpressions(executor, expressions) {
1025
- let returnValue = null;
1026
- for (let i = 0; i < expressions.length; i++) {
1027
- const expr = expressions[i];
1028
- this.visit(expr);
1029
- if (executor.stopFurtherExpressions) {
1030
- returnValue = executor.returnValue;
1031
- break;
1032
- }
1033
- }
1034
- return returnValue;
1035
- }
1036
- setupPrintFunction(context) {
1037
- context.createFunction('print', (params) => {
1038
- const items = params.map(([, , value]) => {
1039
- return value;
1040
- });
1041
- if (this.printToConsole) {
1042
- console.log('::', ...items);
1043
- }
1044
- this.printStream.push(...items);
1045
- return [this, null];
1046
- });
1047
- }
1048
- getNetNamespace(executorNetNamespace, passedNetNamespace) {
1049
- let result = executorNetNamespace;
1050
- if (passedNetNamespace !== null && passedNetNamespace.length > 0) {
1051
- if (passedNetNamespace === '/' || passedNetNamespace === '_') {
1052
- result = '';
1053
- }
1054
- else if (passedNetNamespace[0] === '+') {
1055
- if (executorNetNamespace === '/') {
1056
- result = passedNetNamespace.slice(1);
1057
- }
1058
- else {
1059
- result = executorNetNamespace + passedNetNamespace.slice(2);
1060
- }
1061
- }
1062
- else {
1063
- result = passedNetNamespace;
1064
- }
1065
- result = result + '/';
1066
- }
1067
- return result;
1068
- }
1069
847
  }
1070
- exports.MainVisitor = MainVisitor;
848
+ exports.ParserVisitor = ParserVisitor;
1071
849
  const ComponentRefDesPrefixes = {
1072
850
  'res': 'R',
1073
851
  'cap': 'C',