circuitscript 0.0.18 → 0.0.19

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 });
404
+ }
405
+ }
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;
387
427
  }
388
428
  }
429
+ this.print('did not find block point');
430
+ return null;
389
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) {
@@ -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,31 @@ 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 blockType = BlockTypes.Branch;
238
+ if (blocks.length > 0) {
239
+ const firstBlock = blocks[0];
240
+ if (firstBlock.Branch()) {
241
+ blockType = BlockTypes.Branch;
242
+ }
243
+ else if (firstBlock.Join()) {
244
+ blockType = BlockTypes.Join;
245
+ }
246
+ else if (firstBlock.Parallel()) {
247
+ blockType = BlockTypes.Parallel;
232
248
  }
233
- else if (firstBranch.Join()) {
234
- branchType = BranchType.Join;
249
+ else if (firstBlock.Point()) {
250
+ blockType = BlockTypes.Point;
235
251
  }
236
252
  }
237
- this.getExecutor().enterBranches(branchType);
238
- branches.forEach((branch, index) => {
239
- this.getExecutor().enterBranch(index);
253
+ this.getExecutor().enterBlocks(blockType);
254
+ blocks.forEach((branch, index) => {
255
+ this.getExecutor().enterBlock(index);
240
256
  this.visit(branch);
241
- this.getExecutor().exitBranch(index);
257
+ this.getExecutor().exitBlock(index);
242
258
  });
243
- this.getExecutor().exitBranches();
259
+ this.getExecutor().exitBlocks();
244
260
  return this.getExecutor().getCurrentPoint();
245
261
  }
246
262
  visitBreak_keyword() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "circuitscript",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Interpreter for the circuitscript language",
5
5
  "homepage": "https://circuitscript.net",
6
6
  "engines": {
@@ -17,11 +17,12 @@ Add: 'add';
17
17
  At: 'at';
18
18
  To: 'to';
19
19
  Point: 'point';
20
+ Join: 'join';
21
+ Parallel: 'parallel';
20
22
 
21
23
  Return: 'return';
22
24
  Define: 'def';
23
25
  Import: 'import';
24
- Join: 'join';
25
26
 
26
27
  If: 'if';
27
28
  Not: '!';
@@ -47,18 +48,18 @@ expression: add_component_expr
47
48
  | break_keyword
48
49
  | function_def_expr
49
50
  | wire_expr
50
- | point_expr
51
51
  | import_expr
52
52
  | frame_expr
53
53
  | atom_expr
54
54
 
55
55
  | at_block
56
- | branch_blocks
56
+ | path_blocks
57
+ | point_expr
57
58
  ;
58
59
 
59
- branch_blocks: branch_block_inner+;
60
- branch_block_inner:
61
- (Branch|Join) ':' NEWLINE INDENT (NEWLINE | expression)+ DEDENT;
60
+ path_blocks: path_block_inner+;
61
+ path_block_inner:
62
+ (Branch | Join | Parallel | Point) ':' NEWLINE INDENT (NEWLINE | expression)+ DEDENT;
62
63
 
63
64
  property_set_expr2:
64
65
  atom_expr ':' NEWLINE INDENT ( NEWLINE | assignment_expr2)+ DEDENT;
@@ -73,10 +74,10 @@ pin_select_expr: Pin (INTEGER_VALUE | STRING_VALUE);
73
74
  // This does not have the 'pin' word
74
75
  pin_select_expr2: INTEGER_VALUE | STRING_VALUE;
75
76
 
76
- at_component_expr: At component_select_expr ID?;
77
+ at_component_expr: At ((component_select_expr ID?) | Point); // ID? is for orientation
77
78
  // at_component_expr_next: 'at' component_select_expr (',' component_select_expr)*;
78
79
 
79
- to_component_expr: To component_select_expr (',' component_select_expr)* ID?;
80
+ to_component_expr: To ((component_select_expr (',' component_select_expr)* ID?) | Point); // ID? is for orientation
80
81
  // pin_select_expr_next: 'pin' INTEGER_VALUE (',' INTEGER_VALUE)?;
81
82
 
82
83
  at_to_multiple_expr: At component_select_expr To component_select_expr (',' component_select_expr)* ':' NEWLINE INDENT (NEWLINE | at_to_multiple_line_expr) + DEDENT;