scope.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ValueScope = exports.ValueScopeName = exports.Scope = exports.varKinds = void 0;
  4. const code_1 = require("./code");
  5. class ValueError extends Error {
  6. constructor(name) {
  7. super(`CodeGen: "code" for ${name} not defined`);
  8. this.value = name.value;
  9. }
  10. }
  11. exports.varKinds = {
  12. const: new code_1.Name("const"),
  13. let: new code_1.Name("let"),
  14. var: new code_1.Name("var"),
  15. };
  16. class Scope {
  17. constructor({ prefixes, parent } = {}) {
  18. this._names = {};
  19. this._prefixes = prefixes;
  20. this._parent = parent;
  21. }
  22. toName(nameOrPrefix) {
  23. return nameOrPrefix instanceof code_1.Name ? nameOrPrefix : this.name(nameOrPrefix);
  24. }
  25. name(prefix) {
  26. return new code_1.Name(this._newName(prefix));
  27. }
  28. _newName(prefix) {
  29. const ng = this._names[prefix] || this._nameGroup(prefix);
  30. return `${prefix}${ng.index++}`;
  31. }
  32. _nameGroup(prefix) {
  33. var _a, _b;
  34. if (((_b = (_a = this._parent) === null || _a === void 0 ? void 0 : _a._prefixes) === null || _b === void 0 ? void 0 : _b.has(prefix)) || (this._prefixes && !this._prefixes.has(prefix))) {
  35. throw new Error(`CodeGen: prefix "${prefix}" is not allowed in this scope`);
  36. }
  37. return (this._names[prefix] = { prefix, index: 0 });
  38. }
  39. }
  40. exports.Scope = Scope;
  41. class ValueScopeName extends code_1.Name {
  42. constructor(prefix, nameStr) {
  43. super(nameStr);
  44. this.prefix = prefix;
  45. }
  46. setValue(value, { property, itemIndex }) {
  47. this.value = value;
  48. this.scopePath = code_1._ `.${new code_1.Name(property)}[${itemIndex}]`;
  49. }
  50. }
  51. exports.ValueScopeName = ValueScopeName;
  52. const line = code_1._ `\n`;
  53. class ValueScope extends Scope {
  54. constructor(opts) {
  55. super(opts);
  56. this._values = {};
  57. this._scope = opts.scope;
  58. this.opts = { ...opts, _n: opts.lines ? line : code_1.nil };
  59. }
  60. get() {
  61. return this._scope;
  62. }
  63. name(prefix) {
  64. return new ValueScopeName(prefix, this._newName(prefix));
  65. }
  66. value(nameOrPrefix, value) {
  67. var _a;
  68. if (value.ref === undefined)
  69. throw new Error("CodeGen: ref must be passed in value");
  70. const name = this.toName(nameOrPrefix);
  71. const { prefix } = name;
  72. const valueKey = (_a = value.key) !== null && _a !== void 0 ? _a : value.ref;
  73. let vs = this._values[prefix];
  74. if (vs) {
  75. const _name = vs.get(valueKey);
  76. if (_name)
  77. return _name;
  78. }
  79. else {
  80. vs = this._values[prefix] = new Map();
  81. }
  82. vs.set(valueKey, name);
  83. const s = this._scope[prefix] || (this._scope[prefix] = []);
  84. const itemIndex = s.length;
  85. s[itemIndex] = value.ref;
  86. name.setValue(value, { property: prefix, itemIndex });
  87. return name;
  88. }
  89. getValue(prefix, keyOrRef) {
  90. const vs = this._values[prefix];
  91. if (!vs)
  92. return;
  93. return vs.get(keyOrRef);
  94. }
  95. scopeRefs(scopeName, values = this._values) {
  96. return this._reduceValues(values, (name) => {
  97. if (name.scopePath === undefined)
  98. throw new Error(`CodeGen: name "${name}" has no value`);
  99. return code_1._ `${scopeName}${name.scopePath}`;
  100. });
  101. }
  102. scopeCode(values = this._values, usedValues, getCode) {
  103. return this._reduceValues(values, (name) => {
  104. if (name.value === undefined)
  105. throw new Error(`CodeGen: name "${name}" has no value`);
  106. return name.value.code;
  107. }, usedValues, getCode);
  108. }
  109. _reduceValues(values, valueCode, usedValues = {}, getCode) {
  110. let code = code_1.nil;
  111. for (const prefix in values) {
  112. const vs = values[prefix];
  113. if (!vs)
  114. continue;
  115. const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set());
  116. vs.forEach((name) => {
  117. if (nameSet.has(name))
  118. return;
  119. nameSet.add(name);
  120. let c = valueCode(name);
  121. if (c) {
  122. const def = this.opts.es5 ? exports.varKinds.var : exports.varKinds.const;
  123. code = code_1._ `${code}${def} ${name} = ${c};${this.opts._n}`;
  124. }
  125. else if ((c = getCode === null || getCode === void 0 ? void 0 : getCode(name))) {
  126. code = code_1._ `${code}${c}${this.opts._n}`;
  127. }
  128. else {
  129. throw new ValueError(name);
  130. }
  131. });
  132. }
  133. return code;
  134. }
  135. }
  136. exports.ValueScope = ValueScope;
  137. //# sourceMappingURL=scope.js.map