circuitscript 0.1.29 → 0.1.31

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 (42) hide show
  1. package/dist/cjs/BaseVisitor.js +150 -21
  2. package/dist/cjs/antlr/CircuitScriptLexer.js +241 -236
  3. package/dist/cjs/antlr/CircuitScriptParser.js +568 -431
  4. package/dist/cjs/builtinMethods.js +6 -2
  5. package/dist/cjs/environment.js +4 -0
  6. package/dist/cjs/execute.js +88 -57
  7. package/dist/cjs/globals.js +4 -1
  8. package/dist/cjs/helpers.js +6 -2
  9. package/dist/cjs/objects/ExecutionScope.js +9 -0
  10. package/dist/cjs/objects/types.js +21 -2
  11. package/dist/cjs/parser.js +6 -2
  12. package/dist/cjs/validate/SymbolTable.js +7 -1
  13. package/dist/cjs/validate/SymbolValidatorVisitor.js +54 -7
  14. package/dist/cjs/visitor.js +15 -47
  15. package/dist/esm/BaseVisitor.js +152 -23
  16. package/dist/esm/antlr/CircuitScriptLexer.js +241 -236
  17. package/dist/esm/antlr/CircuitScriptParser.js +567 -429
  18. package/dist/esm/antlr/CircuitScriptVisitor.js +3 -1
  19. package/dist/esm/builtinMethods.js +7 -3
  20. package/dist/esm/environment.js +4 -0
  21. package/dist/esm/execute.js +89 -58
  22. package/dist/esm/globals.js +2 -0
  23. package/dist/esm/helpers.js +6 -2
  24. package/dist/esm/objects/ExecutionScope.js +9 -0
  25. package/dist/esm/objects/types.js +27 -1
  26. package/dist/esm/parser.js +6 -2
  27. package/dist/esm/validate/SymbolTable.js +5 -0
  28. package/dist/esm/validate/SymbolValidatorVisitor.js +53 -6
  29. package/dist/esm/visitor.js +16 -45
  30. package/dist/types/BaseVisitor.d.ts +12 -5
  31. package/dist/types/antlr/CircuitScriptLexer.d.ts +43 -42
  32. package/dist/types/antlr/CircuitScriptParser.d.ts +71 -45
  33. package/dist/types/antlr/CircuitScriptVisitor.d.ts +6 -2
  34. package/dist/types/environment.d.ts +1 -0
  35. package/dist/types/execute.d.ts +2 -2
  36. package/dist/types/globals.d.ts +2 -0
  37. package/dist/types/objects/ExecutionScope.d.ts +3 -1
  38. package/dist/types/objects/types.d.ts +22 -1
  39. package/dist/types/validate/SymbolTable.d.ts +1 -0
  40. package/dist/types/validate/SymbolValidatorVisitor.d.ts +6 -2
  41. package/dist/types/visitor.d.ts +3 -1
  42. package/package.json +4 -1
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseVisitor = void 0;
4
4
  const big_js_1 = require("big.js");
5
+ const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
5
6
  const CircuitScriptVisitor_js_1 = require("./antlr/CircuitScriptVisitor.js");
6
7
  const execute_js_1 = require("./execute.js");
7
8
  const logger_js_1 = require("./logger.js");
@@ -18,10 +19,10 @@ const PinDefinition_js_1 = require("./objects/PinDefinition.js");
18
19
  class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
19
20
  constructor(silent = false, onErrorHandler = null, environment) {
20
21
  super();
21
- this.indentLevel = 0;
22
22
  this.silent = false;
23
23
  this.printStream = [];
24
24
  this.printToConsole = true;
25
+ this.allowParseImports = false;
25
26
  this.acceptedDirections = [types_js_1.Direction.Up, types_js_1.Direction.Down,
26
27
  types_js_1.Direction.Right, types_js_1.Direction.Left];
27
28
  this.resultData = new Map;
@@ -41,17 +42,25 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
41
42
  };
42
43
  this.visitScript = async (ctx) => {
43
44
  this.log('===', 'start', '===');
44
- const imports = ctx.import_expr();
45
- for (let i = 0; i < imports.length; i++) {
46
- const ctxImport = imports[i];
47
- const ID = ctxImport.ID().toString();
48
- await this.handleImportFile(ID, true, ctxImport);
45
+ this.allowParseImports = true;
46
+ for (const ctxImport of ctx.import_expr()) {
47
+ await this.visit(ctxImport);
49
48
  }
49
+ this.allowParseImports = false;
50
50
  const result = this.runExpressions(this.getExecutor(), ctx.expression());
51
51
  this.setResult(ctx, result);
52
52
  this.getExecutor().closeOpenPathBlocks();
53
53
  this.log('===', 'end', '===');
54
54
  };
55
+ this.visitImport_simple = async (ctx) => {
56
+ await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllWithNamespace);
57
+ };
58
+ this.visitImport_all_simple = async (ctx) => {
59
+ await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllMergeIntoNamespace);
60
+ };
61
+ this.visitImport_specific = async (ctx) => {
62
+ await this.importCommon(ctx, types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace);
63
+ };
55
64
  this.visitAssignment_expr = (ctx) => {
56
65
  const ctxAtom = ctx.atom_expr();
57
66
  const ctxFuncCallRef = ctx.function_call_expr();
@@ -431,12 +440,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
431
440
  this.logger = new logger_js_1.Logger();
432
441
  this.onErrorHandler = onErrorHandler;
433
442
  this.environment = environment;
434
- this.log('-- Environment --');
435
- this.log('Module directory: ' + environment.getModuleDirectory());
436
- this.log('Default libs path: ' + environment.getDefaultLibsPath());
437
- this.log('Current file: ' + environment.getCurrentFile());
438
- this.log('-----------------');
439
- this.startingContext = new execute_js_1.ExecutionContext(globals_js_1.DoubleDelimiter1, `${globals_js_1.DoubleDelimiter1}.`, '/', 0, 0, silent, this.logger, this.warnings, null);
443
+ this.startingContext = new execute_js_1.ExecutionContext(globals_js_1.DoubleDelimiter1, globals_js_1.BaseNamespace, '/', 0, 0, silent, this.logger, this.warnings, null);
440
444
  const scope = this.startingContext.scope;
441
445
  scope.sequence.push([
442
446
  ExecutionScope_js_1.SequenceAction.At, scope.componentRoot, scope.currentPin
@@ -498,8 +502,9 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
498
502
  };
499
503
  }
500
504
  log(...params) {
501
- const indentOutput = ''.padStart(this.indentLevel * 4, ' ');
502
- const indentLevelText = this.indentLevel.toString().padStart(3, ' ');
505
+ const indentLevel = this.getScope().scopeLevel;
506
+ const indentOutput = ''.padStart(indentLevel * 4, ' ');
507
+ const indentLevelText = indentLevel.toString().padStart(3, ' ');
503
508
  const args = ['[' + indentLevelText + ']', indentOutput, ...params];
504
509
  this.logger.add(args.join(' '));
505
510
  if (!this.silent) {
@@ -513,6 +518,17 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
513
518
  const result = await ctx.accept(this);
514
519
  return result;
515
520
  }
521
+ async importCommon(ctx, handling) {
522
+ const specificImports = [];
523
+ if (ctx instanceof CircuitScriptParser_js_1.Import_specificContext) {
524
+ const tmpSpecificImports = ctx._funcNames.map(item => {
525
+ return item.text;
526
+ });
527
+ specificImports.push(...tmpSpecificImports);
528
+ }
529
+ const id = ctx._moduleName.text;
530
+ await this.handleImportFile(id, handling, true, ctx, specificImports);
531
+ }
516
532
  getReference(ctx) {
517
533
  const atomStr = ctx.getText();
518
534
  if (atomStr.indexOf('(') !== -1 || atomStr.indexOf(')') !== -1) {
@@ -538,7 +554,8 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
538
554
  let currentReference = executor.resolveVariable(this.executionStack, atomId);
539
555
  if (ctx.trailer_expr().length > 0) {
540
556
  if (!currentReference.found) {
541
- this.throwWithContext(ctx, "Unknown function name: " + atomId);
557
+ this.log(`could not resolve function: ${atomId}`);
558
+ this.throwWithContext(ctx, "could not resolve function: " + atomId);
542
559
  }
543
560
  currentReference.trailers = [];
544
561
  ctx.trailer_expr().forEach(item => {
@@ -555,7 +572,24 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
555
572
  }
556
573
  const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
557
574
  try {
558
- const [, functionResult] = executor.callFunction(currentReference.name, parameters, this.executionStack, useNetNamespace);
575
+ const isModuleFunction = currentReference.rootValue
576
+ && currentReference.rootValue instanceof types_js_1.ImportedModule;
577
+ if (isModuleFunction) {
578
+ this.log('create new module context');
579
+ const importedModule = currentReference.rootValue;
580
+ const importedModuleContext = importedModule.context;
581
+ const newExecutor = this.handleEnterContext(this.getExecutor(), this.executionStack, importedModuleContext.name, ctx, {
582
+ netNamespace: executor.netNamespace,
583
+ namespace: importedModule.moduleNamespace
584
+ }, [], [], false);
585
+ this.log('copy module context scope');
586
+ importedModuleContext.scope.copyTo(newExecutor.scope);
587
+ }
588
+ const [, functionResult] = executor.callFunction(currentReference, parameters, this.executionStack, useNetNamespace);
589
+ if (isModuleFunction) {
590
+ this.log('pop module context scope');
591
+ this.handlePopContext(this.getExecutor(), this.executionStack, "", false);
592
+ }
559
593
  if ((0, utils_js_1.isReference)(functionResult)) {
560
594
  currentReference = functionResult;
561
595
  }
@@ -582,6 +616,54 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
582
616
  }
583
617
  this.setResult(ctx, currentReference);
584
618
  }
619
+ handleEnterContext(executor, executionStack, contextName, ctx, options, funcDefinedParameters, passedInParameters, isBreakContext = true) {
620
+ if (isBreakContext) {
621
+ const parentBreakContext = executor.getParentBreakContext();
622
+ executor.addBreakContext(ctx);
623
+ let useIndex = -1;
624
+ if (parentBreakContext === null) {
625
+ useIndex = options.functionCallIndex;
626
+ }
627
+ else {
628
+ const parentEntry = executor.indexedStack.get(parentBreakContext);
629
+ const { funcCallIndex } = parentEntry;
630
+ if (!funcCallIndex.has(ctx)) {
631
+ funcCallIndex.set(ctx, 0);
632
+ useIndex = 0;
633
+ }
634
+ else {
635
+ useIndex = funcCallIndex.get(ctx) + 1;
636
+ funcCallIndex.set(ctx, useIndex);
637
+ }
638
+ }
639
+ executor.setBreakContextIndex(useIndex);
640
+ }
641
+ return this.enterNewChildContext(executionStack, executor, contextName, options, funcDefinedParameters, passedInParameters);
642
+ }
643
+ handlePopContext(executor, executionStack, namespaceExtension, isBreakContext = true) {
644
+ const poppedContext = executionStack.pop();
645
+ const nextLastExecution = executionStack[executionStack.length - 1];
646
+ const mergedComponents = nextLastExecution.mergeScope(poppedContext.scope, namespaceExtension);
647
+ if (isBreakContext) {
648
+ const scope = this.getScope();
649
+ const indexedStack = [];
650
+ if (scope.breakStack.length > 0) {
651
+ const executor = this.getExecutor();
652
+ scope.breakStack.forEach(stackCtx => {
653
+ const entry = executor.indexedStack.get(stackCtx);
654
+ const { index } = entry;
655
+ indexedStack.push([stackCtx, index]);
656
+ });
657
+ mergedComponents.forEach(component => {
658
+ component.ctxReferences.forEach(ref => {
659
+ ref.indexedStack = [...indexedStack, ...ref.indexedStack];
660
+ });
661
+ });
662
+ }
663
+ executor.popBreakContext();
664
+ }
665
+ return poppedContext;
666
+ }
585
667
  setResult(ctx, value) {
586
668
  this.resultData.set(ctx, value);
587
669
  }
@@ -613,12 +695,34 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
613
695
  this.visit(ctx);
614
696
  return this.getResult(ctx);
615
697
  }
616
- async handleImportFile(name, throwErrors = true, ctx = null) {
698
+ async handleImportFile(name, importHandling, throwErrors = true, ctx = null, specificImports = []) {
617
699
  name = name.trim();
618
700
  const importAlready = this.importedFiles.find(item => {
619
701
  return item.id === name;
620
702
  });
621
703
  if (importAlready) {
704
+ const tmpImportedModule = importAlready.importedModule;
705
+ const alreadyImportedFlag = tmpImportedModule.importHandlingFlag;
706
+ const isMergedNamespace = alreadyImportedFlag === types_js_1.ImportFunctionHandling.AllMergeIntoNamespace
707
+ || alreadyImportedFlag === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace;
708
+ const invalidImportCondition1 = alreadyImportedFlag === types_js_1.ImportFunctionHandling.AllWithNamespace &&
709
+ importHandling !== types_js_1.ImportFunctionHandling.AllWithNamespace;
710
+ const invalidImportCondition2 = alreadyImportedFlag !== types_js_1.ImportFunctionHandling.AllWithNamespace &&
711
+ importHandling === types_js_1.ImportFunctionHandling.AllWithNamespace;
712
+ if (invalidImportCondition1 || invalidImportCondition2) {
713
+ throw new utils_js_2.RuntimeExecutionError(`Namespace import and wildcard/specific imports cannot be mixed: ${name}`, ctx);
714
+ }
715
+ if (isMergedNamespace) {
716
+ if (alreadyImportedFlag === types_js_1.ImportFunctionHandling.AllMergeIntoNamespace) {
717
+ }
718
+ else if (alreadyImportedFlag === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace && importHandling === types_js_1.ImportFunctionHandling.AllMergeIntoNamespace) {
719
+ tmpImportedModule.specifiedImports = [];
720
+ tmpImportedModule.importHandlingFlag = types_js_1.ImportFunctionHandling.AllMergeIntoNamespace;
721
+ }
722
+ else if (alreadyImportedFlag === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace && importHandling === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace) {
723
+ tmpImportedModule.specifiedImports.push(...specificImports);
724
+ }
725
+ }
622
726
  return importAlready;
623
727
  }
624
728
  let hasError = false;
@@ -637,6 +741,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
637
741
  this.log('failed to read file');
638
742
  pathExists = false;
639
743
  }
744
+ let importedModule;
640
745
  if (!pathExists) {
641
746
  try {
642
747
  const tmpFilePath2 = this.environment.getRelativeToDefaultLibs(name + ".cst");
@@ -651,16 +756,36 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
651
756
  }
652
757
  }
653
758
  try {
654
- if (pathExists) {
759
+ if (pathExists && filePathUsed) {
655
760
  this.log('done reading imported file data');
761
+ const executionStack = this.executionStack;
762
+ const executor = this.getExecutor();
763
+ const executionContextName = name;
764
+ const netNamespace = executor.netNamespace;
765
+ const moduleNamespace = `${globals_js_1.BaseNamespace}${name}.`;
766
+ this.enterNewChildContext(executionStack, executor, executionContextName, {
767
+ netNamespace,
768
+ namespace: moduleNamespace,
769
+ }, [], []);
656
770
  const importResult = await this.onImportFile(this, filePathUsed, fileData, this.onErrorHandler);
657
771
  hasError = importResult.hasError;
658
772
  hasParseError = importResult.hasParseError;
773
+ const importContext = executionStack.pop();
774
+ this.log(`import handling flag: ${importHandling}`);
775
+ importedModule = new types_js_1.ImportedModule(name, moduleNamespace, filePathUsed, importContext, importHandling, specificImports);
776
+ if (specificImports.length > 0) {
777
+ this.log('specific import: ' + specificImports.join(', '));
778
+ }
779
+ const scope = this.getScope();
780
+ scope.modules.set(name, importedModule);
781
+ importedModule.context.scope.modules.forEach((module, key) => {
782
+ scope.modules.set(key, module);
783
+ });
659
784
  }
660
785
  }
661
786
  catch (err) {
662
787
  if (ctx != null) {
663
- throw new utils_js_2.RuntimeExecutionError("An error occurred while importing file", ctx);
788
+ throw err;
664
789
  }
665
790
  else {
666
791
  this.log('An error occurred while importing file:', err.message);
@@ -683,6 +808,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
683
808
  hasError,
684
809
  hasParseError,
685
810
  pathExists,
811
+ importedModule: importedModule
686
812
  };
687
813
  this.importedFiles.push(newImportedFile);
688
814
  return newImportedFile;
@@ -770,14 +896,17 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
770
896
  return object.getParam(paramName);
771
897
  }
772
898
  enterNewChildContext(executionStack, parentContext, executionContextName, options, funcDefinedParameters, passedInParameters) {
773
- const { netNamespace = "" } = options;
899
+ const { netNamespace = "", namespace = null, } = options;
774
900
  const currentExecutionContext = executionStack[executionStack.length - 1];
775
901
  const executionLevel = currentExecutionContext.executionLevel;
776
- const executionContextNamespace = currentExecutionContext.namespace
902
+ const executionContextNamespace = namespace ?? currentExecutionContext.namespace
777
903
  + executionContextName + ".";
778
904
  const newExecutor = new execute_js_1.ExecutionContext(executionContextName, executionContextNamespace, netNamespace, executionLevel + 1, this.getExecutor().scope.scopeLevel + 1, currentExecutionContext.silent, currentExecutionContext.logger, currentExecutionContext.warnings, parentContext);
779
905
  executionStack.push(newExecutor);
780
906
  this.setupDefinedParameters(funcDefinedParameters, passedInParameters, newExecutor);
907
+ newExecutor.resolveNet = this.createNetResolver(executionStack);
908
+ newExecutor.resolveComponentPinNet =
909
+ this.createComponentPinNetResolver(executionStack);
781
910
  return newExecutor;
782
911
  }
783
912
  prepareStringValue(value) {