import{a}from"./chunk-YNYSPYQ5.js";import{a as d}from"./chunk-CCNMISUL.js";var S={name:"expr",async execute(s,r){if(s.length===0)return{stdout:"",stderr:`expr: missing operand
`,exitCode:2};try{let c=x(s),h=c==="0"||c===""?1:0;return{stdout:`${c}
`,stderr:"",exitCode:h}}catch(c){return{stdout:"",stderr:`expr: ${d(c.message)}
`,exitCode:2}}}};function x(s){if(s.length===1)return s[0];let r=0;function c(){let e=h();for(;r<s.length&&s[r]==="|";){r++;let t=h();if(e!=="0"&&e!=="")return e;e=t}return e}function h(){let e=m();for(;r<s.length&&s[r]==="&";){r++;let t=m();(e==="0"||e===""||t==="0"||t==="")&&(e="0")}return e}function m(){let e=g();for(;r<s.length;){let t=s[r];if(["=","!=","<",">","<=",">="].includes(t)){r++;let o=g(),n=parseInt(e,10),i=parseInt(o,10),f=!Number.isNaN(n)&&!Number.isNaN(i),l;t==="="?l=f?n===i:e===o:t==="!="?l=f?n!==i:e!==o:t==="<"?l=f?n<i:e<o:t===">"?l=f?n>i:e>o:t==="<="?l=f?n<=i:e<=o:l=f?n>=i:e>=o,e=l?"1":"0"}else break}return e}function g(){let e=p();for(;r<s.length;){let t=s[r];if(t==="+"||t==="-"){r++;let o=p(),n=parseInt(e,10),i=parseInt(o,10);if(Number.isNaN(n)||Number.isNaN(i))throw new Error("non-integer argument");e=String(t==="+"?n+i:n-i)}else break}return e}function p(){let e=N();for(;r<s.length;){let t=s[r];if(t==="*"||t==="/"||t==="%"){r++;let o=N(),n=parseInt(e,10),i=parseInt(o,10);if(Number.isNaN(n)||Number.isNaN(i))throw new Error("non-integer argument");if((t==="/"||t==="%")&&i===0)throw new Error("division by zero");t==="*"?e=String(n*i):t==="/"?e=String(Math.trunc(n/i)):e=String(n%i)}else break}return e}function N(){let e=u();for(;r<s.length&&s[r]===":";){r++;let t=u(),n=a(`^${t}`).match(e);n?e=n[1]!==void 0?n[1]:String(n[0].length):e="0"}return e}function u(){if(r>=s.length)throw new Error("syntax error");let e=s[r];if(e==="match"){r++;let t=u(),o=u(),i=a(o).match(t);return i?i[1]!==void 0?i[1]:String(i[0].length):"0"}if(e==="substr"){r++;let t=u(),o=parseInt(u(),10),n=parseInt(u(),10);if(Number.isNaN(o)||Number.isNaN(n))throw new Error("non-integer argument");return t.substring(o-1,o-1+n)}if(e==="index"){r++;let t=u(),o=u();for(let n=0;n<t.length;n++)if(o.includes(t[n]))return String(n+1);return"0"}if(e==="length"){r++;let t=u();return String(t.length)}if(e==="("){r++;let t=c();if(r>=s.length||s[r]!==")")throw new Error("syntax error");return r++,t}return r++,e}return c()}var E={name:"expr",flags:[],needsArgs:!0};export{S as a,E as b};
