functionalscript 0.0.352 → 0.0.353

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/com/main.f.js CHANGED
@@ -1,8 +1,17 @@
1
+ const text = require('../text/main.f.js')
2
+ const list = require('../types/list/main.f')
3
+ const obj = require('../types/object/main.f.js')
4
+
1
5
  /** @typedef {{readonly[k in string]: Definition}} Library */
2
6
 
3
7
  /** @typedef {Struct|Interface} Definition */
4
8
 
5
- /** @typedef {{readonly struct: FieldArray}} Struct */
9
+ /**
10
+ * @typedef {{
11
+ * readonly interface?: undefined
12
+ * readonly struct: FieldArray
13
+ * }} Struct
14
+ */
6
15
 
7
16
  /** @typedef {readonly Field[]} FieldArray */
8
17
 
@@ -17,14 +26,90 @@
17
26
 
18
27
  /** @typedef {readonly Method[]} MethodArray */
19
28
 
20
- /** @typedef {readonly[string, FieldArray, Type]} Method */
29
+ /** @typedef {readonly[string, FieldArray, Type]} GetMethod */
30
+
31
+ /** @typedef {readonly[string, FieldArray]} SetMethod */
32
+
33
+ /** @typedef {GetMethod|SetMethod} Method */
21
34
 
22
35
  /** @typedef {BaseType|Id|Pointer} Type */
23
36
 
24
37
  /** @typedef {readonly[string]} Id */
25
38
 
26
- /** @typedef {'u8'|'i8'|'u16'|'i16'|'u32'|'i32'|'u64'|'i64'|'usize'|'isize'|'f32'|'f64'|'bool'|''} BaseType */
39
+ /** @typedef {'u8'|'i8'|'u16'|'i16'|'u32'|'i32'|'u64'|'i64'|'usize'|'isize'|'f32'|'f64'|'bool'} BaseType */
27
40
 
28
41
  /** @typedef {{readonly '*': Type}} Pointer */
29
42
 
30
- module.exports = {}
43
+ /** @type {(v: string) => string} */
44
+ const csUsing = v => `using ${v};`
45
+
46
+ /** @type {(type: string) => (name: string) => (body: text.Block) => text.Block} */
47
+ const csBlock = type => name => body => [`${type} ${name}`, '{', body, '}']
48
+
49
+ /** @type {(attributes: list.List<string>) => (type: string) => (name: string) => (body: text.Block) => list.List<text.Item>} */
50
+ const csTypeDef = attributes => type => name => body =>
51
+ list.flat([
52
+ list.map(v=>`[${v}]`)(attributes),
53
+ csBlock(`public ${type}`)(name)(body)
54
+ ])
55
+
56
+ /** @type {(t: BaseType) => string} */
57
+ const csBaseType = t => {
58
+ switch (t) {
59
+ case 'bool': return 'bool'
60
+ case 'f32': return 'float'
61
+ case 'f64': return 'double'
62
+ case 'i16': return 'short'
63
+ case 'i32': return 'int'
64
+ case 'i64': return 'long'
65
+ case 'i8': return 'sbyte'
66
+ case 'isize': return 'IntPtr'
67
+ case 'u16': return 'ushort'
68
+ case 'u32': return 'uint'
69
+ case 'u64': return 'ulong'
70
+ case 'u8': return 'byte'
71
+ case 'usize': return 'UIntPtr'
72
+ }
73
+ }
74
+
75
+ /** @type {(t: Type) => string} */
76
+ const csType = t => {
77
+ if (typeof(t) === 'string') {
78
+ return csBaseType(t)
79
+ }
80
+ if (t instanceof Array) {
81
+ return t[0]
82
+ }
83
+ return `${csType(t['*'])}*`
84
+ }
85
+
86
+ /** @type {(f: Field) => string} */
87
+ const csField = f => `public ${csType(f[1])} ${f[0]};`
88
+
89
+ /** @type {(e: obj.Entry<Definition>) => list.List<text.Item>} */
90
+ const csDef = ([n, d]) => {
91
+ const i = d.interface
92
+ return i === undefined ?
93
+ csTypeDef(['StructLayout(LayoutKind.Sequential)'])('struct')(n)(() => list.map(csField)(d.struct)) :
94
+ csTypeDef([`Guid("${d.guid}")`,'InterfaceType(ComInterfaceType.InterfaceIsUnknown)'])('interface')(n)([])
95
+ }
96
+
97
+ /** @type {(name: string) => (library: Library) => text.Block} */
98
+ const cs = name => library => {
99
+ const v = list.flatMap(csDef)(Object.entries(library))
100
+
101
+ /** @type {text.Block} */
102
+ const h = [
103
+ csUsing('System'),
104
+ csUsing('System.Runtime.InteropServices'),
105
+ ''
106
+ ]
107
+
108
+ const ns = csBlock('namespace')(name)(() => v)
109
+ return () => list.flat([h, ns])
110
+ }
111
+
112
+ module.exports = {
113
+ /** @readonly */
114
+ cs,
115
+ }
package/com/test.f.js CHANGED
@@ -1,23 +1,45 @@
1
1
  const _ = require('./main.f.js')
2
+ const list = require('../types/list/main.f.js')
3
+ const text = require('../text/main.f.js')
2
4
 
3
5
  /** @type {_.Library} */
4
6
  const library = {
5
7
  Slice: {
6
8
  struct: [
7
9
  ['Start', { '*': 'u8' }],
8
- ['End', 'usize'],
10
+ ['Size', 'usize'],
9
11
  ]
10
12
  },
11
13
  IMy: {
12
14
  guid: 'C66FB270-2D80-49AD-BB6E-88C1F90B805D',
13
15
  interface: [
14
16
  ['GetSlice', [], ['Slice']],
15
- ['SetSlice',
16
- [['slice', ['Slice']]],
17
- ''
18
- ],
17
+ ['SetSlice', [['slice', ['Slice']]]],
19
18
  ],
20
19
  }
21
20
  }
22
21
 
22
+ {
23
+ const cs = list.join('\n')(text.flat(' ')(_.cs('My')(library)))
24
+ const e =
25
+ 'using System;\n' +
26
+ 'using System.Runtime.InteropServices;\n' +
27
+ '\n' +
28
+ 'namespace My\n' +
29
+ '{\n' +
30
+ ' [StructLayout(LayoutKind.Sequential)]\n' +
31
+ ' public struct Slice\n' +
32
+ ' {\n' +
33
+ ' public byte* Start;\n' +
34
+ ' public UIntPtr Size;\n' +
35
+ ' }\n' +
36
+ ' [Guid("C66FB270-2D80-49AD-BB6E-88C1F90B805D")]\n' +
37
+ ' [InterfaceType(ComInterfaceType.InterfaceIsUnknown)]\n' +
38
+ ' public interface IMy\n' +
39
+ ' {\n' +
40
+ ' }\n' +
41
+ '}'
42
+ if (cs !== e) { throw [cs,e] }
43
+ }
44
+
23
45
  module.exports = {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.352",
3
+ "version": "0.0.353",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "main.f.js",
6
6
  "scripts": {
package/test.f.js CHANGED
@@ -17,6 +17,7 @@ require('./commonjs/build/test.f.js')
17
17
  require('./types/range/test.f.js')
18
18
  require('./html/test.f.js')
19
19
  require('./text/test.f.js')
20
+ require('./com/test.f.js')
20
21
 
21
22
  /** @type {() => never} */
22
23
  const assert = () => { throw 'assert' }
package/text/main.f.js CHANGED
@@ -1,21 +1,27 @@
1
1
  const list = require('../types/list/main.f.js')
2
2
 
3
- /** @typedef {readonly Item[]} Text */
3
+ /** @typedef {ItemThunk|ItemArray} Block */
4
4
 
5
- /** @typedef {string|Text} Item */
5
+ /** @typedef {readonly Item[]} ItemArray */
6
6
 
7
- /** @type {(indent: string) => (text: Text) => list.List<string>} */
7
+ /** @typedef {() => list.List<Item>} ItemThunk */
8
+
9
+ /** @typedef {string|ItemArray|ItemThunk} Item */
10
+
11
+ /** @type {(indent: string) => (text: Block) => list.List<string>} */
8
12
  const flat = indent => {
9
- /** @type {(v: string) => string} */
10
- const indentFn = v => `${indent}${v}`
11
- const map = list.map(indentFn)
12
- /** @type {(item: Item) => list.List<string>} */
13
- const flatItem = item => typeof(item) === 'string' ? [item] : map(flatText(item))
14
- const flatText = list.flatMap(flatItem)
15
- return flatText
13
+
14
+ /** @type {(prefix: string) => (text: Block) => list.List<string>} */
15
+ const f = prefix => {
16
+ /** @type {(item: Item) => list.List<string>} */
17
+ const g = item => typeof (item) === 'string' ? [`${prefix}${item}`] : f(`${prefix}${indent}`)(item)
18
+ return list.flatMap(g)
19
+ }
20
+
21
+ return f('')
16
22
  }
17
23
 
18
24
  module.exports = {
19
25
  /** @readonly */
20
26
  flat,
21
- }
27
+ }
package/text/test.f.js CHANGED
@@ -2,17 +2,18 @@ const _ = require('./main.f.js')
2
2
  const list = require('../types/list/main.f.js')
3
3
 
4
4
  {
5
- /** @type {_.Text} */
5
+ /** @type {_.Block} */
6
6
  const text = [
7
7
  'a',
8
8
  'b',
9
- [
9
+ () => [
10
10
  'c',
11
- ['d'],
12
- ]
11
+ () => ['d'],
12
+ ],
13
+ 'e',
13
14
  ]
14
- const result = list.join('\n')(_.flat('_')(text))
15
- if (result !== 'a\nb\n_c\n__d') { throw result }
15
+ const result = list.join('\n')(_.flat(':')(text))
16
+ if (result !== 'a\nb\n:c\n::d\ne') { throw result }
16
17
  }
17
18
 
18
19
  module.exports = {}
@@ -172,8 +172,7 @@ const drop = n => apply(dropStep(n))
172
172
  /** @type {<D>(def: D) => <T>(input: List<T>) => D|T} */
173
173
  const first = def => input => {
174
174
  const result = next(input)
175
- if (result === undefined) { return def }
176
- return result.first
175
+ return result === undefined ? def : result.first
177
176
  }
178
177
 
179
178
  /** @type {<D>(first: D) => <T>(tail: List<T>) => D|T} */
@@ -215,6 +214,9 @@ const countdown = count => () => {
215
214
  return { first, tail: countdown(first) }
216
215
  }
217
216
 
217
+ /** @type {<T>(v: T) => (c: number) => List<T>} */
218
+ const repeat = v => n => map(() => v)(countdown(n))
219
+
218
220
  /** @type {<T>(list: List<T>) => List<T>} */
219
221
  const cycle = list => () => {
220
222
  const i = next(list)
@@ -355,6 +357,8 @@ module.exports = {
355
357
  /** @readonly */
356
358
  countdown,
357
359
  /** @readonly */
360
+ repeat,
361
+ /** @readonly */
358
362
  cycle,
359
363
  /** @readonly */
360
364
  scan,
@@ -354,6 +354,16 @@ const stress = () => {
354
354
  }
355
355
  }
356
356
 
357
+ {
358
+ const s = _.join('.')(_.repeat('x')(0))
359
+ if (s != '') { throw s }
360
+ }
361
+
362
+ {
363
+ const s = _.join('.')(_.repeat('x')(5))
364
+ if (s != 'x.x.x.x.x') { throw s }
365
+ }
366
+
357
367
  // stress()
358
368
 
359
369
  module.exports = {