subscript 5.3.2 → 5.4.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.
- package/README.md +1 -1
- package/evaluate.js +6 -10
- package/justin.js +2 -2
- package/package.json +1 -1
- package/subscript.js +1 -1
- package/subscript.min.js +1 -1
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ import { parse, evaluate } from 'subscript.js'
|
|
|
77
77
|
parse.operator('=>', 10) // precedence=10, type=default (0 - binary, 1 - postfix, -1 - prefix)
|
|
78
78
|
|
|
79
79
|
evaluate.operator('=>', ( args, body ) => evaluate(body, args))
|
|
80
|
-
evaluate.operator('|', ( a,
|
|
80
|
+
evaluate.operator('|', ( a, b ) => a.pipe(b))
|
|
81
81
|
|
|
82
82
|
let tree = parse(`
|
|
83
83
|
interval(350)
|
package/evaluate.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
const cache = new WeakMap
|
|
2
|
-
|
|
3
1
|
// calltree → result
|
|
4
|
-
export const evaluate = (node, ctx={}
|
|
5
|
-
// if (fn=cache.get(node)) return fn(ctx)
|
|
6
|
-
|
|
2
|
+
export const evaluate = (node, ctx={}) => {
|
|
7
3
|
if (typeof node === 'string')
|
|
8
4
|
return node[0] === '"' ? node.slice(1,-1) : node[0]==='@' ? node.slice(1) : node in ctx ? ctx[node] : node
|
|
9
5
|
|
|
10
|
-
if (Array.isArray(node)
|
|
6
|
+
if (Array.isArray(node)) {
|
|
11
7
|
// [[a,b], c] or ['+', a, b] or ['myfn', a, b], or
|
|
12
|
-
let
|
|
13
|
-
|
|
14
|
-
return fn.apply(c,args)
|
|
8
|
+
let c = node[0], fn = Array.isArray(c) ? evaluate(c, ctx) : (lookup[c] || ctx[c] || c), args=[], i = 1
|
|
9
|
+
for (;i<node.length;i++) args.push(evaluate(node[i], ctx))
|
|
10
|
+
return args.length > fn.length && fn.length ? args.reduce(fn) : fn.apply(c,args)
|
|
15
11
|
}
|
|
16
12
|
|
|
17
13
|
return node
|
|
@@ -20,6 +16,6 @@ lookup = {},
|
|
|
20
16
|
|
|
21
17
|
// op evaluators
|
|
22
18
|
// multiple args allows shortcuts, lisp compatible, easy manual eval, functions anyways take multiple arguments
|
|
23
|
-
operator = evaluate.operator = (op, fn) => lookup[op] = fn
|
|
19
|
+
operator = evaluate.operator = (op, fn) => lookup[op] = fn
|
|
24
20
|
|
|
25
21
|
export default evaluate
|
package/justin.js
CHANGED
|
@@ -170,8 +170,8 @@ addOps(evaluate.operator, 2, [
|
|
|
170
170
|
'/', (a,b)=>a/b,
|
|
171
171
|
'*', (a,b)=>a*b,
|
|
172
172
|
|
|
173
|
-
'+', (a,b)=>a+b,
|
|
174
|
-
'-', (
|
|
173
|
+
'+', (a,b=0)=>a+b,
|
|
174
|
+
'-', (a,b)=>b==null ? -a : a-b,
|
|
175
175
|
|
|
176
176
|
'>>>', (a,b)=>a>>>b,
|
|
177
177
|
'>>', (a,b)=>a>>b,
|
package/package.json
CHANGED
package/subscript.js
CHANGED
package/subscript.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let r,e;const t=(t,o)=>(e=t,r=0,o=l(),r<e.length?n():
|
|
1
|
+
let r,e;const t=(t,o)=>(e=t,r=0,o=l(),r<e.length?n():h(o)),n=(t="Bad syntax")=>{throw Error(t+" `"+e[r]+"` at "+r)},o=(t=1,n=r)=>{if("number"==typeof t)r+=t;else for(;t(a());)r++;return e.slice(n,r)},a=(t=0)=>e.charCodeAt(r+t),s=(t=1)=>e.substr(r,t),l=(e=0,o,a,s,l=0,u,i)=>{for(;(a=t.space())&&(i=f[a]?.(s,e)||!s&&p(a));)s=i;return o&&(a!=o?n("Unclosed paren"):r++),s};t.space=e=>{for(;(e=a())<=32;)r++;return e};const u=t.token=[],p=(r,e=0,t)=>{for(;e<u.length;)if(t=u[e++](r))return t},f=[],i=t.operator=(e,n=0,u=0,p,i=e.charCodeAt(0),y=e.length,c=f[i],g=u<=0&&e.toUpperCase()!==e)=>(p=u?u>0?r=>r&&[o(y),h(r)]:u<0?r=>!r&&[o(y),h(l(n-1))]:u:o=>{o=[e,h(o)];do{r+=y,o.push(h(l(n)))}while(t.space()==i&&(y<2||s(y)==e)&&(!g||a(y)<=32));return o},f[i]=(r,t)=>t<n&&(y<2||s(y)==e)&&(!g||a(y)<=32)&&p(r)||c&&c(r,t)),h=r=>Array.isArray(r)?r:(r||n()).valueOf(),y=(r,e={})=>{if("string"==typeof r)return'"'===r[0]?r.slice(1,-1):"@"===r[0]?r.slice(1):r in e?e[r]:r;if(Array.isArray(r)){let t=r[0],n=Array.isArray(t)?y(t,e):c[t]||e[t]||t,o=[],a=1;for(;a<r.length;a++)o.push(y(r[a],e));return o.length>n.length&&n.length?o.reduce(n):n.apply(t,o)}return r},c={},g=y.operator=(r,e)=>c[r]=e;u.push((r=>(r=o((r=>r>47&&r<58||46==r)))&&((69==a()||101==a())&&(r+=o(2)+o((r=>r>=48&&r<=57))),isNaN(r=new Number(r))?n("Bad number"):r)),((r,e)=>34==r&&o()+o((e=>e-r))+o()),(r=>o((r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192))));const A=(r,e=2,t)=>{for(let n=0;n<t.length;n+=e)r(t[n],t[n+1],t[n+2])};A(i,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&&[o(),r,"string"==typeof(e=l(18))?'"'+e+'"':e.valueOf()],"[",18,r=>(o(),[".",r,h(l(0,93))]),"]",,,"(",18,(r,e)=>(o(),e=l(0,41),Array.isArray(e)&&","===e[0]?(e[0]=r,e):e?[r,h(e)]:[r]),"(",19,(r,e)=>!r&&(o(),l(0,41)||n()),")",,,]),A(g,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=0)=>r+e,"-",(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]);var d=r=>(r="string"==typeof r?t(r):r,e=>y(r,e));export{d as default,y as evaluate,t as parse};
|