circuitscript 0.0.18 → 0.0.20

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.
@@ -2,8 +2,8 @@ import { ParseTreeVisitor } from 'antlr4';
2
2
  export default class CircuitScriptVisitor extends ParseTreeVisitor {
3
3
  visitScript;
4
4
  visitExpression;
5
- visitBranch_blocks;
6
- visitBranch_block_inner;
5
+ visitPath_blocks;
6
+ visitPath_block_inner;
7
7
  visitProperty_set_expr2;
8
8
  visitAssignment_expr2;
9
9
  visitData_expr_with_assignment;
@@ -1,4 +1,4 @@
1
- import { BranchType, ComponentTypes, GlobalNames, NoNetText, ParamKeys, ReferenceTypes } from './globals.js';
1
+ import { BlockTypes, ComponentTypes, GlobalNames, NoNetText, ParamKeys, ReferenceTypes } from './globals.js';
2
2
  import { ClassComponent } from './objects/ClassComponent.js';
3
3
  import { ActiveObject, ExecutionScope, FrameAction, SequenceAction } from './objects/ExecutionScope.js';
4
4
  import { Net } from './objects/Net.js';
@@ -11,7 +11,7 @@ export class ExecutionContext {
11
11
  netNamespace;
12
12
  executionLevel;
13
13
  scope;
14
- joinPointId = 0;
14
+ tmpPointId = 0;
15
15
  resolveNet = null;
16
16
  stopFurtherExpressions = false;
17
17
  returnValue = null;
@@ -305,24 +305,32 @@ export class ExecutionContext {
305
305
  this.print('created clone of net component:', cloneInstanceName);
306
306
  return clonedComponent;
307
307
  }
308
- enterBranches(branchType) {
309
- this.scope.branchStack.set(this.scope.indentLevel, {
308
+ enterBlocks(blockType) {
309
+ if (blockType === BlockTypes.Point) {
310
+ this.addPoint(`_point.${this.name}.${this.tmpPointId}`, false);
311
+ this.tmpPointId += 1;
312
+ }
313
+ else if (blockType === BlockTypes.Parallel) {
314
+ this.addPoint(`_parallel.${this.name}.${this.tmpPointId}`, false);
315
+ this.tmpPointId += 1;
316
+ }
317
+ this.scope.blockStack.set(this.scope.indentLevel, {
310
318
  entered_at: [
311
319
  this.scope.currentComponent,
312
320
  this.scope.currentPin,
313
321
  this.scope.currentWireId
314
322
  ],
315
- inner_branches: new Map(),
323
+ inner_blocks: new Map(),
316
324
  current_index: null,
317
- type: branchType,
325
+ type: blockType,
318
326
  });
319
- this.print('enter branches');
327
+ this.print('enter blocks');
320
328
  }
321
- exitBranches() {
322
- const stackRef = this.scope.branchStack.get(this.scope.indentLevel);
323
- const { type: branchType } = stackRef;
324
- if (branchType === BranchType.Join) {
325
- const { join_final_point: finalPoint } = stackRef;
329
+ exitBlocks() {
330
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel);
331
+ const { type: blockType } = stackRef;
332
+ if (blockType === BlockTypes.Join || blockType === BlockTypes.Parallel) {
333
+ const { final_point: finalPoint } = stackRef;
326
334
  const [component, pin, wireId] = finalPoint;
327
335
  this.scope.currentComponent = component;
328
336
  this.scope.currentPin = pin;
@@ -333,65 +341,99 @@ export class ExecutionContext {
333
341
  ]);
334
342
  }
335
343
  }
336
- this.print('exit branches');
344
+ else if (blockType === BlockTypes.Point) {
345
+ const { entered_at: [component, pin,] } = stackRef;
346
+ this.atComponent(component, pin, { addSequence: true });
347
+ }
348
+ this.print('exit blocks');
337
349
  }
338
- enterBranch(branchIndex) {
339
- const stackRef = this.scope.branchStack.get(this.scope.indentLevel);
340
- stackRef['branch_index'] = branchIndex;
341
- const { type: branchType } = stackRef;
342
- stackRef['inner_branches'].set(branchIndex, {
350
+ enterBlock(blockIndex) {
351
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel);
352
+ stackRef['block_index'] = blockIndex;
353
+ const { type: blockType } = stackRef;
354
+ stackRef['inner_blocks'].set(blockIndex, {
343
355
  last_net: null,
344
356
  ignore_last_net: false,
345
357
  });
346
- if (branchType === BranchType.Join) {
358
+ if (blockType === BlockTypes.Join || blockType === BlockTypes.Point) {
347
359
  this.scope.currentComponent = null;
348
360
  this.scope.currentPin = null;
349
361
  this.scope.currentWireId = -1;
350
362
  }
351
- this.print(`enter inner branch of type (${branchType}) >>>`);
363
+ else if (blockType === BlockTypes.Parallel) {
364
+ const { entered_at: [component, pin,] } = stackRef;
365
+ this.atComponent(component, pin, { addSequence: true });
366
+ }
367
+ this.print(`enter inner block of type (${blockType}) >>>`);
352
368
  this.scope.indentLevel += 1;
353
369
  }
354
- exitBranch(branchIndex) {
355
- const stackRef = this.scope.branchStack.get(this.scope.indentLevel - 1);
356
- const { type: branchType } = stackRef;
357
- const branchIndexRef = stackRef['inner_branches'].get(branchIndex);
358
- branchIndexRef['last_net'] = [
370
+ exitBlock(blockIndex) {
371
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel - 1);
372
+ const { type: blockType } = stackRef;
373
+ const blockIndexRef = stackRef['inner_blocks'].get(blockIndex);
374
+ blockIndexRef['last_net'] = [
359
375
  this.scope.currentComponent,
360
376
  this.scope.currentPin,
361
377
  this.scope.currentWireId
362
378
  ];
363
- stackRef['branch_index'] = null;
364
- const [preBranchComponent, preBranchPin, preBranchWireId] = stackRef['entered_at'];
379
+ stackRef['block_index'] = null;
365
380
  this.scope.indentLevel -= 1;
366
- this.print('exit inner branch <<<');
367
- if (branchType === BranchType.Branch) {
368
- this.atComponent(preBranchComponent, preBranchPin, { addSequence: true });
369
- if (preBranchWireId !== -1) {
370
- this.scope.sequence.push([SequenceAction.WireJump, preBranchWireId, 1]);
371
- }
372
- }
373
- else if (branchType === BranchType.Join) {
374
- if (branchIndex === 0) {
375
- this.addPoint(`_join.${this.name}.${this.joinPointId}`, false);
376
- this.joinPointId += 1;
377
- stackRef['join_final_point'] = [
381
+ this.print('exit inner block <<<');
382
+ if (blockType === BlockTypes.Branch) {
383
+ const { entered_at: [component, pin, wireId] } = stackRef;
384
+ this.atComponent(component, pin, { addSequence: true });
385
+ if (wireId !== -1) {
386
+ this.scope.sequence.push([SequenceAction.WireJump, wireId, 1]);
387
+ }
388
+ }
389
+ else if (blockType === BlockTypes.Join || blockType === BlockTypes.Parallel) {
390
+ if (blockIndex === 0) {
391
+ const pointIdName = (blockType === BlockTypes.Join) ? '_join' : '_parallel';
392
+ this.addPoint(`${pointIdName}.${this.name}.${this.tmpPointId}`, false);
393
+ this.tmpPointId += 1;
394
+ stackRef['final_point'] = [
378
395
  this.scope.currentComponent,
379
396
  this.scope.currentPin,
380
397
  this.scope.currentWireId
381
398
  ];
382
399
  }
383
400
  else {
384
- const { join_final_point: finalPoint } = stackRef;
385
- const [joinComponent, joinPin, joinWireId] = finalPoint;
386
- this.toComponent(joinComponent, joinPin, { addSequence: true });
401
+ const { final_point: finalPoint } = stackRef;
402
+ const [component, pin,] = finalPoint;
403
+ this.toComponent(component, pin, { addSequence: true });
387
404
  }
388
405
  }
389
406
  }
407
+ atPointBlock() {
408
+ const [component, pin,] = this.getPointBlockLocation();
409
+ this.atComponent(component, pin, {
410
+ addSequence: true
411
+ });
412
+ }
413
+ toPointBlock() {
414
+ const [component, pin,] = this.getPointBlockLocation();
415
+ this.toComponent(component, pin, {
416
+ addSequence: true
417
+ });
418
+ }
419
+ getPointBlockLocation() {
420
+ this.print('get block point');
421
+ for (let i = 0; i < this.scope.indentLevel; i++) {
422
+ const stackRef = this.scope.blockStack.get(this.scope.indentLevel - 1 - i);
423
+ const { entered_at } = stackRef;
424
+ const component = entered_at[0];
425
+ if (component.instanceName.startsWith('_point.')) {
426
+ return entered_at;
427
+ }
428
+ }
429
+ this.print('did not find block point');
430
+ return null;
431
+ }
390
432
  breakBranch() {
391
433
  this.print('break branch');
392
- const branchesInfo = this.scope.branchStack.get(this.scope.indentLevel - 1);
393
- const branchIndex = branchesInfo['branch_index'];
394
- const branchIndexRef = branchesInfo['inner_branches'].get(branchIndex);
434
+ const branchesInfo = this.scope.blockStack.get(this.scope.indentLevel - 1);
435
+ const branchIndex = branchesInfo['block_index'];
436
+ const branchIndexRef = branchesInfo['inner_blocks'].get(branchIndex);
395
437
  branchIndexRef['ignore_last_net'] = true;
396
438
  }
397
439
  createFunction(functionName, __runFunc) {
@@ -574,6 +616,9 @@ export class ExecutionContext {
574
616
  this.print('-- done merging scope --');
575
617
  }
576
618
  addWire(segments) {
619
+ if (this.scope.currentComponent === null) {
620
+ throw "No current component";
621
+ }
577
622
  const tmp = segments.map(item => {
578
623
  const [direction, value = null] = item;
579
624
  return {
@@ -48,8 +48,10 @@ export var ReferenceTypes;
48
48
  ReferenceTypes["variable"] = "variable";
49
49
  ReferenceTypes["instance"] = "instance";
50
50
  })(ReferenceTypes || (ReferenceTypes = {}));
51
- export var BranchType;
52
- (function (BranchType) {
53
- BranchType[BranchType["Branch"] = 1] = "Branch";
54
- BranchType[BranchType["Join"] = 2] = "Join";
55
- })(BranchType || (BranchType = {}));
51
+ export var BlockTypes;
52
+ (function (BlockTypes) {
53
+ BlockTypes[BlockTypes["Branch"] = 1] = "Branch";
54
+ BlockTypes[BlockTypes["Join"] = 2] = "Join";
55
+ BlockTypes[BlockTypes["Parallel"] = 3] = "Parallel";
56
+ BlockTypes[BlockTypes["Point"] = 4] = "Point";
57
+ })(BlockTypes || (BlockTypes = {}));
@@ -4,7 +4,7 @@ export class ExecutionScope {
4
4
  instances = new Map();
5
5
  functions = new Map();
6
6
  variables = new Map();
7
- branchStack = new Map();
7
+ blockStack = new Map();
8
8
  wires = [];
9
9
  frames = [];
10
10
  indentLevel = 0;
@@ -8,7 +8,7 @@ import { PinDefinition, PinIdType } from './objects/PinDefinition.js';
8
8
  import { PinTypes } from './objects/PinTypes.js';
9
9
  import { UndeclaredReference } from './objects/types.js';
10
10
  import { Logger } from './logger.js';
11
- import { BranchType, ComponentTypes, NoNetText } from './globals.js';
11
+ import { BlockTypes, ComponentTypes, NoNetText } from './globals.js';
12
12
  import { SymbolDrawingCommands } from './draw_symbols.js';
13
13
  import { parseFileWithVisitor } from './parser.js';
14
14
  export class MainVisitor extends ParseTreeVisitor {
@@ -186,26 +186,36 @@ export class MainVisitor extends ParseTreeVisitor {
186
186
  return this.getExecutor().addComponentExisting(component, pinValue);
187
187
  }
188
188
  visitAt_component_expr(ctx) {
189
- const [component, pin] = this.visit(ctx.component_select_expr());
190
- const currentPoint = this.getExecutor().atComponent(component, pin, {
191
- addSequence: true,
192
- cloneNetComponent: true
193
- });
194
- if (ctx.ID()) {
195
- this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
189
+ if (ctx.Point()) {
190
+ this.getExecutor().atPointBlock();
196
191
  }
197
- return currentPoint;
192
+ else {
193
+ const [component, pin] = this.visit(ctx.component_select_expr());
194
+ const currentPoint = this.getExecutor().atComponent(component, pin, {
195
+ addSequence: true,
196
+ cloneNetComponent: true
197
+ });
198
+ if (ctx.ID()) {
199
+ this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
200
+ }
201
+ }
202
+ return this.getExecutor().getCurrentPoint();
198
203
  }
199
204
  visitTo_component_expr(ctx) {
200
205
  let currentPoint;
201
- ctx.component_select_expr_list().forEach((item) => {
202
- const [component, pin] = this.visit(item);
203
- currentPoint = this.getExecutor().toComponent(component, pin, {
204
- addSequence: true, cloneNetComponent: true
206
+ if (ctx.Point()) {
207
+ this.getExecutor().toPointBlock();
208
+ }
209
+ else {
210
+ ctx.component_select_expr_list().forEach((item) => {
211
+ const [component, pin] = this.visit(item);
212
+ currentPoint = this.getExecutor().toComponent(component, pin, {
213
+ addSequence: true, cloneNetComponent: true
214
+ });
205
215
  });
206
- });
207
- if (ctx.ID()) {
208
- this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
216
+ if (ctx.ID()) {
217
+ this.setComponentOrientation(currentPoint[0], currentPoint[1], ctx.ID().getText());
218
+ }
209
219
  }
210
220
  return this.getExecutor().getCurrentPoint();
211
221
  }
@@ -222,25 +232,38 @@ export class MainVisitor extends ParseTreeVisitor {
222
232
  return [component, pinId];
223
233
  }
224
234
  }
225
- visitBranch_blocks(ctx) {
226
- const branches = ctx.branch_block_inner_list();
227
- let branchType = BranchType.Branch;
228
- if (branches.length > 0) {
229
- const firstBranch = branches[0];
230
- if (firstBranch.Branch()) {
231
- branchType = BranchType.Branch;
235
+ visitPath_blocks(ctx) {
236
+ const blocks = ctx.path_block_inner_list();
237
+ let blockIndex = 0;
238
+ let blockType = BlockTypes.Branch;
239
+ let prevBlockType = null;
240
+ blocks.forEach((block, index) => {
241
+ if (block.Branch()) {
242
+ blockType = BlockTypes.Branch;
232
243
  }
233
- else if (firstBranch.Join()) {
234
- branchType = BranchType.Join;
244
+ else if (block.Join()) {
245
+ blockType = BlockTypes.Join;
235
246
  }
236
- }
237
- this.getExecutor().enterBranches(branchType);
238
- branches.forEach((branch, index) => {
239
- this.getExecutor().enterBranch(index);
240
- this.visit(branch);
241
- this.getExecutor().exitBranch(index);
247
+ else if (block.Parallel()) {
248
+ blockType = BlockTypes.Parallel;
249
+ }
250
+ else if (block.Point()) {
251
+ blockType = BlockTypes.Point;
252
+ }
253
+ if (prevBlockType !== blockType) {
254
+ if (index > 0) {
255
+ this.getExecutor().exitBlocks();
256
+ }
257
+ this.getExecutor().enterBlocks(blockType);
258
+ blockIndex = 0;
259
+ }
260
+ this.getExecutor().enterBlock(blockIndex);
261
+ this.visit(block);
262
+ this.getExecutor().exitBlock(blockIndex);
263
+ blockIndex += 1;
264
+ prevBlockType = blockType;
242
265
  });
243
- this.getExecutor().exitBranches();
266
+ this.getExecutor().exitBlocks();
244
267
  return this.getExecutor().getCurrentPoint();
245
268
  }
246
269
  visitBreak_keyword() {