functionalscript 0.0.467 → 0.0.468

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 CHANGED
@@ -1,21 +1,64 @@
1
- const { todo } = require('../dev/module.f.cjs')
2
1
  const list = require('../types/list/module.f.cjs')
2
+ const { equal, isEmpty, fold, toArray, scan } = list
3
3
  const byteSet = require('../types/byte_set/module.f.cjs')
4
+ const { toRangeMap } = byteSet
5
+ const sortedSet = require('../types/sorted_set/module.f.cjs')
6
+ const { intersect, union } = sortedSet
7
+ const rangeMap = require('../types/range_map/module.f.cjs')
8
+ const { merge } = rangeMap
9
+ const { unsafeCmp } = require('../types/function/compare/module.f.cjs')
10
+ const operator = require("../types/function/operator/module.f.cjs")
11
+ const { strictEqual } = operator
12
+ const { stringify } = require('../json/module.f.cjs')
13
+ const { identity } = require('../types/function/module.f.cjs')
4
14
 
5
15
  /** @typedef {readonly[string, byteSet.ByteSet, string]} Rule */
6
16
 
7
17
  /** @typedef {list.List<Rule>} Grammar */
8
18
 
9
- /** @typedef {readonly string[]} ByteMap */
10
-
11
19
  /**
12
20
  * @typedef {{
13
- * readonly[state in string]: ByteMap
21
+ * readonly[state in string]: rangeMap.RangeMapArray<string>
14
22
  * }} Dfa
15
23
  */
16
24
 
25
+ const stringifyIdentity = stringify(identity)
26
+
27
+ /** @type {rangeMap.Operators<sortedSet.SortedSet<string>>} */
28
+ const mergeOp = { union: union(unsafeCmp), equal: equal(strictEqual) }
29
+
30
+ /** @type {(s: string) => (set: sortedSet.SortedSet<string>) => boolean} */
31
+ const hasState = s => set => !isEmpty(intersect(unsafeCmp)([s])(set))
32
+
33
+ /** @type {(set: sortedSet.SortedSet<string>) => operator.Fold<Rule, rangeMap.RangeMap<sortedSet.SortedSet<string>>>} */
34
+ const foldOp = set => ([ruleIn, bs, ruleOut]) => rm => {
35
+ if (hasState(ruleIn)(set)) { return merge(mergeOp)(rm)(toRangeMap(bs)(ruleOut)) }
36
+ return rm
37
+ }
38
+
39
+ /** @type {operator.Scan<rangeMap.Entry<sortedSet.SortedSet<string>>, rangeMap.Entry<string>>} */
40
+ const stringifyOp = ([sortedSet, max]) => [[stringifyIdentity(sortedSet), max], stringifyOp]
41
+
42
+ const scanStringify = scan(stringifyOp)
43
+
44
+ /** @type {operator.Scan<rangeMap.Entry<sortedSet.SortedSet<string>>, sortedSet.SortedSet<string>>} */
45
+ const fetchOp = ([item, _]) => [item, fetchOp]
46
+
47
+ const scanFetch = scan(fetchOp)
48
+
49
+ /** @type {(grammar: Grammar) => operator.Fold<sortedSet.SortedSet<string>, Dfa>} */
50
+ const addEntry = grammar => set => dfa => {
51
+ const s = stringifyIdentity(set)
52
+ if (s in dfa) { return dfa }
53
+ const setMap = fold(foldOp(set))(undefined)(grammar)
54
+ const stringMap = toArray(scanStringify(setMap))
55
+ const newDfa = { ...dfa, [s]: stringMap }
56
+ const newStates = scanFetch(setMap)
57
+ return fold(addEntry(grammar))(newDfa)(newStates)
58
+ }
59
+
17
60
  /** @type {(grammar: Grammar) => Dfa} */
18
- const dfa = grammar => todo()
61
+ const dfa = grammar => addEntry(grammar)([''])({})
19
62
 
20
63
  module.exports = {
21
64
  /** @readonly */
package/fsm/test.f.cjs CHANGED
@@ -1,5 +1,79 @@
1
1
  const _ = require('./module.f.cjs')
2
+ const byteSet = require('../types/byte_set/module.f.cjs')
3
+ const { sort, fromEntries } = require('../types/object/module.f.cjs')
4
+ const json = require('../json/module.f.cjs')
5
+ const { identity } = require('../types/function/module.f.cjs')
6
+
7
+ /** @type {(c: string) => number} */
8
+ const toCharCode = c => c.charCodeAt(0)
2
9
 
3
10
  module.exports = {
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('.'))
20
+
21
+ /** @type {_.Grammar} */
22
+ const grammar = [
23
+ ['', digit, 'int'],
24
+ ['int', digit, 'int'],
25
+ ['', digit, 'floatBegin'],
26
+ ['floatBegin', digit, 'floatBegin'],
27
+ ['floatBegin', dot, 'floatDot'],
28
+ ['floatDot', digit, 'float'],
29
+ ['float', digit, 'float'],
30
+ ['', idBegin, 'id'],
31
+ ['id', idNext, 'id']
32
+ ]
33
+
34
+ const dfa = _.dfa(grammar)
35
+ const entries = Object.entries(dfa)
36
+ const sortedEntries = sort(entries)
37
+ const obj = fromEntries(sortedEntries)
38
+ const result = json.stringify(identity)(obj)
39
+
40
+ const expectedObj = {
41
+ '[""]': [
42
+ [ '[]', 35 ],
43
+ [ '["id"]', 36 ],
44
+ [ '[]', 47 ],
45
+ [ '["floatBegin","int"]', 57 ],
46
+ [ '[]', 64 ],
47
+ [ '["id"]', 90 ],
48
+ [ '[]', 94 ],
49
+ [ '["id"]', 95 ],
50
+ [ '[]', 96 ],
51
+ [ '["id"]', 122 ]
52
+ ],
53
+ '["float"]': [ [ '[]', 47 ], [ '["float"]', 57 ] ],
54
+ '["floatBegin","int"]': [
55
+ [ '[]', 45 ],
56
+ [ '["floatDot"]', 46 ],
57
+ [ '[]', 47 ],
58
+ [ '["floatBegin","int"]', 57 ]
59
+ ],
60
+ '["floatDot"]': [ [ '[]', 47 ], [ '["float"]', 57 ] ],
61
+ '["id"]': [
62
+ [ '[]', 35 ],
63
+ [ '["id"]', 36 ],
64
+ [ '[]', 47 ],
65
+ [ '["id"]', 57 ],
66
+ [ '[]', 64 ],
67
+ [ '["id"]', 90 ],
68
+ [ '[]', 94 ],
69
+ [ '["id"]', 95 ],
70
+ [ '[]', 96 ],
71
+ [ '["id"]', 122 ]
72
+ ],
73
+ '[]': []
74
+ };
75
+ const expectedResult = json.stringify(identity)(expectedObj)
4
76
 
77
+ if (result !== expectedResult) {throw result }
78
+ }
5
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.467",
3
+ "version": "0.0.468",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -72,6 +72,8 @@ module.exports = {
72
72
  /** @readonly */
73
73
  unset,
74
74
  /** @readonly */
75
+ one,
76
+ /** @readonly */
75
77
  union,
76
78
  /** @readonly */
77
79
  setRange,
@@ -15,6 +15,11 @@ const operator = require("../function/operator/module.f.cjs")
15
15
  * @typedef {sortedList.SortedList<Entry<T>>} RangeMap
16
16
  */
17
17
 
18
+ /**
19
+ * @template T
20
+ * @typedef {readonly Entry<T>[]} RangeMapArray
21
+ */
22
+
18
23
  /**
19
24
  * @template T
20
25
  * @typedef {{