-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubscript.min.js
1 lines (1 loc) · 4.79 KB
/
subscript.min.js
1
let t,r,e=e=>(t=0,r=e,e=i(),r[t]?n():e||""),n=(e="Bad syntax",n=r.slice(0,t).split("\n"),l=n.pop())=>{let o=r.slice(t-108,t).split("\n").pop(),i=r.slice(t,t+108).split("\n").shift();throw EvalError(`${e} at ${n.length}:${l.length} \`${t>=108?"…":""}${o}┃${i}\``,"font-weight: bold")},l=(e,n=t,l)=>{for(;l=e(r.charCodeAt(t));)t+=l;return r.slice(n,t)},o=(e=1,n=t)=>(t+=e,r.slice(n,t)),i=(r=0,o)=>{let i,p,c,h;for(;(i=s())&&(c=((h=a[i])&&h(p,r))??(!p&&l(e.id)));)p=c;return o&&(i==o?t++:n()),p},s=e=>{for(;(e=r.charCodeAt(t))<=32;)t++;return e};e.id=t=>t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||36==t||95==t||t>=192&&215!=t&&247!=t;let a=[],p=(l,o=32,i,s=l.charCodeAt(0),p=l.length,c=a[s],h=l.toUpperCase()!==l)=>a[s]=(s,a,d,f=t)=>(d?l==d:(p<2||r.substr(t,p)==l)&&(d=l))&&a<o&&!(h&&e.id(r.charCodeAt(t+p)))&&(t+=p,i(s)||(t=f,!c&&n()))||c?.(s,a,d),c=(t,r,e=!1)=>p(t,r,((n,l)=>n&&(l=i(r-(e?.5:0)))&&[t,n,l])),h=(t,r,e)=>p(t,r,(n=>e?n&&[t,n]:!n&&(n=i(r-.5))&&[t,n])),d=(t,r,e)=>{p(t,r,((e,n)=>(n=i(r),e?.[0]!==t&&(e=[t,e||null]),n?.[0]===t?e.push(...n.slice(1)):e.push(n||null),e)))},f=(t,r)=>p(t[0],r,(r=>!r&&[t,i(0,t.charCodeAt(1))])),u=(t,r)=>p(t[0],r,(r=>r&&[t,r,i(0,t.charCodeAt(1))||null]));const g=(t,r)=>[,(t=+l((t=>46===t||t>=48&&t<=57||(69===t||101===t?2:0))))!=t?n():t];a[46]=t=>!t&&g();for(let t=48;t<=57;t++)a[t]=t=>t?n():g();const A={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},y=e=>(l,i,s="")=>{for(l&&n("Unexpected string"),o();(i=r.charCodeAt(t))-e;)92===i?(o(),i=o(),s+=A[i]||i):s+=o();return o()||n("Bad string"),[,s]};a[34]=y(34),a[39]=y(39);const C=t=>Array.isArray(t)?t[0]?v[t[0]](...t.slice(1)):()=>t[1]:C.id(t);C.id=t=>r=>r?.[t];const v={},m=(t,r,e=v[t])=>v[t]=(...t)=>r(...t)||e?.(...t),$=(t,r,e,l,o)=>"()"===t[0]&&2==t.length?$(t[1],r,e):"string"==typeof t?e=>r(e,t,e):"."===t[0]?(l=C(t[1]),o=t[2],t=>r(l(t),o,t)):"[]"===t[0]&&3===t.length?(l=C(t[1]),o=C(t[2]),t=>r(l(t),o(t),t)):e?(t=C(t),e=>r([t(e)],0,e)):()=>n("Bad left value");u("()",170),m("()",((t,r,e)=>void 0!==r&&(e=r?","===r[0]?(r=r.slice(1).map((t=>t?C(t):err())),t=>r.map((r=>r(t)))):(r=C(r),t=>[r(t)]):()=>[],$(t,((t,r,n)=>t[r](...e(n))),!0)))),u("[]",170),m("[]",((t,r)=>r?(t=C(t),r=C(r),e=>t(e)[r(e)]):err())),c(".",170),m(".",((t,r)=>(t=C(t),r=r[0]?r:r[1],e=>t(e)[r]))),f("()",170),m("()",((t,r)=>void 0===r&&(!t&&n("Empty ()"),C(t))));const b=(...t)=>(t=t.map(C),r=>t.map((t=>t(r))).pop());function B(t){if(!t)return"";if(Array.isArray(t)){const[r,...e]=t;return r?"[]"==r||"{}"==r||"()"==r?(e.length>1?B(e.shift()):"")+r[0]+B(e[0])+r[1]:1===e.length?r+B(e[0]):2===e.length?B(e[0])+("."===r?r:" "+r+" ")+B(e[1]):e.filter(Boolean).map((t=>B(t))).join(r+"\n"):JSON.stringify(e[0])}return t}d(",",10),m(",",b),d(";",5),m(";",b),c("=",20,!0),m("=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]=r(n)))))),c("*",120),m("*",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)*r(e)))),c("/",120),m("/",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)/r(e)))),c("%",120),m("%",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)%r(e)))),c("*=",20,!0),m("*=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]*=r(n)))))),c("/=",20,!0),m("/=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]/=r(n)))))),c("%=",20,!0),m("%=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]%=r(n)))))),c("+",110),m("+",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)+r(e)))),c("-",110),m("-",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)-r(e)))),h("+",140),m("+",((t,r)=>!r&&(t=C(t),r=>+t(r)))),h("-",140),m("-",((t,r)=>!r&&(t=C(t),r=>-t(r)))),c("+=",20,!0),m("+=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]+=r(n)))))),c("-=",20,!0),m("-=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]-=r(n)))))),p("++",150,(t=>t?["++-",t]:["++",i(149)])),m("++",(t=>$(t,((t,r)=>++t[r])))),m("++-",(t=>$(t,((t,r)=>t[r]++)))),p("--",150,(t=>t?["--+",t]:["--",i(149)])),m("--",(t=>$(t,((t,r)=>--t[r])))),m("--+",(t=>$(t,((t,r)=>t[r]--)))),h("~",140),m("~",((t,r)=>!r&&(t=C(t),r=>~t(r)))),c("|",50),m("|",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)|r(e)))),c("&",70),m("&",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)&r(e)))),c("^",60),m("^",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)^r(e)))),c("==",80),m("==",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)==r(e)))),c("!=",80),m("!=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)!=r(e)))),c(">",90),m(">",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>r(e)))),c("<",90),m("<",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<r(e)))),c(">=",90),m(">=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>=r(e)))),c("<=",90),m("<=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<=r(e)))),c(">>",100),m(">>",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>>r(e)))),c("<<",100),m("<<",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<<r(e)))),c(">>=",20,!0),m(">>=",((t,r)=>(r=C(r),prop(t,((t,e,n)=>t[e]>>=r(n)))))),c("<<=",20,!0),m("<<=",((t,r)=>(r=C(r),prop(t,((t,e,n)=>t[e]<<=r(n)))))),h("!",140),m("!",((t,r)=>!r&&(t=C(t),r=>!t(r)))),c("||",30),m("||",((t,r)=>(t=C(t),r=C(r),e=>t(e)||r(e)))),c("&&",40),m("&&",((t,r)=>(t=C(t),r=C(r),e=>t(e)&&r(e))));var x=t=>C(e(t));export{u as access,c as binary,C as compile,x as default,f as group,d as nary,m as operator,e as parse,B as stringify,p as token,h as unary};