index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.or = exports.and = exports.not = exports.CodeGen = exports.operators = exports.varKinds = exports.ValueScopeName = exports.ValueScope = exports.Scope = exports.Name = exports.stringify = exports.getProperty = exports.nil = exports.strConcat = exports.str = exports._ = void 0;
  4. const code_1 = require("./code");
  5. const scope_1 = require("./scope");
  6. var code_2 = require("./code");
  7. Object.defineProperty(exports, "_", { enumerable: true, get: function () { return code_2._; } });
  8. Object.defineProperty(exports, "str", { enumerable: true, get: function () { return code_2.str; } });
  9. Object.defineProperty(exports, "strConcat", { enumerable: true, get: function () { return code_2.strConcat; } });
  10. Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return code_2.nil; } });
  11. Object.defineProperty(exports, "getProperty", { enumerable: true, get: function () { return code_2.getProperty; } });
  12. Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return code_2.stringify; } });
  13. Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return code_2.Name; } });
  14. var scope_2 = require("./scope");
  15. Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return scope_2.Scope; } });
  16. Object.defineProperty(exports, "ValueScope", { enumerable: true, get: function () { return scope_2.ValueScope; } });
  17. Object.defineProperty(exports, "ValueScopeName", { enumerable: true, get: function () { return scope_2.ValueScopeName; } });
  18. Object.defineProperty(exports, "varKinds", { enumerable: true, get: function () { return scope_2.varKinds; } });
  19. exports.operators = {
  20. GT: new code_1._Code(">"),
  21. GTE: new code_1._Code(">="),
  22. LT: new code_1._Code("<"),
  23. LTE: new code_1._Code("<="),
  24. EQ: new code_1._Code("==="),
  25. NEQ: new code_1._Code("!=="),
  26. NOT: new code_1._Code("!"),
  27. OR: new code_1._Code("||"),
  28. AND: new code_1._Code("&&"),
  29. };
  30. class Node {
  31. optimizeNodes() {
  32. return this;
  33. }
  34. optimizeNames(_names, _constants) {
  35. return this;
  36. }
  37. }
  38. class Def extends Node {
  39. constructor(varKind, name, rhs) {
  40. super();
  41. this.varKind = varKind;
  42. this.name = name;
  43. this.rhs = rhs;
  44. }
  45. render({ es5, _n }) {
  46. const varKind = es5 ? scope_1.varKinds.var : this.varKind;
  47. const rhs = this.rhs === undefined ? "" : ` = ${this.rhs}`;
  48. return `${varKind} ${this.name}${rhs};` + _n;
  49. }
  50. optimizeNames(names, constants) {
  51. if (!names[this.name.str])
  52. return;
  53. if (this.rhs)
  54. this.rhs = optimizeExpr(this.rhs, names, constants);
  55. return this;
  56. }
  57. get names() {
  58. return this.rhs instanceof code_1._CodeOrName ? this.rhs.names : {};
  59. }
  60. }
  61. class Assign extends Node {
  62. constructor(lhs, rhs, sideEffects) {
  63. super();
  64. this.lhs = lhs;
  65. this.rhs = rhs;
  66. this.sideEffects = sideEffects;
  67. }
  68. render({ _n }) {
  69. return `${this.lhs} = ${this.rhs};` + _n;
  70. }
  71. optimizeNames(names, constants) {
  72. if (this.lhs instanceof code_1.Name && !names[this.lhs.str] && !this.sideEffects)
  73. return;
  74. this.rhs = optimizeExpr(this.rhs, names, constants);
  75. return this;
  76. }
  77. get names() {
  78. const names = this.lhs instanceof code_1.Name ? {} : { ...this.lhs.names };
  79. return addExprNames(names, this.rhs);
  80. }
  81. }
  82. class Label extends Node {
  83. constructor(label) {
  84. super();
  85. this.label = label;
  86. this.names = {};
  87. }
  88. render({ _n }) {
  89. return `${this.label}:` + _n;
  90. }
  91. }
  92. class Break extends Node {
  93. constructor(label) {
  94. super();
  95. this.label = label;
  96. this.names = {};
  97. }
  98. render({ _n }) {
  99. const label = this.label ? ` ${this.label}` : "";
  100. return `break${label};` + _n;
  101. }
  102. }
  103. class Throw extends Node {
  104. constructor(error) {
  105. super();
  106. this.error = error;
  107. }
  108. render({ _n }) {
  109. return `throw ${this.error};` + _n;
  110. }
  111. get names() {
  112. return this.error.names;
  113. }
  114. }
  115. class AnyCode extends Node {
  116. constructor(code) {
  117. super();
  118. this.code = code;
  119. }
  120. render({ _n }) {
  121. return `${this.code};` + _n;
  122. }
  123. optimizeNodes() {
  124. return `${this.code}` ? this : undefined;
  125. }
  126. optimizeNames(names, constants) {
  127. this.code = optimizeExpr(this.code, names, constants);
  128. return this;
  129. }
  130. get names() {
  131. return this.code instanceof code_1._CodeOrName ? this.code.names : {};
  132. }
  133. }
  134. class ParentNode extends Node {
  135. constructor(nodes = []) {
  136. super();
  137. this.nodes = nodes;
  138. }
  139. render(opts) {
  140. return this.nodes.reduce((code, n) => code + n.render(opts), "");
  141. }
  142. optimizeNodes() {
  143. const { nodes } = this;
  144. let i = nodes.length;
  145. while (i--) {
  146. const n = nodes[i].optimizeNodes();
  147. if (Array.isArray(n))
  148. nodes.splice(i, 1, ...n);
  149. else if (n)
  150. nodes[i] = n;
  151. else
  152. nodes.splice(i, 1);
  153. }
  154. return nodes.length > 0 ? this : undefined;
  155. }
  156. optimizeNames(names, constants) {
  157. const { nodes } = this;
  158. let i = nodes.length;
  159. while (i--) {
  160. // iterating backwards improves 1-pass optimization
  161. const n = nodes[i];
  162. if (n.optimizeNames(names, constants))
  163. continue;
  164. subtractNames(names, n.names);
  165. nodes.splice(i, 1);
  166. }
  167. return nodes.length > 0 ? this : undefined;
  168. }
  169. get names() {
  170. return this.nodes.reduce((names, n) => addNames(names, n.names), {});
  171. }
  172. }
  173. class BlockNode extends ParentNode {
  174. render(opts) {
  175. return "{" + opts._n + super.render(opts) + "}" + opts._n;
  176. }
  177. }
  178. class Root extends ParentNode {
  179. }
  180. class Else extends BlockNode {
  181. }
  182. Else.kind = "else";
  183. class If extends BlockNode {
  184. constructor(condition, nodes) {
  185. super(nodes);
  186. this.condition = condition;
  187. }
  188. render(opts) {
  189. let code = `if(${this.condition})` + super.render(opts);
  190. if (this.else)
  191. code += "else " + this.else.render(opts);
  192. return code;
  193. }
  194. optimizeNodes() {
  195. super.optimizeNodes();
  196. const cond = this.condition;
  197. if (cond === true)
  198. return this.nodes; // else is ignored here
  199. let e = this.else;
  200. if (e) {
  201. const ns = e.optimizeNodes();
  202. e = this.else = Array.isArray(ns) ? new Else(ns) : ns;
  203. }
  204. if (e) {
  205. if (cond === false)
  206. return e instanceof If ? e : e.nodes;
  207. if (this.nodes.length)
  208. return this;
  209. return new If(not(cond), e instanceof If ? [e] : e.nodes);
  210. }
  211. if (cond === false || !this.nodes.length)
  212. return undefined;
  213. return this;
  214. }
  215. optimizeNames(names, constants) {
  216. var _a;
  217. this.else = (_a = this.else) === null || _a === void 0 ? void 0 : _a.optimizeNames(names, constants);
  218. if (!(super.optimizeNames(names, constants) || this.else))
  219. return;
  220. this.condition = optimizeExpr(this.condition, names, constants);
  221. return this;
  222. }
  223. get names() {
  224. const names = super.names;
  225. addExprNames(names, this.condition);
  226. if (this.else)
  227. addNames(names, this.else.names);
  228. return names;
  229. }
  230. }
  231. If.kind = "if";
  232. class For extends BlockNode {
  233. }
  234. For.kind = "for";
  235. class ForLoop extends For {
  236. constructor(iteration) {
  237. super();
  238. this.iteration = iteration;
  239. }
  240. render(opts) {
  241. return `for(${this.iteration})` + super.render(opts);
  242. }
  243. optimizeNames(names, constants) {
  244. if (!super.optimizeNames(names, constants))
  245. return;
  246. this.iteration = optimizeExpr(this.iteration, names, constants);
  247. return this;
  248. }
  249. get names() {
  250. return addNames(super.names, this.iteration.names);
  251. }
  252. }
  253. class ForRange extends For {
  254. constructor(varKind, name, from, to) {
  255. super();
  256. this.varKind = varKind;
  257. this.name = name;
  258. this.from = from;
  259. this.to = to;
  260. }
  261. render(opts) {
  262. const varKind = opts.es5 ? scope_1.varKinds.var : this.varKind;
  263. const { name, from, to } = this;
  264. return `for(${varKind} ${name}=${from}; ${name}<${to}; ${name}++)` + super.render(opts);
  265. }
  266. get names() {
  267. const names = addExprNames(super.names, this.from);
  268. return addExprNames(names, this.to);
  269. }
  270. }
  271. class ForIter extends For {
  272. constructor(loop, varKind, name, iterable) {
  273. super();
  274. this.loop = loop;
  275. this.varKind = varKind;
  276. this.name = name;
  277. this.iterable = iterable;
  278. }
  279. render(opts) {
  280. return `for(${this.varKind} ${this.name} ${this.loop} ${this.iterable})` + super.render(opts);
  281. }
  282. optimizeNames(names, constants) {
  283. if (!super.optimizeNames(names, constants))
  284. return;
  285. this.iterable = optimizeExpr(this.iterable, names, constants);
  286. return this;
  287. }
  288. get names() {
  289. return addNames(super.names, this.iterable.names);
  290. }
  291. }
  292. class Func extends BlockNode {
  293. constructor(name, args, async) {
  294. super();
  295. this.name = name;
  296. this.args = args;
  297. this.async = async;
  298. }
  299. render(opts) {
  300. const _async = this.async ? "async " : "";
  301. return `${_async}function ${this.name}(${this.args})` + super.render(opts);
  302. }
  303. }
  304. Func.kind = "func";
  305. class Return extends ParentNode {
  306. render(opts) {
  307. return "return " + super.render(opts);
  308. }
  309. }
  310. Return.kind = "return";
  311. class Try extends BlockNode {
  312. render(opts) {
  313. let code = "try" + super.render(opts);
  314. if (this.catch)
  315. code += this.catch.render(opts);
  316. if (this.finally)
  317. code += this.finally.render(opts);
  318. return code;
  319. }
  320. optimizeNodes() {
  321. var _a, _b;
  322. super.optimizeNodes();
  323. (_a = this.catch) === null || _a === void 0 ? void 0 : _a.optimizeNodes();
  324. (_b = this.finally) === null || _b === void 0 ? void 0 : _b.optimizeNodes();
  325. return this;
  326. }
  327. optimizeNames(names, constants) {
  328. var _a, _b;
  329. super.optimizeNames(names, constants);
  330. (_a = this.catch) === null || _a === void 0 ? void 0 : _a.optimizeNames(names, constants);
  331. (_b = this.finally) === null || _b === void 0 ? void 0 : _b.optimizeNames(names, constants);
  332. return this;
  333. }
  334. get names() {
  335. const names = super.names;
  336. if (this.catch)
  337. addNames(names, this.catch.names);
  338. if (this.finally)
  339. addNames(names, this.finally.names);
  340. return names;
  341. }
  342. }
  343. class Catch extends BlockNode {
  344. constructor(error) {
  345. super();
  346. this.error = error;
  347. }
  348. render(opts) {
  349. return `catch(${this.error})` + super.render(opts);
  350. }
  351. }
  352. Catch.kind = "catch";
  353. class Finally extends BlockNode {
  354. render(opts) {
  355. return "finally" + super.render(opts);
  356. }
  357. }
  358. Finally.kind = "finally";
  359. class CodeGen {
  360. constructor(extScope, opts = {}) {
  361. this._values = {};
  362. this._blockStarts = [];
  363. this._constants = {};
  364. this.opts = { ...opts, _n: opts.lines ? "\n" : "" };
  365. this._extScope = extScope;
  366. this._scope = new scope_1.Scope({ parent: extScope });
  367. this._nodes = [new Root()];
  368. }
  369. toString() {
  370. return this._root.render(this.opts);
  371. }
  372. // returns unique name in the internal scope
  373. name(prefix) {
  374. return this._scope.name(prefix);
  375. }
  376. // reserves unique name in the external scope
  377. scopeName(prefix) {
  378. return this._extScope.name(prefix);
  379. }
  380. // reserves unique name in the external scope and assigns value to it
  381. scopeValue(prefixOrName, value) {
  382. const name = this._extScope.value(prefixOrName, value);
  383. const vs = this._values[name.prefix] || (this._values[name.prefix] = new Set());
  384. vs.add(name);
  385. return name;
  386. }
  387. getScopeValue(prefix, keyOrRef) {
  388. return this._extScope.getValue(prefix, keyOrRef);
  389. }
  390. // return code that assigns values in the external scope to the names that are used internally
  391. // (same names that were returned by gen.scopeName or gen.scopeValue)
  392. scopeRefs(scopeName) {
  393. return this._extScope.scopeRefs(scopeName, this._values);
  394. }
  395. scopeCode() {
  396. return this._extScope.scopeCode(this._values);
  397. }
  398. _def(varKind, nameOrPrefix, rhs, constant) {
  399. const name = this._scope.toName(nameOrPrefix);
  400. if (rhs !== undefined && constant)
  401. this._constants[name.str] = rhs;
  402. this._leafNode(new Def(varKind, name, rhs));
  403. return name;
  404. }
  405. // `const` declaration (`var` in es5 mode)
  406. const(nameOrPrefix, rhs, _constant) {
  407. return this._def(scope_1.varKinds.const, nameOrPrefix, rhs, _constant);
  408. }
  409. // `let` declaration with optional assignment (`var` in es5 mode)
  410. let(nameOrPrefix, rhs, _constant) {
  411. return this._def(scope_1.varKinds.let, nameOrPrefix, rhs, _constant);
  412. }
  413. // `var` declaration with optional assignment
  414. var(nameOrPrefix, rhs, _constant) {
  415. return this._def(scope_1.varKinds.var, nameOrPrefix, rhs, _constant);
  416. }
  417. // assignment code
  418. assign(lhs, rhs, sideEffects) {
  419. return this._leafNode(new Assign(lhs, rhs, sideEffects));
  420. }
  421. // appends passed SafeExpr to code or executes Block
  422. code(c) {
  423. if (typeof c == "function")
  424. c();
  425. else if (c !== code_1.nil)
  426. this._leafNode(new AnyCode(c));
  427. return this;
  428. }
  429. // returns code for object literal for the passed argument list of key-value pairs
  430. object(...keyValues) {
  431. const code = ["{"];
  432. for (const [key, value] of keyValues) {
  433. if (code.length > 1)
  434. code.push(",");
  435. code.push(key);
  436. if (key !== value || this.opts.es5) {
  437. code.push(":");
  438. code_1.addCodeArg(code, value);
  439. }
  440. }
  441. code.push("}");
  442. return new code_1._Code(code);
  443. }
  444. // `if` clause (or statement if `thenBody` and, optionally, `elseBody` are passed)
  445. if(condition, thenBody, elseBody) {
  446. this._blockNode(new If(condition));
  447. if (thenBody && elseBody) {
  448. this.code(thenBody).else().code(elseBody).endIf();
  449. }
  450. else if (thenBody) {
  451. this.code(thenBody).endIf();
  452. }
  453. else if (elseBody) {
  454. throw new Error('CodeGen: "else" body without "then" body');
  455. }
  456. return this;
  457. }
  458. // `else if` clause - invalid without `if` or after `else` clauses
  459. elseIf(condition) {
  460. return this._elseNode(new If(condition));
  461. }
  462. // `else` clause - only valid after `if` or `else if` clauses
  463. else() {
  464. return this._elseNode(new Else());
  465. }
  466. // end `if` statement (needed if gen.if was used only with condition)
  467. endIf() {
  468. return this._endBlockNode(If, Else);
  469. }
  470. _for(node, forBody) {
  471. this._blockNode(node);
  472. if (forBody)
  473. this.code(forBody).endFor();
  474. return this;
  475. }
  476. // a generic `for` clause (or statement if `forBody` is passed)
  477. for(iteration, forBody) {
  478. return this._for(new ForLoop(iteration), forBody);
  479. }
  480. // `for` statement for a range of values
  481. forRange(nameOrPrefix, from, to, forBody, varKind = this.opts.es5 ? scope_1.varKinds.var : scope_1.varKinds.let) {
  482. const name = this._scope.toName(nameOrPrefix);
  483. return this._for(new ForRange(varKind, name, from, to), () => forBody(name));
  484. }
  485. // `for-of` statement (in es5 mode replace with a normal for loop)
  486. forOf(nameOrPrefix, iterable, forBody, varKind = scope_1.varKinds.const) {
  487. const name = this._scope.toName(nameOrPrefix);
  488. if (this.opts.es5) {
  489. const arr = iterable instanceof code_1.Name ? iterable : this.var("_arr", iterable);
  490. return this.forRange("_i", 0, code_1._ `${arr}.length`, (i) => {
  491. this.var(name, code_1._ `${arr}[${i}]`);
  492. forBody(name);
  493. });
  494. }
  495. return this._for(new ForIter("of", varKind, name, iterable), () => forBody(name));
  496. }
  497. // `for-in` statement.
  498. // With option `ownProperties` replaced with a `for-of` loop for object keys
  499. forIn(nameOrPrefix, obj, forBody, varKind = this.opts.es5 ? scope_1.varKinds.var : scope_1.varKinds.const) {
  500. if (this.opts.ownProperties) {
  501. return this.forOf(nameOrPrefix, code_1._ `Object.keys(${obj})`, forBody);
  502. }
  503. const name = this._scope.toName(nameOrPrefix);
  504. return this._for(new ForIter("in", varKind, name, obj), () => forBody(name));
  505. }
  506. // end `for` loop
  507. endFor() {
  508. return this._endBlockNode(For);
  509. }
  510. // `label` statement
  511. label(label) {
  512. return this._leafNode(new Label(label));
  513. }
  514. // `break` statement
  515. break(label) {
  516. return this._leafNode(new Break(label));
  517. }
  518. // `return` statement
  519. return(value) {
  520. const node = new Return();
  521. this._blockNode(node);
  522. this.code(value);
  523. if (node.nodes.length !== 1)
  524. throw new Error('CodeGen: "return" should have one node');
  525. return this._endBlockNode(Return);
  526. }
  527. // `try` statement
  528. try(tryBody, catchCode, finallyCode) {
  529. if (!catchCode && !finallyCode)
  530. throw new Error('CodeGen: "try" without "catch" and "finally"');
  531. const node = new Try();
  532. this._blockNode(node);
  533. this.code(tryBody);
  534. if (catchCode) {
  535. const error = this.name("e");
  536. this._currNode = node.catch = new Catch(error);
  537. catchCode(error);
  538. }
  539. if (finallyCode) {
  540. this._currNode = node.finally = new Finally();
  541. this.code(finallyCode);
  542. }
  543. return this._endBlockNode(Catch, Finally);
  544. }
  545. // `throw` statement
  546. throw(error) {
  547. return this._leafNode(new Throw(error));
  548. }
  549. // start self-balancing block
  550. block(body, nodeCount) {
  551. this._blockStarts.push(this._nodes.length);
  552. if (body)
  553. this.code(body).endBlock(nodeCount);
  554. return this;
  555. }
  556. // end the current self-balancing block
  557. endBlock(nodeCount) {
  558. const len = this._blockStarts.pop();
  559. if (len === undefined)
  560. throw new Error("CodeGen: not in self-balancing block");
  561. const toClose = this._nodes.length - len;
  562. if (toClose < 0 || (nodeCount !== undefined && toClose !== nodeCount)) {
  563. throw new Error(`CodeGen: wrong number of nodes: ${toClose} vs ${nodeCount} expected`);
  564. }
  565. this._nodes.length = len;
  566. return this;
  567. }
  568. // `function` heading (or definition if funcBody is passed)
  569. func(name, args = code_1.nil, async, funcBody) {
  570. this._blockNode(new Func(name, args, async));
  571. if (funcBody)
  572. this.code(funcBody).endFunc();
  573. return this;
  574. }
  575. // end function definition
  576. endFunc() {
  577. return this._endBlockNode(Func);
  578. }
  579. optimize(n = 1) {
  580. while (n-- > 0) {
  581. this._root.optimizeNodes();
  582. this._root.optimizeNames(this._root.names, this._constants);
  583. }
  584. }
  585. _leafNode(node) {
  586. this._currNode.nodes.push(node);
  587. return this;
  588. }
  589. _blockNode(node) {
  590. this._currNode.nodes.push(node);
  591. this._nodes.push(node);
  592. }
  593. _endBlockNode(N1, N2) {
  594. const n = this._currNode;
  595. if (n instanceof N1 || (N2 && n instanceof N2)) {
  596. this._nodes.pop();
  597. return this;
  598. }
  599. throw new Error(`CodeGen: not in block "${N2 ? `${N1.kind}/${N2.kind}` : N1.kind}"`);
  600. }
  601. _elseNode(node) {
  602. const n = this._currNode;
  603. if (!(n instanceof If)) {
  604. throw new Error('CodeGen: "else" without "if"');
  605. }
  606. this._currNode = n.else = node;
  607. return this;
  608. }
  609. get _root() {
  610. return this._nodes[0];
  611. }
  612. get _currNode() {
  613. const ns = this._nodes;
  614. return ns[ns.length - 1];
  615. }
  616. set _currNode(node) {
  617. const ns = this._nodes;
  618. ns[ns.length - 1] = node;
  619. }
  620. }
  621. exports.CodeGen = CodeGen;
  622. function addNames(names, from) {
  623. for (const n in from)
  624. names[n] = (names[n] || 0) + (from[n] || 0);
  625. return names;
  626. }
  627. function addExprNames(names, from) {
  628. return from instanceof code_1._CodeOrName ? addNames(names, from.names) : names;
  629. }
  630. function optimizeExpr(expr, names, constants) {
  631. if (expr instanceof code_1.Name)
  632. return replaceName(expr);
  633. if (!canOptimize(expr))
  634. return expr;
  635. return new code_1._Code(expr._items.reduce((items, c) => {
  636. if (c instanceof code_1.Name)
  637. c = replaceName(c);
  638. if (c instanceof code_1._Code)
  639. items.push(...c._items);
  640. else
  641. items.push(c);
  642. return items;
  643. }, []));
  644. function replaceName(n) {
  645. const c = constants[n.str];
  646. if (c === undefined || names[n.str] !== 1)
  647. return n;
  648. delete names[n.str];
  649. return c;
  650. }
  651. function canOptimize(e) {
  652. return (e instanceof code_1._Code &&
  653. e._items.some((c) => c instanceof code_1.Name && names[c.str] === 1 && constants[c.str] !== undefined));
  654. }
  655. }
  656. function subtractNames(names, from) {
  657. for (const n in from)
  658. names[n] = (names[n] || 0) - (from[n] || 0);
  659. }
  660. function not(x) {
  661. return typeof x == "boolean" || typeof x == "number" || x === null ? !x : code_1._ `!${par(x)}`;
  662. }
  663. exports.not = not;
  664. const andCode = mappend(exports.operators.AND);
  665. // boolean AND (&&) expression with the passed arguments
  666. function and(...args) {
  667. return args.reduce(andCode);
  668. }
  669. exports.and = and;
  670. const orCode = mappend(exports.operators.OR);
  671. // boolean OR (||) expression with the passed arguments
  672. function or(...args) {
  673. return args.reduce(orCode);
  674. }
  675. exports.or = or;
  676. function mappend(op) {
  677. return (x, y) => (x === code_1.nil ? y : y === code_1.nil ? x : code_1._ `${par(x)} ${op} ${par(y)}`);
  678. }
  679. function par(x) {
  680. return x instanceof code_1.Name ? x : code_1._ `(${x})`;
  681. }
  682. //# sourceMappingURL=index.js.map