@mojir/lits 2.2.3 → 2.3.0

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 (65) hide show
  1. package/README.md +1 -8
  2. package/dist/cli/cli.js +1264 -909
  3. package/dist/cli/src/Lits/Lits.d.ts +8 -2
  4. package/dist/cli/src/builtin/bindingNode.d.ts +2 -1
  5. package/dist/cli/src/builtin/interface.d.ts +3 -2
  6. package/dist/cli/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  7. package/dist/cli/src/evaluator/functionExecutors.d.ts +2 -1
  8. package/dist/cli/src/evaluator/index.d.ts +3 -2
  9. package/dist/cli/src/evaluator/interface.d.ts +3 -2
  10. package/dist/cli/src/utils/maybePromise.d.ts +54 -0
  11. package/dist/full.esm.js +1 -1
  12. package/dist/full.esm.js.map +1 -1
  13. package/dist/full.js +1 -1
  14. package/dist/full.js.map +1 -1
  15. package/dist/index.esm.js +1 -1
  16. package/dist/index.esm.js.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/lits.iife.js +1 -1
  20. package/dist/lits.iife.js.map +1 -1
  21. package/dist/modules/assert.esm.js +1 -1
  22. package/dist/modules/assert.esm.js.map +1 -1
  23. package/dist/modules/assert.js +1 -1
  24. package/dist/modules/assert.js.map +1 -1
  25. package/dist/modules/collection.esm.js +1 -1
  26. package/dist/modules/collection.esm.js.map +1 -1
  27. package/dist/modules/collection.js +1 -1
  28. package/dist/modules/collection.js.map +1 -1
  29. package/dist/modules/grid.esm.js +1 -1
  30. package/dist/modules/grid.esm.js.map +1 -1
  31. package/dist/modules/grid.js +1 -1
  32. package/dist/modules/grid.js.map +1 -1
  33. package/dist/modules/number-theory.esm.js +1 -1
  34. package/dist/modules/number-theory.esm.js.map +1 -1
  35. package/dist/modules/number-theory.js +1 -1
  36. package/dist/modules/number-theory.js.map +1 -1
  37. package/dist/modules/sequence.esm.js +1 -1
  38. package/dist/modules/sequence.esm.js.map +1 -1
  39. package/dist/modules/sequence.js +1 -1
  40. package/dist/modules/sequence.js.map +1 -1
  41. package/dist/modules/src/Lits/Lits.d.ts +8 -2
  42. package/dist/modules/src/builtin/bindingNode.d.ts +2 -1
  43. package/dist/modules/src/builtin/interface.d.ts +3 -2
  44. package/dist/modules/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  45. package/dist/modules/src/evaluator/functionExecutors.d.ts +2 -1
  46. package/dist/modules/src/evaluator/index.d.ts +3 -2
  47. package/dist/modules/src/evaluator/interface.d.ts +3 -2
  48. package/dist/modules/src/utils/maybePromise.d.ts +54 -0
  49. package/dist/modules/vector.esm.js +1 -1
  50. package/dist/modules/vector.esm.js.map +1 -1
  51. package/dist/modules/vector.js +1 -1
  52. package/dist/modules/vector.js.map +1 -1
  53. package/dist/src/Lits/Lits.d.ts +8 -2
  54. package/dist/src/builtin/bindingNode.d.ts +2 -1
  55. package/dist/src/builtin/interface.d.ts +3 -2
  56. package/dist/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  57. package/dist/src/evaluator/functionExecutors.d.ts +2 -1
  58. package/dist/src/evaluator/index.d.ts +3 -2
  59. package/dist/src/evaluator/interface.d.ts +3 -2
  60. package/dist/src/utils/maybePromise.d.ts +54 -0
  61. package/dist/testFramework.esm.js +1 -1
  62. package/dist/testFramework.esm.js.map +1 -1
  63. package/dist/testFramework.js +1 -1
  64. package/dist/testFramework.js.map +1 -1
  65. package/package.json +1 -1
package/dist/lits.iife.js CHANGED
@@ -1,2 +1,2 @@
1
- var Lits=function(e){"use strict";function t(e){if(!e.position||!e.code)return"";const t=e.position.column-1,n=e.code.length-t-1;return`${" ".repeat(Math.max(t,0))}^${" ".repeat(Math.max(n,0))}`}class n extends Error{params;constructor(e){super(`recur, params: ${e}`),Object.setPrototypeOf(this,n.prototype),this.name="RecurSignal",this.params=e}}class r extends Error{sourceCodeInfo;shortMessage;constructor(e,n){const o=e instanceof Error?e.message:`${e}`;super(function(e,n){if(!n)return e;const r=`${n.position.line}:${n.position.column}`;return`${e}${n.filePath?`\n${n.filePath}:${r}`:`\nLocation ${r}`}\n${n.code}\n${t(n)}`}(o,n)),this.shortMessage=o,this.sourceCodeInfo=n,Object.setPrototypeOf(this,r.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&t(this.sourceCodeInfo)}}class o extends r{userMessage;constructor(e,t){super(e,t),this.userMessage=e,Object.setPrototypeOf(this,o.prototype),this.name="UserDefinedError"}}class i extends r{symbol;constructor(e,t){super(`Undefined symbol '${e}'.`,t),this.symbol=e,Object.setPrototypeOf(this,i.prototype),this.name="UndefinedSymbolError"}}const s={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},a=new Set(Object.values(s));function u(e){return Object.keys(s).find(t=>s[t]===e)}const c=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const l="^^fn^^",f="^^re^^";function d(e){return null!==e&&"object"==typeof e&&(l in e&&"functionType"in e&&function(e){return"string"==typeof e&&c.has(e)}(e.functionType))}function h(e){return!(!Array.isArray(e)||e.length<2)&&function(e){return"number"==typeof e&&a.has(e)}(e[0])}function p(e){return d(e)?`<function ${e.name||"λ"}>`:h(e)?`${u(e[0])}-node`:null===e?"null":"object"==typeof e&&e instanceof RegExp?`${e}`:"object"==typeof e&&e instanceof Error?e.toString():JSON.stringify(e)}function y(e,t){return e?.sourceCodeInfo??t}function g(e,t,n){return new r(`Expected ${e}, got ${p(t)}.`,y(t,n))}function m(e,t){return v(e,t),e}function v(e,t){if(!function(e){return void 0!==e}(e))throw new r("Unexpected undefined",y(e,t))}function b(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)}function w(e){return null!==e&&"object"==typeof e&&!!e[l]}function x(e,t){if(!w(e))throw g("LitsFunction",e,t)}function S(e){return w(e)&&"UserDefined"===e.functionType}function E(e,t){if(!S(e))throw g("NativeJsFunction",e,t)}function k(e){return w(e)&&"NativeJsFunction"===e.functionType}function N(e,t){if(!k(e))throw g("NativeJsFunction",e,t)}function $(e,t={}){return"number"==typeof e&&(!Number.isNaN(e)&&(!(t.integer&&!Number.isInteger(e))&&(!(t.finite&&!Number.isFinite(e))&&((!t.zero||0===e)&&((!t.nonZero||0!==e)&&(!(t.positive&&e<=0)&&(!(t.negative&&e>=0)&&(!(t.nonPositive&&e>0)&&(!(t.nonNegative&&e<0)&&(!("number"==typeof t.gt&&e<=t.gt)&&(!("number"==typeof t.gte&&e<t.gte)&&(!("number"==typeof t.lt&&e>=t.lt)&&!("number"==typeof t.lte&&e>t.lte)))))))))))))}function P(e,t,n={}){if(!$(e,n))throw new r(`Expected ${function(e){if(e.zero)return"zero";const t=function(e){return e.positive?"positive":e.negative?"negative":e.nonNegative?"non negative":e.nonPositive?"non positive":e.nonZero?"non zero":""}(e),n=e.integer?"integer":"number",r=e.finite?"finite":"",o=function(e){return"number"!=typeof e.gt&&"number"!=typeof e.gte||"number"!=typeof e.lt&&"number"!=typeof e.lte?"number"==typeof e.gt||"number"==typeof e.gte?"number"==typeof e.gt?`n > ${e.gt}`:`n >= ${e.gte}`:"number"==typeof e.lt||"number"==typeof e.lte?"number"==typeof e.lt?`n < ${e.lt}`:`n <= ${e.lte}`:"":`${"number"==typeof e.gt?`${e.gt} < n `:`${e.gte} <= n `}${"number"==typeof e.lt?`< ${e.lt}`:`<= ${e.lte}`}`}(e);return[t,r,n,o].filter(e=>!!e).join(" ")}(n)}, got ${p(e)}.`,y(e,t))}function A(e){return void 0!==e}function I(e,t){return j(e,t),e}function j(e,t){if(!A(e))throw g("not undefined",e,t)}function C(e){return Array.isArray(e)||"string"==typeof e}function O(e,t){if(!C(e))throw g("string or array",e,t)}function U(e){return!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof RegExp||w(e)||B(e))}function F(e,t){if(!U(e))throw g("object",e,t)}function M(e){return C(e)||U(e)}function T(e,t){return _(e,t),e}function _(e,t){if(!M(e))throw g("string, array or object",e,t)}function B(e){return null!==e&&"object"==typeof e&&!!e[f]}function R(e,t){if(!function(e){return B(e)||"string"==typeof e}(e))throw g("string or RegularExpression",e,t)}function D(e,t){return z(e,t),e}function z(e,t){if(!function(e){return"number"==typeof e||!!M(e)||!!w(e)}(e))throw g("FunctionLike",e,t)}function L(e,t){const{min:n}=e;return!("number"==typeof n&&t<n)}function V(e,t,n){const{min:o,max:i}=e;if("number"==typeof o&&t<o)throw new r(`Wrong number of arguments, expected at least ${o}, got ${p(t)}.`,n);if("number"==typeof i&&t>i)throw new r(`Wrong number of arguments, expected at most ${i}, got ${p(t)}.`,n)}function J(e){return{min:e,max:e}}const W={"<<":{evaluate:([e,t],n)=>(P(e,n,{integer:!0}),P(t,n,{integer:!0,nonNegative:!0}),e<<t),arity:J(2)},">>":{evaluate:([e,t],n)=>(P(e,n,{integer:!0}),P(t,n,{integer:!0,nonNegative:!0}),e>>t),arity:J(2)},">>>":{evaluate:([e,t],n)=>(P(e,n,{integer:!0}),P(t,n,{integer:!0,nonNegative:!0}),e>>>t),arity:J(2)},"&":{evaluate:([e,...t],n)=>(P(e,n,{integer:!0}),t.reduce((e,t)=>(P(t,n,{integer:!0}),e&t),e)),arity:{min:2}},"|":{evaluate:([e,...t],n)=>(P(e,n,{integer:!0}),t.reduce((e,t)=>(P(t,n,{integer:!0}),e|t),e)),arity:{min:2}},xor:{evaluate:([e,...t],n)=>(P(e,n,{integer:!0}),t.reduce((e,t)=>(P(t,n,{integer:!0}),e^t),e)),arity:{min:2}}};function q(e,t={}){return"string"==typeof e&&((!t.nonEmpty||0!==e.length)&&(!t.char||1===e.length))}function Y(e,t,n={}){if(!q(e,n))throw g(""+(n.nonEmpty?"non empty string":n.char?"character":"string"),e,t)}function G(e,t,n={}){return Y(e,t,n),e}function K(e){return"string"==typeof e||"number"==typeof e}function X(e,t){return Z(e,t),e}function Z(e,t){if(!K(e))throw g("string or number",e,t)}function H(e,t){return!!M(e)&&("string"==typeof e||Array.isArray(e)?!!$(t,{integer:!0})&&(t>=0&&t<e.length):!!Object.getOwnPropertyDescriptor(e,t))}function Q(e,t,n){if(Z(e,n),Z(t,n),"string"==typeof e&&"string"==typeof t)return e<t?-1:e>t?1:0;if("number"==typeof e&&"number"==typeof t)return Math.sign(e-t);throw new r(`Cannot compare values of different types: ${typeof e} and ${typeof t}`,n)}function ee(e,t,n){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return function(e,t,n=oe){if(e===t)return!0;const r=Math.abs(e-t);if(0===e||0===t||r<n)return r<n;const o=Math.abs(e),i=Math.abs(t);return r/(o+i)<n}(e,t);if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r+=1)if(!ee(I(e[r],n),I(t[r],n),n))return!1;return!0}if(B(e)&&B(t))return e.s===t.s&&e.f===t.f;if(b(e)&&b(t)){const r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let o=0;o<r.length;o+=1){const i=G(r[o],n);if(!ee(e[i],t[i],n))return!1}return!0}return!1}function te(e){return e??null}function ne(...e){const t=new Set;for(const n of e)n.forEach(e=>t.add(e));return t}function re(e,t){t.forEach(t=>e.add(t))}const oe=1e-10;function ie(e,t=0){const n=e.split("\n");for(;n[0]?.match(/^\s*$/);)n.shift();for(;n[n.length-1]?.match(/^\s*$/);)n.pop();const r=n.reduce((e,t)=>{if(t.match(/^\s*$/))return e;const n=t.match(/^\s*/)[0].length;return Math.min(e,n)},1/0);return n.map(e=>" ".repeat(t)+e.slice(r)).join("\n").trimEnd()}function se(e,t){if(!Array.isArray(e))throw g("array",e,t)}function ae(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e)}(e))throw g("array of strings",e,t)}function ue(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e&&1===e.length)}(e))throw g("array of strings",e,t)}const ce={filter:{evaluate:([e,t],n,r,{executeFunction:o})=>{if(_(e,n),z(t,n),Array.isArray(e)){return e.filter(e=>o(t,[e],r,n))}return q(e)?e.split("").filter(e=>o(t,[e],r,n)).join(""):Object.entries(e).filter(([,e])=>o(t,[e],r,n)).reduce((e,[t,n])=>(e[t]=n,e),{})},arity:J(2)},map:{evaluate:(e,t,n,{executeFunction:o})=>{const i=D(e.at(-1),t);if(U(e[0]))return function({colls:e,contextStack:t,executeFunction:n,fn:o,sourceCodeInfo:i}){F(e[0],i);const s=Object.keys(e[0]),a={};return e.forEach(e=>{F(e,i);const t=Object.keys(e);if(t.length!==s.length)throw new r(`All objects must have the same keys. Expected: ${s.join(", ")}. Found: ${t.join(", ")}`,i);if(!t.every(e=>s.includes(e)))throw new r(`All objects must have the same keys. Expected: ${s.join(", ")}. Found: ${t.join(", ")}`,i);Object.entries(e).forEach(([e,t])=>{a[e]||(a[e]=[]),a[e].push(t)})}),s.reduce((e,r)=>(e[r]=n(o,a[r],t,i),e),{})}({colls:e.slice(0,-1),fn:i,sourceCodeInfo:t,contextStack:n,executeFunction:o});const s=e.slice(0,-1);O(s[0],t);const a="string"==typeof s[0];let u=s[0].length;s.slice(1).forEach(e=>{a?Y(e,t):se(e,t),u=Math.min(u,e.length)});const c=[];for(let e=0;e<u;e++)c.push(s.map(t=>t[e]));const l=c.map(e=>o(i,e,n,t));return a?(l.forEach(e=>Y(e,t)),l.join("")):l},arity:{min:2}},reduce:{evaluate:([e,t,n],r,o,{executeFunction:i})=>(_(e,r),z(t,r),j(n,r),"string"==typeof e?(Y(n,r),0===e.length?n:e.split("").reduce((e,n)=>i(t,[e,n],o,r),n)):Array.isArray(e)?0===e.length?n:e.reduce((e,n)=>i(t,[e,n],o,r),n):0===Object.keys(e).length?n:Object.entries(e).reduce((e,[,n])=>i(t,[e,n],o,r),n)),arity:J(3)},get:{evaluate:(e,t)=>{const[n,r]=e,o=te(e[2]);if(Z(r,t),null===n)return o;_(n,t);const i=function(e,t){if(U(e)){if("string"==typeof t&&H(e,t))return te(e[t])}else if($(t,{nonNegative:!0,integer:!0})&&t>=0&&t<e.length)return te(e[t])}(n,r);return void 0===i?o:i},arity:{min:2,max:3}},count:{evaluate:([e],t)=>null===e?0:"string"==typeof e?e.length:(_(e,t),Array.isArray(e)?e.length:Object.keys(e).length),arity:J(1)},"contains?":{evaluate:([e,t],n)=>null!==e&&(_(e,n),q(e)?(Y(t,n),e.includes(t)):C(e)?(j(t,n),!!e.find(e=>ee(I(e),t,n))):(Y(t,n),t in e)),arity:J(2)},assoc:{evaluate:([e,t,n],r)=>(_(e,r),Z(t,r),j(n,r),function(e,t,n,r){if(_(e,r),Z(t,r),Array.isArray(e)||"string"==typeof e){if(P(t,r,{integer:!0}),P(t,r,{gte:0}),P(t,r,{lte:e.length}),"string"==typeof e)return Y(n,r,{char:!0}),`${e.slice(0,t)}${n}${e.slice(t+1)}`;const o=[...e];return o[t]=n,o}Y(t,r);const o={...e};return o[t]=n,o}(e,t,n,r)),arity:J(3)},"++":{evaluate:(e,t)=>($(e[0])||_(e[0],t),Array.isArray(e[0])?e.reduce((e,n)=>(se(n,t),e.concat(n)),[]):K(e[0])?e.reduce((e,n)=>(Z(n,t),`${e}${n}`),""):e.reduce((e,n)=>(F(n,t),Object.assign(e,n)),{})),arity:{min:1}}},le={range:{evaluate:(e,t)=>{const[n,r,o]=e;let i,s,a;P(n,t,{finite:!0}),1===e.length?(i=0,s=n,a=s>=0?1:-1):2===e.length?(P(r,t,{finite:!0}),i=n,s=r,a=s>=i?1:-1):(P(r,t,{finite:!0}),P(o,t,{finite:!0}),i=n,s=r,a=o,P(a,t,s>i?{positive:!0}:s<i?{negative:!0}:{nonZero:!0}));const u=[];for(let e=i;a<0?e>s:e<s;e+=a)u.push(e);return u},arity:{min:1,max:3}},repeat:{evaluate:([e,t],n)=>{P(t,n,{integer:!0,nonNegative:!0});const r=[];for(let n=0;n<t;n+=1)r.push(e);return r},arity:J(2)},flatten:{evaluate:([e,t],n)=>{se(e,n);const r=void 0===t||t===Number.POSITIVE_INFINITY?Number.POSITIVE_INFINITY:function(e,t,n={}){return P(e,t,n),e}(t,n,{integer:!0,nonNegative:!0});return e.flat(r)},arity:{min:1,max:2}},mapcat:{evaluate:([e,t],n,r,{executeFunction:o})=>(se(e,n),z(t,n),e.map(e=>o(t,[e],r,n)).flat(1)),arity:J(2)},"moving-fn":{evaluate:([e,t,n],r,o,{executeFunction:i})=>{se(e,r),P(t,r,{integer:!0,lte:e.length}),z(n,r);const s=[];for(let a=0;a<=e.length-t;a++){const u=i(n,[e.slice(a,a+t)],o,r);s.push(u)}return s},arity:J(3)},"running-fn":{evaluate:([e,t],n,r,{executeFunction:o})=>{se(e,n),z(t,n);const i=[];for(let s=0;s<e.length;s+=1){const a=e.slice(0,s+1);i.push(o(t,[a],r,n))}return i},arity:J(2)}},fe={nth:{evaluate:(e,t)=>{const[n,r]=e,o=te(e[2]);if(P(r,t,{integer:!0}),null===n)return o;if(O(n,t),r>=0&&r<n.length){return te(n[r])}return o},arity:{min:2,max:3}},first:{evaluate:([e],t)=>{if(null===e)return null;O(e,t);return te(e[0])},arity:J(1)},last:{evaluate:([e],t)=>{if(null===e)return null;O(e,t);return te(e.at(-1))},arity:J(1)},pop:{evaluate:([e],t)=>(O(e,t),"string"==typeof e?e.substring(0,e.length-1):e.slice(0,e.length-1)),arity:J(1)},"index-of":{evaluate:([e,t],n)=>{if(j(t,n),null===e)return null;if(O(e,n),"string"==typeof e){Y(t,n);const r=e.indexOf(t);return-1!==r?r:null}{const r=e.findIndex(e=>ee(I(e,n),t),n);return-1!==r?r:null}},arity:J(2)},push:{evaluate:([e,...t],n)=>(O(e,n),"string"==typeof e?(ue(t,n),[e,...t].join("")):[...e,...t]),arity:{min:2}},rest:{evaluate:([e],t)=>(O(e,t),Array.isArray(e)?e.length<=1?[]:e.slice(1):e.substring(1)),arity:J(1)},next:{evaluate:([e],t)=>(O(e,t),Array.isArray(e)?e.length<=1?null:e.slice(1):e.length<=1?null:e.substring(1)),arity:J(1)},reverse:{evaluate:([e],t)=>null===e?null:(O(e,t),Array.isArray(e)?[...e].reverse():e.split("").reverse().join("")),arity:J(1)},second:{evaluate:([e],t)=>null===e?null:(O(e,t),te(e[1])),arity:J(1)},slice:{evaluate:(e,t)=>{const[n,r,o]=e;return O(n,t),P(r,t,{integer:!0}),2===e.length?(Array.isArray(n),n.slice(r)):(P(o,t,{integer:!0}),Array.isArray(n),n.slice(r,o))},arity:{min:2,max:3}},some:{evaluate:([e,t],n,r,{executeFunction:o})=>(z(t,n),null===e?null:(O(e,n),0===e.length?null:"string"==typeof e?e.split("").find(e=>o(t,[e],r,n))??null:te(e.find(e=>o(t,[e],r,n))))),arity:J(2)},sort:{evaluate:(e,t,n,{executeFunction:r})=>{const[o]=e,i=1===e.length,s=i?null:e[1];if(O(o,t),"string"==typeof o){const e=o.split("");return i?e.sort((e,n)=>Q(e,n,t)):(z(s,t),e.sort((e,o)=>{const i=r(s,[e,o],n,t);return P(i,t,{finite:!0}),i})),e.join("")}const a=[...o];return i?a.sort((e,n)=>(Z(e,t),Z(n,t),Q(e,n,t))):a.sort((e,o)=>{z(s,t);const i=r(s,[e,o],n,t);return P(i,t,{finite:!0}),i}),a},arity:{min:1,max:2}}},de=new WeakSet,he=new WeakSet,pe=new WeakSet,ye=new WeakSet,ge=new WeakSet,me=new WeakSet,ve=new WeakSet;function be(e){return Array.isArray(e)?(de.has(e)||(we(e),ke(e)||Ee(e)),e):e}function we(e){return!!Array.isArray(e)&&(!!he.has(e)||!pe.has(e)&&(e.every(e=>$(e))?(de.add(e),he.add(e),!0):(pe.add(e),!1)))}function xe(e,t){if(!we(e))throw new r(`Expected a vector, but got ${e}`,t)}function Se(e,t){if(xe(e,t),0===e.length)throw new r(`Expected a non empty vector, but got ${e}`,t)}function Ee(e){if(!Array.isArray(e))return!1;if(me.has(e))return!0;if(ve.has(e))return!1;if(0===e.length)return ve.add(e),!1;if(!Array.isArray(e[0]))return ve.add(e),!1;const t=e[0].length;for(const n of e.slice(1)){if(!Array.isArray(n))return ve.add(e),!1;if(n.length!==t)return ve.add(e),!1}return de.add(e),me.add(e),!0}function ke(e){if(!Array.isArray(e))return!1;if(ye.has(e))return!0;if(ge.has(e))return!1;if(0===e.length)return ge.add(e),!1;if(!Array.isArray(e[0])||0===e[0].length)return ge.add(e),!1;const t=e[0].length;for(const n of e)if(!Array.isArray(n)||n.length!==t||n.some(e=>!$(e)))return ge.add(e),!1;return de.add(e),me.add(e),ye.add(e),!0}function Ne(e,t){let n=!1,o=!1;for(const i of e)if(we(i))n=!0;else if(ke(i))o=!0;else if(!$(i))throw new r("Invalid parameter type: "+typeof i,t);if(o){if(n)throw new r("Cannot mix vector and matrix types",t);let o=null,i=null;for(const n of e)if(ke(n))if(null===o)o=n.length,i=n[0].length;else if(n.length!==o||n[0].length!==i)throw new r("Matrix dimensions do not match",t);return["matrix",e.map(e=>ke(e)?e:Array.from({length:o},()=>Array.from({length:i},()=>e)))]}if(n){let n=null;for(const o of e)if(we(o))if(null===n)n=o.length;else if(o.length!==n)throw new r("Vector lengths do not match",t);return["vector",e.map(e=>we(e)?e:Array.from({length:n},()=>e))]}return["number",e]}function $e(e){return(t,n)=>{const[r,o]=Ne(t,n);return"number"===r?e(o[0]):"vector"===r?o[0].map(t=>e(t)):o[0].map(t=>t.map(t=>e(t)))}}function Pe(e){return(t,n)=>{const[r,o]=Ne(t,n);return"number"===r?e(o[0],o[1]):"vector"===r?o[0].map((t,n)=>e(t,o[1][n])):o[0].map((t,n)=>t.map((t,r)=>e(t,o[1][n][r])))}}function Ae(e,t){return(n,r)=>{if(0===n.length)return e;const[o,i]=Ne(n,r);if("number"===o)return i.reduce((e,n)=>t(e,n),e);if("vector"===o){const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>t(e,n[r])),e)}{const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>e.map((e,o)=>t(e,n[r][o]))),e)}}}const Ie={inc:{evaluate:$e(e=>e+1),arity:J(1)},dec:{evaluate:$e(e=>e-1),arity:J(1)},"+":{evaluate:Ae(0,(e,t)=>e+t),arity:{}},"*":{evaluate:Ae(1,(e,t)=>e*t),arity:{}},"/":{evaluate:(e,t)=>{if(0===e.length)return 1;const[n,r]=Ne(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?1/e:t.reduce((e,t)=>e/t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e/t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e/t[n][r])),e)}},arity:{}},"-":{evaluate:(e,t)=>{if(0===e.length)return 0;const[n,r]=Ne(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?-e:t.reduce((e,t)=>e-t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e-t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e-t[n][r])),e)}},arity:{}},quot:{evaluate:Pe((e,t)=>Math.trunc(e/t)),arity:J(2)},mod:{evaluate:Pe((e,t)=>e-t*Math.floor(e/t)),arity:J(2)},"%":{evaluate:Pe((e,t)=>e%t),arity:J(2)},sqrt:{evaluate:$e(e=>Math.sqrt(e)),arity:J(1)},cbrt:{evaluate:$e(e=>Math.cbrt(e)),arity:J(1)},"^":{evaluate:Pe((e,t)=>e**t),arity:J(2)},round:{evaluate:([e,t],n)=>{const[r,o]=Ne([e],n);if("number"===r){if(void 0===t||0===t)return Math.round(o[0]);{P(t,n,{integer:!0,positive:!0});const e=10**t;return Math.round(o[0]*e)/e}}if("vector"===r){const e=o[0];if(void 0===t||0===t)return e.map(e=>Math.round(e));{P(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>Math.round(e*r)/r)}}{const e=o[0];if(void 0===t||0===t)return e.map(e=>e.map(e=>Math.round(e)));{P(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>e.map(e=>Math.round(e*r)/r))}}},arity:{min:1,max:2}},trunc:{evaluate:$e(e=>Math.trunc(e)),arity:J(1)},floor:{evaluate:$e(e=>Math.floor(e)),arity:J(1)},ceil:{evaluate:$e(e=>Math.ceil(e)),arity:J(1)},min:{evaluate:(e,t)=>{if(1===e.length&&we(e[0])){const n=e[0];return Se(n,t),n.reduce((e,t)=>Math.min(e,t),1/0)}const[n,...r]=e;return P(n,t),r.reduce((e,n)=>(P(n,t),Math.min(e,n)),n)},arity:{min:1}},max:{evaluate:(e,t)=>{if(1===e.length&&we(e[0])){const n=e[0];return Se(n,t),n.reduce((e,t)=>Math.max(e,t),-1/0)}const[n,...r]=e;return P(n,t),r.reduce((e,n)=>(P(n,t),Math.max(e,n)),n)},arity:{min:1}},abs:{evaluate:$e(e=>Math.abs(e)),arity:J(1)},sign:{evaluate:$e(e=>Math.sign(e)),arity:J(1)}};function je([e,...t],n){const r=I(e,n);for(const e of t)if(!ee(r,I(e,n),n))return!1;return!0}const Ce={"==":{evaluate:(e,t)=>je(e,t),arity:{min:1}},"≠":{evaluate:(e,t)=>!je(e,t),arity:{min:1}},"identical?":{evaluate:e=>function([e,...t]){for(const n of t)if(n!==e)return!1;return!0}(e),arity:{min:1}},">":{evaluate:([e,...t],n)=>{let r=X(e);for(const e of t){if(Q(r,X(e),n)<=0)return!1;r=X(e)}return!0},arity:{min:1}},"<":{evaluate:([e,...t],n)=>{let r=X(e);for(const e of t){if(Q(r,X(e),n)>=0)return!1;r=X(e)}return!0},arity:{min:1}},">=":{evaluate:([e,...t],n)=>{let r=X(e);for(const e of t){if(Q(r,X(e),n)<0)return!1;r=X(e)}return!0},arity:{min:1}},"<=":{evaluate:([e,...t],n)=>{let r=X(e);for(const e of t){if(Q(r,X(e),n)>0)return!1;r=X(e)}return!0},arity:{min:1}},"!":{evaluate:([e])=>!e,arity:J(1)},"epoch->iso-date":{evaluate:([e],t)=>(P(e,t),new Date(e).toISOString()),arity:J(1)},"iso-date->epoch":{evaluate:([e],t)=>{Y(e,t);const n=new Date(e).valueOf();return P(n,t,{finite:!0}),n},arity:J(1)},"write!":{evaluate:(e,t)=>(console.log(...e),e.length>0?I(e[e.length-1],t):null),arity:{}},boolean:{evaluate:([e])=>!!e,arity:J(1)},compare:{evaluate:([e,t],n)=>(Z(e,n),Z(t,n),Q(e,t,n)),arity:J(2)},"json-parse":{evaluate:([e],t)=>(Y(e,t),JSON.parse(e)),arity:J(1)},"json-stringify":{evaluate:([e,t],n)=>(j(e,n),void 0===t?JSON.stringify(e):(P(t,n),JSON.stringify(e,null,t))),arity:{min:1,max:2}},import:{evaluate:([e],t,n)=>{Y(e,t);const o=e.indexOf(".");if(-1!==o){const i=e.substring(0,o),s=e.substring(o+1),a=n.getModule(i);if(!a)throw new r(`Unknown module: '${i}'`,t);const u=a.functions[s];if(!u)throw new r(`Function '${s}' not found in module '${i}'`,t);return{[l]:!0,sourceCodeInfo:t,functionType:"Module",moduleName:i,functionName:s,arity:u.arity}}const i=e,s=n.getModule(i);if(!s)throw new r(`Unknown module: '${i}'`,t);const a={};for(const[e,n]of Object.entries(s.functions))a[e]={[l]:!0,sourceCodeInfo:t,functionType:"Module",moduleName:i,functionName:e,arity:n.arity};return a},arity:J(1)}},Oe={keys:{evaluate:([e],t)=>(F(e,t),Object.keys(e)),arity:J(1)},vals:{evaluate:([e],t)=>(F(e,t),Object.values(e)),arity:J(1)},entries:{evaluate:([e],t)=>(F(e,t),Object.entries(e)),arity:J(1)},find:{evaluate:([e,t],n)=>(F(e,n),Y(t,n),H(e,t)?[t,e[t]]:null),arity:J(2)},dissoc:{evaluate:([e,t],n)=>{F(e,n),Y(t,n);const r={...e};return delete r[t],r},arity:J(2)},merge:{evaluate:(e,t)=>{if(0===e.length)return null;const[n,...r]=e;return F(n,t),r.reduce((e,n)=>(F(n,t),{...e,...n}),{...n})},arity:{min:0}},"merge-with":{evaluate:(e,t,n,{executeFunction:r})=>{const o=e[0],i=e.at(-1),s=e.slice(1,-1);return F(o,t),z(i,t),s.reduce((e,o)=>(F(o,t),Object.entries(o).forEach(o=>{const s=G(o[0],t),a=te(o[1]);H(e,s)?e[s]=r(i,[e[s],a],n,t):e[s]=a}),e),{...o})},arity:{min:2}},zipmap:{evaluate:([e,t],n)=>{ae(e,n),se(t,n);const r=Math.min(e.length,t.length),o={};for(let i=0;i<r;i+=1){o[G(e[i],n)]=te(t[i])}return o},arity:J(2)},"select-keys":{evaluate:([e,t],n)=>(ae(t,n),F(e,n),t.reduce((t,n)=>(H(e,n)&&(t[n]=te(e[n])),t),{})),arity:J(2)}},Ue={"function?":{evaluate:([e])=>w(e),arity:J(1)},"string?":{evaluate:([e])=>"string"==typeof e,arity:J(1)},"number?":{evaluate:([e])=>"number"==typeof e,arity:J(1)},"integer?":{evaluate:([e])=>"number"==typeof e&&$(e,{integer:!0}),arity:J(1)},"boolean?":{evaluate:([e])=>"boolean"==typeof e,arity:J(1)},"null?":{evaluate:([e])=>null==e,arity:J(1)},"zero?":{evaluate:([e],t)=>(P(e,t,{finite:!0}),Math.abs(e)<oe),arity:J(1)},"pos?":{evaluate:([e],t)=>(P(e,t,{finite:!0}),e>0),arity:J(1)},"neg?":{evaluate:([e],t)=>(P(e,t,{finite:!0}),e<0),arity:J(1)},"even?":{evaluate:([e],t)=>(P(e,t,{finite:!0}),e%2==0),arity:J(1)},"odd?":{evaluate:([e],t)=>(P(e,t,{finite:!0}),$(e,{integer:!0})&&e%2!=0),arity:J(1)},"array?":{evaluate:([e])=>Array.isArray(e),arity:J(1)},"collection?":{evaluate:([e])=>M(e),arity:J(1)},"sequence?":{evaluate:([e])=>C(e),arity:J(1)},"object?":{evaluate:([e])=>U(e),arity:J(1)},"regexp?":{evaluate:([e])=>B(e),arity:J(1)},"finite?":{evaluate:([e],t)=>(P(e,t),Number.isFinite(e)),arity:J(1)},"positive-infinity?":{evaluate:([e],t)=>(P(e,t),e===Number.POSITIVE_INFINITY),arity:J(1)},"negative-infinity?":{evaluate:([e],t)=>(P(e,t),e===Number.NEGATIVE_INFINITY),arity:J(1)},"true?":{evaluate:([e])=>!0===e,arity:J(1)},"false?":{evaluate:([e])=>!1===e,arity:J(1)},"empty?":{evaluate:([e],t)=>null===e||(_(e,t),"string"==typeof e||Array.isArray(e)?0===e.length:0===Object.keys(e).length),arity:J(1)},"not-empty?":{evaluate:([e],t)=>null!==e&&(_(e,t),"string"==typeof e||Array.isArray(e)?e.length>0:Object.keys(e).length>0),arity:J(1)},"vector?":{evaluate:([e])=>we(e),arity:J(1)},"matrix?":{evaluate:([e])=>ke(e),arity:J(1)},"grid?":{evaluate:([e])=>Ee(e),arity:J(1)}},Fe={regexp:{evaluate:([e,t],n)=>{Y(e,n);const o=e||"(?:)",i="string"==typeof t?t:"";try{new RegExp(o,i)}catch(e){throw new r(`Invalid regular expression: ${o} ${i}`,n)}return{[f]:!0,sourceCodeInfo:n,s:o,f:i}},arity:{min:1,max:2}},match:{evaluate:([e,t],n)=>{if(function(e,t){if(!B(e))throw g("RegularExpression",e,t)}(t,n),!q(e))return null;const r=new RegExp(t.s,t.f).exec(e);return r?[...r]:null},arity:J(2)},replace:{evaluate:([e,t,n],r)=>{Y(e,r),R(t,r),Y(n,r);const o=B(t)?new RegExp(t.s,`${t.f}`):t;return e.replace(o,n)},arity:J(3)},"replace-all":{evaluate:([e,t,n],r)=>{Y(e,r),R(t,r),Y(n,r);const o=B(t)?new RegExp(t.s,`${t.f.includes("g")?t.f:`${t.f}g`}`):t;return e.replaceAll(o,n)},arity:J(3)}},Me=/^\s*$/,Te={str:{evaluate:e=>e.reduce((e,t)=>e+(null==t?"":U(t)||Array.isArray(t)?JSON.stringify(t):`${t}`),""),arity:{}},number:{evaluate:([e],t)=>{Y(e,t);const n=Number(e);if(Number.isNaN(n))throw new r(`Could not convert '${e}' to a number.`,t);return n},arity:J(1)},"lower-case":{evaluate:([e],t)=>(Y(e,t),e.toLowerCase()),arity:J(1)},"upper-case":{evaluate:([e],t)=>(Y(e,t),e.toUpperCase()),arity:J(1)},trim:{evaluate:([e],t)=>(Y(e,t),e.trim()),arity:J(1)},join:{evaluate:([e,t],n)=>(se(e,n),e.forEach(e=>Z(e,n)),Y(t,n),e.join(t)),arity:J(2)},split:{evaluate:([e,t,n],r)=>{Y(e,r),R(t,r),void 0!==n&&P(n,r,{integer:!0,nonNegative:!0});const o="string"==typeof t?t:new RegExp(t.s,t.f);return e.split(o,n)},arity:{min:2,max:3}},"blank?":{evaluate:([e],t)=>null===e||(Y(e,t),Me.test(e)),arity:J(1)}},_e={"|>":{evaluate:([e,t],n,r,{executeFunction:o})=>(z(t,n),o(t,[e],r,n)),arity:J(2)},apply:{evaluate:([e,...t],n,r,{executeFunction:o})=>{z(e,n);const i=t[t.length-1];se(i,n);return o(e,[...t.slice(0,-1),...i],r,n)},arity:{min:2}},identity:{evaluate:([e])=>te(e),arity:J(1)},comp:{evaluate:(e,t)=>{return e.forEach(e=>z(e,t)),{[l]:!0,sourceCodeInfo:t,functionType:"Comp",params:e,arity:e.length>0?(n=e.at(-1),"number"==typeof n||M(n)?J(1):n.arity):{min:1,max:1}};var n},arity:{}},constantly:{evaluate:([e],t)=>({[l]:!0,sourceCodeInfo:t,functionType:"Constantly",value:te(e),arity:{}}),arity:J(1)}};function Be(e){return ie(`\n ${e.title}\n\n ${e.description.replace(/`(.+?)`/g,"$1").replace(/\$(\w+)/g,"$1").replace(/\*\*\*(.+)\*\*\*/g,"$1").replace(/\*\*(.+)\*\*/g,"$1")}\n\n Signature:\n ${function({title:e,variants:t,args:n,returns:r,_isOperator:o}){const i=t.map(t=>`${` ${e}(${t.argumentNames.map(e=>{let t="";return n[e].rest&&(t+="..."),t+=e,t}).join(", ")})`} -> ${Re(r)}`),s=o?["","Operator:",` a ${e} b -> ${Re(r)}`]:[];return[...i,...s]}(e).join("\n ")}\n\n Arguments:\n ${function(e){return Object.entries(e.args).map(([e,t])=>`${e}: ${Re(t)}`)}(e).join("\n ")}\n\n Examples:\n${e.examples.map(e=>ie(e,4)).join("\n\n")}`)}function Re(e){const t=e.type,n=(Array.isArray(t)?t:[t]).join(" | ");return e.array||e.rest?`Array<${n}>`:n}const De={sum:{evaluate:([e],t)=>(xe(e,t),e.reduce((e,t)=>e+t,0)),arity:J(1)},prod:{evaluate:([e],t)=>(xe(e,t),e.reduce((e,t)=>e*t,1)),arity:J(1)},mean:{evaluate:([e],t)=>(Se(e,t),e.reduce((e,t)=>e+t,0)/e.length),arity:J(1)},median:{evaluate:([e],t)=>(Se(e,t),function(e){const t=[...e].sort((e,t)=>e-t),n=Math.floor(t.length/2);return t.length%2==0?(t[n-1]+t[n])/2:t[n]}(e)),arity:J(1)}},ze={...W,...ce,...le,...fe,...Ie,...function(e){return{doc:{evaluate:([t],n)=>{if(v(e),z(t,n),!w(t))return"";if("Builtin"===t.functionType){const n=e[t.name];return n?Be(n):""}return"UserDefined"===t.functionType||"NativeJsFunction"===t.functionType?t.docString:""},arity:J(1)},arity:{evaluate:([e],t)=>(z(e,t),w(e)?e.arity:J(1)),arity:J(1)}}}({}),...Ce,...Oe,...Ue,...Fe,...Te,..._e,...De};Object.entries(ze).forEach(([e,t])=>{t.name=e});const Le={...ze},Ve={},Je=[];Object.entries(Le).forEach(([e,t],n)=>{Ve[e]=n,Je.push(t)});const We={arity:{},evaluate:(e,t,{evaluateNode:n})=>{let r=!0;for(const o of e[1][1])if(r=n(o,t),!r)break;return r},evaluateAsNormalExpression:(e,t)=>{let n=!0;for(const r of e)if(n=I(r,t),!n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)};function qe(e){const t=e[0];return s.UserDefinedSymbol===t||s.NormalBuiltinSymbol===t||s.SpecialBuiltinSymbol===t}function Ye(e){return s.UserDefinedSymbol===e[0]}function Ge(e,t){return function(e,t){if(!Ye(e))throw g("UserDefinedSymbolNode",e,t)}(e,t),e}function Ke(e){return s.NormalBuiltinSymbol===e[0]}function Xe(e){return s.SpecialBuiltinSymbol===e[0]}function Ze(e){return!!function(e){return e[0]===s.NormalExpression}(e)&&qe(e[1][0])}function He(e){return e[0]===s.Spread}const Qe={arity:J(1),evaluate:(e,t)=>{const n=e[1][1];if(function(e,t){if(!qe(e))throw g("SymbolNode",e,t)}(n),!Ye(n))return!0;return null!==t.lookUp(n)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)};const et=11,tt=12,nt=13,rt=14;function ot(e,t){if(e[0]===nt)Object.values(e[1][0]).forEach(e=>{e[1][1]&&t(e[1][1]),ot(e,t)});else if(e[0]===rt)for(let n=0;n<e[1][0].length;n+=1){const r=e[1][0][n]??null;null!==r&&(r[1][1]&&t(r[1][1]),ot(r,t))}}function it(e,t,n){const r={};return st(e,t,n,e[2],r),r}function st(e,t,n,o,i){if(e[0]===nt){!function(e,t){if(!b(e))throw new r(`Expected UnknownRecord, got ${p(e)}.`,y(e,t))}(t,o);const s=new Set;let a;if(Object.entries(e[1][0]).forEach(([e,r])=>{if(r[0]===tt)return void(a=r);s.add(e);const u=(void 0!==t[e]?t[e]:r[1][1]&&n(r[1][1]))??null;j(u,o),st(r,u,n,o,i)}),a){const e=Object.entries(t).filter(([e])=>!s.has(e)).reduce((e,[t,n])=>(e[t]=I(n),e),{});i[a[1][0]]=e}}else if(e[0]===rt){let r=null;se(t,o);for(let s=0;s<e[1][0].length;s+=1){const a=e[1][0][s]??null;if(null===a)continue;if(a[0]===tt){r=s;break}const u=(void 0!==t[s]?t[s]:a[1][1]&&n(a[1][1]))??null;j(u,o),st(a,u,n,o,i)}if(null!==r){const n=t.slice(r),o=e[1][0][r];i[o[1][0]]=n}}else e[0]===tt?i[e[1][0]]=I(t):i[e[1][0][1]]=I(t)}function at(e){const t={};return ut(e,t),t}function ut(e,t){if(null!==e)if(e[0]===rt)for(const n of e[1][0])ut(n,t);else if(e[0]===nt)for(const n of Object.values(e[1][0]))ut(n,t);else if(e[0]===tt){if(t[e[1][0]])throw new r(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0]]=!0}else{if(t[e[1][0][1]])throw new r(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0][1]]=!0}}const ct={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1],o=r[1][0],i=r[1][1],s=n(i,t),a=it(o,s,e=>n(e,t));return t.exportValues(a,o[2]),s},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i[1][0],a=i[1][1],u=n([a],t,r,o);return ot(s,e=>{re(u,n([e],t,r,o))}),t.addValues(at(s),s[2]),u}},lt={arity:{},evaluate:(e,t,{builtin:n,getUndefinedSymbols:r,evaluateNode:o})=>{const i=e[1][1],s=e[1][2],a=function(e,t,n,r,o){const i={},s=e[0].reduce((e,t)=>(Object.keys(at(t)).forEach(t=>{e[t]={value:null}}),e),{}),a=r(e[1],t.new(s),n,o);a.forEach(e=>{const n=t.getValue(e);A(n)&&(i[e]={value:n})});const u=[e[0],e[1],i];return u}(i,t,n,r,o),u=a[0].filter(e=>e[0]!==tt&&void 0===e[1][1]).length,c={min:u>0?u:void 0,max:a[0].some(e=>e[0]===tt)?void 0:a[0].length};return{[l]:!0,sourceCodeInfo:e[2],functionType:"UserDefined",name:void 0,evaluatedfunction:a,arity:c,docString:s}},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>function(e,t,n,r,o){const i=new Set,s={self:{value:null}};e[0].forEach(e=>{Object.assign(s,at(e)),ot(e,e=>{re(i,n([e],t,r,o))})});const a=t.create(s),u=n(e[1],a,r,o);return re(i,u),i}(e[1][1],t,n,r,o)};const ft={arity:J(0),evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1],o=r[1][0],i=r[1][1],s=n(i,t),a=it(o,s,e=>n(e,t));return t.addValues(a,o[2]),s},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i[1][0],a=i[1][1],u=n([a],t,r,o);return ot(s,e=>{re(u,n([e],t,r,o))}),t.addValues(at(s),s[2]),u}},dt={arity:{},evaluate:(e,t,{evaluateNode:o})=>{const i=e[1][1],s=i.reduce((e,n)=>{const r=o(n[1][1],t.create(e)),i=it(n[1][0],r,e=>o(e,t));return Object.entries(i).forEach(([t,n])=>{e[t]={value:n}}),e},{}),a=t.create(s),u=e[1][2];for(;;){let c=null;try{c=o(u,a)}catch(a){if(a instanceof n){const n=a.params;if(n.length!==i.length)throw new r(`recur expected ${i.length} parameters, got ${p(n.length)}`,e[2]);i.forEach((e,r)=>{const i=it(e[1][0],I(n[r]),e=>o(e,t));for(const[e,t]of Object.entries(i))s[e].value=t});continue}throw a}return c}},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i.reduce((e,t)=>{const n=at(t[1][0]);return Object.keys(n).forEach(t=>{e[t]={value:!0}}),e},{});return ne(n(i.map(e=>e[1][1]),t,r,o),n([e[1][2]],t.create(s),r,o))}};function ht(e,t,n,r){for(const o of e){const[e,i]=o[1],s=it(e,r(i,n),e=>r(e,n));Object.entries(s).forEach(([e,n])=>{t[e]={value:n}})}}function pt(e,t,n,r){const o=t[2],[,i,s]=t[1],a=[],u=i.map(()=>0);let c=!1;for(;!c;){const t={},l=n.create(t);let f=!1;e:for(let e=0;e<i.length;e+=1){const[n,s,a,d]=i[e],[h,p]=n[1],y=T(r(p,l),o),g=C(y)?y:Object.entries(y);if(0===g.length){f=!0,c=!0;break}const v=m(u[e],o);if(v>=g.length){if(f=!0,0===e){c=!0;break}u[e]=0,u[e-1]=m(u[e-1],o)+1;break}const b=it(h,I(g[v],o),e=>r(e,l));if(Object.entries(b).forEach(([e,n])=>{t[e]={value:n}}),s&&ht(s,t,l,r),a&&!r(a,l)){u[e]=m(u[e],o)+1,f=!0;break e}if(d&&!r(d,l)){u[e]=Number.POSITIVE_INFINITY,f=!0;break e}}if(!f){const t=r(s,l);e&&a.push(t),u.length>0&&(u[u.length-1]+=1)}}return e?a:null}function yt(e,t,n,r,o){const i=new Set,s={},[,a,u]=e[1];return a.forEach(e=>{const[a,u,c,l]=e,[f,d]=a[1];n([d],t.create(s),r,o).forEach(e=>i.add(e)),Object.assign(s,at(f)),u&&u.forEach(e=>{const[a,u]=e[1];n([u],t.create(s),r,o).forEach(e=>i.add(e)),Object.assign(s,at(a))}),c&&n([c],t.create(s),r,o).forEach(e=>i.add(e)),l&&n([l],t.create(s),r,o).forEach(e=>i.add(e))}),n([u],t.create(s),r,o).forEach(e=>i.add(e)),i}const gt={arity:J(1),evaluate:(e,t,n)=>pt(!0,e,t,n.evaluateNode),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>yt(e,t,n,r,o)},mt={arity:J(1),evaluate:(e,t,n)=>(pt(!1,e,t,n.evaluateNode),null),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>yt(e,t,n,r,o)},vt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{let r=!1;for(const o of e[1][1])if(r=n(o,t),r)break;return r},evaluateAsNormalExpression:(e,t)=>{let n=!1;for(const r of e)if(n=I(r,t),n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},bt={arity:{min:1},evaluate:(e,t,{evaluateNode:n})=>{for(const r of e[1][1]){if(Ye(r)&&null===t.lookUp(r))continue;const e=n(r,t);if(null!==e)return e}return null},evaluateAsNormalExpression:(e,t)=>{for(const n of e){const e=I(n,t);if(null!==e)return e}return null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},wt={arity:{},evaluate:(e,t,{evaluateNode:r})=>{const o=e[1][1].map(e=>r(e,t));throw new n(o)},evaluateAsNormalExpression:e=>{throw new n(e)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},xt={arity:J(1),evaluate:(e,t,{evaluateNode:n})=>{const r=G(n(e[1][1],t),e[2],{nonEmpty:!0});throw new o(r,e[2])},evaluateAsNormalExpression:(e,t)=>{const n=G(e[0],t,{nonEmpty:!0});throw new o(n,void 0)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)},St={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o,i]=e[1];try{return n(r,t)}catch(e){const r=o?{[o[1]]:{value:e}}:{};return n(i,t.create(r))}},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const[,i,s,a]=e[1],u=n([i],t,r,o),c=s?{[s[1]]:{value:!0}}:{};return ne(u,n([a],t.create(c),r,o))}},Et={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const o=[];for(const i of e[1][1])if(He(i)){const e=n(i[1],t);if(!Array.isArray(e))throw new r("Spread value is not an array",i[2]);o.push(...e)}else o.push(n(i,t));return o},evaluateAsNormalExpression:(e,t)=>{const n=[];for(const r of e)n.push(I(r,t));return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},kt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const o={},i=e[1][1];for(let e=0;e<i.length;e+=2){const s=i[e];if(He(s)){const i=n(s[1],t);if(!b(i))throw new r("Spread value is not an object",s[2]);Object.assign(o,i),e-=1}else{const a=n(s,t),u=i[e+1];if(void 0===u)throw new r("Missing value for key",s[2]);const c=n(u,t);Y(a,s[2]),o[a]=c}}return o},evaluateAsNormalExpression:(e,t)=>{const n={};for(let r=0;r<e.length;r+=2){const o=e[r],i=e[r+1];Y(o,t),n[o]=i??null}return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Nt={"??":0,"&&":1,"||":2,array:3,cond:4,"0_def":5,"defined?":6,block:7,doseq:8,"0_lambda":9,for:10,if:11,let:12,loop:13,object:14,recur:15,switch:16,throw:17,try:18,unless:19},$t=[bt,We,vt,Et,{arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1];for(const[e,o]of r){if(n(e,t))return n(o,t)}return null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].flat(),t,r,o)},ct,Qe,{arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=t.create({});let o=null;for(const t of e[1][1])o=n(t,r);return o},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t.create({}),r,o)},mt,lt,gt,{arity:{min:2,max:3},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return n(r,t)?n(o,t):i?n(i,t):null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)},ft,dt,kt,wt,{arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o]=e[1],i=n(r,t);for(const[e,r]of o){if(n(e,t)===i)return n(r,t)}return null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1],...e[1][2].flat()],t,r,o)},xt,St,{arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return n(r,t)?i?n(i,t):null:n(o,t)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)}],Pt={normalExpressions:Le,specialExpressions:$t,allNormalExpressions:Je},At=Object.keys(Le),It=Object.keys(Nt),jt=(e,t,n,r)=>{const o=Array.isArray(e)?e:[[s.SpecialExpression,[Nt.block,e.body]]],i=new Set;for(const e of o)Ct(e,t,n,r)?.forEach(e=>i.add(e));return i};function Ct(e,t,n,o){const i=e[0];switch(i){case s.UserDefinedSymbol:{const n=e;return null===t.lookUp(n)?new Set([n[1]]):null}case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.String:case s.Number:case s.ReservedSymbol:case s.Binding:return null;case s.NormalExpression:{const r=e,i=new Set;if(Ze(r)){const[,[e]]=r;if(Ye(e)){null===t.lookUp(e)&&i.add(e[1])}}else{const[,[e]]=r;Ct(e,t,n,o)?.forEach(e=>i.add(e))}for(const e of r[1][1])Ct(e,t,n,o)?.forEach(e=>i.add(e));return i}case s.SpecialExpression:{const r=e,i=r[1][0];return(0,n.specialExpressions[i].getUndefinedSymbols)(r,t,{getUndefinedSymbols:jt,builtin:n,evaluateNode:o})}case s.Spread:return Ct(e[1],t,n,o);default:throw new r(`Unhandled node type: ${i}`,e[2])}}const Ot=(1+Math.sqrt(5))/2,Ut={E:Math.E,"-E":-Math.E,"ε":Math.E,"-ε":-Math.E,PI:Math.PI,"-PI":-Math.PI,"π":Math.PI,"-π":-Math.PI,PHI:Ot,"-PHI":-Ot,"φ":Ot,"-φ":-Ot,POSITIVE_INFINITY:Number.POSITIVE_INFINITY,"∞":Number.POSITIVE_INFINITY,NEGATIVE_INFINITY:Number.NEGATIVE_INFINITY,"-∞":Number.NEGATIVE_INFINITY,MAX_SAFE_INTEGER:Number.MAX_SAFE_INTEGER,MIN_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,MAX_VALUE:Number.MAX_VALUE,MIN_VALUE:Number.MIN_VALUE,NaN:Number.NaN},Ft={true:!0,false:!1,null:null,do:null,else:null,case:null,each:null,in:null,when:null,while:null,catch:null,function:null,export:null,as:null,then:null,end:null,_:null,...Ut};const Mt={NativeJsFunction:(e,t,n)=>{try{return te(e.nativeFn.fn(...t))}catch(e){const t="string"==typeof e?e:b(e)&&"string"==typeof e.message?e.message:"<no message>";throw new r(`Native function threw: "${t}"`,n)}},UserDefined:(e,t,o,i,{evaluateNode:s})=>{for(;;){if(!L(e.arity,t.length))throw new r(`Expected ${e.arity} arguments, got ${t.length}.`,o);const a=e.evaluatedfunction,u=a[0],c=u.filter(e=>e[0]!==tt).length,l=i.create(e.evaluatedfunction[2]),f={self:{value:e}},d=[];for(let e=0;e<t.length;e+=1)if(e<c){const n=te(t[e]),r=it(u[e],n,e=>s(e,l.create(f)));Object.entries(r).forEach(([e,t])=>{f[e]={value:t}})}else d.push(te(t[e]));for(let e=t.length;e<c;e++){const t=u[e],n=it(t,s(t[1][1],i.create(f)),e=>s(e,i.create(f)));Object.entries(n).forEach(([e,t])=>{f[e]={value:t}})}const h=u.find(e=>e[0]===tt);if(void 0!==h){const e=it(h,d,e=>s(e,i.create(f)));Object.entries(e).forEach(([e,t])=>{f[e]={value:t}})}try{let e=null;const t=l.create(f);for(const n of a[1])e=s(n,t);return e}catch(e){if(e instanceof n){t=e.params;continue}throw e}}},Partial:(e,t,n,o,{executeFunction:i})=>{const s=[...e.params];if(t.length!==e.placeholders.length)throw new r(`(partial) expects ${e.placeholders.length} arguments, got ${t.length}.`,n);const a=[...t];for(const t of e.placeholders)s.splice(t,0,a.shift());return i(e.function,s,o,n)},Comp:(e,t,n,o,{executeFunction:i})=>{const{params:s}=e;if(0===s.length){if(1!==t.length)throw new r(`(comp) expects one argument, got ${p(t.length)}.`,n);return I(t[0],n)}return I(s.reduceRight((e,t)=>[i(D(t,n),e,o,n)],t)[0],n)},Constantly:e=>e.value,Juxt:(e,t,n,r,{executeFunction:o})=>e.params.map(e=>o(D(e,n),t,r,n)),Complement:(e,t,n,r,{executeFunction:o})=>!o(e.function,t,r,n),EveryPred:(e,t,n,r,{executeFunction:o})=>{for(const i of e.params)for(const e of t){if(!o(D(i,n),[e],r,n))return!1}return!0},SomePred:(e,t,n,r,{executeFunction:o})=>{for(const i of e.params)for(const e of t){if(o(D(i,n),[e],r,n))return!0}return!1},Fnull:(e,t,n,r,{executeFunction:o})=>{const i=t.map((t,n)=>null===t?te(e.params[n]):t);return o(D(e.function,n),i,r,n)},Builtin:(e,t,n,r,{executeFunction:o})=>m(Je[e.normalBuiltinSymbolType],n).evaluate(t,n,r,{executeFunction:o}),SpecialBuiltin:(e,t,n,o,{executeFunction:i})=>{const s=m($t[e.specialBuiltinSymbolType],n);if(s.evaluateAsNormalExpression)return s.evaluateAsNormalExpression(t,n,o,{executeFunction:i});throw new r(`Special builtin function ${e.specialBuiltinSymbolType} is not supported as normal expression.`,n)},Module:(e,t,n,o,{executeFunction:i})=>{const s=o.getModule(e.moduleName);if(!s)throw new r(`Module '${e.moduleName}' not found.`,n);const a=s.functions[e.functionName];if(!a)throw new r(`Function '${e.functionName}' not found in module '${e.moduleName}'.`,n);return V(a.arity,t.length,n),a.evaluate(t,n,o,{executeFunction:i})}};function Tt(e,t){let n=null;for(const r of e.body)n=_t(r,t);return n}function _t(e,t){switch(e[0]){case s.Number:case s.String:return function(e){return e[1]}(e);case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.UserDefinedSymbol:return t.evaluateSymbol(e);case s.ReservedSymbol:return function(e){const t=e[1];if(!["true","false","null"].includes(t))throw new r(`Reserved symbol ${t} cannot be evaluated`,e[2]);return m(Ft[t],e[2])}(e);case s.NormalExpression:{const n=function(e,t){const n=e[2],o=e[1][1],a=[],u=[];if(o.forEach((e,n)=>{if(He(e)){const n=_t(e[1],t);if(!Array.isArray(n))throw new r(`Spread operator requires an array, got ${p(e)}`,e[2]);a.push(...n)}else e[0]===s.ReservedSymbol&&"_"===e[1]?u.push(n):a.push(_t(e,t))}),Ze(e)){const r=e[1][0];if(u.length>0){const e=_t(r,t);return{[l]:!0,function:D(e,n),functionType:"Partial",params:a,placeholders:u,sourceCodeInfo:n,arity:J(u.length)}}if(Ke(r)){const n=r[1];return Pt.allNormalExpressions[n].evaluate(a,e[2],t,{executeFunction:Bt})}{const o=t.getValue(r[1]);if(void 0!==o)return Bt(D(o,n),a,t,n);throw new i(r[1],e[2])}}{const r=D(_t(e[1][0],t),n);if(u.length>0){return{[l]:!0,function:r,functionType:"Partial",params:a,placeholders:u,sourceCodeInfo:n,arity:J(u.length)}}return Bt(r,a,t,n)}}(e,t);if("number"==typeof n&&Number.isNaN(n))throw new r("Number is NaN",e[2]);return be(n)}case s.SpecialExpression:return be(function(e,t){const n=e[1][0],r=m(Pt.specialExpressions[n],e[2]);return(0,r.evaluate)(e,t,{evaluateNode:_t,builtin:Pt,getUndefinedSymbols:jt})}(e,t));default:throw new r(`${u(e[0])}-node cannot be evaluated`,e[2])}}function Bt(e,t,n,o){if(w(e))return Mt[e.functionType](e,t,o,n,{evaluateNode:_t,executeFunction:Bt});if(Array.isArray(e))return function(e,t,n){if(1!==t.length)throw new r("Array as function requires one non negative integer parameter.",n);const o=t[0];return P(o,n,{integer:!0,nonNegative:!0}),te(e[o])}(e,t,o);if(U(e))return function(e,t,n){if(1!==t.length)throw new r("Object as function requires one string parameter.",n);const o=t[0];return Y(o,n),te(e[o])}(e,t,o);if("string"==typeof e)return function(e,t,n){if(1!==t.length)throw new r("String as function requires one Obj parameter.",n);const o=te(t[0]);if(U(o))return te(o[e]);if($(o,{integer:!0}))return te(e[o]);throw new r(`string as function expects Obj or integer parameter, got ${p(o)}`,n)}(e,t,o);if($(e))return function(e,t,n){if(P(e,n,{integer:!0}),1!==t.length)throw new r("Number as function requires one Arr parameter.",n);const o=t[0];return O(o,n),te(o[e])}(e,t,o);throw new r("Unexpected function type",o)}class Rt{contexts;globalContext;values;nativeJsFunctions;modules;constructor({contexts:e,values:t,nativeJsFunctions:n,modules:r}){this.globalContext=m(e[0]),this.contexts=e,this.values=t,this.nativeJsFunctions=n,this.modules=r??new Map}getModule(e){return this.modules.get(e)}create(e){const t=this.globalContext,n=new Rt({contexts:[e,...this.contexts],values:this.values,nativeJsFunctions:this.nativeJsFunctions,modules:this.modules});return n.globalContext=t,n}new(e){return new Rt({contexts:[{},e],modules:this.modules})}exportValues(e,t){for(const[n,o]of Object.entries(e)){if(this.globalContext[n])throw new r(`Cannot redefine exported value "${n}"`,t);const e=Dt(n);if(e)throw new r(`Cannot shadow ${e}`,t);this.globalContext[n]={value:o}}this.contexts[0]!==this.globalContext&&this.addValues(e,t)}addValues(e,t){const n=this.contexts[0];for(const[o,i]of Object.entries(e)){if(n[o])throw new r(`Cannot redefine value "${o}"`,t);const e=Dt(o);if(e)throw new r(`Cannot shadow ${e}`,t);n[o]={value:te(i)}}}getValue(e){for(const t of this.contexts){const n=t[e];if(n)return n.value}const t=this.nativeJsFunctions?.[e];return t||this.values?.[e]}lookUp(e){const t=e[1];for(const e of this.contexts){const n=e[t];if(n)return n}const n=this.values?.[t];if(void 0!==n)return{value:te(n)};const r=this.nativeJsFunctions?.[t];return r?{value:r}:null}evaluateSymbol(e){if(Xe(e)){const t=e[1];switch(t){case Nt["&&"]:case Nt["||"]:case Nt.array:case Nt.object:case Nt["defined?"]:case Nt.recur:case Nt.throw:case Nt["??"]:{const n=m(Pt.specialExpressions[t],e[2]);return{[l]:!0,functionType:"SpecialBuiltin",specialBuiltinSymbolType:t,sourceCodeInfo:e[2],arity:n.arity}}default:throw new r(`Unknown special builtin symbol type: ${t}`,e[2])}}if(Ke(e)){const t=e[1],n=Je[t],r=n.name;return{[l]:!0,functionType:"Builtin",normalBuiltinSymbolType:t,sourceCodeInfo:e[2],arity:n.arity,name:r}}const t=this.lookUp(e);if(b(n=t)&&void 0!==n.value)return t.value;var n;throw new i(e[1],e[2])}}function Dt(e){return It.includes(e)?`special expression "${e}"`:At.includes(e)?`builtin function "${e}"`:"self"===e?`builtin value "${e}"`:null}function zt(e={},t){const n=e.globalContext??{},o=e.contexts?[n,...e.contexts]:[n],i=new Rt({contexts:o,values:e.values,modules:t,nativeJsFunctions:e.jsFunctions&&Object.entries(e.jsFunctions).reduce((e,[t,n])=>{const o=t.split("."),i=o.pop();if(/^[A-Z]/.test(i))throw new r(`Invalid identifier "${t}" in jsFunctions, function name must not start with an uppercase letter`,void 0);let s=e;for(const e of o){if(0===e.length)throw new r(`Invalid empty identifier "${t}" in jsFunctions`,void 0);if(!/^[A-Z]/.test(e))throw new r(`Invalid identifier "${t}" in jsFunctions, module name must start with an uppercase letter`,void 0);s[e]||(s[e]={}),s=s[e]}!function(e){if(!function(e){return"object"==typeof e&&null!==e&&"fn"in e&&"function"==typeof e.fn}(e))throw new TypeError("Expected a NativeJsFunction")}(n);const a={functionType:"NativeJsFunction",nativeFn:n,name:i,[l]:!0,arity:n.arity??{},docString:n.docString??""};return s===e&&function(e){const t=Dt(e);if(t)throw new r(`Cannot shadow ${t}`,void 0)}(i),s[i]=a,e},{})});return e.globalModuleScope?i:i.create({})}const Lt=["^","*","/","%","+","-","<<",">>",">>>","++","<","<=","≤",">",">=","≥","==","!=","≠","&","xor","|","&&","||","??","|>"],Vt=[...Lt,"?",":","->","...",".",",","=",":",";"],Jt=new Set(["comment","cond","def","defined?","block","doseq","if","let","loop","recur","throw","try","unless","while"]);function Wt(e){return!Jt.has(e)}const qt=new Set(Lt);function Yt(e){return qt.has(e)}const Gt=new Set(Vt);function Kt(e){return Gt.has(e)}const Xt=["(",")","[","]","{","}","'",'"',"`",",",".",";"," ","\n","\r","\t"],Zt=["0","1","2","3","4","5","6","7","8","9",...Xt],Ht=new Set(Xt),Qt=new Set(Zt),en=/\s/,tn=[0],nn=(e,t)=>{if('"'!==e[t])return tn;let n='"',r=1,o=e[t+r],i=!1;for(;o&&('"'!==o||i);)r+=1,i?(i=!1,n+=o):("\\"===o&&(i=!0),n+=o),o=e[t+r];return o?(n+='"',[r+1,["string",n]]):[r,["Error",n,void 0,`Unclosed string at position ${t}`]]};function rn(e,t,n,r){return t===n.slice(r,r+t.length)?[t.length,[e,t]]:tn}const on=/\d/,sn=/[0-7]/,an=/[0-9a-f]/i,un=/[01]/,cn=/[\s)\]}(,;]/,ln=(e,t)=>{let n=e[t];if("'"===n){let r=1,o=e[t+r],i=!1;for(;"'"!==o||i;){if(void 0===o)return[r,["Error",n,void 0,`Unclosed quoted symbol at position ${t}`]];r+=1,i?(i=!1,n+=o):("\\"===o&&(i=!0),n+=o),o=e[t+r]}return n+="'",[r+1,["Symbol",n]]}if(!Qt.has(n)){const r=t;let o=e[t+=1];for(;o&&!Ht.has(o);)n+=o,o=e[t+=1];return n.endsWith(":")?[t-r-1,["Symbol",n.slice(0,-1)]]:[t-r,["Symbol",n]]}return tn},fn=[(e,t)=>{let n=e[t];if(!n||!en.test(n))return tn;let r=n;for(n=e[t+=1];n&&en.test(n);)r+=n,n=e[t+=1];return[r.length,["Whitespace",r]]},(e,t)=>{if("/"===e[t]&&"*"===e[t+1]){let n=2,r="/*";for(;("*"!==e[t+n]||"/"!==e[t+n+1])&&t+n+1<e.length;)r+=e[t+n],n+=1;return t+n+1>=e.length?[n,["Error",r,void 0,`Unclosed multi-line comment at position ${t}`]]:(r+="*/",n+=2,[n,["MultiLineComment",r]])}return tn},(e,t)=>{if("/"===e[t]&&"/"===e[t+1]){let n=2,r="//";for(;"\n"!==e[t+n]&&t+n<e.length;)r+=e[t+n],n+=1;return[n,["SingleLineComment",r]]}return tn},(e,t)=>{const n=ln(e,t);if(0===n[0]||!n[1])return tn;let r=n[1][1];r=r.startsWith("'")?r.slice(1,r.length-1):r;return void 0===Ft[r]?tn:[n[0],["ReservedSymbol",r]]},(e,t)=>rn("LParen","(",e,t),(e,t)=>rn("RParen",")",e,t),(e,t)=>rn("LBracket","[",e,t),(e,t)=>rn("RBracket","]",e,t),(e,t)=>rn("LBrace","{",e,t),(e,t)=>rn("RBrace","}",e,t),(e,t)=>{if('"'!==e[t]||'"'!==e[t+1]||'"'!==e[t+2])return tn;let n='"""',r=3,o=e[t+r],i=e.slice(t+r,t+r+3),s=!1;for(;o&&('"""'!==i||s);)r+=1,s?(s=!1,n+=o):("\\"===o&&(s=!0),n+=o),o=e[t+r],i=e.slice(t+r,t+r+3);return o?(n+='"""',[r+3,["DocString",n]]):[r,["Error",n,void 0,`Unclosed doc string at position ${t}`]]},nn,(e,t)=>{if("#"!==e[t])return tn;const[n,r]=nn(e,t+1);if(!r)return tn;if("Error"===r[0]){return[n+1,["Error",`#${r[1]}`,void 0,`Unclosed regexp at position ${t}`]]}t+=n+1;let o=n+1,i="";for(;"g"===e[t]||"i"===e[t];)if(i+=e[t],o+=1,t+=1,i.includes(e[t]))return[o,["Error",`#${r[1]}${i}`,void 0,`Duplicated regexp option "${e[t]}"`]];return[o,["RegexpShorthand",`#${r[1]}${i}`]]},(e,t)=>{if("0"!==e[t])return tn;const n=e[t+1],r="b"===n||"B"===n?"binary":"o"===n||"O"===n?"octal":"x"===n||"X"===n?"hex":null;if(null===r)return tn;let o;for(o=t+2;o<e.length;o+=1){const t=e[o];if("binary"===r&&!un.test(t))break;if("octal"===r&&!sn.test(t))break;if("hex"===r&&!an.test(t))break}const i=o-t;if(i<=2)return tn;const s=e[o];return s&&!cn.test(s)?tn:[i,["BasePrefixedNumber",e.substring(t,o)]]},(e,t)=>{let n;const r="-"===e[t],o="+"===e[t],i=r||o?t+1:t;let s=!1,a=!1;for(n=i;n<e.length;n+=1){const r=e[n];if("_"===r){if(!on.test(e[n-1])||!on.test(e[n+1]))return n===i?tn:[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]]}else if("."===r){if(n===i)return tn;if(s||a)return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];s=!0}else if("e"===r||"E"===r){if(n===i)return tn;if(a)return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];if("."===e[n-1]||"+"===e[n-1]||"-"===e[n-1])return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];"+"!==e[n+1]&&"-"!==e[n+1]||(n+=1),a=!0}else if(!on.test(r))break}if((r||o)&&n===i)return tn;const u=n-t;if(0===u)return tn;const c=e[n];return c&&":"!==c&&!cn.test(c)?[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]]:[u,["Number",e.substring(t,n)]]},(e,t)=>{const n=e.slice(t,t+3);if(t+2<e.length&&Kt(n))return[3,["Operator",n]];const r=e.slice(t,t+2);if(t+1<e.length&&Kt(r))return[2,["Operator",r]];const o=e[t]??"";return Kt(o)?[1,["Operator",o]]:tn},ln];function dn(e,t,n){const r=e.substring(0,t+1).split(/\r\n|\r|\n/),o=r[r.length-1],i=function(e,t){return e.split(/\r\n|\r|\n/)[t]}(e,r.length-1);return{code:i,position:{line:r.length,column:o.length},filePath:n}}function hn(e,t){const n=t;if(0===t){const[r,o]=((e,t)=>{if("#"===e[t]&&"!"===e[t+1]){let n=2,r="#!";for(;"\n"!==e[t+n]&&t+n<e.length;)r+=e[t+n],n+=1;return[n,["SingleLineComment",r]]}return tn})(e,t);if(t+=r,r>0)return[t-n,o]}for(const r of fn){const[o,i]=r(e,t);if(t+=o,0!==o)return[t-n,i]}return[1,["Error",e[n],void 0,"Unrecognized character"]]}function pn(e,t){return"Symbol"===e?.[0]&&(!t||e[1]===t)}function yn(e,t){return function(e,t){pn(e,t)||Fn("Symbol",void 0,e)}(e,t),e}function gn(e,t){return"ReservedSymbol"===e?.[0]&&(!t||e[1]===t)}function mn(e,t){gn(e,t)||Fn("ReservedSymbol",t,e)}function vn(e,t){return mn(e,t),e}function bn(e,t){return"Operator"===e?.[0]&&(!t||e[1]===t)}function wn(e,t){bn(e,t)||Fn("Operator",t,e)}function xn(e){return"LParen"===e?.[0]}function Sn(e){xn(e)||Fn("LParen",void 0,e)}function En(e){return"RParen"===e?.[0]}function kn(e){En(e)||Fn("RParen",void 0,e)}function Nn(e){return"LBracket"===e?.[0]}function $n(e){return function(e){Nn(e)||Fn("LBracket",void 0,e)}(e),e}function Pn(e){return"RBracket"===e?.[0]}function An(e){Pn(e)||Fn("RBracket",void 0,e)}function In(e){return"LBrace"===e?.[0]}function jn(e){return function(e){In(e)||Fn("LBrace",void 0,e)}(e),e}function Cn(e){return"RBrace"===e?.[0]}function On(e){return"string"===e?.[0]}function Un(e){return"Operator"===e?.[0]&&Yt(e[1])}function Fn(e,t,n){const o=n?`${n[0]} '${n[1]}'`:"end of input";throw new r(`Unexpected token: ${o}, expected ${e}${t?` '${t}'`:""}`,n?.[2])}function Mn(e,{removeWhiteSpace:t}){const n=e.tokens.filter(e=>!(function(e){return"SingleLineComment"===e?.[0]}(e)||function(e){return"MultiLineComment"===e?.[0]}(e)||function(e){return"Shebang"===e?.[0]}(e)||t&&function(e){return"Whitespace"===e?.[0]}(e)));return{...e,tokens:n}}const Tn=new Set([...At,...It,...Object.keys(Ft)]);class _n{originalProgram;originalPosition;prefixProgram="";suffixProgram="";searchString="";suggestions=[];suggestionIndex=null;constructor(e,t,n,r){this.originalProgram=e,this.originalPosition=t;const o=this.originalProgram.slice(0,this.originalPosition),i=n.tokenize(o).tokens.at(-1);i&&"Error"!==i[0]&&(this.searchString=i[1],this.prefixProgram=this.originalProgram.slice(0,this.originalPosition-this.searchString.length),this.suffixProgram=this.originalProgram.slice(this.prefixProgram.length+this.searchString.length),this.originalProgram.slice(this.prefixProgram.length+this.searchString.length),this.suggestions=this.generateSuggestions(r))}getNextSuggestion(){return this.getAutoCompleteSuggestionResult(this.getNextSuggestionSymbol())}getPreviousSuggestion(){return this.getAutoCompleteSuggestionResult(this.getPreviousSuggestionSymbol())}getAutoCompleteSuggestionResult(e){return null===e?null:{program:this.prefixProgram+e+this.suffixProgram,position:this.prefixProgram.length+e.length}}getNextSuggestionSymbol(){return 0===this.suggestions.length?null:(null===this.suggestionIndex?this.suggestionIndex=0:(this.suggestionIndex+=1,this.suggestionIndex>=this.suggestions.length&&(this.suggestionIndex=0)),this.suggestions[this.suggestionIndex])}getPreviousSuggestionSymbol(){return 0===this.suggestions.length?null:(null===this.suggestionIndex?this.suggestionIndex=this.suggestions.length-1:(this.suggestionIndex-=1,this.suggestionIndex<0&&(this.suggestionIndex=this.suggestions.length-1)),this.suggestions[this.suggestionIndex])}getSuggestions(){return[...this.suggestions]}getSearchString(){return this.searchString}generateSuggestions(e){const t=new Set(["0_def","0_defn","0_lambda"]),n=this.generateWithPredicate(e,e=>!t.has(e)&&e.startsWith(this.searchString));n.forEach(e=>t.add(e));const r=this.generateWithPredicate(e,e=>!t.has(e)&&e.toLowerCase().startsWith(this.searchString.toLowerCase()));r.forEach(e=>t.add(e));const o=this.generateWithPredicate(e,e=>!t.has(e)&&e.includes(this.searchString));o.forEach(e=>t.add(e));const i=this.generateWithPredicate(e,e=>!t.has(e)&&e.includes(this.searchString.toLowerCase()));return i.forEach(e=>t.add(e)),[...n,...r,...o,...i]}generateWithPredicate(e,t){const n=new Set;return Tn.forEach(e=>{t(e)&&n.add(e)}),Object.keys(e.globalContext??{}).filter(t).forEach(e=>n.add(e)),e.contexts?.forEach(e=>{Object.keys(e).filter(t).forEach(e=>n.add(e))}),Object.keys(e.jsFunctions??{}).filter(t).forEach(e=>n.add(e)),Object.keys(e.values??{}).filter(t).forEach(e=>n.add(e)),[...n].sort((e,t)=>e.localeCompare(t))}}class Bn{tokens;position;storedPosition=0;parseExpression;constructor(e){this.tokens=e.tokens,this.position=0}advance(){this.position+=1}tryPeek(){return this.tokens[this.position]}peek(){const e=this.tokens[this.position];if(!e){const e=this.tokens.at(-1),t=e?e[2]:void 0;throw new r("Unexpected end of input",t)}return e}isAtEnd(){return this.position>=this.tokens.length}peekSourceCodeInfo(){const e=this.tryPeek();return e?e[2]:this.tokens.at(-1)?.[2]}storePosition(){return this.storedPosition=this.position}restorePosition(){this.position=this.storedPosition}peekAhead(e){return this.tokens[this.position+e]}getPosition(){return this.position}getTokenAt(e){return this.tokens[e]}}function Rn(e,t){return t&&(e[2]=t),e}function Dn(e,t){return void 0!==Nt[e]&&"fn"!==e&&"def"!==e&&"defn"!==e?Rn([s.SpecialBuiltinSymbol,Nt[e]],t):void 0!==Ve[e]?Rn([s.NormalBuiltinSymbol,Ve[e]],t):Rn([s.UserDefinedSymbol,e],t)}function zn(e){return e.substring(1,e.length-1).replace(/(\\{2})|(\\')|\\(.)/g,(e,t,n,r)=>t?"\\":n?"'":`\\${r}`)}const Ln=Object.entries(Ve).reduce((e,[t,n])=>(e[n]=t,e),[]),Vn=Object.entries(Nt).reduce((e,[t,n])=>(e[n]=t,e),[]);function Jn(e){return Ye(e)?e[1]:Ke(e)?Ln[e[1]]:Vn[e[1]]}function Wn(e,t,n){const r=Rn([s.NormalExpression,[e,t]],n);return Ke(e)&&V(Je[e[1]].arity,r[1][1].length,n),r}function qn(e){if(e.isAtEnd())return!0;const t=e.tryPeek();return bn(t)?[";",",",":"].includes(t[1]):!!gn(t)&&["else","when","while","case","catch","let","then","end","do"].includes(t[1])}function Yn(e,t,n,o,i){const a=e[1];switch(a){case"^":case"*":case"/":case"%":case"+":case"-":case"<<":case">>":case">>>":case"++":case"<":case"<=":case"≤":case">":case">=":case"≥":case"==":case"!=":case"≠":case"&":case"xor":case"|":case"|>":return Wn(t,[n,o],i);case"&&":case"||":case"??":return Rn([s.SpecialExpression,[Nt[a],[n,o]]],i);default:throw new r(`Unknown binary operator: ${a}`,i)}}function Gn(e){const t=e.peek();if(e.advance(),!pn(t))throw new r(`Expected symbol token, got ${t[0]}`,t[2]);return"'"===t[1][0]?Dn(zn(t[1]),t[2]):Dn(t[1],t[2])}function Kn(e,{requireDefaultValue:t,noRest:n}={}){const o=e.tryPeek();if(pn(o)){const n=Gn(e);if(!Ye(n))throw new r("Expected user defined symbol",o[2]);const i=Xn(e);if(t&&!i)throw new r("Expected assignment",e.peekSourceCodeInfo());return Rn([et,[n,i]],o[2])}if(bn(o,"...")){if(n)throw new r("Rest element not allowed",o[2]);e.advance();const t=Ge(Gn(e));if(bn(e.tryPeek(),"="))throw new r("Rest argument can not have default value",e.peekSourceCodeInfo());return Rn([tt,[t[1],void 0]],o[2])}if(Nn(o)){e.advance();const n=[];let i=e.peek(),s=!1;for(;!Pn(i);){if(s)throw new r("Rest argument must be last",i[2]);if(bn(i,",")){n.push(null),e.advance(),i=e.peek();continue}const t=Kn(e);t[0]===tt&&(s=!0),n.push(t),i=e.peek(),Pn(i)||(wn(i,","),e.advance()),i=e.peek()}e.advance();const a=Xn(e);if(t&&!a)throw new r("Expected assignment",e.peekSourceCodeInfo());return Rn([rt,[n,a]],o[2])}if(In(o)){e.advance();const n={};let i=e.peek(),s=!1;for(;!Cn(i);){if(s)throw new r("Rest argument must be last",i[2]);bn(i,"...")&&(s=!0,e.advance());const t=Gn(e),a=Jn(t);if(i=e.peek(),gn(i,"as")){if(s)throw new r("Rest argument can not have alias",i[2]);e.advance();const t=Ge(Gn(e));if(n[t[1]])throw new r(`Duplicate binding name: ${t}`,i[2]);n[a]=Rn([et,[t,Xn(e)]],o[2])}else if(Cn(i)||bn(i,",")||bn(i,"=")){const a=Ge(t,t[2]);if(n[a[1]])throw new r(`Duplicate binding name: ${a}`,i[2]);if(s&&bn(e.tryPeek(),"="))throw new r("Rest argument can not have default value",e.peekSourceCodeInfo());n[a[1]]=Rn(s?[tt,[a[1],Xn(e)]]:[et,[a,Xn(e)]],o[2])}else if(bn(i,":")){if(e.advance(),i=e.peek(),!In(i)&&!Nn(i))throw new r("Expected object or array",i[2]);n[a]=Kn(e)}Cn(e.peek())||(wn(e.peek(),","),e.advance()),i=e.peek()}e.advance(),i=e.peek();const a=Xn(e);if(t&&!a)throw new r("Expected assignment",i[2]);return Rn([nt,[n,a]],o[2])}throw new r("Expected symbol",e.peekSourceCodeInfo())}function Xn(e){if(bn(e.tryPeek(),"="))return e.advance(),e.parseExpression()}function Zn(e,t){e.advance();const n=Kn(e,{requireDefaultValue:!0,noRest:!0}),r=n[1][1];n[1][1]=void 0;const o=Rn([s.Binding,[n,r]],t[2]);return Rn([s.SpecialExpression,[Nt.let,o]],t[2])}function Hn(e,t){const n=[];for(;!e.isAtEnd()&&!Qn(e,t);)bn(e.tryPeek(),";")?e.advance():n.push(e.parseExpression());if(function(e,t){if(!Qn(e,t))throw new r(`Expected ${t.map(e=>e[1]).join(" or ")}`,e.peekSourceCodeInfo())}(e,t),0===n.length)throw new r("Expected expression",e.peekSourceCodeInfo());return 1===n.length?n[0]:Rn([s.SpecialExpression,[Nt.block,n]],e.peekSourceCodeInfo())}function Qn(e,t){for(const n of t)if(gn(e.tryPeek(),n))return!0;return!1}function er(e){const t=function(e){const t=yn(e.tryPeek()),n=Ge(Gn(e));mn(e.tryPeek(),"in"),e.advance();const r=e.parseExpression();return Rn([s.Binding,[Rn([et,[n,void 0]],t[2]),r]],t[2])}(e),n=[];let o=e.peek();tr(o,["let","when","while"]);const i=[];if("let"===o[1])for(n.push("&let");pn(o,"let");){const t=Zn(e,o),n=i.flatMap(e=>Object.keys(at(e[1][0])));if(Object.keys(at(t[1][1][1][0])).some(e=>n.includes(e)))throw new r("Duplicate binding",t[1][1][2]);i.push(t[1][1]),o=e.peek(),tr(o,["let","when","while"]),o=e.peek()}let a,u;for(;gn(o,"when")||gn(o,"while");){e.advance(),"when"===o[1]?(n.push("&when"),a=e.parseExpression()):(n.push("&while"),u=e.parseExpression()),o=e.peek();tr(o,n.includes("&when")&&n.includes("&while")?[]:n.includes("&when")?["while"]:["when"]),o=e.peek()}return tr(o,[]),[t,i,a,u]}function tr(e,t){if(!function(e,t){if(bn(e,",")||En(e))return!0;for(const n of t){if("let"===n&&pn(e,"let"))return!0;if(["when","while"].includes(n)&&gn(e,n))return!0}return!1}(e,t)){const n=`${[...t,","].map(e=>`"${e}"`).join(", ")} or ")"`;throw new r(`Expected symbol ${n}`,e[2])}}function nr(e,t){e.advance();const n=t[1].substring(1,t[1].length-1).replace(/(\\{2})|(\\")|(\\n)|(\\t)|(\\r)|(\\b)|(\\f)|\\(.)/g,(e,t,n,r,o,i,s,a,u)=>t?"\\":r?"\n":o?"\t":i?"\r":s?"\b":a?"\f":n?'"':u);return Rn([s.String,n],t[2])}function rr(e,t=!1){const n=vn(e.tryPeek(),"do");e.advance();let o="";t&&function(e){return"DocString"===e?.[0]}(e.tryPeek())&&(o=function(e){const t=e.peek(),n=t[2]?["string",t[1].slice(2,-2),t[2]]:["string",t[1].slice(2,-2)],r=nr(e,n);return ie(r[1])}(e));const i=[];for(;!e.isAtEnd()&&!gn(e.tryPeek(),"end");)if(i.push(e.parseExpression()),bn(e.tryPeek(),";"))e.advance();else if(!gn(e.tryPeek(),"end"))throw new r("Expected end",e.peekSourceCodeInfo());return mn(e.tryPeek(),"end"),e.advance(),[Rn([s.SpecialExpression,[Nt.block,i]],n[2]),o]}function or(e){const t=vn(e.tryPeek());e.advance();const n=t[1];return function(e){return e in Ut}(n)?Rn([s.Number,Ut[n]],t[2]):Rn([s.ReservedSymbol,t[1]],t[2])}const ir=/^\$([1-9]\d?)?$/;function sr(e){const t=e.peek();if(xn(t)&&pn(e.peekAhead(1))&&bn(e.peekAhead(2),"->"))return null;try{const n=function(e){const t=e.peek();if(pn(t))return[Rn([et,[Gn(e),void 0]],t[2])];Sn(t),e.advance();let n=!1,o=!1;const i=[];for(;!e.isAtEnd()&&!En(e.peek())&&!pn(e.peek(),"let");){if(n)throw new r("Rest argument must be last",e.peekSourceCodeInfo());const t=Kn(e);if(void 0!==t[1][1]&&(o=!0),t[0]===tt&&(n=!0),o&&!t[1][1])throw new r("Default arguments must be last",e.peekSourceCodeInfo());if(i.push(t),!bn(e.peek(),",")&&!En(e.peek())&&!pn(e.peek(),"let"))throw new r("Expected comma or closing parenthesis",e.peekSourceCodeInfo());bn(e.peek(),",")&&e.advance()}if(!En(e.peek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());return e.advance(),i}(e);if(!bn(e.peek(),"->"))return null;let o;e.advance();let i="";if(gn(e.peek(),"do")){const t=rr(e,!0);i=t[1],o=t[0][1][1]}else o=[e.parseExpression()];return Rn([s.SpecialExpression,[Nt["0_lambda"],[n,o],i]],t[2])}catch{return null}}function ar(e,t){e.advance();const n=[];for(;!e.isAtEnd()&&!En(e.tryPeek());){bn(e.tryPeek(),"...")?(e.advance(),n.push(Rn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()))):n.push(e.parseExpression());const t=e.tryPeek();if(!bn(t,",")&&!En(t))throw new r("Expected comma or closing parenthesis",e.tryPeek()?.[2]);bn(t,",")&&e.advance()}if(!En(e.tryPeek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());if(e.advance(),!Xe(t))return Ke(t)||Ye(t)?Wn(t,n,t[2]):Rn([s.NormalExpression,[t,n]],t[2]);{const e=t[1];switch(V(Pt.specialExpressions[e].arity,n.length,t[2]),e){case Nt["||"]:case Nt["&&"]:case Nt.recur:case Nt.array:case Nt.object:case Nt["??"]:return Rn([s.SpecialExpression,[e,n]],t[2]);case Nt["defined?"]:{const[r]=n;return Rn([s.SpecialExpression,[e,r]],t[2])}case Nt.throw:{const[r]=n;return Rn([s.SpecialExpression,[e,r]],t[2])}case Nt["0_lambda"]:case Nt["0_def"]:throw new r(`${e} is not allowed`,t[2]);default:throw new r(`Unknown special expression: ${e}`,t[2])}}}function ur(e){const t=jn(e.tryPeek());e.advance();const n=[];for(;!e.isAtEnd()&&!Cn(e.tryPeek());){if(bn(e.tryPeek(),"..."))e.advance(),n.push(Rn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()));else{const t=e.tryPeek();if(On(t)){const r=nr(e,t);n.push(Rn([s.String,r[1]],t[2]))}else if(pn(t)){const r=t[1].startsWith("'")?zn(t[1]):t[1];n.push(Rn([s.String,r],t[2])),e.advance()}else{if(!Nn(t))throw new r("Expected key to be a symbol or a string",e.peekSourceCodeInfo());e.advance(),n.push(e.parseExpression()),An(e.tryPeek()),e.advance()}wn(e.tryPeek(),":"),e.advance(),n.push(e.parseExpression())}const t=e.tryPeek();if(!bn(t,",")&&!Cn(t))throw new r("Expected comma or closing brace",e.peekSourceCodeInfo());bn(t,",")&&e.advance()}var o;return Cn(o=e.tryPeek())||Fn("RBrace",void 0,o),e.advance(),Rn([s.SpecialExpression,[Nt.object,n]],t[2])}function cr(e){let t=function(e){const t=e.peek();if(xn(t)){e.storePosition();const t=sr(e);if(t)return t;e.restorePosition(),e.advance();const n=e.parseExpression();if(!En(e.peek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());return e.advance(),n}if(bn(t)){const n=t[1];if(Yt(n))return e.advance(),Rn(void 0!==Nt[n]?[s.SpecialBuiltinSymbol,Nt[n]]:[s.NormalBuiltinSymbol,Ve[n]],t[2]);if("->"===n)return function(e){const t=e.peek();e.advance();const n=e.getPosition();let o,i="";if(gn(e.peek(),"do")){const t=rr(e,!0);i=t[1],o=t[0][1][1]}else o=[e.parseExpression()];const a=e.getPosition()-1;let u=0,c="NOT_SET";for(let o=n;o<=a;o+=1){const n=e.getTokenAt(o);if(pn(n)){const e=ir.exec(n[1]);if(e){const n=e[1]??"1";if("1"===n){if(!e[1]&&"WITH_1"===c||e[1]&&"NAKED"===c)throw new r("Please make up your mind, either use $ or $1",t[2]);c=e[1]?"WITH_1":"NAKED"}if(u=Math.max(u,Number(n)),u>20)throw new r("Can't specify more than 20 arguments",t[2])}}}const l=[];for(let e=1;e<=u;e+=1)1===e&&"NAKED"===c?l.push(Rn([et,[[s.UserDefinedSymbol,"$"],void 0]],t[2])):l.push(Rn([et,[[s.UserDefinedSymbol,`$${e}`],void 0]],t[2]));return Rn([s.SpecialExpression,[Nt["0_lambda"],[l,o],i]],t[2])}(e);throw new r(`Illegal operator: ${n}`,t[2])}if(In(t))return ur(e);if(Nn(t))return function(e){const t=$n(e.tryPeek());e.advance();const n=[];for(;!e.isAtEnd()&&!Pn(e.tryPeek());){bn(e.tryPeek(),"...")?(e.advance(),n.push(Rn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()))):n.push(e.parseExpression());const t=e.tryPeek();if(!bn(t,",")&&!Pn(t))throw new r("Expected comma or closing parenthesis",e.peekSourceCodeInfo());bn(t,",")&&e.advance()}return An(e.tryPeek()),e.advance(),Rn([s.SpecialExpression,[Nt.array,n]],t[2])}(e);const n=t[0];switch(n){case"Number":case"BasePrefixedNumber":return function(e){const t=e.peek();e.advance();const n=t[1],r="-"===n[0],o=(r?n.substring(1):n).replace(/_/g,"");return Rn([s.Number,r?-Number(o):Number(o)],t[2])}(e);case"string":return nr(e,t);case"Symbol":{e.storePosition();const t=sr(e);return t||(e.restorePosition(),Gn(e))}case"ReservedSymbol":return or(e);case"RegexpShorthand":return function(e){const t=e.peek();e.advance();const n=t[1].lastIndexOf('"'),r=t[1].substring(2,n),o=t[1].substring(n+1),i=Rn([s.String,r],t[2]),a=Rn([s.String,o],t[2]);return Rn([s.NormalExpression,[Rn([s.NormalBuiltinSymbol,Ve.regexp],t[2]),[i,a]]],t[2])}(e);default:throw new r(`Unknown token type: ${n}`,t[2])}}(e),n=e.tryPeek();for(;bn(n,".")||Nn(n)||xn(n);)if("."===n[1]){e.advance();const o=e.tryPeek();if(!pn(o))throw new r("Expected symbol",e.peekSourceCodeInfo());t=lr(t,Rn([s.String,o[1]],o[2]),n[2]),e.advance(),n=e.tryPeek()}else if(Nn(n)){e.advance();const o=e.parseExpression();if(!Pn(e.tryPeek()))throw new r("Expected closing bracket",e.peekSourceCodeInfo());t=lr(t,o,n[2]),e.advance(),n=e.tryPeek()}else xn(n)&&(t=ar(e,t),n=e.tryPeek());return t}function lr(e,t,n){return Rn([s.NormalExpression,[[s.NormalBuiltinSymbol,Ve.get],[e,t]]],n)}function fr(e,t){switch(e){case"^":return 12;case"*":case"/":case"%":return 11;case"+":case"-":return 10;case"<<":case">>":case">>>":return 9;case"++":return 8;case"<":case"<=":case"≤":case">":case">=":case"≥":return 7;case"==":case"!=":case"≠":return 6;case"&":case"xor":case"|":return 5;case"&&":case"||":case"??":return 4;case"|>":return 2;default:throw new r(`Unknown binary operator: ${e}`,t)}}function dr(e,t=0,n=!1){const o=e.tryPeek();let i;if(pn(o))switch(o[1]){case"let":return Zn(e,o);case"if":case"unless":i=function(e,t){const n="unless"===t[1];e.advance();const r=e.parseExpression();mn(e.tryPeek(),"then"),e.advance();const o=Hn(e,["else","end"]);let i;return gn(e.tryPeek(),"else")&&(e.advance(),i=Hn(e,["end"])),e.advance(),Rn(n?[s.SpecialExpression,[Nt.unless,[r,o,i]]]:[s.SpecialExpression,[Nt.if,[r,o,i]]],t[2])}(e,o);break;case"cond":i=function(e,t){e.advance();const n=[];for(;!e.isAtEnd()&&!gn(e.tryPeek(),"end");){mn(e.tryPeek(),"case"),e.advance();const t=e.parseExpression();mn(e.tryPeek(),"then"),e.advance();const r=Hn(e,["case","end"]);if(n.push([t,r]),gn(e.tryPeek(),"end"))break}return mn(e.tryPeek()),e.advance(),Rn([s.SpecialExpression,[Nt.cond,n]],t[2])}(e,o);break;case"switch":i=function(e,t){e.advance();const n=e.parseExpression(),r=[];for(;!e.isAtEnd()&&!gn(e.tryPeek(),"end");){mn(e.tryPeek(),"case"),e.advance();const t=e.parseExpression();mn(e.tryPeek(),"then"),e.advance();const n=Hn(e,["case","end"]);if(r.push([t,n]),gn(e.tryPeek(),"end"))break}return mn(e.tryPeek(),"end"),e.advance(),Rn([s.SpecialExpression,[Nt.switch,n,r]],t[2])}(e,o);break;case"for":case"doseq":i=function(e,t){const n="doseq"===t[1];e.advance(),Sn(e.tryPeek()),e.advance();const o=[];for(;!e.isAtEnd()&&!En(e.tryPeek());){const t=er(e),n=o.flatMap(e=>Object.keys(at(e[0][1][0]))),i=at(t[0][1][0]);if(Object.keys(i).some(e=>n.includes(e)))throw new r("Duplicate binding",t[0][2]);o.push(t),bn(e.tryPeek(),",")&&e.advance()}kn(e.tryPeek()),e.advance(),wn(e.tryPeek(),"->"),e.advance();const i=e.parseExpression();return Rn(n?[s.SpecialExpression,[Nt.doseq,o,i]]:[s.SpecialExpression,[Nt.for,o,i]],t[2])}(e,o);break;case"loop":i=function(e,t){e.advance(),Sn(e.tryPeek()),e.advance();const n=[];let o=e.tryPeek();for(;!e.isAtEnd()&&!En(o);){const t=Kn(e,{requireDefaultValue:!0,noRest:!0}),r=t[1][1];t[1][1]=void 0,n.push(Rn([s.Binding,[t,r]],t[2])),bn(e.tryPeek(),",")&&e.advance(),o=e.tryPeek()}if(0===n.length)throw new r("Expected binding",e.peekSourceCodeInfo());kn(o),e.advance(),wn(e.tryPeek(),"->"),e.advance();const i=e.parseExpression();return Rn([s.SpecialExpression,[Nt.loop,n,i]],t[2])}(e,o);break;case"try":i=function(e,t){e.advance();const n=Hn(e,["catch"]);let r;e.advance(),xn(e.tryPeek())&&(e.advance(),r=Gn(e),kn(e.tryPeek()),e.advance());const o=Hn(e,["end"]);return e.advance(),Rn([s.SpecialExpression,[Nt.try,n,r,o]],t[2])}(e,o)}else if(gn(o,"do"))i=rr(e)[0];else if(gn(o,"export")){if(!n)throw new r("export is only allowed in module scope",o[2]);return function(e,t){e.advance();const n=e.tryPeek();if(pn(n,"let")){const r=Zn(e,yn(n));return Rn([s.SpecialExpression,[Nt["0_def"],r[1][1]]],t[2])}throw new r("Expected let",e.peekSourceCodeInfo())}(e,o)}i||=cr(e);let a=e.tryPeek();for(;!qn(e);){if(Un(a)){const n=a[1],r=fr(n,a[2]);if(r<=t&&(12!==r||12!==t))break;const o=Rn(Nt[n]?[s.SpecialBuiltinSymbol,Nt[n]]:[s.NormalBuiltinSymbol,Ve[n]],a[2]);e.advance();i=Yn(a,o,i,dr(e,r),a[2])}else if(pn(a)){if(!Wt(a[1]))break;const n=3;if(n<=t)break;const o=Gn(e),s=dr(e,n);if(Xe(o))throw new r("Special expressions are not allowed in binary functional operators",o[2]);i=Wn(o,[i,s],a[2])}else{if("?"!==a?.[1])break;{if(1<=t)break;e.advance();const n=dr(e);if(!bn(e.tryPeek(),":"))throw new r("Expected :",e.peekSourceCodeInfo());e.advance();const o=dr(e);i=Rn([s.SpecialExpression,[Nt.if,[i,n,o]]],i[2])}}a=e.tryPeek()}return i}class hr{cache={};firstEntry=void 0;lastEntry=void 0;_size=0;maxSize;constructor(e){var t;if(this.maxSize=null===e?null:(t=e,Math.max(0,Math.ceil(t))),"number"==typeof this.maxSize&&this.maxSize<1)throw new Error(`1 is the minimum maxSize, got ${p(e)}`)}getContent(){return Object.entries(this.cache).reduce((e,[t,n])=>(e[t]=n.value,e),{})}get size(){return this._size}get(e){return this.cache[e]?.value}clear(){this.cache={},this.firstEntry=void 0,this.lastEntry=void 0,this._size=0}has(e){return!!this.cache[e]}set(e,t){if(this.has(e))throw new Error(`AstCache - key already present: ${e}`);const n={value:t,nextEntry:void 0,key:e};for(this.cache[e]=n,this._size+=1,this.lastEntry&&(this.lastEntry.nextEntry=n),this.lastEntry=n,this.firstEntry||(this.firstEntry=this.lastEntry);null!==this.maxSize&&this.size>this.maxSize;)this.dropFirstEntry()}dropFirstEntry(){const e=this.firstEntry;delete this.cache[e.key],this._size-=1,this.firstEntry=e.nextEntry}}return e.Lits=class{astCache;astCacheSize;debug;modules;constructor(e={}){if(this.debug=e.debug??!1,this.astCacheSize=e.astCacheSize??null,this.astCacheSize){this.astCache=new hr(this.astCacheSize);const t=e.initialCache??{};for(const e of Object.keys(t))this.astCache.set(e,t[e])}else this.astCache=null;const t=e.modules??[];this.modules=new Map(t.map(e=>[e.name,e]))}getRuntimeInfo(){return{astCacheSize:this.astCacheSize,astCache:this.astCache,debug:this.debug}}run(e,t={}){const n=this.generateAst(e,t);return this.evaluate(n,t)}context(e,t={}){const n="string"==typeof e?this.generateAst(e,t):e,r=zt(t,this.modules);return Tt(n,r),r.globalContext}getUndefinedSymbols(e,t={}){const n="string"==typeof e?this.generateAst(e,t):e,r=zt(t,this.modules);return jt(n,r,Pt,_t)}tokenize(e,t={}){const n=function(e,t,n){let r=0;const o={tokens:[],filePath:n,hasDebugData:t};for(;r<e.length;){const i=t?dn(e,r,n):void 0,s=hn(e,r),[a,u]=s;r+=a,u&&(i&&(u[2]=i),o.tokens.push(u))}return o}(e,this.debug,t.filePath);return t.minify?Mn(n,{removeWhiteSpace:!1}):n}parse(e){const t={body:[],hasDebugData:(e=Mn(e,{removeWhiteSpace:!0})).hasDebugData};return t.body=function(e){e.tokens.forEach(e=>{if("Error"===e[0])throw new r(e[3],e[2])});const t=[],n=new Bn(e);for(n.parseExpression=(e=0,t=!1)=>dr(n,e,t);!n.isAtEnd();)if(t.push(dr(n,0,!0)),bn(n.tryPeek(),";"))n.advance();else if(!n.isAtEnd())throw new r("Expected ;",n.peekSourceCodeInfo());return t}(e),t}evaluate(e,t){return Tt(e,zt(t,this.modules))}transformSymbols(e,t){return function(e,t){return{...e,tokens:e.tokens.map(e=>pn(e)?[e[0],t(e[1])]:e)}}(e,t)}untokenize(e){return function(e){return e.tokens.reduce((e,t)=>`${e}${t[1]}`,"")}(e)}apply(e,t,n={}){const r="FN_2eb7b316_471c_5bfa_90cb_d3dfd9164a59",o=this.generateApplyFunctionCall(r,t),i=this.generateAst(o,n),s=t.reduce((e,t,n)=>(e[`${r}_${n}`]=t,e),{[r]:e});return n.values={...n.values,...s},this.evaluate(i,n)}generateApplyFunctionCall(e,t){const n=t.map((t,n)=>`${e}_${n}`).join(", ");return`${e}(${n})`}generateAst(e,t){if(this.astCache){const t=this.astCache.get(e);if(t)return t}const n=this.tokenize(e,{filePath:t.filePath}),r=this.parse(n);return this.astCache?.set(e,r),r}getAutoCompleter(e,t,n={}){return new _n(e,t,this,n)}},e.asLitsFunction=function(e,t){return x(e,t),e},e.asNativeJsFunction=function(e,t){return N(e,t),e},e.asUserDefinedFunction=function(e,t){return E(e,t),e},e.assertLitsFunction=x,e.assertNativeJsFunction=N,e.assertUserDefinedFunction=E,e.isBuiltinFunction=function(e){return b(e)&&"Builtin"===e.functionType},e.isGrid=Ee,e.isLitsError=function(e){return e instanceof r},e.isLitsFunction=w,e.isMatrix=ke,e.isNativeJsFunction=k,e.isUserDefinedFunction=S,e.isVector=we,e.normalExpressionKeys=At,e.specialExpressionKeys=It,e}({});
1
+ var Lits=function(e){"use strict";function t(e){if(!e.position||!e.code)return"";const t=e.position.column-1,n=e.code.length-t-1;return`${" ".repeat(Math.max(t,0))}^${" ".repeat(Math.max(n,0))}`}class n extends Error{params;constructor(e){super(`recur, params: ${e}`),Object.setPrototypeOf(this,n.prototype),this.name="RecurSignal",this.params=e}}class r extends Error{sourceCodeInfo;shortMessage;constructor(e,n){const o=e instanceof Error?e.message:`${e}`;super(function(e,n){if(!n)return e;const r=`${n.position.line}:${n.position.column}`;return`${e}${n.filePath?`\n${n.filePath}:${r}`:`\nLocation ${r}`}\n${n.code}\n${t(n)}`}(o,n)),this.shortMessage=o,this.sourceCodeInfo=n,Object.setPrototypeOf(this,r.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&t(this.sourceCodeInfo)}}class o extends r{userMessage;constructor(e,t){super(e,t),this.userMessage=e,Object.setPrototypeOf(this,o.prototype),this.name="UserDefinedError"}}class i extends r{symbol;constructor(e,t){super(`Undefined symbol '${e}'.`,t),this.symbol=e,Object.setPrototypeOf(this,i.prototype),this.name="UndefinedSymbolError"}}const s={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},a=new Set(Object.values(s));function u(e){return Object.keys(s).find(t=>s[t]===e)}const c=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const l="^^fn^^",f="^^re^^";function d(e){return null!==e&&"object"==typeof e&&(l in e&&"functionType"in e&&function(e){return"string"==typeof e&&c.has(e)}(e.functionType))}function h(e){return!(!Array.isArray(e)||e.length<2)&&function(e){return"number"==typeof e&&a.has(e)}(e[0])}function p(e){return d(e)?`<function ${e.name||"λ"}>`:h(e)?`${u(e[0])}-node`:null===e?"null":"object"==typeof e&&e instanceof RegExp?`${e}`:"object"==typeof e&&e instanceof Error?e.toString():JSON.stringify(e)}function y(e,t){return e?.sourceCodeInfo??t}function g(e,t,n){return new r(`Expected ${e}, got ${p(t)}.`,y(t,n))}function m(e,t){return v(e,t),e}function v(e,t){if(!function(e){return void 0!==e}(e))throw new r("Unexpected undefined",y(e,t))}function b(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)}function w(e){return null!==e&&"object"==typeof e&&!!e[l]}function x(e,t){if(!w(e))throw g("LitsFunction",e,t)}function S(e){return w(e)&&"UserDefined"===e.functionType}function E(e,t){if(!S(e))throw g("NativeJsFunction",e,t)}function k(e){return w(e)&&"NativeJsFunction"===e.functionType}function N(e,t){if(!k(e))throw g("NativeJsFunction",e,t)}function P(e,t={}){return"number"==typeof e&&(!Number.isNaN(e)&&(!(t.integer&&!Number.isInteger(e))&&(!(t.finite&&!Number.isFinite(e))&&((!t.zero||0===e)&&((!t.nonZero||0!==e)&&(!(t.positive&&e<=0)&&(!(t.negative&&e>=0)&&(!(t.nonPositive&&e>0)&&(!(t.nonNegative&&e<0)&&(!("number"==typeof t.gt&&e<=t.gt)&&(!("number"==typeof t.gte&&e<t.gte)&&(!("number"==typeof t.lt&&e>=t.lt)&&!("number"==typeof t.lte&&e>t.lte)))))))))))))}function $(e,t,n={}){if(!P(e,n))throw new r(`Expected ${function(e){if(e.zero)return"zero";const t=function(e){return e.positive?"positive":e.negative?"negative":e.nonNegative?"non negative":e.nonPositive?"non positive":e.nonZero?"non zero":""}(e),n=e.integer?"integer":"number",r=e.finite?"finite":"",o=function(e){return"number"!=typeof e.gt&&"number"!=typeof e.gte||"number"!=typeof e.lt&&"number"!=typeof e.lte?"number"==typeof e.gt||"number"==typeof e.gte?"number"==typeof e.gt?`n > ${e.gt}`:`n >= ${e.gte}`:"number"==typeof e.lt||"number"==typeof e.lte?"number"==typeof e.lt?`n < ${e.lt}`:`n <= ${e.lte}`:"":`${"number"==typeof e.gt?`${e.gt} < n `:`${e.gte} <= n `}${"number"==typeof e.lt?`< ${e.lt}`:`<= ${e.lte}`}`}(e);return[t,r,n,o].filter(e=>!!e).join(" ")}(n)}, got ${p(e)}.`,y(e,t))}function A(e){return void 0!==e}function I(e,t){return j(e,t),e}function j(e,t){if(!A(e))throw g("not undefined",e,t)}function C(e){return Array.isArray(e)||"string"==typeof e}function O(e,t){if(!C(e))throw g("string or array",e,t)}function U(e){return!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof RegExp||w(e)||_(e))}function F(e,t){if(!U(e))throw g("object",e,t)}function M(e){return C(e)||U(e)}function T(e,t){if(!M(e))throw g("string, array or object",e,t)}function _(e){return null!==e&&"object"==typeof e&&!!e[f]}function B(e,t){if(!function(e){return _(e)||"string"==typeof e}(e))throw g("string or RegularExpression",e,t)}function R(e,t){return D(e,t),e}function D(e,t){if(!function(e){return"number"==typeof e||!!M(e)||!!w(e)}(e))throw g("FunctionLike",e,t)}function z(e,t,n){const{min:o,max:i}=e;if("number"==typeof o&&t<o)throw new r(`Wrong number of arguments, expected at least ${o}, got ${p(t)}.`,n);if("number"==typeof i&&t>i)throw new r(`Wrong number of arguments, expected at most ${i}, got ${p(t)}.`,n)}function L(e){return{min:e,max:e}}const V={"<<":{evaluate:([e,t],n)=>($(e,n,{integer:!0}),$(t,n,{integer:!0,nonNegative:!0}),e<<t),arity:L(2)},">>":{evaluate:([e,t],n)=>($(e,n,{integer:!0}),$(t,n,{integer:!0,nonNegative:!0}),e>>t),arity:L(2)},">>>":{evaluate:([e,t],n)=>($(e,n,{integer:!0}),$(t,n,{integer:!0,nonNegative:!0}),e>>>t),arity:L(2)},"&":{evaluate:([e,...t],n)=>($(e,n,{integer:!0}),t.reduce((e,t)=>($(t,n,{integer:!0}),e&t),e)),arity:{min:2}},"|":{evaluate:([e,...t],n)=>($(e,n,{integer:!0}),t.reduce((e,t)=>($(t,n,{integer:!0}),e|t),e)),arity:{min:2}},xor:{evaluate:([e,...t],n)=>($(e,n,{integer:!0}),t.reduce((e,t)=>($(t,n,{integer:!0}),e^t),e)),arity:{min:2}}};function J(e,t={}){return"string"==typeof e&&((!t.nonEmpty||0!==e.length)&&(!t.char||1===e.length))}function W(e,t,n={}){if(!J(e,n))throw g(""+(n.nonEmpty?"non empty string":n.char?"character":"string"),e,t)}function q(e,t,n={}){return W(e,t,n),e}function Y(e){return"string"==typeof e||"number"==typeof e}function G(e,t){return K(e,t),e}function K(e,t){if(!Y(e))throw g("string or number",e,t)}function X(e,t){return!!M(e)&&("string"==typeof e||Array.isArray(e)?!!P(t,{integer:!0})&&(t>=0&&t<e.length):!!Object.getOwnPropertyDescriptor(e,t))}function Z(e,t,n){if(K(e,n),K(t,n),"string"==typeof e&&"string"==typeof t)return e<t?-1:e>t?1:0;if("number"==typeof e&&"number"==typeof t)return Math.sign(e-t);throw new r(`Cannot compare values of different types: ${typeof e} and ${typeof t}`,n)}function H(e,t,n){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return function(e,t,n=ne){if(e===t)return!0;const r=Math.abs(e-t);if(0===e||0===t||r<n)return r<n;const o=Math.abs(e),i=Math.abs(t);return r/(o+i)<n}(e,t);if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r+=1)if(!H(I(e[r],n),I(t[r],n),n))return!1;return!0}if(_(e)&&_(t))return e.s===t.s&&e.f===t.f;if(b(e)&&b(t)){const r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let o=0;o<r.length;o+=1){const i=q(r[o],n);if(!H(e[i],t[i],n))return!1}return!0}return!1}function Q(e){return e??null}function ee(...e){const t=new Set;for(const n of e)n.forEach(e=>t.add(e));return t}function te(e,t){t.forEach(t=>e.add(t))}const ne=1e-10;function re(e,t=0){const n=e.split("\n");for(;n[0]?.match(/^\s*$/);)n.shift();for(;n[n.length-1]?.match(/^\s*$/);)n.pop();const r=n.reduce((e,t)=>{if(t.match(/^\s*$/))return e;const n=t.match(/^\s*/)[0].length;return Math.min(e,n)},1/0);return n.map(e=>" ".repeat(t)+e.slice(r)).join("\n").trimEnd()}function oe(e,t){if(!Array.isArray(e))throw g("array",e,t)}function ie(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e)}(e))throw g("array of strings",e,t)}function se(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e&&1===e.length)}(e))throw g("array of strings",e,t)}function ae(e,t){return e instanceof Promise?e.then(t):t(e)}function ue(e,t){const n=[];for(let r=0;r<e.length;r++){const o=t(e[r],r);if(o instanceof Promise)return ce(o,n,e,t,r);n.push(o)}return n}async function ce(e,t,n,r,o){t.push(await e);for(let e=o+1;e<n.length;e++)t.push(await r(n[e],e));return t}function le(e,t,n){let r=n;for(let n=0;n<e.length;n++){const o=t(r,e[n],n);if(o instanceof Promise)return fe(o,e,t,n);r=o}return r}async function fe(e,t,n,r){let o=await e;for(let e=r+1;e<t.length;e++)o=await n(o,t[e],e);return o}function de(e,t){for(let n=0;n<e.length;n++){const r=t(e[n],n);if(r instanceof Promise)return he(r,e,t,n)}}async function he(e,t,n,r){await e;for(let e=r+1;e<t.length;e++)await n(t[e],e)}function pe(e,t){try{const n=e();return n instanceof Promise?n.catch(t):n}catch(e){return t(e)}}const ye={filter:{evaluate:([e,t],n,r,{executeFunction:o})=>{if(T(e,n),D(t,n),Array.isArray(e))return le(e,(e,i)=>ae(o(t,[i],r,n),t=>(t&&e.push(i),e)),[]);if(J(e)){return ae(le(e.split(""),(e,i)=>ae(o(t,[i],r,n),t=>(t&&e.push(i),e)),[]),e=>e.join(""))}return le(Object.entries(e),(e,[i,s])=>ae(o(t,[s],r,n),t=>(t&&(e[i]=s),e)),{})},arity:L(2)},map:{evaluate:(e,t,n,{executeFunction:o})=>{const i=R(e.at(-1),t);if(U(e[0]))return function({colls:e,contextStack:t,executeFunction:n,fn:o,sourceCodeInfo:i}){F(e[0],i);const s=Object.keys(e[0]),a={};return e.forEach(e=>{F(e,i);const t=Object.keys(e);if(t.length!==s.length)throw new r(`All objects must have the same keys. Expected: ${s.join(", ")}. Found: ${t.join(", ")}`,i);if(!t.every(e=>s.includes(e)))throw new r(`All objects must have the same keys. Expected: ${s.join(", ")}. Found: ${t.join(", ")}`,i);Object.entries(e).forEach(([e,t])=>{a[e]||(a[e]=[]),a[e].push(t)})}),le(s,(e,r)=>ae(n(o,a[r],t,i),t=>(e[r]=t,e)),{})}({colls:e.slice(0,-1),fn:i,sourceCodeInfo:t,contextStack:n,executeFunction:o});const s=e.slice(0,-1);O(s[0],t);const a="string"==typeof s[0];let u=s[0].length;s.slice(1).forEach(e=>{a?W(e,t):oe(e,t),u=Math.min(u,e.length)});const c=[];for(let e=0;e<u;e++)c.push(s.map(t=>t[e]));const l=ue(c,e=>o(i,e,n,t));return a?ae(l,e=>(e.forEach(e=>W(e,t)),e.join(""))):l},arity:{min:2}},reduce:{evaluate:([e,t,n],r,o,{executeFunction:i})=>(T(e,r),D(t,r),j(n,r),"string"==typeof e?(W(n,r),0===e.length?n:le(e.split(""),(e,n)=>i(t,[e,n],o,r),n)):Array.isArray(e)?0===e.length?n:le(e,(e,n)=>i(t,[e,n],o,r),n):0===Object.keys(e).length?n:le(Object.entries(e),(e,[,n])=>i(t,[e,n],o,r),n)),arity:L(3)},get:{evaluate:(e,t)=>{const[n,r]=e,o=Q(e[2]);if(K(r,t),null===n)return o;T(n,t);const i=function(e,t){if(U(e)){if("string"==typeof t&&X(e,t))return Q(e[t])}else if(P(t,{nonNegative:!0,integer:!0})&&t>=0&&t<e.length)return Q(e[t])}(n,r);return void 0===i?o:i},arity:{min:2,max:3}},count:{evaluate:([e],t)=>null===e?0:"string"==typeof e?e.length:(T(e,t),Array.isArray(e)?e.length:Object.keys(e).length),arity:L(1)},"contains?":{evaluate:([e,t],n)=>null!==e&&(T(e,n),J(e)?(W(t,n),e.includes(t)):C(e)?(j(t,n),!!e.find(e=>H(I(e),t,n))):(W(t,n),t in e)),arity:L(2)},assoc:{evaluate:([e,t,n],r)=>(T(e,r),K(t,r),j(n,r),function(e,t,n,r){if(T(e,r),K(t,r),Array.isArray(e)||"string"==typeof e){if($(t,r,{integer:!0}),$(t,r,{gte:0}),$(t,r,{lte:e.length}),"string"==typeof e)return W(n,r,{char:!0}),`${e.slice(0,t)}${n}${e.slice(t+1)}`;const o=[...e];return o[t]=n,o}W(t,r);const o={...e};return o[t]=n,o}(e,t,n,r)),arity:L(3)},"++":{evaluate:(e,t)=>(P(e[0])||T(e[0],t),Array.isArray(e[0])?e.reduce((e,n)=>(oe(n,t),e.concat(n)),[]):Y(e[0])?e.reduce((e,n)=>(K(n,t),`${e}${n}`),""):e.reduce((e,n)=>(F(n,t),Object.assign(e,n)),{})),arity:{min:1}}},ge={range:{evaluate:(e,t)=>{const[n,r,o]=e;let i,s,a;$(n,t,{finite:!0}),1===e.length?(i=0,s=n,a=s>=0?1:-1):2===e.length?($(r,t,{finite:!0}),i=n,s=r,a=s>=i?1:-1):($(r,t,{finite:!0}),$(o,t,{finite:!0}),i=n,s=r,a=o,$(a,t,s>i?{positive:!0}:s<i?{negative:!0}:{nonZero:!0}));const u=[];for(let e=i;a<0?e>s:e<s;e+=a)u.push(e);return u},arity:{min:1,max:3}},repeat:{evaluate:([e,t],n)=>{$(t,n,{integer:!0,nonNegative:!0});const r=[];for(let n=0;n<t;n+=1)r.push(e);return r},arity:L(2)},flatten:{evaluate:([e,t],n)=>{oe(e,n);const r=void 0===t||t===Number.POSITIVE_INFINITY?Number.POSITIVE_INFINITY:function(e,t,n={}){return $(e,t,n),e}(t,n,{integer:!0,nonNegative:!0});return e.flat(r)},arity:{min:1,max:2}},mapcat:{evaluate:([e,t],n,r,{executeFunction:o})=>(oe(e,n),D(t,n),ae(ue(e,e=>o(t,[e],r,n)),e=>e.flat(1))),arity:L(2)},"moving-fn":{evaluate:([e,t,n],r,o,{executeFunction:i})=>{oe(e,r),$(t,r,{integer:!0,lte:e.length}),D(n,r);const s=[];for(let n=0;n<=e.length-t;n++)s.push(e.slice(n,n+t));return ue(s,e=>i(n,[e],o,r))},arity:L(3)},"running-fn":{evaluate:([e,t],n,r,{executeFunction:o})=>{oe(e,n),D(t,n);const i=[];for(let t=0;t<e.length;t+=1)i.push(e.slice(0,t+1));return ue(i,e=>o(t,[e],r,n))},arity:L(2)}},me={nth:{evaluate:(e,t)=>{const[n,r]=e,o=Q(e[2]);if($(r,t,{integer:!0}),null===n)return o;if(O(n,t),r>=0&&r<n.length){return Q(n[r])}return o},arity:{min:2,max:3}},first:{evaluate:([e],t)=>{if(null===e)return null;O(e,t);return Q(e[0])},arity:L(1)},last:{evaluate:([e],t)=>{if(null===e)return null;O(e,t);return Q(e.at(-1))},arity:L(1)},pop:{evaluate:([e],t)=>(O(e,t),"string"==typeof e?e.substring(0,e.length-1):e.slice(0,e.length-1)),arity:L(1)},"index-of":{evaluate:([e,t],n)=>{if(j(t,n),null===e)return null;if(O(e,n),"string"==typeof e){W(t,n);const r=e.indexOf(t);return-1!==r?r:null}{const r=e.findIndex(e=>H(I(e,n),t),n);return-1!==r?r:null}},arity:L(2)},push:{evaluate:([e,...t],n)=>(O(e,n),"string"==typeof e?(se(t,n),[e,...t].join("")):[...e,...t]),arity:{min:2}},rest:{evaluate:([e],t)=>(O(e,t),Array.isArray(e)?e.length<=1?[]:e.slice(1):e.substring(1)),arity:L(1)},next:{evaluate:([e],t)=>(O(e,t),Array.isArray(e)?e.length<=1?null:e.slice(1):e.length<=1?null:e.substring(1)),arity:L(1)},reverse:{evaluate:([e],t)=>null===e?null:(O(e,t),Array.isArray(e)?[...e].reverse():e.split("").reverse().join("")),arity:L(1)},second:{evaluate:([e],t)=>null===e?null:(O(e,t),Q(e[1])),arity:L(1)},slice:{evaluate:(e,t)=>{const[n,r,o]=e;return O(n,t),$(r,t,{integer:!0}),2===e.length?(Array.isArray(n),n.slice(r)):($(o,t,{integer:!0}),Array.isArray(n),n.slice(r,o))},arity:{min:2,max:3}},some:{evaluate:([e,t],n,r,{executeFunction:o})=>{if(D(t,n),null===e)return null;if(O(e,n),0===e.length)return null;return le("string"==typeof e?e.split(""):e,(e,i)=>null!==e?e:ae(o(t,[i],r,n),e=>e?Q(i):null),null)},arity:L(2)},sort:{evaluate:(e,t,n,{executeFunction:r})=>{const[o]=e,i=1===e.length,s=i?null:e[1];if(O(o,t),"string"==typeof o){const e=o.split("");return i?e.sort((e,n)=>Z(e,n,t)):(D(s,t),e.sort((e,o)=>{const i=r(s,[e,o],n,t);if(i instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return $(i,t,{finite:!0}),i})),e.join("")}const a=[...o];return i?a.sort((e,n)=>(K(e,t),K(n,t),Z(e,n,t))):a.sort((e,o)=>{D(s,t);const i=r(s,[e,o],n,t);if(i instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return $(i,t,{finite:!0}),i}),a},arity:{min:1,max:2}}},ve=new WeakSet,be=new WeakSet,we=new WeakSet,xe=new WeakSet,Se=new WeakSet,Ee=new WeakSet,ke=new WeakSet;function Ne(e){return Array.isArray(e)?(ve.has(e)||(Pe(e),je(e)||Ie(e)),e):e}function Pe(e){return!!Array.isArray(e)&&(!!be.has(e)||!we.has(e)&&(e.every(e=>P(e))?(ve.add(e),be.add(e),!0):(we.add(e),!1)))}function $e(e,t){if(!Pe(e))throw new r(`Expected a vector, but got ${e}`,t)}function Ae(e,t){if($e(e,t),0===e.length)throw new r(`Expected a non empty vector, but got ${e}`,t)}function Ie(e){if(!Array.isArray(e))return!1;if(Ee.has(e))return!0;if(ke.has(e))return!1;if(0===e.length)return ke.add(e),!1;if(!Array.isArray(e[0]))return ke.add(e),!1;const t=e[0].length;for(const n of e.slice(1)){if(!Array.isArray(n))return ke.add(e),!1;if(n.length!==t)return ke.add(e),!1}return ve.add(e),Ee.add(e),!0}function je(e){if(!Array.isArray(e))return!1;if(xe.has(e))return!0;if(Se.has(e))return!1;if(0===e.length)return Se.add(e),!1;if(!Array.isArray(e[0])||0===e[0].length)return Se.add(e),!1;const t=e[0].length;for(const n of e)if(!Array.isArray(n)||n.length!==t||n.some(e=>!P(e)))return Se.add(e),!1;return ve.add(e),Ee.add(e),xe.add(e),!0}function Ce(e,t){let n=!1,o=!1;for(const i of e)if(Pe(i))n=!0;else if(je(i))o=!0;else if(!P(i))throw new r("Invalid parameter type: "+typeof i,t);if(o){if(n)throw new r("Cannot mix vector and matrix types",t);let o=null,i=null;for(const n of e)if(je(n))if(null===o)o=n.length,i=n[0].length;else if(n.length!==o||n[0].length!==i)throw new r("Matrix dimensions do not match",t);return["matrix",e.map(e=>je(e)?e:Array.from({length:o},()=>Array.from({length:i},()=>e)))]}if(n){let n=null;for(const o of e)if(Pe(o))if(null===n)n=o.length;else if(o.length!==n)throw new r("Vector lengths do not match",t);return["vector",e.map(e=>Pe(e)?e:Array.from({length:n},()=>e))]}return["number",e]}function Oe(e){return(t,n)=>{const[r,o]=Ce(t,n);return"number"===r?e(o[0]):"vector"===r?o[0].map(t=>e(t)):o[0].map(t=>t.map(t=>e(t)))}}function Ue(e){return(t,n)=>{const[r,o]=Ce(t,n);return"number"===r?e(o[0],o[1]):"vector"===r?o[0].map((t,n)=>e(t,o[1][n])):o[0].map((t,n)=>t.map((t,r)=>e(t,o[1][n][r])))}}function Fe(e,t){return(n,r)=>{if(0===n.length)return e;const[o,i]=Ce(n,r);if("number"===o)return i.reduce((e,n)=>t(e,n),e);if("vector"===o){const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>t(e,n[r])),e)}{const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>e.map((e,o)=>t(e,n[r][o]))),e)}}}const Me={inc:{evaluate:Oe(e=>e+1),arity:L(1)},dec:{evaluate:Oe(e=>e-1),arity:L(1)},"+":{evaluate:Fe(0,(e,t)=>e+t),arity:{}},"*":{evaluate:Fe(1,(e,t)=>e*t),arity:{}},"/":{evaluate:(e,t)=>{if(0===e.length)return 1;const[n,r]=Ce(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?1/e:t.reduce((e,t)=>e/t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e/t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e/t[n][r])),e)}},arity:{}},"-":{evaluate:(e,t)=>{if(0===e.length)return 0;const[n,r]=Ce(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?-e:t.reduce((e,t)=>e-t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e-t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e-t[n][r])),e)}},arity:{}},quot:{evaluate:Ue((e,t)=>Math.trunc(e/t)),arity:L(2)},mod:{evaluate:Ue((e,t)=>e-t*Math.floor(e/t)),arity:L(2)},"%":{evaluate:Ue((e,t)=>e%t),arity:L(2)},sqrt:{evaluate:Oe(e=>Math.sqrt(e)),arity:L(1)},cbrt:{evaluate:Oe(e=>Math.cbrt(e)),arity:L(1)},"^":{evaluate:Ue((e,t)=>e**t),arity:L(2)},round:{evaluate:([e,t],n)=>{const[r,o]=Ce([e],n);if("number"===r){if(void 0===t||0===t)return Math.round(o[0]);{$(t,n,{integer:!0,positive:!0});const e=10**t;return Math.round(o[0]*e)/e}}if("vector"===r){const e=o[0];if(void 0===t||0===t)return e.map(e=>Math.round(e));{$(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>Math.round(e*r)/r)}}{const e=o[0];if(void 0===t||0===t)return e.map(e=>e.map(e=>Math.round(e)));{$(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>e.map(e=>Math.round(e*r)/r))}}},arity:{min:1,max:2}},trunc:{evaluate:Oe(e=>Math.trunc(e)),arity:L(1)},floor:{evaluate:Oe(e=>Math.floor(e)),arity:L(1)},ceil:{evaluate:Oe(e=>Math.ceil(e)),arity:L(1)},min:{evaluate:(e,t)=>{if(1===e.length&&Pe(e[0])){const n=e[0];return Ae(n,t),n.reduce((e,t)=>Math.min(e,t),1/0)}const[n,...r]=e;return $(n,t),r.reduce((e,n)=>($(n,t),Math.min(e,n)),n)},arity:{min:1}},max:{evaluate:(e,t)=>{if(1===e.length&&Pe(e[0])){const n=e[0];return Ae(n,t),n.reduce((e,t)=>Math.max(e,t),-1/0)}const[n,...r]=e;return $(n,t),r.reduce((e,n)=>($(n,t),Math.max(e,n)),n)},arity:{min:1}},abs:{evaluate:Oe(e=>Math.abs(e)),arity:L(1)},sign:{evaluate:Oe(e=>Math.sign(e)),arity:L(1)}};function Te([e,...t],n){const r=I(e,n);for(const e of t)if(!H(r,I(e,n),n))return!1;return!0}const _e={"==":{evaluate:(e,t)=>Te(e,t),arity:{min:1}},"≠":{evaluate:(e,t)=>!Te(e,t),arity:{min:1}},"identical?":{evaluate:e=>function([e,...t]){for(const n of t)if(n!==e)return!1;return!0}(e),arity:{min:1}},">":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(Z(r,G(e),n)<=0)return!1;r=G(e)}return!0},arity:{min:1}},"<":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(Z(r,G(e),n)>=0)return!1;r=G(e)}return!0},arity:{min:1}},">=":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(Z(r,G(e),n)<0)return!1;r=G(e)}return!0},arity:{min:1}},"<=":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(Z(r,G(e),n)>0)return!1;r=G(e)}return!0},arity:{min:1}},"!":{evaluate:([e])=>!e,arity:L(1)},"epoch->iso-date":{evaluate:([e],t)=>($(e,t),new Date(e).toISOString()),arity:L(1)},"iso-date->epoch":{evaluate:([e],t)=>{W(e,t);const n=new Date(e).valueOf();return $(n,t,{finite:!0}),n},arity:L(1)},"write!":{evaluate:(e,t)=>(console.log(...e),e.length>0?I(e[e.length-1],t):null),arity:{}},boolean:{evaluate:([e])=>!!e,arity:L(1)},compare:{evaluate:([e,t],n)=>(K(e,n),K(t,n),Z(e,t,n)),arity:L(2)},"json-parse":{evaluate:([e],t)=>(W(e,t),JSON.parse(e)),arity:L(1)},"json-stringify":{evaluate:([e,t],n)=>(j(e,n),void 0===t?JSON.stringify(e):($(t,n),JSON.stringify(e,null,t))),arity:{min:1,max:2}},import:{evaluate:([e],t,n)=>{W(e,t);const o=e.indexOf(".");if(-1!==o){const i=e.substring(0,o),s=e.substring(o+1),a=n.getModule(i);if(!a)throw new r(`Unknown module: '${i}'`,t);const u=a.functions[s];if(!u)throw new r(`Function '${s}' not found in module '${i}'`,t);return{[l]:!0,sourceCodeInfo:t,functionType:"Module",moduleName:i,functionName:s,arity:u.arity}}const i=e,s=n.getModule(i);if(!s)throw new r(`Unknown module: '${i}'`,t);const a={};for(const[e,n]of Object.entries(s.functions))a[e]={[l]:!0,sourceCodeInfo:t,functionType:"Module",moduleName:i,functionName:e,arity:n.arity};return a},arity:L(1)}},Be={keys:{evaluate:([e],t)=>(F(e,t),Object.keys(e)),arity:L(1)},vals:{evaluate:([e],t)=>(F(e,t),Object.values(e)),arity:L(1)},entries:{evaluate:([e],t)=>(F(e,t),Object.entries(e)),arity:L(1)},find:{evaluate:([e,t],n)=>(F(e,n),W(t,n),X(e,t)?[t,e[t]]:null),arity:L(2)},dissoc:{evaluate:([e,t],n)=>{F(e,n),W(t,n);const r={...e};return delete r[t],r},arity:L(2)},merge:{evaluate:(e,t)=>{if(0===e.length)return null;const[n,...r]=e;return F(n,t),r.reduce((e,n)=>(F(n,t),{...e,...n}),{...n})},arity:{min:0}},"merge-with":{evaluate:(e,t,n,{executeFunction:r})=>{const o=e[0],i=e.at(-1),s=e.slice(1,-1);return F(o,t),D(i,t),le(s,(e,o)=>{F(o,t);return ae(le(Object.entries(o),(e,o)=>{const s=q(o[0],t),a=Q(o[1]);return X(e,s)?ae(r(i,[e[s],a],n,t),t=>(e[s]=t,e)):(e[s]=a,e)},e),e=>e)},{...o})},arity:{min:2}},zipmap:{evaluate:([e,t],n)=>{ie(e,n),oe(t,n);const r=Math.min(e.length,t.length),o={};for(let i=0;i<r;i+=1){o[q(e[i],n)]=Q(t[i])}return o},arity:L(2)},"select-keys":{evaluate:([e,t],n)=>(ie(t,n),F(e,n),t.reduce((t,n)=>(X(e,n)&&(t[n]=Q(e[n])),t),{})),arity:L(2)}},Re={"function?":{evaluate:([e])=>w(e),arity:L(1)},"string?":{evaluate:([e])=>"string"==typeof e,arity:L(1)},"number?":{evaluate:([e])=>"number"==typeof e,arity:L(1)},"integer?":{evaluate:([e])=>"number"==typeof e&&P(e,{integer:!0}),arity:L(1)},"boolean?":{evaluate:([e])=>"boolean"==typeof e,arity:L(1)},"null?":{evaluate:([e])=>null==e,arity:L(1)},"zero?":{evaluate:([e],t)=>($(e,t,{finite:!0}),Math.abs(e)<ne),arity:L(1)},"pos?":{evaluate:([e],t)=>($(e,t,{finite:!0}),e>0),arity:L(1)},"neg?":{evaluate:([e],t)=>($(e,t,{finite:!0}),e<0),arity:L(1)},"even?":{evaluate:([e],t)=>($(e,t,{finite:!0}),e%2==0),arity:L(1)},"odd?":{evaluate:([e],t)=>($(e,t,{finite:!0}),P(e,{integer:!0})&&e%2!=0),arity:L(1)},"array?":{evaluate:([e])=>Array.isArray(e),arity:L(1)},"collection?":{evaluate:([e])=>M(e),arity:L(1)},"sequence?":{evaluate:([e])=>C(e),arity:L(1)},"object?":{evaluate:([e])=>U(e),arity:L(1)},"regexp?":{evaluate:([e])=>_(e),arity:L(1)},"finite?":{evaluate:([e],t)=>($(e,t),Number.isFinite(e)),arity:L(1)},"positive-infinity?":{evaluate:([e],t)=>($(e,t),e===Number.POSITIVE_INFINITY),arity:L(1)},"negative-infinity?":{evaluate:([e],t)=>($(e,t),e===Number.NEGATIVE_INFINITY),arity:L(1)},"true?":{evaluate:([e])=>!0===e,arity:L(1)},"false?":{evaluate:([e])=>!1===e,arity:L(1)},"empty?":{evaluate:([e],t)=>null===e||(T(e,t),"string"==typeof e||Array.isArray(e)?0===e.length:0===Object.keys(e).length),arity:L(1)},"not-empty?":{evaluate:([e],t)=>null!==e&&(T(e,t),"string"==typeof e||Array.isArray(e)?e.length>0:Object.keys(e).length>0),arity:L(1)},"vector?":{evaluate:([e])=>Pe(e),arity:L(1)},"matrix?":{evaluate:([e])=>je(e),arity:L(1)},"grid?":{evaluate:([e])=>Ie(e),arity:L(1)}},De={regexp:{evaluate:([e,t],n)=>{W(e,n);const o=e||"(?:)",i="string"==typeof t?t:"";try{new RegExp(o,i)}catch(e){throw new r(`Invalid regular expression: ${o} ${i}`,n)}return{[f]:!0,sourceCodeInfo:n,s:o,f:i}},arity:{min:1,max:2}},match:{evaluate:([e,t],n)=>{if(function(e,t){if(!_(e))throw g("RegularExpression",e,t)}(t,n),!J(e))return null;const r=new RegExp(t.s,t.f).exec(e);return r?[...r]:null},arity:L(2)},replace:{evaluate:([e,t,n],r)=>{W(e,r),B(t,r),W(n,r);const o=_(t)?new RegExp(t.s,`${t.f}`):t;return e.replace(o,n)},arity:L(3)},"replace-all":{evaluate:([e,t,n],r)=>{W(e,r),B(t,r),W(n,r);const o=_(t)?new RegExp(t.s,`${t.f.includes("g")?t.f:`${t.f}g`}`):t;return e.replaceAll(o,n)},arity:L(3)}},ze=/^\s*$/,Le={str:{evaluate:e=>e.reduce((e,t)=>e+(null==t?"":U(t)||Array.isArray(t)?JSON.stringify(t):`${t}`),""),arity:{}},number:{evaluate:([e],t)=>{W(e,t);const n=Number(e);if(Number.isNaN(n))throw new r(`Could not convert '${e}' to a number.`,t);return n},arity:L(1)},"lower-case":{evaluate:([e],t)=>(W(e,t),e.toLowerCase()),arity:L(1)},"upper-case":{evaluate:([e],t)=>(W(e,t),e.toUpperCase()),arity:L(1)},trim:{evaluate:([e],t)=>(W(e,t),e.trim()),arity:L(1)},join:{evaluate:([e,t],n)=>(oe(e,n),e.forEach(e=>K(e,n)),W(t,n),e.join(t)),arity:L(2)},split:{evaluate:([e,t,n],r)=>{W(e,r),B(t,r),void 0!==n&&$(n,r,{integer:!0,nonNegative:!0});const o="string"==typeof t?t:new RegExp(t.s,t.f);return e.split(o,n)},arity:{min:2,max:3}},"blank?":{evaluate:([e],t)=>null===e||(W(e,t),ze.test(e)),arity:L(1)}},Ve={"|>":{evaluate:([e,t],n,r,{executeFunction:o})=>(D(t,n),o(t,[e],r,n)),arity:L(2)},apply:{evaluate:([e,...t],n,r,{executeFunction:o})=>{D(e,n);const i=t[t.length-1];oe(i,n);return o(e,[...t.slice(0,-1),...i],r,n)},arity:{min:2}},identity:{evaluate:([e])=>Q(e),arity:L(1)},comp:{evaluate:(e,t)=>{return e.forEach(e=>D(e,t)),{[l]:!0,sourceCodeInfo:t,functionType:"Comp",params:e,arity:e.length>0?(n=e.at(-1),"number"==typeof n||M(n)?L(1):n.arity):{min:1,max:1}};var n},arity:{}},constantly:{evaluate:([e],t)=>({[l]:!0,sourceCodeInfo:t,functionType:"Constantly",value:Q(e),arity:{}}),arity:L(1)}};function Je(e){return re(`\n ${e.title}\n\n ${e.description.replace(/`(.+?)`/g,"$1").replace(/\$(\w+)/g,"$1").replace(/\*\*\*(.+)\*\*\*/g,"$1").replace(/\*\*(.+)\*\*/g,"$1")}\n\n Signature:\n ${function({title:e,variants:t,args:n,returns:r,_isOperator:o}){const i=t.map(t=>`${` ${e}(${t.argumentNames.map(e=>{let t="";return n[e].rest&&(t+="..."),t+=e,t}).join(", ")})`} -> ${We(r)}`),s=o?["","Operator:",` a ${e} b -> ${We(r)}`]:[];return[...i,...s]}(e).join("\n ")}\n\n Arguments:\n ${function(e){return Object.entries(e.args).map(([e,t])=>`${e}: ${We(t)}`)}(e).join("\n ")}\n\n Examples:\n${e.examples.map(e=>re(e,4)).join("\n\n")}`)}function We(e){const t=e.type,n=(Array.isArray(t)?t:[t]).join(" | ");return e.array||e.rest?`Array<${n}>`:n}const qe={sum:{evaluate:([e],t)=>($e(e,t),e.reduce((e,t)=>e+t,0)),arity:L(1)},prod:{evaluate:([e],t)=>($e(e,t),e.reduce((e,t)=>e*t,1)),arity:L(1)},mean:{evaluate:([e],t)=>(Ae(e,t),e.reduce((e,t)=>e+t,0)/e.length),arity:L(1)},median:{evaluate:([e],t)=>(Ae(e,t),function(e){const t=[...e].sort((e,t)=>e-t),n=Math.floor(t.length/2);return t.length%2==0?(t[n-1]+t[n])/2:t[n]}(e)),arity:L(1)}},Ye={...V,...ye,...ge,...me,...Me,...function(e){return{doc:{evaluate:([t],n)=>{if(v(e),D(t,n),!w(t))return"";if("Builtin"===t.functionType){const n=e[t.name];return n?Je(n):""}return"UserDefined"===t.functionType||"NativeJsFunction"===t.functionType?t.docString:""},arity:L(1)},arity:{evaluate:([e],t)=>(D(e,t),w(e)?e.arity:L(1)),arity:L(1)}}}({}),..._e,...Be,...Re,...De,...Le,...Ve,...qe};Object.entries(Ye).forEach(([e,t])=>{t.name=e});const Ge={...Ye},Ke={},Xe=[];Object.entries(Ge).forEach(([e,t],n)=>{Ke[e]=n,Xe.push(t)});const Ze={arity:{},evaluate:(e,t,{evaluateNode:n})=>le(e[1][1],(e,r)=>e?n(r,t):e,!0),evaluateAsNormalExpression:(e,t)=>{let n=!0;for(const r of e)if(n=I(r,t),!n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},He={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1];return function e(o){if(o>=r.length)return null;const[i,s]=r[o];return ae(n(i,t),r=>r?n(s,t):e(o+1))}(0)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].flat(),t,r,o)},Qe={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o]=e[1];return ae(n(r,t),e=>function r(i){if(i>=o.length)return null;const[s,a]=o[i];return ae(n(s,t),o=>o===e?n(a,t):r(i+1))}(0))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1],...e[1][2].flat()],t,r,o)};function et(e){const t=e[0];return s.UserDefinedSymbol===t||s.NormalBuiltinSymbol===t||s.SpecialBuiltinSymbol===t}function tt(e){return s.UserDefinedSymbol===e[0]}function nt(e,t){return function(e,t){if(!tt(e))throw g("UserDefinedSymbolNode",e,t)}(e,t),e}function rt(e){return s.NormalBuiltinSymbol===e[0]}function ot(e){return s.SpecialBuiltinSymbol===e[0]}function it(e){return!!function(e){return e[0]===s.NormalExpression}(e)&&et(e[1][0])}function st(e){return e[0]===s.Spread}const at={arity:L(1),evaluate:(e,t)=>{const n=e[1][1];if(function(e,t){if(!et(e))throw g("SymbolNode",e,t)}(n),!tt(n))return!0;return null!==t.lookUp(n)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)};const ut=11,ct=12,lt=13,ft=14;function dt(e,t){if(e[0]===lt)Object.values(e[1][0]).forEach(e=>{e[1][1]&&t(e[1][1]),dt(e,t)});else if(e[0]===ft)for(let n=0;n<e[1][0].length;n+=1){const r=e[1][0][n]??null;null!==r&&(r[1][1]&&t(r[1][1]),dt(r,t))}}function ht(e,t,n){const r=e[2],o={};return ae(pt(e,t,n,r,o),()=>o)}function pt(e,t,n,o,i){if(e[0]===lt){!function(e,t){if(!b(e))throw new r(`Expected UnknownRecord, got ${p(e)}.`,y(e,t))}(t,o);const s=new Set;let a;return ae(de(Object.entries(e[1][0]),([e,r])=>{if(r[0]===ct)return void(a=r);s.add(e);const u=t[e];return ae(void 0!==u?u:r[1][1]?n(r[1][1]):null,e=>{const t=e??null;return j(t,o),pt(r,t,n,o,i)})}),()=>{if(a){const e=Object.entries(t).filter(([e])=>!s.has(e)).reduce((e,[t,n])=>(e[t]=I(n),e),{});i[a[1][0]]=e}})}if(e[0]===ft){let r=null;oe(t,o);const s=[];for(let t=0;t<e[1][0].length;t+=1){const n=e[1][0][t]??null;if(null!==n){if(n[0]===ct){r=t;break}s.push({element:n,index:t})}}return ae(de(s,({element:e,index:r})=>{const s=t[r];return ae(void 0!==s?s:e[1][1]?n(e[1][1]):null,t=>{const r=t??null;return j(r,o),pt(e,r,n,o,i)})}),()=>{if(null!==r){const n=t.slice(r),o=e[1][0][r];i[o[1][0]]=n}})}e[0]===ct?i[e[1][0]]=I(t):i[e[1][0][1]]=I(t)}function yt(e){const t={};return gt(e,t),t}function gt(e,t){if(null!==e)if(e[0]===ft)for(const n of e[1][0])gt(n,t);else if(e[0]===lt)for(const n of Object.values(e[1][0]))gt(n,t);else if(e[0]===ct){if(t[e[1][0]])throw new r(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0]]=!0}else{if(t[e[1][0][1]])throw new r(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0][1]]=!0}}const mt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1],o=r[1][0],i=r[1][1];return ae(n(i,t),e=>ae(ht(o,e,e=>n(e,t)),n=>(t.exportValues(n,o[2]),e)))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i[1][0],a=i[1][1],u=n([a],t,r,o);return dt(s,e=>{te(u,n([e],t,r,o))}),t.addValues(yt(s),s[2]),u}},vt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=t.create({});return le(e[1][1],(e,t)=>n(t,r),null)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t.create({}),r,o)},bt={arity:{},evaluate:(e,t,{builtin:n,getUndefinedSymbols:r,evaluateNode:o})=>{const i=e[1][1],s=e[1][2],a=function(e,t,n,r,o){const i={},s=e[0].reduce((e,t)=>(Object.keys(yt(t)).forEach(t=>{e[t]={value:null}}),e),{}),a=r(e[1],t.new(s),n,o);a.forEach(e=>{const n=t.getValue(e);A(n)&&(i[e]={value:n})});const u=[e[0],e[1],i];return u}(i,t,n,r,o),u=a[0].filter(e=>e[0]!==ct&&void 0===e[1][1]).length,c={min:u>0?u:void 0,max:a[0].some(e=>e[0]===ct)?void 0:a[0].length};return{[l]:!0,sourceCodeInfo:e[2],functionType:"UserDefined",name:void 0,evaluatedfunction:a,arity:c,docString:s}},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>function(e,t,n,r,o){const i=new Set,s={self:{value:null}};e[0].forEach(e=>{Object.assign(s,yt(e)),dt(e,e=>{te(i,n([e],t,r,o))})});const a=t.create(s),u=n(e[1],a,r,o);return te(i,u),i}(e[1][1],t,n,r,o)};const wt={arity:{min:2,max:3},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return ae(n(r,t),e=>e?n(o,t):i?n(i,t):null)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)},xt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return ae(n(r,t),e=>e?i?n(i,t):null:n(o,t))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)},St={arity:L(0),evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1],o=r[1][0],i=r[1][1];return ae(n(i,t),e=>ae(ht(o,e,e=>n(e,t)),n=>(t.addValues(n,o[2]),e)))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i[1][0],a=i[1][1],u=n([a],t,r,o);return dt(s,e=>{te(u,n([e],t,r,o))}),t.addValues(yt(s),s[2]),u}},Et={arity:{},evaluate:(e,t,{evaluateNode:o})=>{const i=e[1][1],s=le(i,(e,n)=>ae(o(n[1][1],t.create(e)),r=>ae(ht(n[1][0],r,e=>o(e,t)),t=>(Object.entries(t).forEach(([t,n])=>{e[t]={value:n}}),e))),{});return ae(s,s=>{const a=t.create(s),u=e[1][2];function c(n){if(n.length!==i.length)throw new r(`recur expected ${i.length} parameters, got ${p(n.length)}`,e[2]);return ae(de(i,(e,r)=>ae(ht(e[1][0],I(n[r]),e=>o(e,t)),e=>{for(const[t,n]of Object.entries(e))s[t].value=n})),()=>l())}function l(){return pe(()=>o(u,a),e=>{if(e instanceof n)return c(e.params);throw e})}for(;;)try{const e=o(u,a);return e instanceof Promise?e.catch(e=>{if(e instanceof n)return c(e.params);throw e}):e}catch(a){if(a instanceof n){const n=a.params;if(n.length!==i.length)throw new r(`recur expected ${i.length} parameters, got ${p(n.length)}`,e[2]);for(let e=0;e<i.length;e+=1){const r=ht(i[e][1][0],I(n[e]),e=>o(e,t));if(r instanceof Promise)return r.then(r=>{for(const[e,t]of Object.entries(r))s[e].value=t;return ae(de(i.slice(e+1),(r,i)=>ae(ht(r[1][0],I(n[e+1+i]),e=>o(e,t)),e=>{for(const[t,n]of Object.entries(e))s[t].value=n})),()=>l())});for(const[e,t]of Object.entries(r))s[e].value=t}continue}throw a}})},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],s=i.reduce((e,t)=>{const n=yt(t[1][0]);return Object.keys(n).forEach(t=>{e[t]={value:!0}}),e},{});return ee(n(i.map(e=>e[1][1]),t,r,o),n([e[1][2]],t.create(s),r,o))}};function kt(e,t,n,r){const o=t[2],[,i,s]=t[1],a=[],u=i.map(()=>0);return function t(){const c={},l=n.create(c);return ae(function e(t){if(t>=i.length)return"continue";const[n,s,a,f]=i[t],[d,h]=n[1];return ae(r(h,l),n=>{const i=function(e,t){return T(e,t),e}(n,o),h=C(i)?i:Object.entries(i);if(0===h.length)return"abort";const p=m(u[t],o);if(p>=h.length)return 0===t?"abort":(u[t]=0,u[t-1]=m(u[t-1],o)+1,"skip");const y=I(h[p],o);return ae(ht(d,y,e=>r(e,l)),n=>(Object.entries(n).forEach(([e,t])=>{c[e]={value:t}}),ae(s.length>0?function(e,t,n,r){let o;for(const i of e)o=ae(o,()=>{const[e,o]=i[1];return ae(r(o,n),o=>ae(ht(e,o,e=>r(e,n)),e=>{Object.entries(e).forEach(([e,n])=>{t[e]={value:n}})}))});return o}(s,c,l,r):void 0,()=>a?ae(r(a,l),n=>n?f?ae(r(f,l),n=>n?e(t+1):(u[t]=Number.POSITIVE_INFINITY,"skip")):e(t+1):(u[t]=m(u[t],o)+1,"skip")):f?ae(r(f,l),n=>n?e(t+1):(u[t]=Number.POSITIVE_INFINITY,"skip")):e(t+1))))})}(0),n=>"abort"===n?e?a:null:"skip"===n?t():ae(r(s,l),n=>(e&&a.push(n),u.length>0&&(u[u.length-1]+=1),t())))}()}function Nt(e,t,n,r,o){const i=new Set,s={},[,a,u]=e[1];return a.forEach(e=>{const[a,u,c,l]=e,[f,d]=a[1];n([d],t.create(s),r,o).forEach(e=>i.add(e)),Object.assign(s,yt(f)),u&&u.forEach(e=>{const[a,u]=e[1];n([u],t.create(s),r,o).forEach(e=>i.add(e)),Object.assign(s,yt(a))}),c&&n([c],t.create(s),r,o).forEach(e=>i.add(e)),l&&n([l],t.create(s),r,o).forEach(e=>i.add(e))}),n([u],t.create(s),r,o).forEach(e=>i.add(e)),i}const Pt={arity:L(1),evaluate:(e,t,n)=>kt(!0,e,t,n.evaluateNode),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>Nt(e,t,n,r,o)},$t={arity:L(1),evaluate:(e,t,n)=>ae(kt(!1,e,t,n.evaluateNode),()=>null),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>Nt(e,t,n,r,o)},At={arity:{},evaluate:(e,t,{evaluateNode:n})=>le(e[1][1],(e,r)=>e||n(r,t),!1),evaluateAsNormalExpression:(e,t)=>{let n=!1;for(const r of e)if(n=I(r,t),n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},It={arity:{min:1},evaluate:(e,t,{evaluateNode:n})=>{const r=Symbol("qq-sentinel");return ae(le(e[1][1],(e,o)=>e!==r?e:tt(o)&&null===t.lookUp(o)?r:ae(n(o,t),e=>null!==e?e:r),r),e=>e===r?null:e)},evaluateAsNormalExpression:(e,t)=>{for(const n of e){const e=I(n,t);if(null!==e)return e}return null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},jt={arity:{},evaluate:(e,t,{evaluateNode:r})=>ae(ue(e[1][1],e=>r(e,t)),e=>{throw new n(e)}),evaluateAsNormalExpression:e=>{throw new n(e)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Ct={arity:L(1),evaluate:(e,t,{evaluateNode:n})=>ae(n(e[1][1],t),t=>{const n=q(t,e[2],{nonEmpty:!0});throw new o(n,e[2])}),evaluateAsNormalExpression:(e,t)=>{const n=q(e[0],t,{nonEmpty:!0});throw new o(n,void 0)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)},Ot={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o,i]=e[1];return pe(()=>n(r,t),e=>{const r=o?{[o[1]]:{value:e}}:{};return n(i,t.create(r))})},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const[,i,s,a]=e[1],u=n([i],t,r,o),c=s?{[s[1]]:{value:!0}}:{};return ee(u,n([a],t.create(c),r,o))}},Ut={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const o=[];return ae(de(e[1][1],e=>st(e)?ae(n(e[1],t),t=>{if(!Array.isArray(t))throw new r("Spread value is not an array",e[2]);o.push(...t)}):ae(n(e,t),e=>{o.push(e)})),()=>o)},evaluateAsNormalExpression:(e,t)=>{const n=[];for(const r of e)n.push(I(r,t));return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Ft={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const o={},i=e[1][1];return function e(s){if(s>=i.length)return o;const a=i[s];if(st(a))return ae(n(a[1],t),t=>{if(!b(t))throw new r("Spread value is not an object",a[2]);return Object.assign(o,t),e(s+1)});{const u=i[s+1];if(void 0===u)throw new r("Missing value for key",a[2]);return ae(n(a,t),r=>ae(n(u,t),t=>(W(r,a[2]),o[r]=t,e(s+2))))}}(0)},evaluateAsNormalExpression:(e,t)=>{const n={};for(let r=0;r<e.length;r+=2){const o=e[r],i=e[r+1];W(o,t),n[o]=i??null}return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Mt={"??":0,"&&":1,"||":2,array:3,cond:4,"0_def":5,"defined?":6,block:7,doseq:8,"0_lambda":9,for:10,if:11,let:12,loop:13,object:14,recur:15,switch:16,throw:17,try:18,unless:19},Tt=[It,Ze,At,Ut,He,mt,at,vt,$t,bt,Pt,wt,St,Et,Ft,jt,Qe,Ct,Ot,xt],_t={normalExpressions:Ge,specialExpressions:Tt,allNormalExpressions:Xe},Bt=Object.keys(Ge),Rt=Object.keys(Mt),Dt=(e,t,n,r)=>{const o=Array.isArray(e)?e:[[s.SpecialExpression,[Mt.block,e.body]]],i=new Set;for(const e of o)zt(e,t,n,r)?.forEach(e=>i.add(e));return i};function zt(e,t,n,o){const i=e[0];switch(i){case s.UserDefinedSymbol:{const n=e;return null===t.lookUp(n)?new Set([n[1]]):null}case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.String:case s.Number:case s.ReservedSymbol:case s.Binding:return null;case s.NormalExpression:{const r=e,i=new Set;if(it(r)){const[,[e]]=r;if(tt(e)){null===t.lookUp(e)&&i.add(e[1])}}else{const[,[e]]=r;zt(e,t,n,o)?.forEach(e=>i.add(e))}for(const e of r[1][1])zt(e,t,n,o)?.forEach(e=>i.add(e));return i}case s.SpecialExpression:{const r=e,i=r[1][0];return(0,n.specialExpressions[i].getUndefinedSymbols)(r,t,{getUndefinedSymbols:Dt,builtin:n,evaluateNode:o})}case s.Spread:return zt(e[1],t,n,o);default:throw new r(`Unhandled node type: ${i}`,e[2])}}const Lt=(1+Math.sqrt(5))/2,Vt={E:Math.E,"-E":-Math.E,"ε":Math.E,"-ε":-Math.E,PI:Math.PI,"-PI":-Math.PI,"π":Math.PI,"-π":-Math.PI,PHI:Lt,"-PHI":-Lt,"φ":Lt,"-φ":-Lt,POSITIVE_INFINITY:Number.POSITIVE_INFINITY,"∞":Number.POSITIVE_INFINITY,NEGATIVE_INFINITY:Number.NEGATIVE_INFINITY,"-∞":Number.NEGATIVE_INFINITY,MAX_SAFE_INTEGER:Number.MAX_SAFE_INTEGER,MIN_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,MAX_VALUE:Number.MAX_VALUE,MIN_VALUE:Number.MIN_VALUE,NaN:Number.NaN},Jt={true:!0,false:!1,null:null,do:null,else:null,case:null,each:null,in:null,when:null,while:null,catch:null,function:null,export:null,as:null,then:null,end:null,_:null,...Vt};const Wt={NativeJsFunction:(e,t,n)=>{try{const o=e.nativeFn.fn(...t);return o instanceof Promise?o.then(e=>Q(e),e=>{const t="string"==typeof e?e:b(e)&&"string"==typeof e.message?e.message:"<no message>";throw new r(`Native function threw: "${t}"`,n)}):Q(o)}catch(e){const t="string"==typeof e?e:b(e)&&"string"==typeof e.message?e.message:"<no message>";throw new r(`Native function threw: "${t}"`,n)}},UserDefined:(e,t,o,i,{evaluateNode:s})=>{function a(t){if(!function(e,t){const{min:n}=e;return!("number"==typeof n&&t<n)}(e.arity,t.length))throw new r(`Expected ${e.arity} arguments, got ${t.length}.`,o);const u=e.evaluatedfunction,c=u[0],l=c.filter(e=>e[0]!==ct).length,f=i.create(e.evaluatedfunction[2]),d={self:{value:e}},h=[];let p,y;for(let e=0;e<t.length;e+=1)if(e<l){const n=e;p=ae(p,()=>{const e=Q(t[n]);return ae(ht(c[n],e,e=>s(e,f.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})})})}else h.push(Q(t[e]));for(let e=t.length;e<l;e++){const t=e;y=ae(y,()=>{const e=c[t];return ae(s(e[1][1],i.create(d)),t=>ae(ht(e,t,e=>s(e,i.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})}))})}return ae(p,()=>ae(y,()=>{const e=c.find(e=>e[0]===ct),t=void 0!==e?ae(ht(e,h,e=>s(e,i.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})}):void 0;return ae(t,()=>{const e=f.create(d),t=le(u[1],(t,n)=>s(n,e),null);return t instanceof Promise?t.catch(e=>{if(e instanceof n)return a(e.params);throw e}):t})}))}for(;;)try{return a(t)}catch(e){if(e instanceof n){t=e.params;continue}throw e}},Partial:(e,t,n,o,{executeFunction:i})=>{const s=[...e.params];if(t.length!==e.placeholders.length)throw new r(`(partial) expects ${e.placeholders.length} arguments, got ${t.length}.`,n);const a=[...t];for(const t of e.placeholders)s.splice(t,0,a.shift());return i(e.function,s,o,n)},Comp:(e,t,n,o,{executeFunction:i})=>{const{params:s}=e;if(0===s.length){if(1!==t.length)throw new r(`(comp) expects one argument, got ${p(t.length)}.`,n);return I(t[0],n)}let a=t;for(let e=s.length-1;e>=0;e--){const t=s[e];a=ae(a,e=>ae(i(R(t,n),e,o,n),e=>[e]))}return ae(a,e=>I(e[0],n))},Constantly:e=>e.value,Juxt:(e,t,n,r,{executeFunction:o})=>ue(e.params,e=>o(R(e,n),t,r,n)),Complement:(e,t,n,r,{executeFunction:o})=>ae(o(e.function,t,r,n),e=>!e),EveryPred:(e,t,n,r,{executeFunction:o})=>{const i=[];for(const s of e.params)for(const e of t)i.push(()=>o(R(s,n),[e],r,n));return le(i,(e,t)=>!!e&&ae(t(),e=>!!e),!0)},SomePred:(e,t,n,r,{executeFunction:o})=>{const i=[];for(const s of e.params)for(const e of t)i.push(()=>o(R(s,n),[e],r,n));return le(i,(e,t)=>!!e||ae(t(),e=>!!e),!1)},Fnull:(e,t,n,r,{executeFunction:o})=>{const i=t.map((t,n)=>null===t?Q(e.params[n]):t);return o(R(e.function,n),i,r,n)},Builtin:(e,t,n,r,{executeFunction:o})=>m(Xe[e.normalBuiltinSymbolType],n).evaluate(t,n,r,{executeFunction:o}),SpecialBuiltin:(e,t,n,o,{executeFunction:i})=>{const s=m(Tt[e.specialBuiltinSymbolType],n);if(s.evaluateAsNormalExpression)return s.evaluateAsNormalExpression(t,n,o,{executeFunction:i});throw new r(`Special builtin function ${e.specialBuiltinSymbolType} is not supported as normal expression.`,n)},Module:(e,t,n,o,{executeFunction:i})=>{const s=o.getModule(e.moduleName);if(!s)throw new r(`Module '${e.moduleName}' not found.`,n);const a=s.functions[e.functionName];if(!a)throw new r(`Function '${e.functionName}' not found in module '${e.moduleName}'.`,n);return z(a.arity,t.length,n),a.evaluate(t,n,o,{executeFunction:i})}};function qt(e,t){return le(e.body,(e,n)=>Yt(n,t),null)}function Yt(e,t){switch(e[0]){case s.Number:case s.String:return function(e){return e[1]}(e);case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.UserDefinedSymbol:return t.evaluateSymbol(e);case s.ReservedSymbol:return function(e){const t=e[1];if(!["true","false","null"].includes(t))throw new r(`Reserved symbol ${t} cannot be evaluated`,e[2]);return m(Jt[t],e[2])}(e);case s.NormalExpression:{const n=function(e,t){const n=e[2];return ae(function(e,t){const n=[],o=[],i=de(e,(e,i)=>st(e)?ae(Yt(e[1],t),t=>{if(!Array.isArray(t))throw new r(`Spread operator requires an array, got ${p(e)}`,e[2]);n.push(...t)}):e[0]!==s.ReservedSymbol||"_"!==e[1]?ae(Yt(e,t),e=>{n.push(e)}):void o.push(i));return ae(i,()=>({params:n,placeholders:o}))}(e[1][1],t),({params:r,placeholders:o})=>{if(it(e)){const s=e[1][0];if(o.length>0){return ae(Yt(s,t),e=>({[l]:!0,function:R(e,n),functionType:"Partial",params:r,placeholders:o,sourceCodeInfo:n,arity:L(o.length)}))}if(rt(s)){const n=s[1];return _t.allNormalExpressions[n].evaluate(r,e[2],t,{executeFunction:Gt})}{const o=t.getValue(s[1]);if(void 0!==o)return Gt(R(o,n),r,t,n);throw new i(s[1],e[2])}}return ae(Yt(e[1][0],t),e=>{const i=R(e,n);if(o.length>0){return{[l]:!0,function:i,functionType:"Partial",params:r,placeholders:o,sourceCodeInfo:n,arity:L(o.length)}}return Gt(i,r,t,n)})})}(e,t);return ae(n,t=>{if("number"==typeof t&&Number.isNaN(t))throw new r("Number is NaN",e[2]);return Ne(t)})}case s.SpecialExpression:return ae(function(e,t){const n=e[1][0],r=m(_t.specialExpressions[n],e[2]);return(0,r.evaluate)(e,t,{evaluateNode:Yt,builtin:_t,getUndefinedSymbols:Dt})}(e,t),e=>Ne(e));default:throw new r(`${u(e[0])}-node cannot be evaluated`,e[2])}}function Gt(e,t,n,o){if(w(e))return Wt[e.functionType](e,t,o,n,{evaluateNode:Yt,executeFunction:Gt});if(Array.isArray(e))return function(e,t,n){if(1!==t.length)throw new r("Array as function requires one non negative integer parameter.",n);const o=t[0];return $(o,n,{integer:!0,nonNegative:!0}),Q(e[o])}(e,t,o);if(U(e))return function(e,t,n){if(1!==t.length)throw new r("Object as function requires one string parameter.",n);const o=t[0];return W(o,n),Q(e[o])}(e,t,o);if("string"==typeof e)return function(e,t,n){if(1!==t.length)throw new r("String as function requires one Obj parameter.",n);const o=Q(t[0]);if(U(o))return Q(o[e]);if(P(o,{integer:!0}))return Q(e[o]);throw new r(`string as function expects Obj or integer parameter, got ${p(o)}`,n)}(e,t,o);if(P(e))return function(e,t,n){if($(e,n,{integer:!0}),1!==t.length)throw new r("Number as function requires one Arr parameter.",n);const o=t[0];return O(o,n),Q(o[e])}(e,t,o);throw new r("Unexpected function type",o)}class Kt{contexts;globalContext;values;nativeJsFunctions;modules;constructor({contexts:e,values:t,nativeJsFunctions:n,modules:r}){this.globalContext=m(e[0]),this.contexts=e,this.values=t,this.nativeJsFunctions=n,this.modules=r??new Map}getModule(e){return this.modules.get(e)}create(e){const t=this.globalContext,n=new Kt({contexts:[e,...this.contexts],values:this.values,nativeJsFunctions:this.nativeJsFunctions,modules:this.modules});return n.globalContext=t,n}new(e){return new Kt({contexts:[{},e],modules:this.modules})}exportValues(e,t){for(const[n,o]of Object.entries(e)){if(this.globalContext[n])throw new r(`Cannot redefine exported value "${n}"`,t);const e=Xt(n);if(e)throw new r(`Cannot shadow ${e}`,t);this.globalContext[n]={value:o}}this.contexts[0]!==this.globalContext&&this.addValues(e,t)}addValues(e,t){const n=this.contexts[0];for(const[o,i]of Object.entries(e)){if(n[o])throw new r(`Cannot redefine value "${o}"`,t);const e=Xt(o);if(e)throw new r(`Cannot shadow ${e}`,t);n[o]={value:Q(i)}}}getValue(e){for(const t of this.contexts){const n=t[e];if(n)return n.value}const t=this.nativeJsFunctions?.[e];return t||this.values?.[e]}lookUp(e){const t=e[1];for(const e of this.contexts){const n=e[t];if(n)return n}const n=this.values?.[t];if(void 0!==n)return{value:Q(n)};const r=this.nativeJsFunctions?.[t];return r?{value:r}:null}evaluateSymbol(e){if(ot(e)){const t=e[1];switch(t){case Mt["&&"]:case Mt["||"]:case Mt.array:case Mt.object:case Mt["defined?"]:case Mt.recur:case Mt.throw:case Mt["??"]:{const n=m(_t.specialExpressions[t],e[2]);return{[l]:!0,functionType:"SpecialBuiltin",specialBuiltinSymbolType:t,sourceCodeInfo:e[2],arity:n.arity}}default:throw new r(`Unknown special builtin symbol type: ${t}`,e[2])}}if(rt(e)){const t=e[1],n=Xe[t],r=n.name;return{[l]:!0,functionType:"Builtin",normalBuiltinSymbolType:t,sourceCodeInfo:e[2],arity:n.arity,name:r}}const t=this.lookUp(e);if(b(n=t)&&void 0!==n.value)return t.value;var n;throw new i(e[1],e[2])}}function Xt(e){return Rt.includes(e)?`special expression "${e}"`:Bt.includes(e)?`builtin function "${e}"`:"self"===e?`builtin value "${e}"`:null}function Zt(e={},t){const n=e.globalContext??{},o=e.contexts?[n,...e.contexts]:[n],i=new Kt({contexts:o,values:e.values,modules:t,nativeJsFunctions:e.jsFunctions&&Object.entries(e.jsFunctions).reduce((e,[t,n])=>{const o=t.split("."),i=o.pop();if(/^[A-Z]/.test(i))throw new r(`Invalid identifier "${t}" in jsFunctions, function name must not start with an uppercase letter`,void 0);let s=e;for(const e of o){if(0===e.length)throw new r(`Invalid empty identifier "${t}" in jsFunctions`,void 0);if(!/^[A-Z]/.test(e))throw new r(`Invalid identifier "${t}" in jsFunctions, module name must start with an uppercase letter`,void 0);s[e]||(s[e]={}),s=s[e]}!function(e){if(!function(e){return"object"==typeof e&&null!==e&&"fn"in e&&"function"==typeof e.fn}(e))throw new TypeError("Expected a NativeJsFunction")}(n);const a={functionType:"NativeJsFunction",nativeFn:n,name:i,[l]:!0,arity:n.arity??{},docString:n.docString??""};return s===e&&function(e){const t=Xt(e);if(t)throw new r(`Cannot shadow ${t}`,void 0)}(i),s[i]=a,e},{})});return e.globalModuleScope?i:i.create({})}const Ht=["^","*","/","%","+","-","<<",">>",">>>","++","<","<=","≤",">",">=","≥","==","!=","≠","&","xor","|","&&","||","??","|>"],Qt=[...Ht,"?",":","->","...",".",",","=",":",";"],en=new Set(["comment","cond","def","defined?","block","doseq","if","let","loop","recur","throw","try","unless","while"]);function tn(e){return!en.has(e)}const nn=new Set(Ht);function rn(e){return nn.has(e)}const on=new Set(Qt);function sn(e){return on.has(e)}const an=["(",")","[","]","{","}","'",'"',"`",",",".",";"," ","\n","\r","\t"],un=["0","1","2","3","4","5","6","7","8","9",...an],cn=new Set(an),ln=new Set(un),fn=/\s/,dn=[0],hn=(e,t)=>{if('"'!==e[t])return dn;let n='"',r=1,o=e[t+r],i=!1;for(;o&&('"'!==o||i);)r+=1,i?(i=!1,n+=o):("\\"===o&&(i=!0),n+=o),o=e[t+r];return o?(n+='"',[r+1,["string",n]]):[r,["Error",n,void 0,`Unclosed string at position ${t}`]]};function pn(e,t,n,r){return t===n.slice(r,r+t.length)?[t.length,[e,t]]:dn}const yn=/\d/,gn=/[0-7]/,mn=/[0-9a-f]/i,vn=/[01]/,bn=/[\s)\]}(,;]/,wn=(e,t)=>{let n=e[t];if("'"===n){let r=1,o=e[t+r],i=!1;for(;"'"!==o||i;){if(void 0===o)return[r,["Error",n,void 0,`Unclosed quoted symbol at position ${t}`]];r+=1,i?(i=!1,n+=o):("\\"===o&&(i=!0),n+=o),o=e[t+r]}return n+="'",[r+1,["Symbol",n]]}if(!ln.has(n)){const r=t;let o=e[t+=1];for(;o&&!cn.has(o);)n+=o,o=e[t+=1];return n.endsWith(":")?[t-r-1,["Symbol",n.slice(0,-1)]]:[t-r,["Symbol",n]]}return dn},xn=[(e,t)=>{let n=e[t];if(!n||!fn.test(n))return dn;let r=n;for(n=e[t+=1];n&&fn.test(n);)r+=n,n=e[t+=1];return[r.length,["Whitespace",r]]},(e,t)=>{if("/"===e[t]&&"*"===e[t+1]){let n=2,r="/*";for(;("*"!==e[t+n]||"/"!==e[t+n+1])&&t+n+1<e.length;)r+=e[t+n],n+=1;return t+n+1>=e.length?[n,["Error",r,void 0,`Unclosed multi-line comment at position ${t}`]]:(r+="*/",n+=2,[n,["MultiLineComment",r]])}return dn},(e,t)=>{if("/"===e[t]&&"/"===e[t+1]){let n=2,r="//";for(;"\n"!==e[t+n]&&t+n<e.length;)r+=e[t+n],n+=1;return[n,["SingleLineComment",r]]}return dn},(e,t)=>{const n=wn(e,t);if(0===n[0]||!n[1])return dn;let r=n[1][1];r=r.startsWith("'")?r.slice(1,r.length-1):r;return void 0===Jt[r]?dn:[n[0],["ReservedSymbol",r]]},(e,t)=>pn("LParen","(",e,t),(e,t)=>pn("RParen",")",e,t),(e,t)=>pn("LBracket","[",e,t),(e,t)=>pn("RBracket","]",e,t),(e,t)=>pn("LBrace","{",e,t),(e,t)=>pn("RBrace","}",e,t),(e,t)=>{if('"'!==e[t]||'"'!==e[t+1]||'"'!==e[t+2])return dn;let n='"""',r=3,o=e[t+r],i=e.slice(t+r,t+r+3),s=!1;for(;o&&('"""'!==i||s);)r+=1,s?(s=!1,n+=o):("\\"===o&&(s=!0),n+=o),o=e[t+r],i=e.slice(t+r,t+r+3);return o?(n+='"""',[r+3,["DocString",n]]):[r,["Error",n,void 0,`Unclosed doc string at position ${t}`]]},hn,(e,t)=>{if("#"!==e[t])return dn;const[n,r]=hn(e,t+1);if(!r)return dn;if("Error"===r[0]){return[n+1,["Error",`#${r[1]}`,void 0,`Unclosed regexp at position ${t}`]]}t+=n+1;let o=n+1,i="";for(;"g"===e[t]||"i"===e[t];)if(i+=e[t],o+=1,t+=1,i.includes(e[t]))return[o,["Error",`#${r[1]}${i}`,void 0,`Duplicated regexp option "${e[t]}"`]];return[o,["RegexpShorthand",`#${r[1]}${i}`]]},(e,t)=>{if("0"!==e[t])return dn;const n=e[t+1],r="b"===n||"B"===n?"binary":"o"===n||"O"===n?"octal":"x"===n||"X"===n?"hex":null;if(null===r)return dn;let o;for(o=t+2;o<e.length;o+=1){const t=e[o];if("binary"===r&&!vn.test(t))break;if("octal"===r&&!gn.test(t))break;if("hex"===r&&!mn.test(t))break}const i=o-t;if(i<=2)return dn;const s=e[o];return s&&!bn.test(s)?dn:[i,["BasePrefixedNumber",e.substring(t,o)]]},(e,t)=>{let n;const r="-"===e[t],o="+"===e[t],i=r||o?t+1:t;let s=!1,a=!1;for(n=i;n<e.length;n+=1){const r=e[n];if("_"===r){if(!yn.test(e[n-1])||!yn.test(e[n+1]))return n===i?dn:[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]]}else if("."===r){if(n===i)return dn;if(s||a)return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];s=!0}else if("e"===r||"E"===r){if(n===i)return dn;if(a)return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];if("."===e[n-1]||"+"===e[n-1]||"-"===e[n-1])return[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]];"+"!==e[n+1]&&"-"!==e[n+1]||(n+=1),a=!0}else if(!yn.test(r))break}if((r||o)&&n===i)return dn;const u=n-t;if(0===u)return dn;const c=e[n];return c&&":"!==c&&!bn.test(c)?[n-t+1,["Error",e.substring(t,n+1),void 0,`Invalid number format at position ${n+1}`]]:[u,["Number",e.substring(t,n)]]},(e,t)=>{const n=e.slice(t,t+3);if(t+2<e.length&&sn(n))return[3,["Operator",n]];const r=e.slice(t,t+2);if(t+1<e.length&&sn(r))return[2,["Operator",r]];const o=e[t]??"";return sn(o)?[1,["Operator",o]]:dn},wn];function Sn(e,t,n){const r=e.substring(0,t+1).split(/\r\n|\r|\n/),o=r[r.length-1],i=function(e,t){return e.split(/\r\n|\r|\n/)[t]}(e,r.length-1);return{code:i,position:{line:r.length,column:o.length},filePath:n}}function En(e,t){const n=t;if(0===t){const[r,o]=((e,t)=>{if("#"===e[t]&&"!"===e[t+1]){let n=2,r="#!";for(;"\n"!==e[t+n]&&t+n<e.length;)r+=e[t+n],n+=1;return[n,["SingleLineComment",r]]}return dn})(e,t);if(t+=r,r>0)return[t-n,o]}for(const r of xn){const[o,i]=r(e,t);if(t+=o,0!==o)return[t-n,i]}return[1,["Error",e[n],void 0,"Unrecognized character"]]}function kn(e,t){return"Symbol"===e?.[0]&&(!t||e[1]===t)}function Nn(e,t){return function(e,t){kn(e,t)||Jn("Symbol",void 0,e)}(e,t),e}function Pn(e,t){return"ReservedSymbol"===e?.[0]&&(!t||e[1]===t)}function $n(e,t){Pn(e,t)||Jn("ReservedSymbol",t,e)}function An(e,t){return $n(e,t),e}function In(e,t){return"Operator"===e?.[0]&&(!t||e[1]===t)}function jn(e,t){In(e,t)||Jn("Operator",t,e)}function Cn(e){return"LParen"===e?.[0]}function On(e){Cn(e)||Jn("LParen",void 0,e)}function Un(e){return"RParen"===e?.[0]}function Fn(e){Un(e)||Jn("RParen",void 0,e)}function Mn(e){return"LBracket"===e?.[0]}function Tn(e){return function(e){Mn(e)||Jn("LBracket",void 0,e)}(e),e}function _n(e){return"RBracket"===e?.[0]}function Bn(e){_n(e)||Jn("RBracket",void 0,e)}function Rn(e){return"LBrace"===e?.[0]}function Dn(e){return function(e){Rn(e)||Jn("LBrace",void 0,e)}(e),e}function zn(e){return"RBrace"===e?.[0]}function Ln(e){return"string"===e?.[0]}function Vn(e){return"Operator"===e?.[0]&&rn(e[1])}function Jn(e,t,n){const o=n?`${n[0]} '${n[1]}'`:"end of input";throw new r(`Unexpected token: ${o}, expected ${e}${t?` '${t}'`:""}`,n?.[2])}function Wn(e,{removeWhiteSpace:t}){const n=e.tokens.filter(e=>!(function(e){return"SingleLineComment"===e?.[0]}(e)||function(e){return"MultiLineComment"===e?.[0]}(e)||function(e){return"Shebang"===e?.[0]}(e)||t&&function(e){return"Whitespace"===e?.[0]}(e)));return{...e,tokens:n}}const qn=new Set([...Bt,...Rt,...Object.keys(Jt)]);class Yn{originalProgram;originalPosition;prefixProgram="";suffixProgram="";searchString="";suggestions=[];suggestionIndex=null;constructor(e,t,n,r){this.originalProgram=e,this.originalPosition=t;const o=this.originalProgram.slice(0,this.originalPosition),i=n.tokenize(o).tokens.at(-1);i&&"Error"!==i[0]&&(this.searchString=i[1],this.prefixProgram=this.originalProgram.slice(0,this.originalPosition-this.searchString.length),this.suffixProgram=this.originalProgram.slice(this.prefixProgram.length+this.searchString.length),this.originalProgram.slice(this.prefixProgram.length+this.searchString.length),this.suggestions=this.generateSuggestions(r))}getNextSuggestion(){return this.getAutoCompleteSuggestionResult(this.getNextSuggestionSymbol())}getPreviousSuggestion(){return this.getAutoCompleteSuggestionResult(this.getPreviousSuggestionSymbol())}getAutoCompleteSuggestionResult(e){return null===e?null:{program:this.prefixProgram+e+this.suffixProgram,position:this.prefixProgram.length+e.length}}getNextSuggestionSymbol(){return 0===this.suggestions.length?null:(null===this.suggestionIndex?this.suggestionIndex=0:(this.suggestionIndex+=1,this.suggestionIndex>=this.suggestions.length&&(this.suggestionIndex=0)),this.suggestions[this.suggestionIndex])}getPreviousSuggestionSymbol(){return 0===this.suggestions.length?null:(null===this.suggestionIndex?this.suggestionIndex=this.suggestions.length-1:(this.suggestionIndex-=1,this.suggestionIndex<0&&(this.suggestionIndex=this.suggestions.length-1)),this.suggestions[this.suggestionIndex])}getSuggestions(){return[...this.suggestions]}getSearchString(){return this.searchString}generateSuggestions(e){const t=new Set(["0_def","0_defn","0_lambda"]),n=this.generateWithPredicate(e,e=>!t.has(e)&&e.startsWith(this.searchString));n.forEach(e=>t.add(e));const r=this.generateWithPredicate(e,e=>!t.has(e)&&e.toLowerCase().startsWith(this.searchString.toLowerCase()));r.forEach(e=>t.add(e));const o=this.generateWithPredicate(e,e=>!t.has(e)&&e.includes(this.searchString));o.forEach(e=>t.add(e));const i=this.generateWithPredicate(e,e=>!t.has(e)&&e.includes(this.searchString.toLowerCase()));return i.forEach(e=>t.add(e)),[...n,...r,...o,...i]}generateWithPredicate(e,t){const n=new Set;return qn.forEach(e=>{t(e)&&n.add(e)}),Object.keys(e.globalContext??{}).filter(t).forEach(e=>n.add(e)),e.contexts?.forEach(e=>{Object.keys(e).filter(t).forEach(e=>n.add(e))}),Object.keys(e.jsFunctions??{}).filter(t).forEach(e=>n.add(e)),Object.keys(e.values??{}).filter(t).forEach(e=>n.add(e)),[...n].sort((e,t)=>e.localeCompare(t))}}class Gn{tokens;position;storedPosition=0;parseExpression;constructor(e){this.tokens=e.tokens,this.position=0}advance(){this.position+=1}tryPeek(){return this.tokens[this.position]}peek(){const e=this.tokens[this.position];if(!e){const e=this.tokens.at(-1),t=e?e[2]:void 0;throw new r("Unexpected end of input",t)}return e}isAtEnd(){return this.position>=this.tokens.length}peekSourceCodeInfo(){const e=this.tryPeek();return e?e[2]:this.tokens.at(-1)?.[2]}storePosition(){return this.storedPosition=this.position}restorePosition(){this.position=this.storedPosition}peekAhead(e){return this.tokens[this.position+e]}getPosition(){return this.position}getTokenAt(e){return this.tokens[e]}}function Kn(e,t){return t&&(e[2]=t),e}function Xn(e,t){return void 0!==Mt[e]&&"fn"!==e&&"def"!==e&&"defn"!==e?Kn([s.SpecialBuiltinSymbol,Mt[e]],t):void 0!==Ke[e]?Kn([s.NormalBuiltinSymbol,Ke[e]],t):Kn([s.UserDefinedSymbol,e],t)}function Zn(e){return e.substring(1,e.length-1).replace(/(\\{2})|(\\')|\\(.)/g,(e,t,n,r)=>t?"\\":n?"'":`\\${r}`)}const Hn=Object.entries(Ke).reduce((e,[t,n])=>(e[n]=t,e),[]),Qn=Object.entries(Mt).reduce((e,[t,n])=>(e[n]=t,e),[]);function er(e){return tt(e)?e[1]:rt(e)?Hn[e[1]]:Qn[e[1]]}function tr(e,t,n){const r=Kn([s.NormalExpression,[e,t]],n);return rt(e)&&z(Xe[e[1]].arity,r[1][1].length,n),r}function nr(e){if(e.isAtEnd())return!0;const t=e.tryPeek();return In(t)?[";",",",":"].includes(t[1]):!!Pn(t)&&["else","when","while","case","catch","let","then","end","do"].includes(t[1])}function rr(e,t,n,o,i){const a=e[1];switch(a){case"^":case"*":case"/":case"%":case"+":case"-":case"<<":case">>":case">>>":case"++":case"<":case"<=":case"≤":case">":case">=":case"≥":case"==":case"!=":case"≠":case"&":case"xor":case"|":case"|>":return tr(t,[n,o],i);case"&&":case"||":case"??":return Kn([s.SpecialExpression,[Mt[a],[n,o]]],i);default:throw new r(`Unknown binary operator: ${a}`,i)}}function or(e){const t=e.peek();if(e.advance(),!kn(t))throw new r(`Expected symbol token, got ${t[0]}`,t[2]);return"'"===t[1][0]?Xn(Zn(t[1]),t[2]):Xn(t[1],t[2])}function ir(e,{requireDefaultValue:t,noRest:n}={}){const o=e.tryPeek();if(kn(o)){const n=or(e);if(!tt(n))throw new r("Expected user defined symbol",o[2]);const i=sr(e);if(t&&!i)throw new r("Expected assignment",e.peekSourceCodeInfo());return Kn([ut,[n,i]],o[2])}if(In(o,"...")){if(n)throw new r("Rest element not allowed",o[2]);e.advance();const t=nt(or(e));if(In(e.tryPeek(),"="))throw new r("Rest argument can not have default value",e.peekSourceCodeInfo());return Kn([ct,[t[1],void 0]],o[2])}if(Mn(o)){e.advance();const n=[];let i=e.peek(),s=!1;for(;!_n(i);){if(s)throw new r("Rest argument must be last",i[2]);if(In(i,",")){n.push(null),e.advance(),i=e.peek();continue}const t=ir(e);t[0]===ct&&(s=!0),n.push(t),i=e.peek(),_n(i)||(jn(i,","),e.advance()),i=e.peek()}e.advance();const a=sr(e);if(t&&!a)throw new r("Expected assignment",e.peekSourceCodeInfo());return Kn([ft,[n,a]],o[2])}if(Rn(o)){e.advance();const n={};let i=e.peek(),s=!1;for(;!zn(i);){if(s)throw new r("Rest argument must be last",i[2]);In(i,"...")&&(s=!0,e.advance());const t=or(e),a=er(t);if(i=e.peek(),Pn(i,"as")){if(s)throw new r("Rest argument can not have alias",i[2]);e.advance();const t=nt(or(e));if(n[t[1]])throw new r(`Duplicate binding name: ${t}`,i[2]);n[a]=Kn([ut,[t,sr(e)]],o[2])}else if(zn(i)||In(i,",")||In(i,"=")){const a=nt(t,t[2]);if(n[a[1]])throw new r(`Duplicate binding name: ${a}`,i[2]);if(s&&In(e.tryPeek(),"="))throw new r("Rest argument can not have default value",e.peekSourceCodeInfo());n[a[1]]=Kn(s?[ct,[a[1],sr(e)]]:[ut,[a,sr(e)]],o[2])}else if(In(i,":")){if(e.advance(),i=e.peek(),!Rn(i)&&!Mn(i))throw new r("Expected object or array",i[2]);n[a]=ir(e)}zn(e.peek())||(jn(e.peek(),","),e.advance()),i=e.peek()}e.advance(),i=e.peek();const a=sr(e);if(t&&!a)throw new r("Expected assignment",i[2]);return Kn([lt,[n,a]],o[2])}throw new r("Expected symbol",e.peekSourceCodeInfo())}function sr(e){if(In(e.tryPeek(),"="))return e.advance(),e.parseExpression()}function ar(e,t){e.advance();const n=ir(e,{requireDefaultValue:!0,noRest:!0}),r=n[1][1];n[1][1]=void 0;const o=Kn([s.Binding,[n,r]],t[2]);return Kn([s.SpecialExpression,[Mt.let,o]],t[2])}function ur(e,t){const n=[];for(;!e.isAtEnd()&&!cr(e,t);)In(e.tryPeek(),";")?e.advance():n.push(e.parseExpression());if(function(e,t){if(!cr(e,t))throw new r(`Expected ${t.map(e=>e[1]).join(" or ")}`,e.peekSourceCodeInfo())}(e,t),0===n.length)throw new r("Expected expression",e.peekSourceCodeInfo());return 1===n.length?n[0]:Kn([s.SpecialExpression,[Mt.block,n]],e.peekSourceCodeInfo())}function cr(e,t){for(const n of t)if(Pn(e.tryPeek(),n))return!0;return!1}function lr(e){const t=function(e){const t=Nn(e.tryPeek()),n=nt(or(e));$n(e.tryPeek(),"in"),e.advance();const r=e.parseExpression();return Kn([s.Binding,[Kn([ut,[n,void 0]],t[2]),r]],t[2])}(e),n=[];let o=e.peek();fr(o,["let","when","while"]);const i=[];if("let"===o[1])for(n.push("&let");kn(o,"let");){const t=ar(e,o),n=i.flatMap(e=>Object.keys(yt(e[1][0])));if(Object.keys(yt(t[1][1][1][0])).some(e=>n.includes(e)))throw new r("Duplicate binding",t[1][1][2]);i.push(t[1][1]),o=e.peek(),fr(o,["let","when","while"]),o=e.peek()}let a,u;for(;Pn(o,"when")||Pn(o,"while");){e.advance(),"when"===o[1]?(n.push("&when"),a=e.parseExpression()):(n.push("&while"),u=e.parseExpression()),o=e.peek();fr(o,n.includes("&when")&&n.includes("&while")?[]:n.includes("&when")?["while"]:["when"]),o=e.peek()}return fr(o,[]),[t,i,a,u]}function fr(e,t){if(!function(e,t){if(In(e,",")||Un(e))return!0;for(const n of t){if("let"===n&&kn(e,"let"))return!0;if(["when","while"].includes(n)&&Pn(e,n))return!0}return!1}(e,t)){const n=`${[...t,","].map(e=>`"${e}"`).join(", ")} or ")"`;throw new r(`Expected symbol ${n}`,e[2])}}function dr(e,t){e.advance();const n=t[1].substring(1,t[1].length-1).replace(/(\\{2})|(\\")|(\\n)|(\\t)|(\\r)|(\\b)|(\\f)|\\(.)/g,(e,t,n,r,o,i,s,a,u)=>t?"\\":r?"\n":o?"\t":i?"\r":s?"\b":a?"\f":n?'"':u);return Kn([s.String,n],t[2])}function hr(e,t=!1){const n=An(e.tryPeek(),"do");e.advance();let o="";t&&function(e){return"DocString"===e?.[0]}(e.tryPeek())&&(o=function(e){const t=e.peek(),n=t[2]?["string",t[1].slice(2,-2),t[2]]:["string",t[1].slice(2,-2)],r=dr(e,n);return re(r[1])}(e));const i=[];for(;!e.isAtEnd()&&!Pn(e.tryPeek(),"end");)if(i.push(e.parseExpression()),In(e.tryPeek(),";"))e.advance();else if(!Pn(e.tryPeek(),"end"))throw new r("Expected end",e.peekSourceCodeInfo());return $n(e.tryPeek(),"end"),e.advance(),[Kn([s.SpecialExpression,[Mt.block,i]],n[2]),o]}function pr(e){const t=An(e.tryPeek());e.advance();const n=t[1];return function(e){return e in Vt}(n)?Kn([s.Number,Vt[n]],t[2]):Kn([s.ReservedSymbol,t[1]],t[2])}const yr=/^\$([1-9]\d?)?$/;function gr(e){const t=e.peek();if(Cn(t)&&kn(e.peekAhead(1))&&In(e.peekAhead(2),"->"))return null;try{const n=function(e){const t=e.peek();if(kn(t))return[Kn([ut,[or(e),void 0]],t[2])];On(t),e.advance();let n=!1,o=!1;const i=[];for(;!e.isAtEnd()&&!Un(e.peek())&&!kn(e.peek(),"let");){if(n)throw new r("Rest argument must be last",e.peekSourceCodeInfo());const t=ir(e);if(void 0!==t[1][1]&&(o=!0),t[0]===ct&&(n=!0),o&&!t[1][1])throw new r("Default arguments must be last",e.peekSourceCodeInfo());if(i.push(t),!In(e.peek(),",")&&!Un(e.peek())&&!kn(e.peek(),"let"))throw new r("Expected comma or closing parenthesis",e.peekSourceCodeInfo());In(e.peek(),",")&&e.advance()}if(!Un(e.peek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());return e.advance(),i}(e);if(!In(e.peek(),"->"))return null;let o;e.advance();let i="";if(Pn(e.peek(),"do")){const t=hr(e,!0);i=t[1],o=t[0][1][1]}else o=[e.parseExpression()];return Kn([s.SpecialExpression,[Mt["0_lambda"],[n,o],i]],t[2])}catch{return null}}function mr(e,t){e.advance();const n=[];for(;!e.isAtEnd()&&!Un(e.tryPeek());){In(e.tryPeek(),"...")?(e.advance(),n.push(Kn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()))):n.push(e.parseExpression());const t=e.tryPeek();if(!In(t,",")&&!Un(t))throw new r("Expected comma or closing parenthesis",e.tryPeek()?.[2]);In(t,",")&&e.advance()}if(!Un(e.tryPeek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());if(e.advance(),!ot(t))return rt(t)||tt(t)?tr(t,n,t[2]):Kn([s.NormalExpression,[t,n]],t[2]);{const e=t[1];switch(z(_t.specialExpressions[e].arity,n.length,t[2]),e){case Mt["||"]:case Mt["&&"]:case Mt.recur:case Mt.array:case Mt.object:case Mt["??"]:return Kn([s.SpecialExpression,[e,n]],t[2]);case Mt["defined?"]:{const[r]=n;return Kn([s.SpecialExpression,[e,r]],t[2])}case Mt.throw:{const[r]=n;return Kn([s.SpecialExpression,[e,r]],t[2])}case Mt["0_lambda"]:case Mt["0_def"]:throw new r(`${e} is not allowed`,t[2]);default:throw new r(`Unknown special expression: ${e}`,t[2])}}}function vr(e){const t=Dn(e.tryPeek());e.advance();const n=[];for(;!e.isAtEnd()&&!zn(e.tryPeek());){if(In(e.tryPeek(),"..."))e.advance(),n.push(Kn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()));else{const t=e.tryPeek();if(Ln(t)){const r=dr(e,t);n.push(Kn([s.String,r[1]],t[2]))}else if(kn(t)){const r=t[1].startsWith("'")?Zn(t[1]):t[1];n.push(Kn([s.String,r],t[2])),e.advance()}else{if(!Mn(t))throw new r("Expected key to be a symbol or a string",e.peekSourceCodeInfo());e.advance(),n.push(e.parseExpression()),Bn(e.tryPeek()),e.advance()}jn(e.tryPeek(),":"),e.advance(),n.push(e.parseExpression())}const t=e.tryPeek();if(!In(t,",")&&!zn(t))throw new r("Expected comma or closing brace",e.peekSourceCodeInfo());In(t,",")&&e.advance()}var o;return zn(o=e.tryPeek())||Jn("RBrace",void 0,o),e.advance(),Kn([s.SpecialExpression,[Mt.object,n]],t[2])}function br(e){let t=function(e){const t=e.peek();if(Cn(t)){e.storePosition();const t=gr(e);if(t)return t;e.restorePosition(),e.advance();const n=e.parseExpression();if(!Un(e.peek()))throw new r("Expected closing parenthesis",e.peekSourceCodeInfo());return e.advance(),n}if(In(t)){const n=t[1];if(rn(n))return e.advance(),Kn(void 0!==Mt[n]?[s.SpecialBuiltinSymbol,Mt[n]]:[s.NormalBuiltinSymbol,Ke[n]],t[2]);if("->"===n)return function(e){const t=e.peek();e.advance();const n=e.getPosition();let o,i="";if(Pn(e.peek(),"do")){const t=hr(e,!0);i=t[1],o=t[0][1][1]}else o=[e.parseExpression()];const a=e.getPosition()-1;let u=0,c="NOT_SET";for(let o=n;o<=a;o+=1){const n=e.getTokenAt(o);if(kn(n)){const e=yr.exec(n[1]);if(e){const n=e[1]??"1";if("1"===n){if(!e[1]&&"WITH_1"===c||e[1]&&"NAKED"===c)throw new r("Please make up your mind, either use $ or $1",t[2]);c=e[1]?"WITH_1":"NAKED"}if(u=Math.max(u,Number(n)),u>20)throw new r("Can't specify more than 20 arguments",t[2])}}}const l=[];for(let e=1;e<=u;e+=1)1===e&&"NAKED"===c?l.push(Kn([ut,[[s.UserDefinedSymbol,"$"],void 0]],t[2])):l.push(Kn([ut,[[s.UserDefinedSymbol,`$${e}`],void 0]],t[2]));return Kn([s.SpecialExpression,[Mt["0_lambda"],[l,o],i]],t[2])}(e);throw new r(`Illegal operator: ${n}`,t[2])}if(Rn(t))return vr(e);if(Mn(t))return function(e){const t=Tn(e.tryPeek());e.advance();const n=[];for(;!e.isAtEnd()&&!_n(e.tryPeek());){In(e.tryPeek(),"...")?(e.advance(),n.push(Kn([s.Spread,e.parseExpression()],e.peekSourceCodeInfo()))):n.push(e.parseExpression());const t=e.tryPeek();if(!In(t,",")&&!_n(t))throw new r("Expected comma or closing parenthesis",e.peekSourceCodeInfo());In(t,",")&&e.advance()}return Bn(e.tryPeek()),e.advance(),Kn([s.SpecialExpression,[Mt.array,n]],t[2])}(e);const n=t[0];switch(n){case"Number":case"BasePrefixedNumber":return function(e){const t=e.peek();e.advance();const n=t[1],r="-"===n[0],o=(r?n.substring(1):n).replace(/_/g,"");return Kn([s.Number,r?-Number(o):Number(o)],t[2])}(e);case"string":return dr(e,t);case"Symbol":{e.storePosition();const t=gr(e);return t||(e.restorePosition(),or(e))}case"ReservedSymbol":return pr(e);case"RegexpShorthand":return function(e){const t=e.peek();e.advance();const n=t[1].lastIndexOf('"'),r=t[1].substring(2,n),o=t[1].substring(n+1),i=Kn([s.String,r],t[2]),a=Kn([s.String,o],t[2]);return Kn([s.NormalExpression,[Kn([s.NormalBuiltinSymbol,Ke.regexp],t[2]),[i,a]]],t[2])}(e);default:throw new r(`Unknown token type: ${n}`,t[2])}}(e),n=e.tryPeek();for(;In(n,".")||Mn(n)||Cn(n);)if("."===n[1]){e.advance();const o=e.tryPeek();if(!kn(o))throw new r("Expected symbol",e.peekSourceCodeInfo());t=wr(t,Kn([s.String,o[1]],o[2]),n[2]),e.advance(),n=e.tryPeek()}else if(Mn(n)){e.advance();const o=e.parseExpression();if(!_n(e.tryPeek()))throw new r("Expected closing bracket",e.peekSourceCodeInfo());t=wr(t,o,n[2]),e.advance(),n=e.tryPeek()}else Cn(n)&&(t=mr(e,t),n=e.tryPeek());return t}function wr(e,t,n){return Kn([s.NormalExpression,[[s.NormalBuiltinSymbol,Ke.get],[e,t]]],n)}function xr(e,t){switch(e){case"^":return 12;case"*":case"/":case"%":return 11;case"+":case"-":return 10;case"<<":case">>":case">>>":return 9;case"++":return 8;case"<":case"<=":case"≤":case">":case">=":case"≥":return 7;case"==":case"!=":case"≠":return 6;case"&":case"xor":case"|":return 5;case"&&":case"||":case"??":return 4;case"|>":return 2;default:throw new r(`Unknown binary operator: ${e}`,t)}}function Sr(e,t=0,n=!1){const o=e.tryPeek();let i;if(kn(o))switch(o[1]){case"let":return ar(e,o);case"if":case"unless":i=function(e,t){const n="unless"===t[1];e.advance();const r=e.parseExpression();$n(e.tryPeek(),"then"),e.advance();const o=ur(e,["else","end"]);let i;return Pn(e.tryPeek(),"else")&&(e.advance(),i=ur(e,["end"])),e.advance(),Kn(n?[s.SpecialExpression,[Mt.unless,[r,o,i]]]:[s.SpecialExpression,[Mt.if,[r,o,i]]],t[2])}(e,o);break;case"cond":i=function(e,t){e.advance();const n=[];for(;!e.isAtEnd()&&!Pn(e.tryPeek(),"end");){$n(e.tryPeek(),"case"),e.advance();const t=e.parseExpression();$n(e.tryPeek(),"then"),e.advance();const r=ur(e,["case","end"]);if(n.push([t,r]),Pn(e.tryPeek(),"end"))break}return $n(e.tryPeek()),e.advance(),Kn([s.SpecialExpression,[Mt.cond,n]],t[2])}(e,o);break;case"switch":i=function(e,t){e.advance();const n=e.parseExpression(),r=[];for(;!e.isAtEnd()&&!Pn(e.tryPeek(),"end");){$n(e.tryPeek(),"case"),e.advance();const t=e.parseExpression();$n(e.tryPeek(),"then"),e.advance();const n=ur(e,["case","end"]);if(r.push([t,n]),Pn(e.tryPeek(),"end"))break}return $n(e.tryPeek(),"end"),e.advance(),Kn([s.SpecialExpression,[Mt.switch,n,r]],t[2])}(e,o);break;case"for":case"doseq":i=function(e,t){const n="doseq"===t[1];e.advance(),On(e.tryPeek()),e.advance();const o=[];for(;!e.isAtEnd()&&!Un(e.tryPeek());){const t=lr(e),n=o.flatMap(e=>Object.keys(yt(e[0][1][0]))),i=yt(t[0][1][0]);if(Object.keys(i).some(e=>n.includes(e)))throw new r("Duplicate binding",t[0][2]);o.push(t),In(e.tryPeek(),",")&&e.advance()}Fn(e.tryPeek()),e.advance(),jn(e.tryPeek(),"->"),e.advance();const i=e.parseExpression();return Kn(n?[s.SpecialExpression,[Mt.doseq,o,i]]:[s.SpecialExpression,[Mt.for,o,i]],t[2])}(e,o);break;case"loop":i=function(e,t){e.advance(),On(e.tryPeek()),e.advance();const n=[];let o=e.tryPeek();for(;!e.isAtEnd()&&!Un(o);){const t=ir(e,{requireDefaultValue:!0,noRest:!0}),r=t[1][1];t[1][1]=void 0,n.push(Kn([s.Binding,[t,r]],t[2])),In(e.tryPeek(),",")&&e.advance(),o=e.tryPeek()}if(0===n.length)throw new r("Expected binding",e.peekSourceCodeInfo());Fn(o),e.advance(),jn(e.tryPeek(),"->"),e.advance();const i=e.parseExpression();return Kn([s.SpecialExpression,[Mt.loop,n,i]],t[2])}(e,o);break;case"try":i=function(e,t){e.advance();const n=ur(e,["catch"]);let r;e.advance(),Cn(e.tryPeek())&&(e.advance(),r=or(e),Fn(e.tryPeek()),e.advance());const o=ur(e,["end"]);return e.advance(),Kn([s.SpecialExpression,[Mt.try,n,r,o]],t[2])}(e,o)}else if(Pn(o,"do"))i=hr(e)[0];else if(Pn(o,"export")){if(!n)throw new r("export is only allowed in module scope",o[2]);return function(e,t){e.advance();const n=e.tryPeek();if(kn(n,"let")){const r=ar(e,Nn(n));return Kn([s.SpecialExpression,[Mt["0_def"],r[1][1]]],t[2])}throw new r("Expected let",e.peekSourceCodeInfo())}(e,o)}i||=br(e);let a=e.tryPeek();for(;!nr(e);){if(Vn(a)){const n=a[1],r=xr(n,a[2]);if(r<=t&&(12!==r||12!==t))break;const o=Kn(Mt[n]?[s.SpecialBuiltinSymbol,Mt[n]]:[s.NormalBuiltinSymbol,Ke[n]],a[2]);e.advance();i=rr(a,o,i,Sr(e,r),a[2])}else if(kn(a)){if(!tn(a[1]))break;const n=3;if(n<=t)break;const o=or(e),s=Sr(e,n);if(ot(o))throw new r("Special expressions are not allowed in binary functional operators",o[2]);i=tr(o,[i,s],a[2])}else{if("?"!==a?.[1])break;{if(1<=t)break;e.advance();const n=Sr(e);if(!In(e.tryPeek(),":"))throw new r("Expected :",e.peekSourceCodeInfo());e.advance();const o=Sr(e);i=Kn([s.SpecialExpression,[Mt.if,[i,n,o]]],i[2])}}a=e.tryPeek()}return i}class Er{cache={};firstEntry=void 0;lastEntry=void 0;_size=0;maxSize;constructor(e){var t;if(this.maxSize=null===e?null:(t=e,Math.max(0,Math.ceil(t))),"number"==typeof this.maxSize&&this.maxSize<1)throw new Error(`1 is the minimum maxSize, got ${p(e)}`)}getContent(){return Object.entries(this.cache).reduce((e,[t,n])=>(e[t]=n.value,e),{})}get size(){return this._size}get(e){return this.cache[e]?.value}clear(){this.cache={},this.firstEntry=void 0,this.lastEntry=void 0,this._size=0}has(e){return!!this.cache[e]}set(e,t){if(this.has(e))throw new Error(`AstCache - key already present: ${e}`);const n={value:t,nextEntry:void 0,key:e};for(this.cache[e]=n,this._size+=1,this.lastEntry&&(this.lastEntry.nextEntry=n),this.lastEntry=n,this.firstEntry||(this.firstEntry=this.lastEntry);null!==this.maxSize&&this.size>this.maxSize;)this.dropFirstEntry()}dropFirstEntry(){const e=this.firstEntry;delete this.cache[e.key],this._size-=1,this.firstEntry=e.nextEntry}}return e.Lits=class{astCache;astCacheSize;debug;modules;constructor(e={}){if(this.debug=e.debug??!1,this.astCacheSize=e.astCacheSize??null,this.astCacheSize){this.astCache=new Er(this.astCacheSize);const t=e.initialCache??{};for(const e of Object.keys(t))this.astCache.set(e,t[e])}else this.astCache=null;const t=e.modules??[];this.modules=new Map(t.map(e=>[e.name,e]))}getRuntimeInfo(){return{astCacheSize:this.astCacheSize,astCache:this.astCache,debug:this.debug}}async={run:async(e,t={})=>{const n=this.generateAst(e,t);return this.evaluate(n,t)},context:async(e,t={})=>{const n="string"==typeof e?this.generateAst(e,t):e,r=Zt(t,this.modules);return await qt(n,r),r.globalContext},apply:async(e,t,n={})=>this.apply(e,t,n)};run(e,t={}){const n=this.generateAst(e,t),r=this.evaluate(n,t);if(r instanceof Promise)throw new TypeError("Unexpected async result in synchronous run(). Use lits.async.run() for async operations.");return r}context(e,t={}){const n="string"==typeof e?this.generateAst(e,t):e,r=Zt(t,this.modules);if(qt(n,r)instanceof Promise)throw new TypeError("Unexpected async result in synchronous context(). Use lits.async.context() for async operations.");return r.globalContext}getUndefinedSymbols(e,t={}){const n="string"==typeof e?this.generateAst(e,t):e,r=Zt(t,this.modules);return Dt(n,r,_t,Yt)}tokenize(e,t={}){const n=function(e,t,n){let r=0;const o={tokens:[],filePath:n,hasDebugData:t};for(;r<e.length;){const i=t?Sn(e,r,n):void 0,s=En(e,r),[a,u]=s;r+=a,u&&(i&&(u[2]=i),o.tokens.push(u))}return o}(e,this.debug,t.filePath);return t.minify?Wn(n,{removeWhiteSpace:!1}):n}parse(e){const t={body:[],hasDebugData:(e=Wn(e,{removeWhiteSpace:!0})).hasDebugData};return t.body=function(e){e.tokens.forEach(e=>{if("Error"===e[0])throw new r(e[3],e[2])});const t=[],n=new Gn(e);for(n.parseExpression=(e=0,t=!1)=>Sr(n,e,t);!n.isAtEnd();)if(t.push(Sr(n,0,!0)),In(n.tryPeek(),";"))n.advance();else if(!n.isAtEnd())throw new r("Expected ;",n.peekSourceCodeInfo());return t}(e),t}evaluate(e,t){return qt(e,Zt(t,this.modules))}transformSymbols(e,t){return function(e,t){return{...e,tokens:e.tokens.map(e=>kn(e)?[e[0],t(e[1])]:e)}}(e,t)}untokenize(e){return function(e){return e.tokens.reduce((e,t)=>`${e}${t[1]}`,"")}(e)}apply(e,t,n={}){const r="FN_2eb7b316_471c_5bfa_90cb_d3dfd9164a59",o=this.generateApplyFunctionCall(r,t),i=this.generateAst(o,n),s=t.reduce((e,t,n)=>(e[`${r}_${n}`]=t,e),{[r]:e});return n.values={...n.values,...s},this.evaluate(i,n)}generateApplyFunctionCall(e,t){const n=t.map((t,n)=>`${e}_${n}`).join(", ");return`${e}(${n})`}generateAst(e,t){if(this.astCache){const t=this.astCache.get(e);if(t)return t}const n=this.tokenize(e,{filePath:t.filePath}),r=this.parse(n);return this.astCache?.set(e,r),r}getAutoCompleter(e,t,n={}){return new Yn(e,t,this,n)}},e.asLitsFunction=function(e,t){return x(e,t),e},e.asNativeJsFunction=function(e,t){return N(e,t),e},e.asUserDefinedFunction=function(e,t){return E(e,t),e},e.assertLitsFunction=x,e.assertNativeJsFunction=N,e.assertUserDefinedFunction=E,e.isBuiltinFunction=function(e){return b(e)&&"Builtin"===e.functionType},e.isGrid=Ie,e.isLitsError=function(e){return e instanceof r},e.isLitsFunction=w,e.isMatrix=je,e.isNativeJsFunction=k,e.isUserDefinedFunction=S,e.isVector=Pe,e.normalExpressionKeys=Bt,e.specialExpressionKeys=Rt,e}({});
2
2
  //# sourceMappingURL=lits.iife.js.map