functionalscript 0.8.2 → 0.9.2

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.
Files changed (91) hide show
  1. package/README.md +5 -5
  2. package/bnf/data/module.f.d.ts +10 -1
  3. package/bnf/data/module.f.js +58 -19
  4. package/bnf/data/test.f.d.ts +2 -0
  5. package/bnf/data/test.f.js +244 -3
  6. package/bnf/module.f.d.ts +6 -2
  7. package/bnf/module.f.js +8 -9
  8. package/cas/module.f.d.ts +18 -0
  9. package/cas/module.f.js +95 -0
  10. package/cas/test.f.d.ts +2 -0
  11. package/cas/test.f.js +1 -0
  12. package/ci/module.f.d.ts +3 -2
  13. package/ci/module.f.js +11 -9
  14. package/ci/module.js +3 -3
  15. package/crypto/sha2/module.f.js +0 -1
  16. package/dev/module.f.d.ts +1 -1
  17. package/dev/module.f.js +14 -6
  18. package/dev/version/module.f.d.ts +2 -11
  19. package/dev/version/module.f.js +17 -14
  20. package/dev/version/test.f.d.ts +3 -1
  21. package/dev/version/test.f.js +26 -10
  22. package/djs/module.f.d.ts +1 -1
  23. package/djs/module.f.js +4 -4
  24. package/djs/parser-new/module.f.d.ts +3 -0
  25. package/djs/parser-new/module.f.js +150 -0
  26. package/djs/parser-new/test.f.d.ts +5 -0
  27. package/djs/parser-new/test.f.js +201 -0
  28. package/djs/serializer/module.f.js +6 -6
  29. package/djs/transpiler/module.f.js +3 -2
  30. package/djs/transpiler/test.f.js +12 -11
  31. package/fjs/module.f.d.ts +2 -0
  32. package/fjs/module.f.js +25 -0
  33. package/{dev/tf → fjs}/module.js +1 -1
  34. package/fsc/module.f.js +1 -1
  35. package/io/module.d.ts +4 -0
  36. package/io/module.f.d.ts +14 -13
  37. package/io/module.f.js +21 -0
  38. package/io/module.js +7 -1
  39. package/io/virtual/module.f.d.ts +1 -1
  40. package/io/virtual/module.f.js +3 -3
  41. package/js/tokenizer/module.f.d.ts +1 -1
  42. package/js/tokenizer/module.f.js +0 -1
  43. package/json/module.f.js +3 -3
  44. package/nanvm-lib/tests/test.f.d.ts +1 -1
  45. package/nanvm-lib/tests/test.f.js +114 -104
  46. package/package.json +6 -7
  47. package/path/module.f.d.ts +1 -0
  48. package/path/module.f.js +6 -3
  49. package/text/utf16/module.f.js +1 -1
  50. package/types/bit_vec/module.f.d.ts +1 -0
  51. package/types/bit_vec/module.f.js +1 -0
  52. package/types/btree/find/module.f.d.ts +7 -8
  53. package/types/btree/set/module.f.js +21 -0
  54. package/types/cbase32/module.f.d.ts +6 -0
  55. package/types/cbase32/module.f.js +71 -0
  56. package/types/cbase32/test.f.d.ts +7 -0
  57. package/types/cbase32/test.f.js +74 -0
  58. package/types/effect/mock/module.f.d.ts +5 -0
  59. package/types/effect/mock/module.f.js +14 -0
  60. package/types/effect/module.d.ts +2 -0
  61. package/types/effect/module.f.d.ts +23 -0
  62. package/types/effect/module.f.js +14 -0
  63. package/types/effect/module.js +11 -0
  64. package/types/effect/node/module.f.d.ts +52 -0
  65. package/types/effect/node/module.f.js +7 -0
  66. package/types/effect/node/test.f.d.ts +27 -0
  67. package/types/effect/node/test.f.js +259 -0
  68. package/types/effect/node/virtual/module.f.d.ts +13 -0
  69. package/types/effect/node/virtual/module.f.js +98 -0
  70. package/types/function/module.f.d.ts +1 -1
  71. package/types/function/module.f.js +1 -1
  72. package/types/function/test.f.js +1 -1
  73. package/types/list/module.f.d.ts +1 -0
  74. package/types/list/module.f.js +6 -6
  75. package/types/nullable/module.f.d.ts +2 -0
  76. package/types/nullable/module.f.js +1 -0
  77. package/types/nullable/test.f.d.ts +1 -1
  78. package/types/nullable/test.f.js +23 -11
  79. package/types/option/module.f.d.ts +9 -0
  80. package/types/option/module.f.js +6 -0
  81. package/types/uint8array/module.f.d.ts +11 -0
  82. package/types/uint8array/module.f.js +21 -0
  83. package/types/uint8array/test.f.d.ts +12 -0
  84. package/types/uint8array/test.f.js +58 -0
  85. package/website/module.d.ts +1 -0
  86. package/website/module.f.d.ts +3 -0
  87. package/website/module.f.js +9 -0
  88. package/website/module.js +3 -0
  89. package/fsc/module.d.ts +0 -2
  90. package/fsc/module.js +0 -4
  91. /package/{dev/tf → fjs}/module.d.ts +0 -0
@@ -0,0 +1,13 @@
1
+ import { type Vec } from "../../../bit_vec/module.f.ts";
2
+ import type { MemOperationMap } from "../../mock/module.f.ts";
3
+ import type { NodeOperations } from "../module.f.ts";
4
+ export type VirtualDir = {
5
+ readonly [name in string]?: VirtualDir | Vec;
6
+ };
7
+ export type VirtualState = {
8
+ stdout: string;
9
+ stderr: string;
10
+ root: VirtualDir;
11
+ };
12
+ export declare const emptyState: VirtualState;
13
+ export declare const virtual: MemOperationMap<NodeOperations, VirtualState>;
@@ -0,0 +1,98 @@
1
+ import { parse } from "../../../../path/module.f.js";
2
+ import { isVec } from "../../../bit_vec/module.f.js";
3
+ import { error, ok } from "../../../result/module.f.js";
4
+ export const emptyState = {
5
+ stdout: '',
6
+ stderr: '',
7
+ root: {},
8
+ };
9
+ const operation = (op) => {
10
+ const f = (dir, path) => {
11
+ if (path.length === 0) {
12
+ return op(dir, path);
13
+ }
14
+ const [first, ...rest] = path;
15
+ const subDir = dir[first];
16
+ if (subDir === undefined || isVec(subDir)) {
17
+ return op(dir, path);
18
+ }
19
+ const [newSubDir, r] = f(subDir, rest);
20
+ return [{ ...dir, [first]: newSubDir }, r];
21
+ };
22
+ return (state, path) => {
23
+ const [root, result] = f(state.root, parse(path));
24
+ return [{ ...state, root }, result];
25
+ };
26
+ };
27
+ // TODO: we can have a better implementation with some code shared with `operation`.
28
+ const readOperation = (op) => operation((dir, path) => [dir, op(dir, path)]);
29
+ const okVoid = ok(undefined);
30
+ const mkdir = (recursive) => operation((dir, path) => {
31
+ let d = {};
32
+ let i = path.length;
33
+ if (i > 1 && !recursive) {
34
+ return [dir, error('non-recursive')];
35
+ }
36
+ while (i > 0) {
37
+ i -= 1;
38
+ d = { [path[i]]: d };
39
+ }
40
+ dir = { ...dir, ...d };
41
+ return [dir, okVoid];
42
+ });
43
+ const readFileError = error('no such file');
44
+ const readFile = readOperation((dir, path) => {
45
+ if (path.length !== 1) {
46
+ return readFileError;
47
+ }
48
+ const file = dir[path[0]];
49
+ if (!isVec(file)) {
50
+ return error(`'${path[0]}' is not a file`);
51
+ }
52
+ return ok(file);
53
+ });
54
+ const writeFileError = error('invalid file');
55
+ const writeFile = (payload) => operation((dir, path) => {
56
+ if (path.length !== 1) {
57
+ return [dir, writeFileError];
58
+ }
59
+ const [name] = path;
60
+ const file = dir[name];
61
+ // fail if the file is a directory
62
+ if (file !== undefined && !isVec(file)) {
63
+ return [dir, writeFileError];
64
+ }
65
+ dir = { ...dir, [name]: payload };
66
+ return [dir, okVoid];
67
+ });
68
+ const invalidPath = error('invalid path');
69
+ const { entries } = Object;
70
+ const readdir = (base, recursive) => readOperation((dir, path) => {
71
+ if (path.length !== 0) {
72
+ return invalidPath;
73
+ }
74
+ const f = (parentPath, d) => {
75
+ let result = [];
76
+ for (const [name, content] of entries(d)) {
77
+ if (content === undefined) {
78
+ continue;
79
+ }
80
+ const isFile = isVec(content);
81
+ result = [...result, { name, parentPath, isFile }];
82
+ if (!isFile && recursive) {
83
+ result = [...result, ...f(`${parentPath}/${name}`, content)];
84
+ }
85
+ }
86
+ return result;
87
+ };
88
+ return ok(f(base, dir));
89
+ });
90
+ const console = (name) => (state, payload) => [{ ...state, [name]: `${state[name]}${payload}\n` }, undefined];
91
+ export const virtual = {
92
+ error: console('stderr'),
93
+ log: console('stdout'),
94
+ mkdir: (state, [path, p]) => mkdir(p !== undefined)(state, path),
95
+ readFile,
96
+ readdir: (state, [path, { recursive }]) => readdir(path, recursive === true)(state, path),
97
+ writeFile: (state, [path, payload]) => writeFile(payload)(state, path),
98
+ };
@@ -19,7 +19,7 @@ export declare const flip: <A, B, C>(f: (a: A) => (b: B) => C) => (b: B) => (a:
19
19
  */
20
20
  type Fn<I, O> = {
21
21
  readonly result: Func<I, O>;
22
- readonly then: <T>(g: Func<O, T>) => Fn<I, T>;
22
+ readonly map: <T>(g: Func<O, T>) => Fn<I, T>;
23
23
  };
24
24
  /**
25
25
  * Creates an `Fn` instance from a function, enabling chaining of transformations.
@@ -15,5 +15,5 @@ export const flip = f => b => a => f(a)(b);
15
15
  */
16
16
  export const fn = (result) => ({
17
17
  result,
18
- then: g => fn(compose(result)(g))
18
+ map: g => fn(compose(result)(g))
19
19
  });
@@ -3,7 +3,7 @@ export default () => {
3
3
  const f = x => [x];
4
4
  const g = ([x]) => [x.length];
5
5
  const w = ([x]) => x;
6
- const r = fn(f).then(g).then(w).result;
6
+ const r = fn(f).map(g).map(w).result;
7
7
  const result = r('hello');
8
8
  if (result !== 5) {
9
9
  throw r;
@@ -12,6 +12,7 @@ type Concat<T> = {
12
12
  readonly head: List<T>;
13
13
  readonly tail: List<T>;
14
14
  };
15
+ export declare const fromArrayLike: <T>(array: ArrayLike<T>) => Result<T>;
15
16
  export declare const concat: <T>(head: List<T>) => (tail: List<T>) => List<T>;
16
17
  export declare const next: <T>(head: List<T>) => Result<T>;
17
18
  export declare const iterable: <T>(list: List<T>) => Iterable<T>;
@@ -1,6 +1,6 @@
1
1
  import { identity, fn, compose } from "../function/module.f.js";
2
2
  import { addition, logicalNot, strictEqual, stateScanToScan, foldToScan, reduceToScan, } from "../function/operator/module.f.js";
3
- const fromArray = (array) => {
3
+ export const fromArrayLike = (array) => {
4
4
  const at = (i) => i < array.length ? { first: array[i], tail: () => at(i + 1) } : null;
5
5
  return at(0);
6
6
  };
@@ -16,7 +16,7 @@ export const next = (head) => {
16
16
  while (true) {
17
17
  head = trampoline(head);
18
18
  if (head instanceof Array) {
19
- head = fromArray(head);
19
+ head = fromArrayLike(head);
20
20
  }
21
21
  else if (head !== null && 'head' in head) {
22
22
  [head, tail] = [head.head, concat(head.tail)(tail)];
@@ -96,12 +96,12 @@ export const last = first => tail => {
96
96
  export const find = def => f => compose(filter(f))(first(def));
97
97
  export const some = find(false)(identity);
98
98
  export const isEmpty = fn(map(() => true))
99
- .then(some)
100
- .then(logicalNot)
99
+ .map(some)
100
+ .map(logicalNot)
101
101
  .result;
102
102
  export const every = fn(map(logicalNot))
103
- .then(some)
104
- .then(logicalNot)
103
+ .map(some)
104
+ .map(logicalNot)
105
105
  .result;
106
106
  export const includes = value => compose(map(strictEqual(value)))(some);
107
107
  export const countdown = count => () => {
@@ -1,3 +1,5 @@
1
+ import type { Option } from '../option/module.f.ts';
1
2
  export type Nullable<T> = T | null;
2
3
  export declare const map: <T, R>(f: (value: T) => R) => (value: Nullable<T>) => Nullable<R>;
3
4
  export declare const match: <T, R>(f: (_: T) => R) => (none: () => R) => (_: Nullable<T>) => Nullable<R>;
5
+ export declare const toOption: <T>(value: Nullable<T>) => Option<T>;
@@ -1,2 +1,3 @@
1
1
  export const map = f => value => value === null ? null : f(value);
2
2
  export const match = f => none => value => value === null ? none() : f(value);
3
+ export const toOption = (value) => value === null ? [] : [value];
@@ -1,2 +1,2 @@
1
- declare const _default: () => void;
1
+ declare const _default: (() => void)[];
2
2
  export default _default;
@@ -1,12 +1,24 @@
1
- import { map } from "./module.f.js";
2
- export default () => {
3
- const optionSq = map((v) => v * v);
4
- const sq3 = optionSq(3);
5
- if (sq3 !== 9) {
6
- throw sq3;
1
+ import { map, toOption } from "./module.f.js";
2
+ export default [
3
+ () => {
4
+ const optionSq = map((v) => v * v);
5
+ const sq3 = optionSq(3);
6
+ if (sq3 !== 9) {
7
+ throw sq3;
8
+ }
9
+ const sqNull = optionSq(null);
10
+ if (sqNull !== null) {
11
+ throw sqNull;
12
+ }
13
+ },
14
+ () => {
15
+ const opt1 = toOption(5);
16
+ if (opt1.length !== 1 || opt1[0] !== 5) {
17
+ throw opt1;
18
+ }
19
+ const opt2 = toOption(null);
20
+ if (opt2.length !== 0) {
21
+ throw opt2;
22
+ }
7
23
  }
8
- const sqNull = optionSq(null);
9
- if (sqNull !== null) {
10
- throw sqNull;
11
- }
12
- };
24
+ ];
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Optional tuple-based value representation.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Represents an optional value as an empty tuple or a tuple with one value.
8
+ */
9
+ export type Option<T> = readonly [T] | readonly [];
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Optional tuple-based value representation.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
@@ -0,0 +1,11 @@
1
+ import { type Vec } from "../bit_vec/module.f.ts";
2
+ /**
3
+ * Converts a Uint8Array into an MSB-first bit vector.
4
+ */
5
+ export declare const toVec: (input: Uint8Array) => Vec;
6
+ /**
7
+ * Converts an MSB-first bit vector into a Uint8Array.
8
+ */
9
+ export declare const fromVec: (input: Vec) => Uint8Array;
10
+ export declare const decodeUtf8: (input: Uint8Array) => string;
11
+ export declare const encodeUtf8: (input: string) => Uint8Array;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Conversions between Uint8Array values and bit vectors.
3
+ *
4
+ * @module
5
+ */
6
+ import { utf8, utf8ToString } from "../../text/module.f.js";
7
+ import { msb, u8List, u8ListToVec } from "../bit_vec/module.f.js";
8
+ import { compose } from "../function/module.f.js";
9
+ import { fromArrayLike, iterable } from "../list/module.f.js";
10
+ const u8ListToVecMsb = u8ListToVec(msb);
11
+ const u8ListMsb = u8List(msb);
12
+ /**
13
+ * Converts a Uint8Array into an MSB-first bit vector.
14
+ */
15
+ export const toVec = (input) => u8ListToVecMsb(fromArrayLike(input));
16
+ /**
17
+ * Converts an MSB-first bit vector into a Uint8Array.
18
+ */
19
+ export const fromVec = (input) => Uint8Array.from(iterable(u8ListMsb(input)));
20
+ export const decodeUtf8 = compose(toVec)(utf8ToString);
21
+ export const encodeUtf8 = compose(utf8)(fromVec);
@@ -0,0 +1,12 @@
1
+ declare const _default: {
2
+ empty: () => void;
3
+ roundTrip: () => void;
4
+ unalignedLength: () => void;
5
+ encodeUtf8Empty: () => void;
6
+ encodeUtf8Ascii: () => void;
7
+ encodeUtf8Multibyte: () => void;
8
+ decodeUtf8Ascii: () => void;
9
+ decodeUtf8Multibyte: () => void;
10
+ utf8RoundTrip: () => void;
11
+ };
12
+ export default _default;
@@ -0,0 +1,58 @@
1
+ import { vec } from "../bit_vec/module.f.js";
2
+ import { toVec, fromVec, decodeUtf8, encodeUtf8 } from "./module.f.js";
3
+ import { strictEqual } from "../function/operator/module.f.js";
4
+ import { equal, fromArrayLike } from "../list/module.f.js";
5
+ const assertEq = (a, b) => {
6
+ if (a !== b) {
7
+ throw [a, b];
8
+ }
9
+ };
10
+ const assertArrayEq = (a, b) => {
11
+ if (!equal(strictEqual)(fromArrayLike(a))(fromArrayLike(b))) {
12
+ throw [a, b];
13
+ }
14
+ };
15
+ export default {
16
+ empty: () => {
17
+ const input = new Uint8Array();
18
+ const vec = toVec(input);
19
+ const output = fromVec(vec);
20
+ assertArrayEq(output, input);
21
+ },
22
+ roundTrip: () => {
23
+ const input = Uint8Array.from([0, 1, 2, 3, 255]);
24
+ const vec = toVec(input);
25
+ const output = fromVec(vec);
26
+ assertArrayEq(output, input);
27
+ },
28
+ unalignedLength: () => {
29
+ const input = vec(4n)(0xfn);
30
+ const output = fromVec(input);
31
+ assertArrayEq(output, Uint8Array.from([0xF0]));
32
+ },
33
+ encodeUtf8Empty: () => {
34
+ const output = encodeUtf8('');
35
+ assertArrayEq(output, new Uint8Array());
36
+ },
37
+ encodeUtf8Ascii: () => {
38
+ const output = encodeUtf8('Hi!');
39
+ assertArrayEq(output, Uint8Array.from([72, 105, 33]));
40
+ },
41
+ encodeUtf8Multibyte: () => {
42
+ const output = encodeUtf8('✓');
43
+ assertArrayEq(output, Uint8Array.from([0xE2, 0x9C, 0x93]));
44
+ },
45
+ decodeUtf8Ascii: () => {
46
+ const output = decodeUtf8(Uint8Array.from([102, 115]));
47
+ assertEq(output, 'fs');
48
+ },
49
+ decodeUtf8Multibyte: () => {
50
+ const output = decodeUtf8(Uint8Array.from([0xE2, 0x9C, 0x93]));
51
+ assertEq(output, '✓');
52
+ },
53
+ utf8RoundTrip: () => {
54
+ const input = 'FunctionalScript 🐝';
55
+ const output = decodeUtf8(encodeUtf8(input));
56
+ assertEq(output, input);
57
+ }
58
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { type NodeEffect } from '../types/effect/node/module.f.ts';
2
+ declare const _default: () => NodeEffect<number>;
3
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { htmlToString } from "../html/module.f.js";
2
+ import { writeFile } from "../types/effect/node/module.f.js";
3
+ import { utf8 } from "../text/module.f.js";
4
+ const html = ['body',
5
+ ['a', { href: 'https://github.com/functionalscript/functionalscript' }, 'GitHub Repository']
6
+ ];
7
+ const program = writeFile('index.html', utf8(htmlToString(html)))
8
+ .map(() => 0);
9
+ export default () => program;
@@ -0,0 +1,3 @@
1
+ import { nodeRun } from "../io/module.js";
2
+ import run from "./module.f.js";
3
+ await nodeRun(run);
package/fsc/module.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/fsc/module.js DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- import node from "../io/module.js";
3
- import { compile } from "../djs/module.f.js";
4
- await node(compile);
File without changes