subscript 5.5.1 → 5.5.2
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.
- package/README.md +1 -1
- package/evaluate.js +11 -5
- package/justin.js +1 -1
- package/justin.min.js +1 -1
- package/package.json +1 -1
- package/subscript.js +3 -3
- package/subscript.min.js +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ Default operators include common operators for the listed languages in the follo
|
|
|
69
69
|
* `&&`
|
|
70
70
|
* `||`
|
|
71
71
|
|
|
72
|
-
Operators can be extended via `parse.operator(str, prec, type)` and `evaluate.operator(str, fn)` functions for any unary/binary/postfix operators, calls,
|
|
72
|
+
Operators can be extended via `parse.operator(str, prec, type)` and `evaluate.operator(str, fn)` functions for any unary/binary/postfix operators, calls, prop chains, groups etc.
|
|
73
73
|
|
|
74
74
|
```js
|
|
75
75
|
import { parse, evaluate } from 'subscript.js'
|
package/evaluate.js
CHANGED
|
@@ -5,9 +5,14 @@ export const evaluate = (node, ctx={}) => {
|
|
|
5
5
|
|
|
6
6
|
if (Array.isArray(node)) {
|
|
7
7
|
// [[a,b], c] or ['+', a, b] or ['myfn', a, b], or
|
|
8
|
-
let c = node[0],
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
let c = node[0], l = node.length,
|
|
9
|
+
fn = Array.isArray(c) ? evaluate(c, ctx) : (lookup[c] || ctx[c] || c),
|
|
10
|
+
i=1, res=[]
|
|
11
|
+
|
|
12
|
+
if (fn.ab) { res=evaluate(node[i++],ctx); do { res = fn(res, evaluate(node[i++],ctx)) } while ( i < l ) }
|
|
13
|
+
else { while ( i < l ) res.push(evaluate(node[i++], ctx)); res = fn.apply(ctx,res) }
|
|
14
|
+
|
|
15
|
+
return res
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
return node
|
|
@@ -15,7 +20,8 @@ export const evaluate = (node, ctx={}) => {
|
|
|
15
20
|
lookup = {},
|
|
16
21
|
|
|
17
22
|
// op evaluators
|
|
18
|
-
// multiple args
|
|
19
|
-
|
|
23
|
+
// multiple args (fn.r=false) are caused by:
|
|
24
|
+
// ||&& shortcuts, lisp compatiblity, manual eval, functions multiple arguments, right precedence
|
|
25
|
+
operator = evaluate.operator = (op, fn) => (lookup[op] = fn).ab = fn.length==2
|
|
20
26
|
|
|
21
27
|
export default evaluate
|
package/justin.js
CHANGED
package/justin.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const r=(t,n={})=>{if("string"==typeof t)return"@"===t[0]?t.slice(1):n[t];if(Array.isArray(t)){let o=t[0],a=Array.isArray(o)?r(o,n):e[o]||n[o]||o,s=[],l
|
|
1
|
+
const r=(t,n={})=>{if("string"==typeof t)return"@"===t[0]?t.slice(1):n[t];if(Array.isArray(t)){let o=t[0],a=t.length,s=Array.isArray(o)?r(o,n):e[o]||n[o]||o,l=1,i=[];if(s.ab){i=r(t[l++],n);do{i=s(i,r(t[l++],n))}while(l<a)}else{for(;l<a;)i.push(r(t[l++],n));i=s.apply(o,i)}return i}return t},e={};let t,n;r.operator=(r,t)=>(e[r]=t,t.ab=2==t.length);const o=(r,e)=>(n=r,t=0,e=f(),t<n.length?a():y(e)),a=(r="Bad syntax")=>{throw Error(r+" `"+n[t]+"` at "+t)},s=(r=1,e=t)=>{if("number"==typeof r)t+=r;else for(;r(l());)t++;return n.slice(e,t)},l=(r=0)=>n.charCodeAt(t+r),i=(r=1)=>n.substr(t,r),f=(r=0,e,n,s,l=0,i,f)=>{for(;(n=o.space())&&(i=(f=c[n])&&f(s,r)||!s&&p(n));)s=i;return e&&(n!=e?a("Unclosed paren"):t++),s};o.space=r=>{for(;(r=l())<=32;)t++;return r};const u=o.token=[],p=(r,e=0,t)=>{for(;e<u.length;)if(t=u[e++](r))return t},c=[];o.operator=(r,e=0,n=0,a,l=r.charCodeAt(0),u=r.length,p=c[l])=>(a=n?n>0?r=>r&&[s(u),y(r)]:n<0?r=>!r&&[s(u),y(f(e-1))]:n:n=>{n=[r,y(n)];do{t+=u,n.push(y(f(e)))}while(o.space()==l&&(u<2||i(u)==r));return n},c[l]=(t,n)=>n<e&&(u<2||i(u)==r)&&a(t)||p&&p(t,n));const y=r=>Array.isArray(r)?r:(r||a()).valueOf();o.token.push((r=>(r=s((r=>r>47&&r<58||46==r)))&&((69==l()||101==l())&&(r+=s(2)+s((r=>r>=48&&r<=57))),isNaN(r=new Number(r))?a("Bad number"):r)),((r,e,t,n)=>{if(34===r||39===r){for(i(),s(),n="";(t=l())-r;)92===t?(s(),n+=h[i()]||i()):n+=i(),s();return s(),"@"+n}}),(r=>s((r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192&&215!=r&&247!=r))));const h={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"};o.space=r=>{for(;(r=l())<=32||47===r;)if(r<=32)s();else if(47===r)if(42===l(1))s(2),s((r=>42!==r&&47!==l(1))),s(2);else{if(47!==l(1))break;s(2),s((r=>r>=32))}return r};const d=(r,e=2,t)=>{for(let n=0;n<t.length;n+=e)r(t[n],t[n+1],t[n+2])};d(o.operator,3,[",",1,,"|",6,,"||",4,,"&",8,,"&&",5,,"^",7,,"==",9,,"!=",9,,">",10,,">=",10,,">>",11,,">>>",11,,"<",10,,"<=",10,,"<<",11,,"+",12,,"+",15,-1,"++",15,-1,"++",16,1,"-",12,,"-",15,-1,"--",15,-1,"--",16,1,"!",15,-1,"*",13,,"/",13,,"%",13,,".",18,(r,e)=>r&&[s(),r,"@"+f(18)],"[",18,r=>(s(),[".",r,y(f(0,93))]),"]",,,"(",18,(r,e)=>(s(),e=f(0,41),Array.isArray(e)&&","===e[0]?(e[0]=r,e):e?[r,y(e)]:[r]),"(",19,(r,e)=>!r&&(s(),f(0,41)||a()),")",,,";",1,,"===",9,,"!==",9,,"**",14,,"~",15,-1,"?",3,r=>{let e;return r||a("Expected expression"),s(),o.space(),e=f(),["?:",r,e[1],e[2]]},":",10,,"in",10,r=>l(2)<=32&&[s(2),"@"+r,f(10)],"[",20,(r,e)=>!r&&(s(),(e=f(0,93))?","==e[0]?(e[0]="[",e):["[",e]:["["]),"{",20,(r,e)=>!r&&(s(),(e=f(0,125))?":"==e[0]?["{",A(e)]:","==e[0]?(e[0]="{",e.map(A)):["{",e]:["{"]),"}",,,"null",20,r=>!r&&(s(4),b(null)),"false",20,r=>!r&&(s(5),b(!1)),"true",20,r=>!r&&(s(4),b(!0)),"undefined",20,r=>!r&&(s(9),b(void 0))]);const A=r=>Array.isArray(r)?(r[1]=("@"===r[1][0]?"":"@")+r[1],r):r,b=r=>({valueOf:()=>r});d(r.operator,2,["!",r=>!r,"++",r=>++r,"--",r=>--r,".",(r,e)=>r?r[e]:r,"%",(r,e)=>r%e,"/",(r,e)=>r/e,"*",(r,e)=>r*e,"+",(r,e)=>r+(e||0),"-",(r,e)=>null==e?-r:r-e,">>>",(r,e)=>r>>>e,">>",(r,e)=>r>>e,"<<",(r,e)=>r<<e,">=",(r,e)=>r>=e,">",(r,e)=>r>e,"<=",(r,e)=>r<=e,"<",(r,e)=>r<e,"!=",(r,e)=>r!=e,"==",(r,e)=>r==e,"&",(r,e)=>r&e,"^",(r,e)=>r^e,"|",(r,e)=>r|e,"&&",(...r)=>r.every(Boolean),"||",(...r)=>r.some(Boolean),",",(r,e)=>e,"**",(...r)=>r.reduceRight(((r,e)=>Math.pow(e,r))),"~",r=>~r,"?:",(r,e,t)=>r?e:t,"in",(r,e)=>r in e,"[",(...r)=>Array(...r),"{",(...r)=>Object.fromEntries(r),":",(r,e)=>[r,e]]);var g=e=>(e="string"==typeof e?o(e):e,t=>r(e,t));export{g as default,r as evaluate,o as parse};
|
package/package.json
CHANGED
package/subscript.js
CHANGED
|
@@ -101,7 +101,7 @@ addOps(evalOp, 2, [
|
|
|
101
101
|
'/', (a,b)=>a/b,
|
|
102
102
|
'*', (a,b)=>a*b,
|
|
103
103
|
|
|
104
|
-
'+', (a,b
|
|
104
|
+
'+', (a,b)=>a+(b||0),
|
|
105
105
|
'-', (a,b)=>b==null ? -a : a-b,
|
|
106
106
|
|
|
107
107
|
'>>>', (a,b)=>a>>>b,
|
|
@@ -119,8 +119,8 @@ addOps(evalOp, 2, [
|
|
|
119
119
|
'&', (a,b)=>a&b,
|
|
120
120
|
'^', (a,b)=>a^b,
|
|
121
121
|
'|', (a,b)=>a|b,
|
|
122
|
-
'&&', (
|
|
123
|
-
'||', (
|
|
122
|
+
'&&', (a,b)=>a&&b,
|
|
123
|
+
'||', (a,b)=>a||b,
|
|
124
124
|
',', (a,b)=>(a,b)
|
|
125
125
|
])
|
|
126
126
|
|
package/subscript.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let r,e;const t=(t,
|
|
1
|
+
let r,e;const t=(t,n)=>(e=t,r=0,n=l(),r<e.length?a():h(n)),a=(t="Bad syntax")=>{throw Error(t+" `"+e[r]+"` at "+r)},n=(t=1,a=r)=>{if("number"==typeof t)r+=t;else for(;t(o());)r++;return e.slice(a,r)},o=(t=0)=>e.charCodeAt(r+t),s=(t=1)=>e.substr(r,t),l=(e=0,n,o,s,l=0,u,p)=>{for(;(o=t.space())&&(u=(p=i[o])&&p(s,e)||!s&&f(o));)s=u;return n&&(o!=n?a("Unclosed paren"):r++),s};t.space=e=>{for(;(e=o())<=32;)r++;return e};const u=t.token=[],f=(r,e=0,t)=>{for(;e<u.length;)if(t=u[e++](r))return t},i=[],p=t.operator=(e,a=0,o=0,u,f=e.charCodeAt(0),p=e.length,y=i[f])=>(u=o?o>0?r=>r&&[n(p),h(r)]:o<0?r=>!r&&[n(p),h(l(a-1))]:o:n=>{n=[e,h(n)];do{r+=p,n.push(h(l(a)))}while(t.space()==f&&(p<2||s(p)==e));return n},i[f]=(r,t)=>t<a&&(p<2||s(p)==e)&&u(r)||y&&y(r,t)),h=r=>Array.isArray(r)?r:(r||a()).valueOf(),y=(r,e={})=>{if("string"==typeof r)return"@"===r[0]?r.slice(1):e[r];if(Array.isArray(r)){let t=r[0],a=r.length,n=Array.isArray(t)?y(t,e):c[t]||e[t]||t,o=1,s=[];if(n.ab){s=y(r[o++],e);do{s=n(s,y(r[o++],e))}while(o<a)}else{for(;o<a;)s.push(y(r[o++],e));s=n.apply(e,s)}return s}return r},c={},A=y.operator=(r,e)=>(c[r]=e).ab=2==e.length;u.push((r=>(r=n((r=>r>47&&r<58||46==r)))&&((69==o()||101==o())&&(r+=n(2)+n((r=>r>=48&&r<=57))),isNaN(r=new Number(r))?a("Bad number"):r)),((r,e,t)=>34==r&&(n(),t=n((e=>e-r)),n(),"@"+t)),(r=>n((r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192))));const d=(r,e=2,t)=>{for(let a=0;a<t.length;a+=e)r(t[a],t[a+1],t[a+2])};d(p,3,[",",1,,"|",6,,"||",4,,"&",8,,"&&",5,,"^",7,,"==",9,,"!=",9,,">",10,,">=",10,,">>",11,,">>>",11,,"<",10,,"<=",10,,"<<",11,,"+",12,,"+",15,-1,"++",15,-1,"++",16,1,"-",12,,"-",15,-1,"--",15,-1,"--",16,1,"!",15,-1,"*",13,,"/",13,,"%",13,,".",18,(r,e)=>r&&[n(),r,"@"+l(18)],"[",18,r=>(n(),[".",r,h(l(0,93))]),"]",,,"(",18,(r,e)=>(n(),e=l(0,41),Array.isArray(e)&&","===e[0]?(e[0]=r,e):e?[r,h(e)]:[r]),"(",19,(r,e)=>!r&&(n(),l(0,41)||a()),")",,,]),d(A,2,["!",r=>!r,"++",r=>++r,"--",r=>--r,".",(r,e)=>r?r[e]:r,"%",(r,e)=>r%e,"/",(r,e)=>r/e,"*",(r,e)=>r*e,"+",(r,e)=>r+(e||0),"-",(r,e)=>null==e?-r:r-e,">>>",(r,e)=>r>>>e,">>",(r,e)=>r>>e,"<<",(r,e)=>r<<e,">=",(r,e)=>r>=e,">",(r,e)=>r>e,"<=",(r,e)=>r<=e,"<",(r,e)=>r<e,"!=",(r,e)=>r!=e,"==",(r,e)=>r==e,"&",(r,e)=>r&e,"^",(r,e)=>r^e,"|",(r,e)=>r|e,"&&",(r,e)=>r&&e,"||",(r,e)=>r||e,",",(r,e)=>e]);var g=r=>(r="string"==typeof r?t(r):r,e=>y(r,e));export{g as default,y as evaluate,t as parse};
|