subscript 8.4.0 → 8.5.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 +7 -4
- package/feature/add.js +3 -3
- package/feature/bitwise.js +0 -4
- package/feature/logic.js +1 -1
- package/feature/shift.js +7 -0
- package/justin.min.js +1 -1
- package/package.json +1 -1
- package/src/parse.js +28 -17
- package/subscript.js +2 -1
- package/subscript.min.js +1 -1
package/README.md
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
* expressions evaluators, calculators
|
|
9
9
|
* subsets of languages (eg. [justin](#justin))
|
|
10
10
|
* sandboxes, playgrounds, safe eval
|
|
11
|
-
* custom DSL (eg. [
|
|
11
|
+
* custom DSL (eg. [lino](https://github.com/dy/lino)) <!-- uneural -->
|
|
12
12
|
* preprocessors (eg. [prepr](https://github.com/dy/prepr))
|
|
13
13
|
|
|
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_),
|
|
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.
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
@@ -29,7 +29,7 @@ fn({ a: { b:1 }, c: 5, Math })
|
|
|
29
29
|
|
|
30
30
|
## Operators
|
|
31
31
|
|
|
32
|
-
_Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)) (
|
|
32
|
+
_Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)) (_JavaScript_, _C_, _C++_, _Java_, _C#_, _PHP_, _Swift_, _Objective-C_, _Kotlin_, _Perl_ etc.):
|
|
33
33
|
|
|
34
34
|
* `a.b`, `a[b]`, `a(b)`
|
|
35
35
|
* `a++`, `a--`, `++a`, `--a`
|
|
@@ -45,7 +45,8 @@ _Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of
|
|
|
45
45
|
|
|
46
46
|
### Justin
|
|
47
47
|
|
|
48
|
-
_Just-in_ is no-keywords JS subset, _JSON_ + _expressions_ (see [thread](https://github.com/endojs/Jessie/issues/66)).<br/>
|
|
48
|
+
_Just-in_ is no-keywords JS subset, _JSON_ + _expressions_ (see [thread](https://github.com/endojs/Jessie/issues/66)).<br/>
|
|
49
|
+
It extends _subscript_ with:
|
|
49
50
|
|
|
50
51
|
+ `a === b`, `a !== b`
|
|
51
52
|
+ `a ** b`, `a **= b`
|
|
@@ -123,6 +124,8 @@ _Subscript_ provides premade language [features](./features) and API to customiz
|
|
|
123
124
|
* `token(str, precedence, lnode => node)` − register custom token or literal. Callback takes left-side node and returns complete expression node.
|
|
124
125
|
* `operator(str, (a, b) => ctx => value)` − register evaluator for an operator. Callback takes node arguments and returns evaluator function.
|
|
125
126
|
|
|
127
|
+
Longer operators should be registered after shorter ones, eg. first `|`, then `||`, then `||=`.
|
|
128
|
+
|
|
126
129
|
```js
|
|
127
130
|
import script, { compile, operator, unary, binary, token } from './subscript.js'
|
|
128
131
|
|
package/feature/add.js
CHANGED
|
@@ -3,12 +3,12 @@ import { binary, unary } from '../src/parse.js'
|
|
|
3
3
|
import { PREC_ADD, PREC_PREFIX, PREC_ASSIGN } from '../src/const.js'
|
|
4
4
|
import { compile, prop, operator } from '../src/compile.js'
|
|
5
5
|
|
|
6
|
-
unary('+', PREC_PREFIX), operator('+', (a, b) => !b && (a = compile(a), ctx => +a(ctx)))
|
|
7
|
-
unary('-', PREC_PREFIX), operator('-', (a, b) => !b && (a = compile(a), ctx => -a(ctx)))
|
|
8
|
-
|
|
9
6
|
binary('+', PREC_ADD), operator('+', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) + b(ctx)))
|
|
10
7
|
binary('-', PREC_ADD), operator('-', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) - b(ctx)))
|
|
11
8
|
|
|
9
|
+
unary('+', PREC_PREFIX), operator('+', (a, b) => !b && (a = compile(a), ctx => +a(ctx)))
|
|
10
|
+
unary('-', PREC_PREFIX), operator('-', (a, b) => !b && (a = compile(a), ctx => -a(ctx)))
|
|
11
|
+
|
|
12
12
|
binary('+=', PREC_ASSIGN, true)
|
|
13
13
|
operator('+=', (a, b) => (
|
|
14
14
|
b = compile(b),
|
package/feature/bitwise.js
CHANGED
|
@@ -9,7 +9,3 @@ binary('|', PREC_OR), operator('|', (a, b) => b && (a = compile(a), b = compile(
|
|
|
9
9
|
binary('&', PREC_AND), operator('&', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) & b(ctx)))
|
|
10
10
|
|
|
11
11
|
binary('^', PREC_XOR), operator('^', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) ^ b(ctx)))
|
|
12
|
-
|
|
13
|
-
binary('>>', PREC_SHIFT), operator('>>', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) >> b(ctx)))
|
|
14
|
-
|
|
15
|
-
binary('<<', PREC_SHIFT), operator('<<', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) << b(ctx)))
|
package/feature/logic.js
CHANGED
package/feature/shift.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PREC_OR, PREC_AND, PREC_SHIFT, PREC_XOR, PREC_PREFIX } from "../src/const.js"
|
|
2
|
+
import { unary, binary } from "../src/parse.js"
|
|
3
|
+
import { operator, compile } from "../src/compile.js"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
binary('>>', PREC_SHIFT), operator('>>', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) >> b(ctx)))
|
|
7
|
+
binary('<<', PREC_SHIFT), operator('<<', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) << b(ctx)))
|
package/justin.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let e,r,t=t=>(e=0,r=t,t=
|
|
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};
|
package/package.json
CHANGED
package/src/parse.js
CHANGED
|
@@ -25,10 +25,12 @@ export let idx, cur,
|
|
|
25
25
|
skip = (n = 1, from = idx) => (idx += n, cur.slice(from, idx)),
|
|
26
26
|
|
|
27
27
|
// a + b - c
|
|
28
|
-
expr = (prec = 0, end
|
|
28
|
+
expr = (prec = 0, end) => {
|
|
29
|
+
let cc, token, newNode, fn
|
|
30
|
+
|
|
29
31
|
// chunk/token parser
|
|
30
32
|
while (
|
|
31
|
-
(cc =
|
|
33
|
+
(cc = space()) && // till not end
|
|
32
34
|
// FIXME: extra work is happening here, when lookup bails out due to lower precedence -
|
|
33
35
|
// it makes extra `space` call for parent exprs on the same character to check precedence again
|
|
34
36
|
(newNode =
|
|
@@ -43,6 +45,9 @@ export let idx, cur,
|
|
|
43
45
|
return token
|
|
44
46
|
},
|
|
45
47
|
|
|
48
|
+
// skip space chars, return first non-space character
|
|
49
|
+
space = cc => { while ((cc = cur.charCodeAt(idx)) <= SPACE) idx++; return cc },
|
|
50
|
+
|
|
46
51
|
// parse identifier (configurable)
|
|
47
52
|
id = parse.id = c =>
|
|
48
53
|
(c >= 48 && c <= 57) || // 0..9
|
|
@@ -51,9 +56,6 @@ export let idx, cur,
|
|
|
51
56
|
c == 36 || c == 95 || // $, _,
|
|
52
57
|
(c >= 192 && c != 215 && c != 247), // any non-ASCII
|
|
53
58
|
|
|
54
|
-
// skip space chars, return first non-space character
|
|
55
|
-
space = parse.space = cc => { while ((cc = cur.charCodeAt(idx)) <= SPACE) idx++; return cc },
|
|
56
|
-
|
|
57
59
|
// operator/token lookup table
|
|
58
60
|
// lookup[0] is id parser to let configs redefine it
|
|
59
61
|
lookup = [],
|
|
@@ -68,9 +70,17 @@ export let idx, cur,
|
|
|
68
70
|
l = op.length,
|
|
69
71
|
prev = lookup[c],
|
|
70
72
|
word = op.toUpperCase() !== op // make sure word boundary comes after word operator
|
|
71
|
-
) => lookup[c] = (a, curPrec, from = idx) =>
|
|
72
|
-
(
|
|
73
|
-
|
|
73
|
+
) => lookup[c] = (a, curPrec, curOp, from = idx) =>
|
|
74
|
+
(
|
|
75
|
+
(curOp ?
|
|
76
|
+
op == curOp :
|
|
77
|
+
((l < 2 || cur.substr(idx, l) == op) && (curOp = op)) // save matched op to avoid mismatches like `|` as part of `||`
|
|
78
|
+
) &&
|
|
79
|
+
curPrec < prec && // matches precedence AFTER operator matched
|
|
80
|
+
!(word && parse.id(cur.charCodeAt(idx + l))) && // finished word, not part of bigger word
|
|
81
|
+
(idx += l, map(a) || (idx = from, !prev && err())) // throw if operator didn't detect usage pattern: (a;^b) etc
|
|
82
|
+
) ||
|
|
83
|
+
prev?.(a, curPrec, curOp),
|
|
74
84
|
|
|
75
85
|
// right assoc is indicated by negative precedence (meaning go from right to left)
|
|
76
86
|
binary = (op, prec, right = false) => token(op, prec, (a, b) => a && (b = expr(prec - (right ? .5 : 0))) && [op, a, b]),
|
|
@@ -78,15 +88,16 @@ export let idx, cur,
|
|
|
78
88
|
// post indicates postfix rather than prefix operator
|
|
79
89
|
unary = (op, prec, post) => token(op, prec, a => post ? (a && [op, a]) : (!a && (a = expr(prec - .5)) && [op, a])),
|
|
80
90
|
|
|
81
|
-
// skips means ,,, ;;; are allowed
|
|
82
|
-
nary = (op, prec) => {
|
|
83
|
-
token(op, prec,
|
|
84
|
-
(b
|
|
85
|
-
|
|
86
|
-
(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
// FIXME: skips means ,,, ;;; are allowed
|
|
92
|
+
nary = (op, prec, skips) => {
|
|
93
|
+
token(op, prec,
|
|
94
|
+
(a, b) => (
|
|
95
|
+
b = expr(prec),
|
|
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
|
|
99
|
+
a
|
|
100
|
+
))
|
|
90
101
|
)
|
|
91
102
|
},
|
|
92
103
|
|
package/subscript.js
CHANGED
|
@@ -6,13 +6,14 @@ import './feature/string.js'
|
|
|
6
6
|
import './feature/call.js'
|
|
7
7
|
import './feature/access.js'
|
|
8
8
|
import './feature/group.js'
|
|
9
|
+
import './feature/assign.js'
|
|
9
10
|
import './feature/mult.js'
|
|
10
11
|
import './feature/add.js'
|
|
11
12
|
import './feature/increment.js'
|
|
12
13
|
import './feature/bitwise.js'
|
|
13
14
|
import './feature/compare.js'
|
|
15
|
+
import './feature/shift.js'
|
|
14
16
|
import './feature/logic.js'
|
|
15
|
-
import './feature/assign.js'
|
|
16
17
|
import compile from './src/compile.js'
|
|
17
18
|
import parse from './src/parse.js'
|
|
18
19
|
|
package/subscript.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let t,r,e=e=>(t=0,r=e,e=
|
|
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};
|