-
Notifications
You must be signed in to change notification settings - Fork 4
/
justin.js
48 lines (40 loc) · 1.91 KB
/
justin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// no-keywords js, just in https://github.com/endojs/Jessie/issues/66
import { err, token, binary, unary } from './src/parse.js'
import compile, { operator, prop } from './src/compile.js'
import subscript from './subscript.js'
import './feature/comment.js'
import './feature/pow.js'
import './feature/ternary.js'
import './feature/bool.js'
import './feature/array.js'
import './feature/object.js'
import './feature/arrow.js'
import './feature/optional.js'
import './feature/spread.js'
import { PREC_ASSIGN, PREC_EQ, PREC_LOR, PREC_COMP } from './src/const.js'
binary('in', PREC_COMP), operator('in', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) in b(ctx)))
// register !==, ===
binary('===', PREC_EQ), binary('!==', 9)
operator('===', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) === b(ctx)))
operator('!==', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) !== b(ctx)))
// add nullish coalescing
binary('??', PREC_LOR)
operator('??', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) ?? b(ctx)))
binary('??=', PREC_ASSIGN, true)
operator('??=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] ??= b(ctx)))))
// complete logical assignments
binary('||=', PREC_ASSIGN, true)
operator('||=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] ||= b(ctx)))))
binary('&&=', PREC_ASSIGN, true)
operator('&&=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] &&= b(ctx)))))
// unsigned shift
binary('>>>', PREC_EQ)
operator('>>>', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) >>> b(ctx)))
binary('>>>=', PREC_ASSIGN, true)
operator('>>>=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] >>>= b(ctx)))))
// add JS literals
token('undefined', 20, a => a ? err() : [, undefined])
token('NaN', 20, a => a ? err() : [, NaN])
token('null', 20, a => a ? err() : [, null])
export default subscript
export * from './subscript.js'