functionalscript 0.0.477 → 0.0.479

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.
@@ -52,13 +52,13 @@ const joinComma = join(', ')
52
52
  /** @type {(name: string) => (lib: types.Library) => text.Block} */
53
53
  const cpp = name => lib => {
54
54
 
55
- /** @type {(t: types.Type) => string|undefined} */
55
+ /** @type {(t: types.Type) => string|null} */
56
56
  const interface_ = t => {
57
57
  if (!(t instanceof Array) || t.length !== 1) {
58
- return undefined
58
+ return null
59
59
  }
60
60
  const [name] = t
61
- return lib[name].interface !== undefined ? name : undefined
61
+ return 'interface' in lib[name] ? name : null
62
62
  }
63
63
 
64
64
  /** @type {(i: (t: string) => string) => (t: types.Type) => string} */
@@ -66,8 +66,8 @@ const cpp = name => lib => {
66
66
  if (typeof (t) === 'string') { return baseType(t) }
67
67
  if (t.length === 2) { return `${type(t[1])} const*` }
68
68
  const [id] = t
69
- if (lib[id].interface === undefined) { return id }
70
- return i(id)
69
+ if ('interface' in lib[id]) { return i(id) }
70
+ return id
71
71
  }
72
72
 
73
73
  const type = objectType(comRef)
@@ -101,7 +101,7 @@ const cpp = name => lib => {
101
101
  const paramArrayStr = `(${joinComma(mapParam(paramL))})`
102
102
  const m = methodHeader(result)(paramArrayStr)
103
103
  const resultName = interface_(paramArray._)
104
- if (resultName === undefined) {
104
+ if (resultName === null) {
105
105
  return [m(name)]
106
106
  }
107
107
  return [
@@ -127,15 +127,15 @@ const cpp = name => lib => {
127
127
  }
128
128
 
129
129
  /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
130
- const def = ([name, d]) => d.interface === undefined
131
- ? struct(name)(defStruct(d))
132
- : [
130
+ const def = ([name, d]) => 'interface' in d
131
+ ? [
133
132
  `class ${name} : public ::nanocom::IUnknown`,
134
133
  '{',
135
134
  'public:',
136
135
  defInterface(d),
137
136
  '};'
138
137
  ]
138
+ : struct(name)(defStruct(d))
139
139
 
140
140
  /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
141
141
  const forward = ([name]) => [`struct ${name};`]
@@ -96,14 +96,13 @@ const flatMapMethod = flatMap(method)
96
96
 
97
97
  /** @type {(e: obj.Entry<types.Definition>) => list.List<text.Item>} */
98
98
  const def = ([n, d]) => {
99
- const i = d.interface
100
- return i === undefined ?
99
+ return !('interface' in d) ?
101
100
  struct(n)(mapField(entries(d.struct))) :
102
101
  typeDef
103
102
  ([`Guid("${d.guid}")`, 'InterfaceType(ComInterfaceType.InterfaceIsIUnknown)'])
104
103
  ('interface')
105
104
  (n)
106
- (flatMapMethod(entries(i)))
105
+ (flatMapMethod(entries(d.interface)))
107
106
  }
108
107
 
109
108
  const flatMapDef = flatMap(def)
@@ -84,7 +84,7 @@ const whereContent = h => ({ where, content }) => {
84
84
 
85
85
  /** @type {(impl: Impl) => text.Block} */
86
86
  const rustImpl = i => {
87
- const p = i.param === undefined ? '' : `<${i.param}>`
87
+ const p = 'param' in i ? `<${i.param}>` : ''
88
88
  const header = `impl${p} ${i.trait} for ${i.type}`
89
89
  return whereContent(header)(i)
90
90
  }
@@ -135,7 +135,7 @@ const rust = library => {
135
135
  if (t.length === 2) { return `*const ${f(ref)(t[1])}` }
136
136
  const [id] = t
137
137
  const fullId = `${p}${id}`
138
- return library[id].interface === undefined ? fullId : o(fullId)
138
+ return 'interface' in library[id] ? o(fullId) : fullId
139
139
  }
140
140
  return f
141
141
  }
@@ -157,8 +157,7 @@ const rust = library => {
157
157
 
158
158
  /** @type {(first: readonly string[]) => (p: types.FieldArray) => string} */
159
159
  const func = first => p => {
160
- const result = p._
161
- const resultStr = result === undefined ? '' : ` -> ${type(super_)(ref)(result)}`
160
+ const resultStr = '_' in p ? ` -> ${type(super_)(ref)(p._)}` : ''
162
161
  const params = commaJoin(flat([first, mapParam(paramList(p))]))
163
162
  return `(${params})${resultStr}`
164
163
  }
@@ -248,7 +247,7 @@ const rust = library => {
248
247
  }
249
248
 
250
249
  /** @type {(type: object.Entry<types.Definition>) => text.Block} */
251
- const def = ([name, type]) => (type.interface === undefined ? struct(type.struct) : interface_(type))(name)
250
+ const def = ([name, type]) => ('interface' in type ? interface_(type) : struct(type.struct))(name)
252
251
 
253
252
  return flat([['#![allow(non_snake_case)]'], flatMap(def)(entries(library))])
254
253
  }
@@ -10,7 +10,6 @@ const { entries } = Object
10
10
 
11
11
  /**
12
12
  * @typedef {{
13
- * readonly interface?: undefined
14
13
  * readonly struct: FieldArray
15
14
  * }} Struct
16
15
  */
@@ -17,15 +17,15 @@
17
17
  // - `localPackageId` examples:
18
18
  // - `functionalscript`
19
19
 
20
- type PackageGet = (packageId: string) => Package | undefined
20
+ type PackageGet = (packageId: string) => Package | null
21
21
 
22
22
  // A local file id is
23
23
 
24
24
  type Package = {
25
25
  // returns a global package id.
26
- readonly dependency: (localPackageId: string) => string | undefined
26
+ readonly dependency: (localPackageId: string) => string | null
27
27
  // returns source of the file.
28
- readonly file: (localFileId: string) => string | undefined
28
+ readonly file: (localFileId: string) => string | null
29
29
  }
30
30
  ```
31
31
 
@@ -38,7 +38,7 @@ type Package = {
38
38
  //
39
39
  // A module is a compiled and initialized source file.
40
40
 
41
- type ModuleGet = (moduleId: string) => ModuleState | undefined
41
+ type ModuleGet = (moduleId: string) => ModuleState | null
42
42
 
43
43
  type ModuleMapInterface<M> = {
44
44
  readonly get: (moduleMap: M) => ModuleGet
@@ -55,8 +55,8 @@ const getOrBuild = compile => packageGet => moduleMapInterface => {
55
55
  /** @type {function_.Require<readonly[map.Map<string>, M]>} */
56
56
  const require_ = p => ([requireMap, m]) => {
57
57
  /** @type {(e: unknown) => function_.Result<readonly[map.Map<string>, M]>} */
58
- const error = e => [['error', 'file not found'], [requireMap, m]]
59
- if (moduleDir === undefined) { return error('file not found') }
58
+ const error = e => [['error', e], [requireMap, m]]
59
+ if (moduleDir === null) { return error('file not found') }
60
60
  const r = parseAndFind(packageGet)(moduleDir)(p)
61
61
  if (r === undefined) { return error('file not found') }
62
62
  const rIdStr = idToString(r.id)
@@ -38,10 +38,10 @@ const object = require('../../types/object/module.f.cjs')
38
38
  * }} Id
39
39
  */
40
40
 
41
- /** @type {(id: Id) => Id | undefined} */
41
+ /** @type {(id: Id) => Id | null} */
42
42
  const dir = id => {
43
43
  const len = id.path.length
44
- if (len < 1) { return undefined }
44
+ if (len < 1) { return null }
45
45
  return {
46
46
  package: id.package,
47
47
  path: id.path.slice(0, len - 1)
package/commonjs/test.cjs CHANGED
@@ -6,7 +6,7 @@ module.exports = {
6
6
  const source = 'module.exports = "Hello world!"'
7
7
  const m = _.compile(source)
8
8
  if (m[0] !== 'ok') { throw m }
9
- const [result] = m[1](() => { throw 0 })(undefined)
9
+ const [result] = m[1](() => { throw 0 })(null)
10
10
  if (result[0] !== 'ok') { throw result }
11
11
  if (result[1] !== 'Hello world!') { throw result }
12
12
  },
@@ -24,7 +24,7 @@ module.exports = {
24
24
  const source = 'a = 5'
25
25
  const m = _.compile(source)
26
26
  if (m[0] !== 'ok') { throw m }
27
- const [result] = m[1](() => { throw 0 })(undefined)
27
+ const [result] = m[1](() => { throw 0 })(null)
28
28
  if (result[0] !== 'error') { throw result }
29
29
  },
30
30
  test: () => {
package/fsc/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # FunctionalScript Compiler
2
+
3
+ ## Tokenizer
4
+
5
+ - `!` - logicalNot
6
+ - `!=` - nonStrictNotEqual
7
+ - `!==` - notEqual
8
+ - `"` - doubleStringBegin
9
+ - `$` - idBegin
10
+ - `%` - remainder
11
+ - `%=` - remainder assignment
12
+ - `&` - bitwiseAnd
13
+ - `&&` - logicalAnd
14
+ - `&&=` - logicalAndAssignment
15
+ - `&=` - bitwiseAndAssignment
16
+ - `'` - singleStringBegin
17
+ - `(` - groupingBegin
18
+ - `)` - groupingEnd
19
+ - `*` - multiplication
20
+ - `**` - exponential
21
+ - `**=` - exponentialAssignment
22
+ - `*/` - commentEnd
23
+ - `*=` - multiplicationAssignment
24
+ - `+` - addition
25
+ - `++` - increment
26
+ - `+=` - additionAssignment
27
+ - `,` - comma
28
+ - `-` - subtraction
29
+ - `--` - decrement
30
+ - `-=` - subtractionAssignment
31
+ - `.` - dot
32
+ - `...` - spread
33
+ - `/` - division
34
+ - `/*` - commentBegin
35
+ - `/=` - divisionAssignment
36
+ - `0..9` - numberBegin
37
+ - `:` - colon
38
+ - `;` - semicolon
39
+ - `<` - less
40
+ - `<<` - leftShift
41
+ - `<<=` - leftShiftAssignment
42
+ - `<=` - lessEqual
43
+ - `=` - assignment
44
+ - `==` - nonStrictEqual ?
45
+ - `===` - equal
46
+ - `=>` - arrow
47
+ - `>` - greater
48
+ - `<=` - greaterEqual
49
+ - `>>` - rightShift
50
+ - `>>>` - unsignedRightShift
51
+ - `>>>=` unsignedRightShiftAssignment
52
+ - `?` - question
53
+ - `?.` - optional chaining
54
+ - `??` - nullish coalescing
55
+ - `A..Z` - idBegin
56
+ - `Infinity` - infinity
57
+ - `NaN` - nan
58
+ - `[` - propertyBegin
59
+ - `]` - propertyEnd`
60
+ - `^` - bitwiseXor
61
+ - `^=` - bitwiseXorAssignment
62
+ - `_` - idBegin
63
+ - '`' - templateBegin
64
+ - `a..z` - idBegin
65
+ - `async` ?
66
+ - `await` ?
67
+ - `break`
68
+ - `case`
69
+ - `catch`
70
+ - `class`
71
+ - `const`
72
+ - `continue`
73
+ - `debugger`
74
+ - `delete` ?
75
+ - `do` ?
76
+ - `else`
77
+ - `export`
78
+ - `exports` <= non-standard
79
+ - `false`
80
+ - `function` ?
81
+ - `globalThis` ?
82
+ - `for`
83
+ - `if`
84
+ - `import`
85
+ - `in`
86
+ - `instanceof`
87
+ - `let`
88
+ - `module` <= non-standard
89
+ - `new` ?
90
+ - `null`
91
+ - `of`
92
+ - `require` <= non-standard
93
+ - `return`
94
+ - `super`
95
+ - `switch`
96
+ - `this` ?
97
+ - `throw`
98
+ - `true`
99
+ - `try`
100
+ - `typeof`
101
+ - `undefined`
102
+ - `var`
103
+ - `void`
104
+ - `while`
105
+ - `yield`
106
+ - `{` - objectBegin
107
+ - `|` - bitwiseOr
108
+ - `|=` - bitwiseOrAssignment
109
+ - `||` - logicalOr
110
+ - `||=` - logicalOrAssignment
111
+ - `}` - objectEnd
112
+ - `~` - bitwiseNot
113
+ - `~=` - bitwiseNotAssignment
package/fsc/module.f.cjs CHANGED
@@ -1,4 +1,118 @@
1
- const ascii = require('../text/ascii/module.f.cjs')
1
+ const operator = require('../types/function/operator/module.f.cjs')
2
+ const range_map = require('../types/range_map/module.f.cjs')
3
+ const { merge: rangeMapMerge, fromRange, get } = range_map
4
+ const { reduce: listReduce } = require('../types/list/module.f.cjs')
5
+ const { range: asciiRange } = require('../text/ascii/module.f.cjs')
6
+ const { fromCharCode } = String
7
+ const { fn } = require('../types/function/module.f.cjs')
8
+ const list = require('../types/list/module.f.cjs')
9
+ const _range = require('../types/range/module.f.cjs')
10
+ const { one } = _range
11
+ const { toArray, map } = list
12
+
13
+ /** @typedef {readonly[readonly string[], ToResult]} Result */
14
+
15
+ /** @typedef {(a: number) => Result} ToResult */
16
+
17
+ /**
18
+ * @template T
19
+ * @typedef {(state: T) => ToResult} CreateToResult
20
+ */
21
+
22
+ /**
23
+ * @template T
24
+ * @typedef {range_map.RangeMapArray<CreateToResult<T>>} State
25
+ */
26
+
27
+ /** @type {ToResult} */
28
+ const unknownSymbol = a => [[`unknown symbol ${a}`], unknownSymbol]
29
+
30
+ /** @type {<T>(a: T) => ToResult} */
31
+ const def = () => unknownSymbol
32
+
33
+ /** @type {<T>(a: CreateToResult<T>) => (b: CreateToResult<T>) => CreateToResult<T>} */
34
+ const union = a => b => {
35
+ if (a === def || a === b) { return b }
36
+ if (b === def) { return a }
37
+ throw [a, b]
38
+ }
39
+
40
+ /** @type {readonly never[]} */
41
+ const empty = []
42
+
43
+ /** @type {<T>(a: list.List<State<T>>) => State<T>} */
44
+ const reduce = a => {
45
+ /** @typedef {typeof a extends list.List<State<infer T>> ? T : never} T */
46
+ /** @type {range_map.RangeMerge<CreateToResult<T>>} */
47
+ const merge = rangeMapMerge({
48
+ union,
49
+ equal: operator.strictEqual,
50
+ })
51
+ return toArray(listReduce(merge)(empty)(a))
52
+ }
53
+
54
+
55
+ const codePointRange = fromRange(def)
56
+
57
+ const range = fn(asciiRange).then(codePointRange).result
58
+
59
+ /** @type {(l: readonly string[]) => <T>(f: CreateToResult<T>) => State<T>} */
60
+ const rangeSet = l => f => {
61
+ /** @typedef {typeof f extends CreateToResult<infer T> ? T : never} T */
62
+ /** @type {(a: _range.Range) => (f: CreateToResult<T>) => State<T>} */
63
+ const codePointRange = fromRange(def)
64
+ /** @type {(r: string) => State<T>} */
65
+ const g = r => codePointRange(asciiRange(r))(f)
66
+ return reduce(map(g)(l))
67
+ }
68
+
69
+ /** @type {<T>(a: list.List<State<T>>) => CreateToResult<T>} */
70
+ const create = a => {
71
+ /** @typedef {typeof a extends list.List<State<infer T>> ? T : never} T */
72
+ const i = reduce(a)
73
+ /** @type {(v: number) => (i: State<T>) => (v: T) => ToResult} */
74
+ const x = get(def)
75
+ return v => c => x(c)(i)(v)(c)
76
+ }
77
+
78
+ const terminal = -1
79
+
80
+ const init = create([
81
+ codePointRange(one(terminal))(() => () => [[], unknownSymbol]),
82
+ rangeSet(['\t', ' '])(() => () => [[' '], unknownSymbol]),
83
+ rangeSet(['\n', '\r'])(() => () => [['\n'], unknownSymbol]),
84
+ range('!')(() => () => [['!'], unknownSymbol]),
85
+ range('"')(() => () => [['"'], unknownSymbol]),
86
+ rangeSet(['$', '_', 'AZ', 'az'])(() => c => [[fromCharCode(c)], unknownSymbol]),
87
+ range('%')(() => () => [['%'], unknownSymbol]),
88
+ range('&')(() => () => [['&'], unknownSymbol]),
89
+ range("'")(() => () => [["'"], unknownSymbol]),
90
+ range('(')(() => () => [['('], unknownSymbol]),
91
+ range(')')(() => () => [[')'], unknownSymbol]),
92
+ range('*')(() => () => [['*'], unknownSymbol]),
93
+ range('+')(() => () => [['+'], unknownSymbol]),
94
+ range(',')(() => () => [[','], unknownSymbol]),
95
+ range('-')(() => () => [['-'], unknownSymbol]),
96
+ range('.')(() => () => [['.'], unknownSymbol]),
97
+ range('/')(() => () => [['/'], unknownSymbol]),
98
+ range('09')(() => a => [[fromCharCode(a)], unknownSymbol]),
99
+ range(':')(() => () => [[':'], unknownSymbol]),
100
+ range(';')(() => () => [[';'], unknownSymbol]),
101
+ range('<')(() => () => [['<'], unknownSymbol]),
102
+ range('=')(() => () => [['='], unknownSymbol]),
103
+ range('>')(() => () => [['>'], unknownSymbol]),
104
+ range('?')(() => () => [['?'], unknownSymbol]),
105
+ range('[')(() => () => [['['], unknownSymbol]),
106
+ range(']')(() => () => [[']'], unknownSymbol]),
107
+ range('^')(() => () => [['^'], unknownSymbol]),
108
+ range('`')(() => () => [['`'], unknownSymbol]),
109
+ range('{')(() => () => [['{'], unknownSymbol]),
110
+ range('|')(() => () => [['|'], unknownSymbol]),
111
+ range('}')(() => () => [['}'], unknownSymbol]),
112
+ range('~')(() => () => [['~'], unknownSymbol]),
113
+ ])
2
114
 
3
115
  module.exports = {
116
+ /** @readonly */
117
+ init,
4
118
  }
package/fsc/test.f.cjs CHANGED
@@ -1,6 +1,18 @@
1
1
  const _ = require('./module.f.cjs')
2
+ const { one } = require('../text/ascii/module.f.cjs')
3
+ const { get } = require('../types/range_map/module.f.cjs')
4
+ const { stringify } = require('../json/module.f.cjs')
5
+ const s = stringify(i => i)
6
+
7
+ /** @type {(v: string) => string} */
8
+ const f = v => {
9
+ const n = one(v)
10
+ return s(_.init(undefined)(n)[0])
11
+ }
2
12
 
3
13
  module.exports = {
4
14
  a: () => {
15
+ const x = f('1')
16
+ if (x != '["1"]') { throw x }
5
17
  }
6
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.477",
3
+ "version": "0.0.479",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -13,7 +13,9 @@ const one = s => at(s)(0)
13
13
  /** @type {(s: string) => _range.Range} */
14
14
  const range = s => {
15
15
  const f = at(s)
16
- return [f(0), f(1)]
16
+ const f0 = f(0)
17
+ if (s.length === 1) { return [f0, f0] }
18
+ return [f0, f(1)]
17
19
  }
18
20
 
19
21
  module.exports = {
@@ -0,0 +1,12 @@
1
+ const { range } = require('./module.f.cjs')
2
+ const json = require('../../json/module.f.cjs')
3
+ const { sort } = require('../../types/object/module.f.cjs')
4
+
5
+ const stringify = json.stringify(sort)
6
+
7
+ module.exports = {
8
+ range: () => {
9
+ const r = stringify(range("A"))
10
+ if (r !== '[65,65]') { throw r }
11
+ }
12
+ }
@@ -3,8 +3,7 @@ const json = require('../../json/module.f.cjs')
3
3
  const { sort } = require('../../types/object/module.f.cjs')
4
4
  const { list } = require('../../types/module.f.cjs')
5
5
 
6
- /** @type {(a: readonly json.Unknown[]) => string} */
7
- const stringify = a => json.stringify(sort)(a)
6
+ const stringify = json.stringify(sort)
8
7
 
9
8
  module.exports = {
10
9
  toCodePoint: [
@@ -3,7 +3,12 @@
3
3
  /** @type {(range: Range) => (i: number) => boolean} */
4
4
  const contains = ([b, e]) => i => b <= i && i <= e
5
5
 
6
+ /** @type {(i: number) => Range} */
7
+ const one = a => [a, a]
8
+
6
9
  module.exports = {
7
10
  /** @readonly */
8
11
  contains,
12
+ /** @readonly */
13
+ one,
9
14
  }
@@ -37,7 +37,7 @@ const _range = require('../range/module.f.cjs')
37
37
 
38
38
  /**
39
39
  * @template T
40
- * @typedef {(a: RangeMap<T>) => (b: RangeMap<T>) => RangeMap<T>} RangeMerge
40
+ * @typedef {operator.Reduce<RangeMap<T>>} RangeMerge
41
41
  */
42
42
 
43
43
  /** @type {<T>(union: operator.Reduce<T>) => (equal: operator.Equal<T>) => sortedList.ReduceOp<Entry<T>, RangeState<T>>} */
@@ -33,7 +33,7 @@ module.exports = {
33
33
  intersect: [
34
34
  () => {
35
35
  const result = stringify(toArray(_.intersect(unsafeCmp)([2, 3, 4])([1, 3, 5])))
36
- console.log(result)
36
+ // console.log(result)
37
37
  if (result !== '[3]') { throw result }
38
38
  },
39
39
  () => {