subscript 8.5.0 → 9.0.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 CHANGED
@@ -4,11 +4,11 @@
4
4
 
5
5
  #### Used for:
6
6
 
7
- * templates (eg. [sprae](https://github.com/dy/sprae), [templize](https://github.com/dy/templize))
7
+ * templates (eg. [sprae](https://github.com/dy/sprae))
8
8
  * expressions evaluators, calculators
9
9
  * subsets of languages (eg. [justin](#justin))
10
10
  * sandboxes, playgrounds, safe eval
11
- * custom DSL (eg. [lino](https://github.com/dy/lino)) <!-- uneural -->
11
+ * custom DSL (eg. [piezo](https://github.com/dy/piezo)) <!-- uneural -->
12
12
  * preprocessors (eg. [prepr](https://github.com/dy/prepr))
13
13
 
14
14
  _Subscript_ has [3.5kb](https://npmfs.com/package/subscript/7.4.3/subscript.min.js) footprint (compare to [11.4kb](https://npmfs.com/package/jsep/1.2.0/dist/jsep.min.js) _jsep_ + [4.5kb](https://npmfs.com/package/expression-eval/5.0.0/dist/expression-eval.module.js) _expression-eval_), best [performance](#performance) and extensive test coverage.
@@ -38,7 +38,7 @@ _Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of
38
38
  * `a < b`, `a <= b`, `a > b`, `a >= b`, `a == b`, `a != b`
39
39
  * `~a`, `a & b`, `a ^ b`, `a | b`, `a << b`, `a >> b`
40
40
  * `!a`, `a && b`, `a || b`
41
- * `a = b`, `a += b`, `a -= b`, `a *= b`, `a /= b`, `a %= b`
41
+ * `a = b`, `a += b`, `a -= b`, `a *= b`, `a /= b`, `a %= b`, , `a <<= b`, `a >>= b`
42
42
  * `(a, (b))`, `a; b;`
43
43
  * `"abc"`, `'abc'`
44
44
  * `0.1`, `1.2e+3`
@@ -52,6 +52,7 @@ It extends _subscript_ with:
52
52
  + `a ** b`, `a **= b`
53
53
  + `a ?? b`, `a ??= b`
54
54
  + `a ||= b`, `a &&= b`
55
+ + `a >>> b`, `a >>>= b`
55
56
  + `a ? b : c`, `a?.b`
56
57
  + `...a`
57
58
  + `[a, b]`
@@ -103,13 +104,36 @@ const fn = compile(['+', ['*', 'min', [,60]], [,'sec']])
103
104
  fn({min: 5}) // min*60 + "sec" == "300sec"
104
105
 
105
106
  // node kinds
106
- ['+', a]; // unary prefix or postfix operator `+a`
107
+ ['+', a]; // unary operator `+a`
107
108
  ['+', a, b]; // binary operator `a + b`
108
109
  ['+', a, b, c]; // n-ary operator `a + b + c`
109
110
  ['()', a]; // group operator `(a)`
110
- ['(', a, b]; // access operator `a(b)`
111
+ ['()', a, b]; // access operator `a(b)`
111
112
  [, a]; // literal value `'a'`
112
113
  a; // variable (from scope)
114
+ null; // placeholder
115
+
116
+ // eg.
117
+ ['()', 'a'] // (a)
118
+ ['()', 'a', null] // a()
119
+ ```
120
+ <!--
121
+ ['()'] // ()
122
+ ['()', 'a'] // (a)
123
+ ['()', 'a', 'b'] // a(b)
124
+ ['+',1] // +1
125
+ ['+',1,,] // 1+
126
+ -->
127
+
128
+ ### Stringify
129
+
130
+ To convert tree back to code, there's codegenerator function:
131
+
132
+ ```js
133
+ import { stringify } from 'subscript.js'
134
+
135
+ stringify(['+', ['*', 'min', [,60]], [,'sec']])
136
+ // 'min*60 + "sec" == "300sec"'
113
137
  ```
114
138
 
115
139
  ## Extending
@@ -153,23 +177,12 @@ See [`./feature/*`](./feature) or [`./justin.js`](./justin.js) for examples.
153
177
  <!--
154
178
  ## Ideas
155
179
 
156
- These are custom DSL operators snippets for your inspiration:
157
-
158
-
159
- ```html
160
- template-parts proposal
161
- <template id="timer">
162
- <time datetime="{{ date.toUTCString() }}">{{ date.toLocaleTimeString() }}</time>
163
- </template>
164
- ```
165
-
166
180
  * Keyed arrays <code>[a:1, b:2, c:3]</code>
167
181
  * 7!` (factorial)
168
182
  * `5s`, `5rem` (units)
169
- * `?`, `?.`, `??`
170
183
  * `arrᵀ` - transpose
171
184
  * `int 5` (typecast)
172
- * `$a` (param expansion)
185
+ * `$a` (parameter expansion)
173
186
  * `1 to 10 by 2`
174
187
  * `a if b else c`
175
188
  * `a, b in c`
@@ -177,12 +190,11 @@ template-parts proposal
177
190
  * vector operators
178
191
  * set operators
179
192
  * polynomial operators
180
-
181
- like versions, units, hashes, urls, regexes etc
182
-
183
- 2a as `2*a`
184
-
185
- string interpolation ` ${} 1 ${} `
193
+ * versions
194
+ * hashes, urls
195
+ * regexes
196
+ * 2a as `2*a`
197
+ * string interpolation ` ${} 1 ${} `
186
198
  -->
187
199
 
188
200
  ## Performance
package/feature/access.js CHANGED
@@ -4,7 +4,7 @@ import { CBRACK, PREC_ACCESS } from '../src/const.js'
4
4
 
5
5
  // a[b]
6
6
  access('[]', PREC_ACCESS)
7
- operator('[', (a, b) => !b ? err() : (a = compile(a), b = compile(b), ctx => a(ctx)[b(ctx)]))
7
+ operator('[]', (a, b) => !b ? err() : (a = compile(a), b = compile(b), ctx => a(ctx)[b(ctx)]))
8
8
 
9
9
  // a.b
10
10
  binary('.', PREC_ACCESS)
package/feature/array.js CHANGED
@@ -4,7 +4,7 @@ import { PREC_TOKEN } from '../src/const.js'
4
4
 
5
5
  // [a,b,c]
6
6
  group('[]', PREC_TOKEN)
7
- operator('[]', (a, b) => (
7
+ operator('[]', (a, b) => b === undefined && (
8
8
  a = !a ? [] : a[0] === ',' ? a.slice(1) : [a],
9
9
  a = a.map(a => a[0] === '...' ? (a = compile(a[1]), ctx => a(ctx)) : (a = compile(a), ctx => [a(ctx)])),
10
10
  ctx => a.flatMap(a => (a(ctx))))
package/feature/call.js CHANGED
@@ -4,7 +4,7 @@ import { PREC_ACCESS } from '../src/const.js'
4
4
 
5
5
  // a(b,c,d), a()
6
6
  access('()', PREC_ACCESS)
7
- operator('(', (a, b, args) => (
7
+ operator('()', (a, b, args) => b !== undefined && (
8
8
  args = !b ? () => [] : // a()
9
9
  b[0] === ',' ? (b = b.slice(1).map(b => !b ? err() : compile(b)), ctx => b.map(arg => arg(ctx))) : // a(b,c)
10
10
  (b = compile(b), ctx => [b(ctx)]), // a(b)
package/feature/group.js CHANGED
@@ -5,7 +5,7 @@ import { PREC_ACCESS, PREC_GROUP, PREC_SEQ, PREC_STATEMENT } from '../src/const.
5
5
  // (a,b,c), (a)
6
6
  // FIXME: try raising group precedence (it causes conflict in ?. though)
7
7
  group('()', PREC_ACCESS)
8
- operator('()', (a) => (!a && err('Empty ()'), compile(a)))
8
+ operator('()', (a, b) => b === undefined && (!a && err('Empty ()'), compile(a)))
9
9
 
10
10
  const last = (...args) => (args = args.map(compile), ctx => args.map(arg => arg(ctx)).pop())
11
11
  nary(',', PREC_SEQ), operator(',', last)
@@ -5,10 +5,10 @@ import { PREC_POSTFIX } from "../src/const.js"
5
5
  let inc, dec
6
6
  token('++', PREC_POSTFIX, a => a ? ['++-', a] : ['++', expr(PREC_POSTFIX - 1)])
7
7
  // ++a, ++((a)), ++a.b, ++a[b]
8
- operator('++', inc = (a) => prop(a, (obj, path, ctx) => ++obj[path]))
9
- operator('++-', inc = (a) => prop(a, (obj, path, ctx) => obj[path]++))
8
+ operator('++', inc = (a) => prop(a, (obj, path) => ++obj[path]))
9
+ operator('++-', inc = (a) => prop(a, (obj, path) => obj[path]++))
10
10
 
11
11
  token('--', PREC_POSTFIX, a => a ? ['--+', a] : ['--', expr(PREC_POSTFIX - 1)])
12
12
  // --a, --a.b, --a[b]
13
- operator('--', dec = (a) => (prop(a, (obj, path, ctx) => --obj[path])))
14
- operator('--+', dec = (a) => (prop(a, (obj, path, ctx) => obj[path]--)))
13
+ operator('--', dec = (a) => (prop(a, (obj, path) => --obj[path])))
14
+ operator('--+', dec = (a) => (prop(a, (obj, path) => obj[path]--)))
package/feature/object.js CHANGED
@@ -5,7 +5,7 @@ import { PREC_ASSIGN, PREC_SEQ, PREC_TOKEN } from '../src/const.js'
5
5
 
6
6
  // {a:1, b:2, c:3}
7
7
  group('{}', PREC_TOKEN)
8
- operator('{}', (a, b) => (
8
+ operator('{}', (a, b) => b === undefined && (
9
9
  // {}, {a:b}, {a}, {a, b}
10
10
  a = (!a ? [] : a[0] !== ',' ? [a] : a.slice(1)),
11
11
  a = a.map(p => compile(typeof p === 'string' ? [':', p, p] : p)),
@@ -13,7 +13,7 @@ token('?.', PREC_ACCESS, (a, b) => a && (b = expr(PREC_ACCESS), !b?.map) && ['?.
13
13
  operator('?.', (a, b) => b && (a = compile(a), ctx => a(ctx)?.[b]))
14
14
 
15
15
  // a?.x() - keep context, but watch out a?.()
16
- operator('(', (a, b, container, args, path, optional) => (a[0] === '?.') && (a[2] || Array.isArray(a[1])) && (
16
+ operator('()', (a, b, container, args, path, optional) => b !== undefined && (a[0] === '?.') && (a[2] || Array.isArray(a[1])) && (
17
17
  args = !b ? () => [] : // a()
18
18
  b[0] === ',' ? (b = b.slice(1).map(compile), ctx => b.map(a => a(ctx))) : // a(b,c)
19
19
  (b = compile(b), ctx => [b(ctx)]), // a(b)
@@ -22,7 +22,7 @@ operator('(', (a, b, container, args, path, optional) => (a[0] === '?.') && (a[2
22
22
  !a[2] && (optional = true, a = a[1]),
23
23
 
24
24
  // a?.['x']?.()
25
- a[0] === '[' ? (path = compile(a[2])) : (path = () => a[2]),
25
+ a[0] === '[]' && a.length === 3 ? (path = compile(a[2])) : (path = () => a[2]),
26
26
  (container = compile(a[1]), optional ?
27
27
  ctx => (container(ctx)?.[path(ctx)]?.(...args(ctx))) :
28
28
  ctx => (container(ctx)?.[path(ctx)](...args(ctx)))
package/feature/shift.js CHANGED
@@ -1,7 +1,12 @@
1
- import { PREC_OR, PREC_AND, PREC_SHIFT, PREC_XOR, PREC_PREFIX } from "../src/const.js"
1
+ import { PREC_OR, PREC_AND, PREC_SHIFT, PREC_XOR, PREC_PREFIX, PREC_ASSIGN } from "../src/const.js"
2
2
  import { unary, binary } from "../src/parse.js"
3
3
  import { operator, compile } from "../src/compile.js"
4
4
 
5
5
 
6
6
  binary('>>', PREC_SHIFT), operator('>>', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) >> b(ctx)))
7
7
  binary('<<', PREC_SHIFT), operator('<<', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) << b(ctx)))
8
+
9
+ binary('>>=', PREC_ASSIGN, true)
10
+ operator('>>=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] >>= b(ctx)))))
11
+ binary('<<=', PREC_ASSIGN, true)
12
+ operator('<<=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] <<= b(ctx)))))
package/justin.js CHANGED
@@ -33,6 +33,12 @@ operator('||=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path
33
33
  binary('&&=', PREC_ASSIGN, true)
34
34
  operator('&&=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] &&= b(ctx)))))
35
35
 
36
+ // unsigned shift
37
+ binary('>>>', PREC_EQ)
38
+ operator('>>>', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) >>> b(ctx)))
39
+ binary('>>>=', PREC_ASSIGN, true)
40
+ operator('>>>=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] >>>= b(ctx)))))
41
+
36
42
  // add JS literals
37
43
  token('undefined', 20, a => a ? err() : [, undefined])
38
44
  token('NaN', 20, a => a ? err() : [, NaN])
package/justin.min.js CHANGED
@@ -1 +1 @@
1
- let e,r,t=t=>(e=0,r=t,t=i(),r[e]?a():t||""),a=(t="Bad syntax",a=r.slice(0,e).split("\n"),l=a.pop())=>{let s=r.slice(e-108,e).split("\n").pop(),i=r.slice(e,e+108).split("\n").shift();throw EvalError(`${t} at ${a.length}:${l.length} \`${e>=108?"…":""}${s}┃${i}\``,"font-weight: bold")},l=(t,a=e,l)=>{for(;l=t(r.charCodeAt(e));)e+=l;return r.slice(a,e)},s=(t=1,a=e)=>(e+=t,r.slice(a,e)),i=(r=0,s)=>{let i,p,c,d;for(;(i=n())&&(c=((d=o[i])&&d(p,r))??(!p&&l(t.id)));)p=c;return s&&(i==s?e++:a()),p},n=t=>{for(;(t=r.charCodeAt(e))<=32;)e++;return t};t.id=e=>e>=48&&e<=57||e>=65&&e<=90||e>=97&&e<=122||36==e||95==e||e>=192&&215!=e&&247!=e;let o=[],p=(l,s=32,i,n=l.charCodeAt(0),p=l.length,c=o[n],d=l.toUpperCase()!==l)=>o[n]=(n,o,f,h=e)=>(f?l==f:(p<2||r.substr(e,p)==l)&&(f=l))&&o<s&&!(d&&t.id(r.charCodeAt(e+p)))&&(e+=p,i(n)||(e=h,!c&&a()))||c?.(n,o,f),c=(e,r,t=!1)=>p(e,r,((a,l)=>a&&(l=i(r-(t?.5:0)))&&[e,a,l])),d=(e,r,t)=>p(e,r,(a=>t?a&&[e,a]:!a&&(a=i(r-.5))&&[e,a])),f=(e,r,t)=>{p(e,r,((t,a)=>(a=i(r),t?.[0]!==e&&(t=[e,t]),a?.[0]===e?t.push(...a.slice(1)):t.push(a),t)))},h=(e,r)=>p(e[0],r,(r=>!r&&[e,i(0,e.charCodeAt(1))])),u=(e,r)=>p(e[0],r,(r=>r&&[e[0],r,i(0,e.charCodeAt(1))]));const A=e=>Array.isArray(e)?e[0]?m[e[0]](...e.slice(1)):()=>e[1]:A.id(e);A.id=e=>r=>r?.[e];const m={},y=(e,r,t=m[e])=>m[e]=(...e)=>r(...e)||t&&t(...e),C=(e,r,t,l,s)=>"()"===e[0]?C(e[1],r,t):"string"==typeof e?t=>r(t,e,t):"."===e[0]?(l=A(e[1]),s=e[2],e=>r(l(e),s,e)):"["===e[0]?(l=A(e[1]),s=A(e[2]),e=>r(l(e),s(e),e)):t?(e=A(e),t=>r([e(t)],0,t)):()=>a("Bad left value"),g=(e,r)=>[,(e=+l((e=>46===e||e>=48&&e<=57||(69===e||101===e?2:0))))!=e?a():e];o[46]=e=>!e&&g();for(let e=48;e<=57;e++)o[e]=e=>e?a():g();const b={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},v=t=>(l,i,n="")=>{for(l&&a("Unexpected string"),s();(i=r.charCodeAt(e))-t;)92===i?(s(),i=s(),n+=b[i]||i):n+=s();return s()||a("Bad string"),[,n]};o[34]=v(34),o[39]=v(39),u("()",170),y("(",((e,r,t)=>(t=r?","===r[0]?(r=r.slice(1).map((e=>e?A(e):err())),e=>r.map((r=>r(e)))):(r=A(r),e=>[r(e)]):()=>[],C(e,((e,r,a)=>e[r](...t(a))),!0)))),u("[]",170),y("[",((e,r)=>r?(e=A(e),r=A(r),t=>e(t)[r(t)]):err())),c(".",170),y(".",((e,r)=>(e=A(e),r=r[0]?r:r[1],t=>e(t)[r]))),h("()",170),y("()",(e=>(!e&&a("Empty ()"),A(e))));const $=(...e)=>(e=e.map(A),r=>e.map((e=>e(r))).pop());f(",",10),y(",",$),f(";",5),y(";",$),c("=",20,!0),y("=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]=r(a)))))),c("*",120),y("*",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)*r(t)))),c("/",120),y("/",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)/r(t)))),c("%",120),y("%",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)%r(t)))),c("*=",20,!0),y("*=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]*=r(a)))))),c("/=",20,!0),y("/=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]/=r(a)))))),c("%=",20,!0),y("%=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]%=r(a)))))),c("+",110),y("+",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)+r(t)))),c("-",110),y("-",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)-r(t)))),d("+",140),y("+",((e,r)=>!r&&(e=A(e),r=>+e(r)))),d("-",140),y("-",((e,r)=>!r&&(e=A(e),r=>-e(r)))),c("+=",20,!0),y("+=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]+=r(a)))))),c("-=",20,!0),y("-=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]-=r(a)))))),p("++",150,(e=>e?["++-",e]:["++",i(149)])),y("++",(e=>C(e,((e,r,t)=>++e[r])))),y("++-",(e=>C(e,((e,r,t)=>e[r]++)))),p("--",150,(e=>e?["--+",e]:["--",i(149)])),y("--",(e=>C(e,((e,r,t)=>--e[r])))),y("--+",(e=>C(e,((e,r,t)=>e[r]--)))),d("~",140),y("~",((e,r)=>!r&&(e=A(e),r=>~e(r)))),c("|",50),y("|",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)|r(t)))),c("&",70),y("&",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)&r(t)))),c("^",60),y("^",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)^r(t)))),c("==",80),y("==",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)==r(t)))),c("!=",80),y("!=",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)!=r(t)))),c(">",90),y(">",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)>r(t)))),c("<",90),y("<",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)<r(t)))),c(">=",90),y(">=",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)>=r(t)))),c("<=",90),y("<=",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)<=r(t)))),c(">>",100),y(">>",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)>>r(t)))),c("<<",100),y("<<",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)<<r(t)))),d("!",140),y("!",((e,r)=>!r&&(e=A(e),r=>!e(r)))),c("||",30),y("||",((e,r)=>(e=A(e),r=A(r),t=>e(t)||r(t)))),c("&&",40),y("&&",((e,r)=>(e=A(e),r=A(r),t=>e(t)&&r(t))));var E=e=>A(t(e));p("/*",200,((t,a)=>(l((t=>42!==t&&47!==r.charCodeAt(e+1))),s(2),t||i(a)||[]))),p("//",200,((e,r)=>(l((e=>e>=32)),e||i(r)||[]))),c("**",130,!0),y("**",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)**r(t)))),p("?",20,((e,r,t)=>e&&(r=i(19))&&l((e=>58===e))&&["?",e,r,i(19)])),y("?",((e,r,t)=>(e=A(e),r=A(r),t=A(t),a=>e(a)?r(a):t(a)))),p("true",200,(e=>e?err():[,!0])),p("false",200,(e=>e?err():[,!1])),h("[]",200),y("[]",((e,r)=>(e=(e=e?","===e[0]?e.slice(1):[e]:[]).map((e=>"..."===e[0]?(e=A(e[1]),r=>e(r)):(e=A(e),r=>[e(r)]))),r=>e.flatMap((e=>e(r)))))),h("{}",200),y("{}",((e,r)=>(e=(e=e?","!==e[0]?[e]:e.slice(1):[]).map((e=>A("string"==typeof e?[":",e,e]:e))),r=>Object.fromEntries(e.flatMap((e=>e(r))))))),c(":",19,!0),y(":",((e,r)=>(r=A(r),Array.isArray(e)?(e=A(e),t=>[[e(t),r(t)]]):t=>[[e,r(t)]]))),c("=>",20,!0),y("=>",((e,r)=>(e=(e="()"===e[0]?e[1]:e)?e=","===e[0]?e.slice(1):[e]:[],r=A("{}"===r[0]?r[1]:r),(t=null)=>(t=Object.create(t),(...a)=>(e.map(((e,r)=>t[e]=a[r])),r(t)))))),c(""),p("?.",170,(e=>e&&["?.",e])),y("?.",(e=>(e=A(e),r=>e(r)||(()=>{})))),p("?.",170,((e,r)=>e&&!(r=i(170))?.map&&["?.",e,r])),y("?.",((e,r)=>r&&(e=A(e),t=>e(t)?.[r]))),y("(",((e,r,t,a,l,s)=>"?."===e[0]&&(e[2]||Array.isArray(e[1]))&&(a=r?","===r[0]?(r=r.slice(1).map(A),e=>r.map((r=>r(e)))):(r=A(r),e=>[r(e)]):()=>[],!e[2]&&(e=e[1]),l="["===e[0]?A(e[2]):()=>e[2],t=A(e[1]),e=>t(e)?.[l(e)]?.(...a(e))))),d("...",140),y("...",(e=>(e=A(e),r=>Object.entries(e(r))))),c("in",90),y("in",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)in r(t)))),c("===",80),c("!==",9),y("===",((e,r)=>(e=A(e),r=A(r),t=>e(t)===r(t)))),y("!==",((e,r)=>(e=A(e),r=A(r),t=>e(t)!==r(t)))),c("??",30),y("??",((e,r)=>r&&(e=A(e),r=A(r),t=>e(t)??r(t)))),c("??=",20,!0),y("??=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]??=r(a)))))),c("||=",20,!0),y("||=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]||=r(a)))))),c("&&=",20,!0),y("&&=",((e,r)=>(r=A(r),C(e,((e,t,a)=>e[t]&&=r(a)))))),p("undefined",20,(e=>e?a():[,void 0])),p("NaN",20,(e=>e?a():[,NaN])),p("null",20,(e=>e?a():[,null]));export{u as access,c as binary,A as compile,E as default,h as group,f as nary,y as operator,t as parse,p as token,d as unary};
1
+ let r,e,t=t=>(r=0,e=t,t=a(),e[r]?n():t||""),n=(t="Bad syntax",n=e.slice(0,r).split("\n"),l=n.pop())=>{let i=e.slice(r-108,r).split("\n").pop(),a=e.slice(r,r+108).split("\n").shift();throw EvalError(`${t} at ${n.length}:${l.length} \`${r>=108?"…":""}${i}┃${a}\``,"font-weight: bold")},l=(t,n=r,l)=>{for(;l=t(e.charCodeAt(r));)r+=l;return e.slice(n,r)},i=(t=1,n=r)=>(r+=t,e.slice(n,r)),a=(e=0,i)=>{let a,p,c,d;for(;(a=o())&&(c=((d=s[a])&&d(p,e))??(!p&&l(t.id)));)p=c;return i&&(a==i?r++:n()),p},o=t=>{for(;(t=e.charCodeAt(r))<=32;)r++;return t};t.id=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192&&215!=r&&247!=r;let s=[],p=(l,i=32,a,o=l.charCodeAt(0),p=l.length,c=s[o],d=l.toUpperCase()!==l)=>s[o]=(o,s,f,h=r)=>(f?l==f:(p<2||e.substr(r,p)==l)&&(f=l))&&s<i&&!(d&&t.id(e.charCodeAt(r+p)))&&(r+=p,a(o)||(r=h,!c&&n()))||c?.(o,s,f),c=(r,e,t=!1)=>p(r,e,((n,l)=>n&&(l=a(e-(t?.5:0)))&&[r,n,l])),d=(r,e,t)=>p(r,e,(n=>t?n&&[r,n]:!n&&(n=a(e-.5))&&[r,n])),f=(r,e,t)=>{p(r,e,((t,n)=>(n=a(e),t?.[0]!==r&&(t=[r,t||null]),n?.[0]===r?t.push(...n.slice(1)):t.push(n||null),t)))},h=(r,e)=>p(r[0],e,(e=>!e&&[r,a(0,r.charCodeAt(1))])),u=(r,e)=>p(r[0],e,(e=>e&&[r,e,a(0,r.charCodeAt(1))||null]));const A=r=>Array.isArray(r)?r[0]?g[r[0]](...r.slice(1)):()=>r[1]:A.id(r);A.id=r=>e=>e?.[r];const g={},m=(r,e,t=g[r])=>g[r]=(...r)=>e(...r)||t?.(...r),y=(r,e,t,l,i)=>"()"===r[0]&&2==r.length?y(r[1],e,t):"string"==typeof r?t=>e(t,r,t):"."===r[0]?(l=A(r[1]),i=r[2],r=>e(l(r),i,r)):"[]"===r[0]&&3===r.length?(l=A(r[1]),i=A(r[2]),r=>e(l(r),i(r),r)):t?(r=A(r),t=>e([r(t)],0,t)):()=>n("Bad left value"),v=(r,e)=>[,(r=+l((r=>46===r||r>=48&&r<=57||(69===r||101===r?2:0))))!=r?n():r];s[46]=r=>!r&&v();for(let r=48;r<=57;r++)s[r]=r=>r?n():v();const C={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},b=t=>(l,a,o="")=>{for(l&&n("Unexpected string"),i();(a=e.charCodeAt(r))-t;)92===a?(i(),a=i(),o+=C[a]||a):o+=i();return i()||n("Bad string"),[,o]};s[34]=b(34),s[39]=b(39),u("()",170),m("()",((r,e,t)=>void 0!==e&&(t=e?","===e[0]?(e=e.slice(1).map((r=>r?A(r):err())),r=>e.map((e=>e(r)))):(e=A(e),r=>[e(r)]):()=>[],y(r,((r,e,n)=>r[e](...t(n))),!0)))),u("[]",170),m("[]",((r,e)=>e?(r=A(r),e=A(e),t=>r(t)[e(t)]):err())),c(".",170),m(".",((r,e)=>(r=A(r),e=e[0]?e:e[1],t=>r(t)[e]))),h("()",170),m("()",((r,e)=>void 0===e&&(!r&&n("Empty ()"),A(r))));const $=(...r)=>(r=r.map(A),e=>r.map((r=>r(e))).pop());function N(r){if(!r)return"";if(Array.isArray(r)){const[e,...t]=r;return e?"[]"==e||"{}"==e||"()"==e?(t.length>1?N(t.shift()):"")+e[0]+N(t[0])+e[1]:1===t.length?e+N(t[0]):2===t.length?N(t[0])+("."===e?e:" "+e+" ")+N(t[1]):t.filter(Boolean).map((r=>N(r))).join(e+"\n"):JSON.stringify(t[0])}return r}f(",",10),m(",",$),f(";",5),m(";",$),c("=",20,!0),m("=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]=e(n)))))),c("*",120),m("*",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)*e(t)))),c("/",120),m("/",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)/e(t)))),c("%",120),m("%",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)%e(t)))),c("*=",20,!0),m("*=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]*=e(n)))))),c("/=",20,!0),m("/=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]/=e(n)))))),c("%=",20,!0),m("%=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]%=e(n)))))),c("+",110),m("+",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)+e(t)))),c("-",110),m("-",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)-e(t)))),d("+",140),m("+",((r,e)=>!e&&(r=A(r),e=>+r(e)))),d("-",140),m("-",((r,e)=>!e&&(r=A(r),e=>-r(e)))),c("+=",20,!0),m("+=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]+=e(n)))))),c("-=",20,!0),m("-=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]-=e(n)))))),p("++",150,(r=>r?["++-",r]:["++",a(149)])),m("++",(r=>y(r,((r,e)=>++r[e])))),m("++-",(r=>y(r,((r,e)=>r[e]++)))),p("--",150,(r=>r?["--+",r]:["--",a(149)])),m("--",(r=>y(r,((r,e)=>--r[e])))),m("--+",(r=>y(r,((r,e)=>r[e]--)))),d("~",140),m("~",((r,e)=>!e&&(r=A(r),e=>~r(e)))),c("|",50),m("|",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)|e(t)))),c("&",70),m("&",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)&e(t)))),c("^",60),m("^",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)^e(t)))),c("==",80),m("==",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)==e(t)))),c("!=",80),m("!=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)!=e(t)))),c(">",90),m(">",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>e(t)))),c("<",90),m("<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<e(t)))),c(">=",90),m(">=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>=e(t)))),c("<=",90),m("<=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<=e(t)))),c(">>",100),m(">>",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>>e(t)))),c("<<",100),m("<<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<<e(t)))),c(">>=",20,!0),m(">>=",((r,e)=>(e=A(e),prop(r,((r,t,n)=>r[t]>>=e(n)))))),c("<<=",20,!0),m("<<=",((r,e)=>(e=A(e),prop(r,((r,t,n)=>r[t]<<=e(n)))))),d("!",140),m("!",((r,e)=>!e&&(r=A(r),e=>!r(e)))),c("||",30),m("||",((r,e)=>(r=A(r),e=A(e),t=>r(t)||e(t)))),c("&&",40),m("&&",((r,e)=>(r=A(r),e=A(e),t=>r(t)&&e(t))));var j=r=>A(t(r));p("/*",200,((t,n)=>(l((t=>42!==t&&47!==e.charCodeAt(r+1))),i(2),t||a(n)||[]))),p("//",200,((r,e)=>(l((r=>r>=32)),r||a(e)||[]))),c("**",130,!0),m("**",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)**e(t)))),p("?",20,((r,e,t)=>r&&(e=a(19))&&l((r=>58===r))&&["?",r,e,a(19)])),m("?",((r,e,t)=>(r=A(r),e=A(e),t=A(t),n=>r(n)?e(n):t(n)))),p("true",200,(r=>r?err():[,!0])),p("false",200,(r=>r?err():[,!1])),h("[]",200),m("[]",((r,e)=>void 0===e&&(r=(r=r?","===r[0]?r.slice(1):[r]:[]).map((r=>"..."===r[0]?(r=A(r[1]),e=>r(e)):(r=A(r),e=>[r(e)]))),e=>r.flatMap((r=>r(e)))))),h("{}",200),m("{}",((r,e)=>void 0===e&&(r=(r=r?","!==r[0]?[r]:r.slice(1):[]).map((r=>A("string"==typeof r?[":",r,r]:r))),e=>Object.fromEntries(r.flatMap((r=>r(e))))))),c(":",19,!0),m(":",((r,e)=>(e=A(e),Array.isArray(r)?(r=A(r),t=>[[r(t),e(t)]]):t=>[[r,e(t)]]))),c("=>",20,!0),m("=>",((r,e)=>(r=(r="()"===r[0]?r[1]:r)?r=","===r[0]?r.slice(1):[r]:[],e=A("{}"===e[0]?e[1]:e),(t=null)=>(t=Object.create(t),(...n)=>(r.map(((r,e)=>t[r]=n[e])),e(t)))))),c(""),p("?.",170,(r=>r&&["?.",r])),m("?.",(r=>(r=A(r),e=>r(e)||(()=>{})))),p("?.",170,((r,e)=>r&&!(e=a(170))?.map&&["?.",r,e])),m("?.",((r,e)=>e&&(r=A(r),t=>r(t)?.[e]))),m("()",((r,e,t,n,l,i)=>void 0!==e&&"?."===r[0]&&(r[2]||Array.isArray(r[1]))&&(n=e?","===e[0]?(e=e.slice(1).map(A),r=>e.map((e=>e(r)))):(e=A(e),r=>[e(r)]):()=>[],!r[2]&&(r=r[1]),l="[]"===r[0]&&3===r.length?A(r[2]):()=>r[2],t=A(r[1]),r=>t(r)?.[l(r)]?.(...n(r))))),d("...",140),m("...",(r=>(r=A(r),e=>Object.entries(r(e))))),c("in",90),m("in",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)in e(t)))),c("===",80),c("!==",9),m("===",((r,e)=>(r=A(r),e=A(e),t=>r(t)===e(t)))),m("!==",((r,e)=>(r=A(r),e=A(e),t=>r(t)!==e(t)))),c("??",30),m("??",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)??e(t)))),c("??=",20,!0),m("??=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]??=e(n)))))),c("||=",20,!0),m("||=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]||=e(n)))))),c("&&=",20,!0),m("&&=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]&&=e(n)))))),c(">>>",80),m(">>>",((r,e)=>(r=A(r),e=A(e),t=>r(t)>>>e(t)))),c(">>>=",20,!0),m(">>>=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]>>>=e(n)))))),p("undefined",20,(r=>r?n():[,void 0])),p("NaN",20,(r=>r?n():[,NaN])),p("null",20,(r=>r?n():[,null]));export{u as access,c as binary,A as compile,j as default,h as group,f as nary,m as operator,t as parse,N as stringify,p as token,d as unary};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "subscript",
3
- "version": "8.5.0",
3
+ "version": "9.0.0",
4
4
  "description": "Fast and tiny expression evaluator with minimal syntax.",
5
5
  "main": "subscript.js",
6
6
  "module": "subscript.js",
package/src/compile.js CHANGED
@@ -10,18 +10,18 @@ export const compile = (node) => !Array.isArray(node) ? compile.id(node) : !node
10
10
  operators = {},
11
11
 
12
12
  // register an operator
13
- operator = (op, fn, prev = operators[op]) => (operators[op] = (...args) => fn(...args) || prev && prev(...args)),
13
+ operator = (op, fn, prev = operators[op]) => (operators[op] = (...args) => fn(...args) || prev?.(...args)),
14
14
 
15
15
  // takes node and returns evaluator depending on the case with passed params (container, path, ctx) =>
16
16
  prop = (a, fn, generic, obj, path) => (
17
17
  // (((x))) => x
18
- a[0] === '()' ? prop(a[1], fn, generic) :
18
+ a[0] === '()' && a.length == 2 ? prop(a[1], fn, generic) :
19
19
  // (_, name, ctx) => ctx[path]
20
20
  typeof a === 'string' ? ctx => fn(ctx, a, ctx) :
21
21
  // (container, path, ctx) => container(ctx)[path]
22
22
  a[0] === '.' ? (obj = compile(a[1]), path = a[2], ctx => fn(obj(ctx), path, ctx)) :
23
23
  // (container, path, ctx) => container(ctx)[path(ctx)]
24
- a[0] === '[' ? (obj = compile(a[1]), path = compile(a[2]), ctx => fn(obj(ctx), path(ctx), ctx)) :
24
+ a[0] === '[]' && a.length === 3 ? (obj = compile(a[1]), path = compile(a[2]), ctx => fn(obj(ctx), path(ctx), ctx)) :
25
25
  // (src, _, ctx) => src(ctx)
26
26
  generic ? (a = compile(a), ctx => fn([a(ctx)], 0, ctx)) : () => err('Bad left value')
27
27
  )
package/src/parse.js CHANGED
@@ -94,8 +94,8 @@ export let idx, cur,
94
94
  (a, b) => (
95
95
  b = expr(prec),
96
96
  (
97
- (a?.[0] !== op) && (a = [op, a]), // if beginning of sequence - init node
98
- b?.[0] === op ? a.push(...b.slice(1)) : a.push(b), // comments can return same-token expr
97
+ (a?.[0] !== op) && (a = [op, a || null]), // if beginning of sequence - init node
98
+ b?.[0] === op ? a.push(...b.slice(1)) : a.push(b || null), // comments can return same-token expr
99
99
  a
100
100
  ))
101
101
  )
@@ -103,10 +103,11 @@ export let idx, cur,
103
103
 
104
104
  // register (a), [b], {c} etc groups
105
105
  // FIXME: add "Unclosed paren" error
106
- group = (op, prec) => token(op[0], prec, a => (!a && ([op, expr(0, op.charCodeAt(1))]))),
106
+ group = (op, prec) => token(op[0], prec, a => (!a && [op, expr(0, op.charCodeAt(1))])),
107
107
 
108
108
  // register a(b), a[b], a<b> etc,
109
- access = (op, prec) => token(op[0], prec, a => (a && [op[0], a, expr(0, op.charCodeAt(1))]))
109
+ // NOTE: we make sure `null` indicates placeholder
110
+ access = (op, prec) => token(op[0], prec, a => (a && [op, a, expr(0, op.charCodeAt(1)) || null]))
110
111
 
111
112
 
112
113
  export default parse
@@ -0,0 +1,31 @@
1
+ // convert ast to code string (codegen)
2
+
3
+ // FIXME: possible enhancements
4
+ // * pairs via options
5
+ // * custom spacing/newlines (`a.b` vs `a + b` vs `a, b` vs `a; b`)
6
+ // * newlines?
7
+ // * custom literals?
8
+ export function stringify(node) {
9
+ if (!node) return ''
10
+
11
+ if (Array.isArray(node)) {
12
+ const [op, ...args] = node;
13
+
14
+ // 1, "x"
15
+ if (!op) return JSON.stringify(args[0])
16
+
17
+ // (a), a(b)
18
+ if (op == '[]' || op == '{}' || op == '()') return (args.length > 1 ? stringify(args.shift()) : '') + op[0] + (stringify(args[0])) + op[1]
19
+
20
+ // +a
21
+ if (args.length === 1) return op + stringify(args[0])
22
+
23
+ // a + b
24
+ if (args.length === 2) return stringify(args[0]) + (op === '.' ? op : (' ' + op + ' ')) + stringify(args[1])
25
+
26
+ // a; b; c
27
+ return args.filter(Boolean).map(a => stringify(a)).join(op + '\n')
28
+ }
29
+
30
+ return node;
31
+ }
package/subscript.js CHANGED
@@ -19,5 +19,6 @@ import parse from './src/parse.js'
19
19
 
20
20
  export { parse, access, binary, unary, nary, group, token } from './src/parse.js'
21
21
  export { compile, operator } from './src/compile.js'
22
+ export { stringify } from './src/stringify.js'
22
23
 
23
24
  export default s => compile(parse(s))
package/subscript.min.js CHANGED
@@ -1 +1 @@
1
- let t,r,e=e=>(t=0,r=e,e=l(),r[t]?s():e||""),s=(e="Bad syntax",s=r.slice(0,t).split("\n"),o=s.pop())=>{let a=r.slice(t-108,t).split("\n").pop(),l=r.slice(t,t+108).split("\n").shift();throw EvalError(`${e} at ${s.length}:${o.length} \`${t>=108?"…":""}${a}┃${l}\``,"font-weight: bold")},o=(e,s=t,o)=>{for(;o=e(r.charCodeAt(t));)t+=o;return r.slice(s,t)},a=(e=1,s=t)=>(t+=e,r.slice(s,t)),l=(r=0,a)=>{let l,c,p,d;for(;(l=n())&&(p=((d=i[l])&&d(c,r))??(!c&&o(e.id)));)c=p;return a&&(l==a?t++:s()),c},n=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 i=[],c=(o,a=32,l,n=o.charCodeAt(0),c=o.length,p=i[n],d=o.toUpperCase()!==o)=>i[n]=(n,i,h,f=t)=>(h?o==h:(c<2||r.substr(t,c)==o)&&(h=o))&&i<a&&!(d&&e.id(r.charCodeAt(t+c)))&&(t+=c,l(n)||(t=f,!p&&s()))||p?.(n,i,h),p=(t,r,e=!1)=>c(t,r,((s,o)=>s&&(o=l(r-(e?.5:0)))&&[t,s,o])),d=(t,r,e)=>c(t,r,(s=>e?s&&[t,s]:!s&&(s=l(r-.5))&&[t,s])),h=(t,r,e)=>{c(t,r,((e,s)=>(s=l(r),e?.[0]!==t&&(e=[t,e]),s?.[0]===t?e.push(...s.slice(1)):e.push(s),e)))},f=(t,r)=>c(t[0],r,(r=>!r&&[t,l(0,t.charCodeAt(1))])),u=(t,r)=>c(t[0],r,(r=>r&&[t[0],r,l(0,t.charCodeAt(1))]));const A=(t,r)=>[,(t=+o((t=>46===t||t>=48&&t<=57||(69===t||101===t?2:0))))!=t?s():t];i[46]=t=>!t&&A();for(let t=48;t<=57;t++)i[t]=t=>t?s():A();const C={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},g=e=>(o,l,n="")=>{for(o&&s("Unexpected string"),a();(l=r.charCodeAt(t))-e;)92===l?(a(),l=a(),n+=C[l]||l):n+=a();return a()||s("Bad string"),[,n]};i[34]=g(34),i[39]=g(39);const $=t=>Array.isArray(t)?t[0]?m[t[0]](...t.slice(1)):()=>t[1]:$.id(t);$.id=t=>r=>r?.[t];const m={},v=(t,r,e=m[t])=>m[t]=(...t)=>r(...t)||e&&e(...t),y=(t,r,e,o,a)=>"()"===t[0]?y(t[1],r,e):"string"==typeof t?e=>r(e,t,e):"."===t[0]?(o=$(t[1]),a=t[2],t=>r(o(t),a,t)):"["===t[0]?(o=$(t[1]),a=$(t[2]),t=>r(o(t),a(t),t)):e?(t=$(t),e=>r([t(e)],0,e)):()=>s("Bad left value");u("()",170),v("(",((t,r,e)=>(e=r?","===r[0]?(r=r.slice(1).map((t=>t?$(t):err())),t=>r.map((r=>r(t)))):(r=$(r),t=>[r(t)]):()=>[],y(t,((t,r,s)=>t[r](...e(s))),!0)))),u("[]",170),v("[",((t,r)=>r?(t=$(t),r=$(r),e=>t(e)[r(e)]):err())),p(".",170),v(".",((t,r)=>(t=$(t),r=r[0]?r:r[1],e=>t(e)[r]))),f("()",170),v("()",(t=>(!t&&s("Empty ()"),$(t))));const b=(...t)=>(t=t.map($),r=>t.map((t=>t(r))).pop());h(",",10),v(",",b),h(";",5),v(";",b),p("=",20,!0),v("=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]=r(s)))))),p("*",120),v("*",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)*r(e)))),p("/",120),v("/",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)/r(e)))),p("%",120),v("%",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)%r(e)))),p("*=",20,!0),v("*=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]*=r(s)))))),p("/=",20,!0),v("/=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]/=r(s)))))),p("%=",20,!0),v("%=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]%=r(s)))))),p("+",110),v("+",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)+r(e)))),p("-",110),v("-",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)-r(e)))),d("+",140),v("+",((t,r)=>!r&&(t=$(t),r=>+t(r)))),d("-",140),v("-",((t,r)=>!r&&(t=$(t),r=>-t(r)))),p("+=",20,!0),v("+=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]+=r(s)))))),p("-=",20,!0),v("-=",((t,r)=>(r=$(r),y(t,((t,e,s)=>t[e]-=r(s)))))),c("++",150,(t=>t?["++-",t]:["++",l(149)])),v("++",(t=>y(t,((t,r,e)=>++t[r])))),v("++-",(t=>y(t,((t,r,e)=>t[r]++)))),c("--",150,(t=>t?["--+",t]:["--",l(149)])),v("--",(t=>y(t,((t,r,e)=>--t[r])))),v("--+",(t=>y(t,((t,r,e)=>t[r]--)))),d("~",140),v("~",((t,r)=>!r&&(t=$(t),r=>~t(r)))),p("|",50),v("|",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)|r(e)))),p("&",70),v("&",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)&r(e)))),p("^",60),v("^",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)^r(e)))),p("==",80),v("==",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)==r(e)))),p("!=",80),v("!=",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)!=r(e)))),p(">",90),v(">",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)>r(e)))),p("<",90),v("<",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)<r(e)))),p(">=",90),v(">=",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)>=r(e)))),p("<=",90),v("<=",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)<=r(e)))),p(">>",100),v(">>",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)>>r(e)))),p("<<",100),v("<<",((t,r)=>r&&(t=$(t),r=$(r),e=>t(e)<<r(e)))),d("!",140),v("!",((t,r)=>!r&&(t=$(t),r=>!t(r)))),p("||",30),v("||",((t,r)=>(t=$(t),r=$(r),e=>t(e)||r(e)))),p("&&",40),v("&&",((t,r)=>(t=$(t),r=$(r),e=>t(e)&&r(e))));var x=t=>$(e(t));export{u as access,p as binary,$ as compile,x as default,f as group,h as nary,v as operator,e as parse,c as token,d as unary};
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};