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