-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
790 lines (728 loc) · 19.4 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
"use strict"
/**
* Class of S-Expression resolver that includes parser, serializer, tree
* constructors, and tree walker utilities.
*
* Creates an instance of SExpr. Optional `options` input for configuring
* default behavior, such as how to recognize null, boolean values as it's up to
* the programmer to decide the syntax. Nevertheless, here is the default that
* you can override.
* ```javascript
* {
* truthy: ['true', '#t'],
* falsy: ['false', '#f'],
* nully: ['null', '#nil']
* }
* ```
*/
class SExpr {
logVerbose = null
logTrace = null
Type = {
Atom: null,
}
/**
*
* @param {*} [options={}]
*/
constructor(options = {}) {
// /**
// * Public field for programmers to store arbitrary data that might be useful
// * for parsing expressions
// * @public
// * @default {}
// */
// this.context = {}
this.truthy = ["true", "#t"] // => true
this.truthy = options.truthy || this.truthy
this.falsy = ["false", "#f"] // => false
this.falsy = options.falsy || this.falsy
this.nully = ["null", "#nil"] // => null
this.nully = options.nully || this.nully
this.ATOM = "[ ATOM ]"
this.BOOLEAN = "[ BOOLEAN ]"
this.NUMBER = "[ NUMBER ]"
this.STRING = "[ STRING ]"
this.EMPTY = "[ EMPTY ]"
this.FUNCTION = "[ FUNCTION ]"
this.NULL = "[ NULL ]"
this.ROOT = "[ ROOT ]"
this.defaults = {
[this.ATOM]: {
evaluate: async (data, context, state, entity) => {
return { [entity]: data }
},
},
[this.STRING]: {
evaluate: async (data, context, state, entity) => {
return data[entity]
},
},
[this.NUMBER]: {
evaluate: async (data, context, state, entity) => {
return data[entity]
},
},
[this.BOOLEAN]: {
evaluate: async (data, context, state, entity) => {
return data[entity]
},
},
[this.NULL]: {
evaluate: async (data, context, state, entity) => {
// console.data[entity]
// process.exit()
return data[entity]
},
},
[this.FUNCTION]: {
evaluate: async (data, context, state, entity) => {
return {
[entity]: data,
}
},
},
}
// console.dir(this.defaults)
}
findContext(context, name, base) {
const handlers = context.handlers
const defaults = context.defaults
if (base === undefined) {
base = name
}
let ctx
if (handlers) {
ctx = handlers[name] || handlers[base]
}
if (defaults) {
ctx = ctx || defaults[name] || defaults[base]
}
ctx = ctx || {}
return ctx
}
/**
* interpret a parsed expression tree (AST) into data structures in according
* to a notation type, currently just "functional" notation which is similar
* to LISP dialects such as CLIPS, Clojure, Scheme, Racket, etc.
*
* @param {*} E
* @return {*}
*/
async interpret(
expression,
context = {},
state = { scoped: [], globals: {} },
entity = this.ROOT
) {
if (context.defaults === undefined) {
context.defaults = this.defaults
}
let components = []
if (context.notation === undefined || context.notation === "functional") {
for (let e of expression) {
if (this.isExpression(e)) {
const entity = this.first(e)
if (this.isMissing(entity)) {
// components.push({ "": [] })
components.push({})
} else if (this.isAtom(entity)) {
// console.dir(entity)
// process.exit()
const handler = entity.toUpperCase()
const handlerContext = this.findContext(
context,
handler,
this.FUNCTION
)
if (handlerContext.defaults === undefined) {
handlerContext.defaults = context.defaults
}
const handlerState = { ...state }
handlerState.scoped = [...state.scoped]
handlerState.scoped.push({})
// console.log(`rest = ${JSON.stringify(this.rest(e))}`)
const result = await this.interpret(
this.rest(e),
handlerContext,
handlerState,
entity
)
// console.dir(result)
components.push(result)
// TODO: validate parameters using avj
// components.push({
// [name]: params,
// })
} else {
throw new Error(
`Invalid AST for functional notation ${JSON.stringify(e)}`
)
}
} else if (this.isAtom(e)) {
const handlerContext = this.findContext(context, this.ATOM)
if (handlerContext.evaluate) {
let evaluated = await handlerContext.evaluate(
e,
context,
state,
this.ATOM
)
components.push(evaluated)
} else {
throw new Error("can't evaluate " + JSON.stringify(e))
// components.push(entity)
}
// components.push({ [this.ATOM]: e })
} else {
let entity = ""
if (this.isNumber(e)) {
entity = this.NUMBER
} else if (this.isBoolean(e)) {
entity = this.BOOLEAN
} else if (this.isString(e)) {
entity = this.STRING
} else if (this.isNull(e)) {
entity = this.NULL
} else {
throw new Error("unsupported value type: " + JSON.stringify(e))
}
const handlerContext = this.findContext(context, entity)
const value = { [entity]: this.valueOf(e) }
if (handlerContext.evaluate) {
const evaluated = await handlerContext.evaluate(
value,
context,
state,
entity
)
components.push(evaluated) // normal form / self evaluated, e.g. string, number, boolean
} else {
throw new Error("can't evaluate " + JSON.stringify(value))
// components.push(entity)
}
//
// components.push({ [key]: this.valueOf(e) }) // normal form / self evaluated, e.g. string, number, boolean
}
}
} else if (context.notation === null) {
components = expression
} else {
throw new Error("unsupported notation: " + context.notation)
}
if (context.evaluate) {
return await context.evaluate(components, context, state, entity)
} else {
const handlerContext = this.findContext(context, entity, this.FUNCTION)
if (handlerContext.evaluate) {
return await handlerContext.evaluate(components, context, state, entity)
} else {
throw new Error(
`can't evaluate '${entity}' with arguments ${JSON.stringify(
components
)}`
)
}
}
}
/**
* strip comments from S-expression string
* @param {string} str code which might have comments
* @returns {string} code without comments
*/
stripComments(str) {
// (#\|[^|]*\|+(?:[^#|][^|]*\|+)*#|;[^"\n\r]*(?:"[^"\n\r]*"[^"\n\r]*)*[\r\n])|("[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[\S\s][^;#"'\\]*)
/*
Regex modified from https://stackoverflow.com/a/58784551
Comments are captured into group 1, i.e. $1
Non-comments are captured into group 2, i.e. $2
*/
const regex = new RegExp(
/(#\|[^|]*\|+(?:[^#|][^|]*\|+)*#|;[^"\n\r]*(?:"[^"\n\r]*"[^"\n\r]*)*[\r\n])|("[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[\S\s][^;#"'\\]*)/g
)
// console.dir(regex.exec(str))
str = str.replaceAll(regex, "$2")
return str
}
/**
* Parse a S-expression string into a JSON object representing an expression
* tree
*
* @param {string} str S-expression string
* @param {*} [opts = { includedRootParentheses: true }] deserializing options
* @returns {json} an expression tree in form of list that can include nested
* lists similar to the structure of the input S-expression
* @ref improved on: https://rosettacode.org/wiki/S-expressions#JavaScript
*/
parse(str, opts = { includedRootParentheses: true }) {
// TODO: consider handle try/catch here to report error message
if (!opts.includedRootParentheses) {
str = `(\n${str}\n)` // parsing logic requires 1 root expression
}
str = this.stripComments(str + "\n")
// const t = str.match(/\s*("[^"]*"|\(|\)|"|[^\s()"]+)/g)
const t = str.match(/\s*("[^"\\]*(?:\\[\s\S][^"\\]*)*"|\(|\)|"|[^\s()"]+)/g)
// console.log(JSON.stringify(t, null, 4))
let o, c, i
for (o, c = 0, i = t.length - 1; i >= 0; i--) {
let n,
ti = t[i].trim()
if (ti == '"') return
else if (ti == "(") (t[i] = "["), (c += 1)
else if (ti == ")") (t[i] = "]"), (c -= 1)
else if ((n = +ti) == ti) t[i] = n
else
t[i] =
"'" +
ti
.replaceAll("\\", "\\\\")
.replaceAll("\r", "\\\\r")
.replaceAll("\n", "\\\\n")
.replaceAll("\t", "\\\\t")
.replaceAll("'", "\\'") +
"'"
if (i > 0 && ti != "]" && t[i - 1].trim() != "(") t.splice(i, 0, ",")
if (!c)
if (!o) o = true
else return
}
return c ? undefined : eval(t.join(""))
}
/**
* Serialize an expression tree into an S-expression string
*
* @param {*} ast parsed expression (abstract syntax tree)
* @param {*} [opts = { includingRootParentheses: true }] serializing options
* @return {*}
*/
serialize(ast, opts = { includingRootParentheses: true }, level = 0) {
// TODO: add pretty option for indentation
let forms = []
for (let e of ast) {
if (this.isExpression(e)) {
forms.push(this.serialize(e, opts, level + 1))
} else {
forms.push(e)
}
}
if (level === 0 && !opts.includingRootParentheses) {
return forms.join(" ")
} else {
return `(${forms.join(" ")})`
}
}
/**
* Create an identifier symbol
* @example
* const S = new SExpr()
* const node = S.expression(S.identifier('a'))
* // ['a']
* @param {string} id
* @return {string} symbol
*/
identifier(id) {
return id
}
/**
* Check if a node is an identifier, optionally compare to a given name
* @example
* const S = new SExpr()
* const node = S.expression(S.identifier('a'))
* console.log(S.isAtom(S.first(node)))
* // true
* console.log(S.isAtom(S.first(node, 'a')))
* // true
* @param {any} e a node to check
* @param {string} [id=undefined] optional id name to compare to
* @return {boolean} true if it is an identifier
*/
isAtom(e, id = undefined) {
const isId =
e &&
!this.isExpression(e) &&
!this.isNumber(e) &&
!this.isString(e) &&
!this.isBoolean(e) &&
!this.isNull(e)
if (id) {
return isId && e === id
}
return isId
}
// isEqual (a, b) {
// return deepEqual(a, b, { strict: true })
// }
/**
* Compare whether 2 nodes are identical
*
* @param {any} a a node
* @param {any} b another node to compare to
* @return {boolean} true if they are the same
*/
isEqual(a, b) {
const aArray = Array.isArray(a)
const bArray = Array.isArray(b)
if (aArray != bArray) {
return false
}
if (!aArray) {
return a === b
}
if (a.length != b.length) {
return false
}
for (let i in a) {
if (!this.isEqual(a[i], b[i])) {
return false
}
}
return true
}
/**
* Create an expression node
*
* @param {rest} exps optional initialization list of elements
* @return {json} a tree node
*/
expression(...exps) {
if (exps) {
return [...exps]
}
return []
}
/**
* Check if a node is an expression, and optionally compare to a given
* expression
*
* @param {any} e a node to check whether it's an expression
* @param {json} [s=undefined] optional expression to compare to
* @return {boolean} true if it's an expression (and equals the compared
* expression if provided)
*/
isExpression(e, s = undefined) {
const isExpr = Array.isArray(e)
if (s) {
return isExpr && isEqual(e, s)
}
return isExpr
}
/**
* Create a boolean node with given state
*
* @param {boolean} v boolean value
* @return {string} a node with name corresponding to a boolean value
*/
boolean(v) {
if (v) {
return `${this.truthy[0]}`
} else {
return `${this.falsy[0]}`
}
}
/**
* Check if a node is a boolean value, optionally compare to a given state
*
* @param {any} e a node to check whether it's a boolean
* @param {boolean} [b=undefined] optional state to compare to
* @return {boolean} true if it's a boolean (and equals the given state if
* provided)
*/
isBoolean(e, b = undefined) {
if (typeof e === "string" || e instanceof String) {
for (let t of this.truthy) {
if (e === t) {
if (b != undefined) {
return b == true
}
return true
}
}
for (let f of this.falsy) {
if (e === f) {
if (b != undefined) {
return b == false
}
return true
}
}
}
return false
}
/**
* Check if a node is considered truthy. Anything but an explicit false value
* is truthy.
*
* @param {any} e a node to check if it's truthy
* @return {boolean} true if it's truthy
*/
isTruthy(e) {
if (typeof e == "string" || e instanceof String) {
for (let f of this.falsy) {
if (e === f) {
return false
}
}
}
return true
}
/**
* Check if a node doesn't exist, a.k.a undefined
* @param {any} e a node to check if it doesn't exist
* @returns {boolean} true if it doesn't exist (undefined)
*/
isMissing(e) {
return e === undefined
}
/**
* Create a null node.
*
* @return {string} a node with name representing null value
*/
null() {
return `${this.nully[0]}`
}
/**
* Check if a node is null.
*
* @param {any} e a node to check if it's null
* @return {boolean} true if it's null
*/
isNull(e) {
if (typeof e == "string" || e instanceof String) {
for (let n of this.nully) {
if (e === n) {
return true
}
}
}
return false
}
/**
* Create a number node
*
* @param {number} n value of the new node
* @return {number} a node with number value
*/
number(n) {
return n
}
/**
* Check if a node is a number
*
* @param {any} e a node to check if it's a number, optionally compare to a given value
* @param {number} [n=undefined] an optional value to compare to
* @return {boolean} true if it's a number (and equals the given value if provided)
*/
isNumber(e, n) {
const isNum = typeof e === "number" || e instanceof Number
if (n != undefined) {
return isNum && isEqual(e, n)
}
return isNum
}
/**
* Create a string node.
*
* @param {string} str string value of the node
* @return {string} a node with string value
*/
string(str) {
return `"${str}"`
}
/**
* Check if a node is a string, optionally compare to a given string.
*
* @param {any} e a node to check if it's a string
* @param {string} [s=undefined] optional string to compare to
* @return {*} true if it's a string (and equals the given string if provided)
*/
isString(e, s) {
const isStr = (typeof e === "string" || e instanceof String) && e[0] === '"'
if (s != undefined) {
return isStr && isEqual(e, s)
}
return isStr
}
/**
* Get a value content of a symbol (not expression).
*
* @param {any} e a node to extract value
* @return {any} value
*/
valueOf(e) {
if (typeof e === "string" || e instanceof String) {
// maybe quoted string
if (e[0] === '"') {
// return e.slice(1, e.length - 1)
// console.log(e)
// console.log(JSON.parse(e))
return JSON.parse(e)
}
// maybe null
for (let n of this.nully) {
if (e === n) {
return null
}
}
// maybe boolean
for (let t of this.truthy) {
if (e === t) {
return true
}
}
for (let f of this.falsy) {
if (e === f) {
return false
}
}
}
// maybe number or identifier
return e
}
/**
* Get the 1st child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
first(e) {
if (Array.isArray(e)) {
return e[0]
}
return undefined
}
/**
* Get the 2nd child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
second(e) {
if (Array.isArray(e)) {
return e[1]
}
return undefined
}
/**
* Get the 3rd child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
third(e) {
if (Array.isArray(e)) {
return e[2]
}
return undefined
}
/**
* Get the 4th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
fourth(e) {
if (Array.isArray(e)) {
return e[3]
}
return undefined
}
/**
* Get the 5th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
fifth(e) {
if (Array.isArray(e)) {
return e[4]
}
return undefined
}
/**
* Get the 6th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
sixth(e) {
if (Array.isArray(e)) {
return e[5]
}
return undefined
}
/**
* Get the 7th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
seventh(e) {
if (Array.isArray(e)) {
return e[6]
}
return undefined
}
/**
* Get the 8th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
eighth(e) {
if (Array.isArray(e)) {
return e[7]
}
return undefined
}
/**
* Get the 9th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
ninth(e) {
if (Array.isArray(e)) {
return e[8]
}
return undefined
}
/**
* Get the 10th child of a node.
*
* @param {any} e a node to get its child
* @return {any} a child node if exists
*/
tenth(e) {
if (Array.isArray(e)) {
return e[9]
}
return undefined
}
/**
* Get the n-th child of a node. Similar to the shorthand `first`, `second`, `third`, `fourth`, `fifth` ... `tenth`, but at any position provided.
*
* @param {any} e a node to get its child
* @param {number} n position of the child node, starting from 1
* @return {any} a child node if exists
*/
nth(e, n) {
if (Array.isArray(e)) {
return e[n - 1]
}
return undefined
}
/**
* Skip the first child node and get the rest
*
* @param {any} e a node to get its child
* @return {any} the rest of the nodes or undefined if the input node is not an expression
*/
rest(e) {
if (this.isExpression(e)) {
return e.slice(1)
}
return undefined
}
}
module.exports = SExpr