functionalscript 0.0.473 → 0.0.474
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/fsm/module.f.cjs +14 -2
- package/fsm/test.f.cjs +59 -26
- package/package.json +1 -1
- package/types/range_map/module.f.cjs +26 -1
- package/types/range_map/test.f.cjs +50 -0
package/fsm/module.f.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const list = require('../types/list/module.f.cjs')
|
|
2
|
-
const { equal, isEmpty, fold, toArray, scan } = list
|
|
2
|
+
const { equal, isEmpty, fold, toArray, scan, foldScan } = list
|
|
3
3
|
const byteSet = require('../types/byte_set/module.f.cjs')
|
|
4
4
|
const { toRangeMap } = byteSet
|
|
5
5
|
const sortedSet = require('../types/sorted_set/module.f.cjs')
|
|
@@ -57,10 +57,22 @@ const addEntry = grammar => set => dfa => {
|
|
|
57
57
|
return fold(addEntry(grammar))(newDfa)(newStates)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
const initialState = ['']
|
|
61
|
+
|
|
62
|
+
const initialStateStringify = stringifyIdentity(initialState)
|
|
63
|
+
|
|
60
64
|
/** @type {(grammar: Grammar) => Dfa} */
|
|
61
|
-
const dfa = grammar => addEntry(grammar)(
|
|
65
|
+
const dfa = grammar => addEntry(grammar)(initialState)({})
|
|
66
|
+
|
|
67
|
+
/** @type {(dfa: Dfa) => operator.Fold<number, string>} */
|
|
68
|
+
const runOp = dfa => input => s => rangeMap.get(input)(dfa[s])
|
|
69
|
+
|
|
70
|
+
/** @type {(dfa: Dfa) => (input: list.List<number>) => list.List<string>} */
|
|
71
|
+
const run = dfa => input => foldScan(runOp(dfa))(initialStateStringify)(input)
|
|
62
72
|
|
|
63
73
|
module.exports = {
|
|
64
74
|
/** @readonly */
|
|
65
75
|
dfa,
|
|
76
|
+
/** @readonly */
|
|
77
|
+
run
|
|
66
78
|
}
|
package/fsm/test.f.cjs
CHANGED
|
@@ -3,39 +3,45 @@ const byteSet = require('../types/byte_set/module.f.cjs')
|
|
|
3
3
|
const { sort, fromEntries } = require('../types/object/module.f.cjs')
|
|
4
4
|
const json = require('../json/module.f.cjs')
|
|
5
5
|
const { identity } = require('../types/function/module.f.cjs')
|
|
6
|
+
const { toArray } = require('../types/list/module.f.cjs')
|
|
6
7
|
|
|
7
8
|
/** @type {(c: string) => number} */
|
|
8
9
|
const toCharCode = c => c.charCodeAt(0)
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
dfa: () => {
|
|
12
|
-
const lowercaseAlpha = byteSet.range([toCharCode('a'), toCharCode('z')])
|
|
13
|
-
const uppercaseAlpha = byteSet.range([toCharCode('A'), toCharCode('Z')])
|
|
14
|
-
const alpha = byteSet.union(lowercaseAlpha)(uppercaseAlpha)
|
|
15
|
-
const idSymbol = byteSet.union(byteSet.one(toCharCode('_')))(byteSet.one(toCharCode('$')))
|
|
16
|
-
const idBegin = byteSet.union(alpha)(idSymbol)
|
|
17
|
-
const digit = byteSet.range([toCharCode('0'), toCharCode('9')])
|
|
18
|
-
const idNext = byteSet.union(idBegin)(digit)
|
|
19
|
-
const dot = byteSet.one(toCharCode('.'))
|
|
11
|
+
const stringifyIdentity = json.stringify(identity)
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
13
|
+
const buildDfa = () => {
|
|
14
|
+
const lowercaseAlpha = byteSet.range([toCharCode('a'), toCharCode('z')])
|
|
15
|
+
const uppercaseAlpha = byteSet.range([toCharCode('A'), toCharCode('Z')])
|
|
16
|
+
const alpha = byteSet.union(lowercaseAlpha)(uppercaseAlpha)
|
|
17
|
+
const idSymbol = byteSet.union(byteSet.one(toCharCode('_')))(byteSet.one(toCharCode('$')))
|
|
18
|
+
const idBegin = byteSet.union(alpha)(idSymbol)
|
|
19
|
+
const digit = byteSet.range([toCharCode('0'), toCharCode('9')])
|
|
20
|
+
const idNext = byteSet.union(idBegin)(digit)
|
|
21
|
+
const dot = byteSet.one(toCharCode('.'))
|
|
22
|
+
|
|
23
|
+
/** @type {_.Grammar} */
|
|
24
|
+
const grammar = [
|
|
25
|
+
['', digit, 'int'],
|
|
26
|
+
['int', digit, 'int'],
|
|
27
|
+
['', digit, 'floatBegin'],
|
|
28
|
+
['floatBegin', digit, 'floatBegin'],
|
|
29
|
+
['floatBegin', dot, 'floatDot'],
|
|
30
|
+
['floatDot', digit, 'float'],
|
|
31
|
+
['float', digit, 'float'],
|
|
32
|
+
['', idBegin, 'id'],
|
|
33
|
+
['id', idNext, 'id']
|
|
34
|
+
]
|
|
35
|
+
return _.dfa(grammar)
|
|
36
|
+
}
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
module.exports = {
|
|
39
|
+
dfa: () => {
|
|
40
|
+
const dfa = buildDfa()
|
|
35
41
|
const entries = Object.entries(dfa)
|
|
36
42
|
const sortedEntries = sort(entries)
|
|
37
43
|
const obj = fromEntries(sortedEntries)
|
|
38
|
-
const result =
|
|
44
|
+
const result = stringifyIdentity(obj)
|
|
39
45
|
|
|
40
46
|
const expectedObj = {
|
|
41
47
|
'[""]': [
|
|
@@ -72,8 +78,35 @@ module.exports = {
|
|
|
72
78
|
],
|
|
73
79
|
'[]': []
|
|
74
80
|
};
|
|
75
|
-
const expectedResult =
|
|
81
|
+
const expectedResult = stringifyIdentity(expectedObj)
|
|
76
82
|
|
|
77
83
|
if (result !== expectedResult) {throw result }
|
|
78
|
-
}
|
|
84
|
+
},
|
|
85
|
+
run: [
|
|
86
|
+
() => {
|
|
87
|
+
const dfa = buildDfa()
|
|
88
|
+
const input = [toCharCode('a'), toCharCode('1')]
|
|
89
|
+
const result = stringifyIdentity(toArray(_.run(dfa)(input)))
|
|
90
|
+
|
|
91
|
+
const expectedOutput = [
|
|
92
|
+
'["id"]',
|
|
93
|
+
'["id"]'
|
|
94
|
+
]
|
|
95
|
+
const expectedResult = stringifyIdentity(expectedOutput)
|
|
96
|
+
if (result !== expectedResult) { throw result }
|
|
97
|
+
},
|
|
98
|
+
() => {
|
|
99
|
+
const dfa = buildDfa()
|
|
100
|
+
const input = [toCharCode('0'), toCharCode('.'), toCharCode('1')]
|
|
101
|
+
const result = stringifyIdentity(toArray(_.run(dfa)(input)))
|
|
102
|
+
|
|
103
|
+
const expectedOutput = [
|
|
104
|
+
'["floatBegin","int"]',
|
|
105
|
+
'["floatDot"]',
|
|
106
|
+
'["float"]'
|
|
107
|
+
]
|
|
108
|
+
const expectedResult = stringifyIdentity(expectedOutput)
|
|
109
|
+
if (result !== expectedResult) { throw result }
|
|
110
|
+
},
|
|
111
|
+
]
|
|
79
112
|
}
|
package/package.json
CHANGED
|
@@ -60,7 +60,32 @@ const tailReduce = equal => state => tail => {
|
|
|
60
60
|
/** @type {<T>(op: Operators<T>) => RangeMerge<T>} */
|
|
61
61
|
const merge = ({union, equal}) => genericMerge({reduceOp: reduceOp(union)(equal), tailReduce: tailReduce(equal)})(undefined)
|
|
62
62
|
|
|
63
|
+
/** @type {<T>(value: number) => (rm: RangeMapArray<T>) => T|undefined} */
|
|
64
|
+
const get = value => rm => {
|
|
65
|
+
let b = 0
|
|
66
|
+
let e = rm.length - 1
|
|
67
|
+
while (true) {
|
|
68
|
+
if (b >= rm.length) return undefined
|
|
69
|
+
if (e - b < 0) return rm[b][0]
|
|
70
|
+
const mid = b + (e - b >> 1)
|
|
71
|
+
const sign = cmp(value)(rm[mid][1])
|
|
72
|
+
switch(sign) {
|
|
73
|
+
case -1: {
|
|
74
|
+
e = mid - 1
|
|
75
|
+
break
|
|
76
|
+
}
|
|
77
|
+
case 0: { return rm[mid][0] }
|
|
78
|
+
case 1: {
|
|
79
|
+
b = mid + 1
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
63
86
|
module.exports = {
|
|
64
87
|
/** @readonly */
|
|
65
|
-
merge
|
|
88
|
+
merge,
|
|
89
|
+
/** @readonly */
|
|
90
|
+
get
|
|
66
91
|
}
|
|
@@ -77,5 +77,55 @@ module.exports = {
|
|
|
77
77
|
const result = stringify(list.toArray(merged))
|
|
78
78
|
if (result !== '[[["a"],1],[["a","b"],2],[["a"],5]]') { throw result }
|
|
79
79
|
}
|
|
80
|
+
],
|
|
81
|
+
get: [
|
|
82
|
+
() => {
|
|
83
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
84
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
85
|
+
const result = stringify(_.get(5)(rm))
|
|
86
|
+
if (result !== '["a"]') { throw result }
|
|
87
|
+
},
|
|
88
|
+
() => {
|
|
89
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
90
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
91
|
+
const result = stringify(_.get(10)(rm))
|
|
92
|
+
if (result !== '["a"]') { throw result }
|
|
93
|
+
},
|
|
94
|
+
() => {
|
|
95
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
96
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
97
|
+
const result = stringify(_.get(15)(rm))
|
|
98
|
+
if (result !== '["b"]') { throw result }
|
|
99
|
+
},
|
|
100
|
+
() => {
|
|
101
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
102
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
103
|
+
const result = stringify(_.get(20)(rm))
|
|
104
|
+
if (result !== '["b"]') { throw result }
|
|
105
|
+
},
|
|
106
|
+
() => {
|
|
107
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
108
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
109
|
+
const result = stringify(_.get(25)(rm))
|
|
110
|
+
if (result !== '["c"]') { throw result }
|
|
111
|
+
},
|
|
112
|
+
() => {
|
|
113
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
114
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
115
|
+
const result = stringify(_.get(30)(rm))
|
|
116
|
+
if (result !== '["c"]') { throw result }
|
|
117
|
+
},
|
|
118
|
+
() => {
|
|
119
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
120
|
+
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]]
|
|
121
|
+
const result = _.get(35)(rm)
|
|
122
|
+
if (result !== undefined) { throw result }
|
|
123
|
+
},
|
|
124
|
+
() => {
|
|
125
|
+
/** @type {_.RangeMapArray<sortedSet.SortedSet<string>>} */
|
|
126
|
+
const rm = []
|
|
127
|
+
const result = _.get(10)(rm)
|
|
128
|
+
if (result !== undefined) { throw result }
|
|
129
|
+
}
|
|
80
130
|
]
|
|
81
131
|
}
|