@shd101wyy/yo 0.1.25 → 0.1.27
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/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +40 -40
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +30 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +78 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +583 -567
- package/out/cjs/yo-cli.cjs +664 -632
- package/out/cjs/yo-lsp.cjs +510 -485
- package/out/esm/index.mjs +538 -522
- package/out/types/src/codegen/codegen-c.d.ts +2 -2
- package/out/types/src/codegen/functions/collection.d.ts +2 -2
- package/out/types/src/codegen/functions/context.d.ts +3 -2
- package/out/types/src/codegen/types/collection.d.ts +2 -2
- package/out/types/src/codegen/utils/index.d.ts +3 -1
- package/out/types/src/doc/builder.d.ts +2 -2
- package/out/types/src/evaluator/calls/closure-type.d.ts +2 -2
- package/out/types/src/evaluator/calls/record-type.d.ts +11 -0
- package/out/types/src/evaluator/context.d.ts +8 -9
- package/out/types/src/evaluator/index.d.ts +3 -3
- package/out/types/src/evaluator/types/record.d.ts +14 -0
- package/out/types/src/evaluator/types/validation.d.ts +2 -2
- package/out/types/src/evaluator/values/anonymous-module.d.ts +5 -5
- package/out/types/src/evaluator/values/impl.d.ts +1 -1
- package/out/types/src/expr.d.ts +1 -4
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/function-value.d.ts +1 -1
- package/out/types/src/lsp/document-manager.d.ts +1 -1
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/module-manager.d.ts +3 -3
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/src/types/creators.d.ts +3 -4
- package/out/types/src/types/definitions.d.ts +8 -19
- package/out/types/src/types/guards.d.ts +3 -3
- package/out/types/src/types/tags.d.ts +0 -1
- package/out/types/src/types/utils.d.ts +1 -1
- package/out/types/src/value-tag.d.ts +0 -1
- package/out/types/src/value.d.ts +6 -13
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +12 -20
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6316 -4333
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
- package/out/types/src/evaluator/calls/module-type.d.ts +0 -11
- package/out/types/src/evaluator/types/module.d.ts +0 -19
package/std/encoding/utf16.yo
CHANGED
|
@@ -10,23 +10,20 @@
|
|
|
10
10
|
//! words := utf8_to_utf16("hello");
|
|
11
11
|
//! s := utf16_to_utf8(words);
|
|
12
12
|
//! ```
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
{
|
|
16
|
-
{
|
|
17
|
-
{ Error, AnyError, Exception } :: import "../error";
|
|
18
|
-
|
|
13
|
+
open(import("../string"));
|
|
14
|
+
{ ArrayList } :: import("../collections/array_list");
|
|
15
|
+
{ EncodingError } :: import("./hex");
|
|
16
|
+
{ Error, AnyError, Exception } :: import("../error");
|
|
19
17
|
// ============================================================================
|
|
20
18
|
// UTF-8 → UTF-16
|
|
21
19
|
// ============================================================================
|
|
22
|
-
|
|
23
20
|
/// Convert a UTF-8 string to an `ArrayList(u16)` of UTF-16 code units.
|
|
24
21
|
///
|
|
25
22
|
/// Handles BMP characters directly and encodes supplementary characters as surrogate pairs.
|
|
26
|
-
utf8_to_utf16 :: (fn(s: str) -> ArrayList(u16))({
|
|
23
|
+
utf8_to_utf16 :: (fn(s : str) -> ArrayList(u16))({
|
|
27
24
|
out := ArrayList(u16).new();
|
|
28
|
-
i
|
|
29
|
-
while
|
|
25
|
+
i := usize(0);
|
|
26
|
+
while(i < s.len(), {
|
|
30
27
|
b0 := u32(s.bytes(i));
|
|
31
28
|
cond(
|
|
32
29
|
(b0 < u32(0x80)) => {
|
|
@@ -34,73 +31,70 @@ utf8_to_utf16 :: (fn(s: str) -> ArrayList(u16))({
|
|
|
34
31
|
i = (i + usize(1));
|
|
35
32
|
},
|
|
36
33
|
((b0 & u32(0xE0)) == u32(0xC0)) => {
|
|
37
|
-
b1
|
|
38
|
-
cp
|
|
34
|
+
b1 := u32(s.bytes(i + usize(1)));
|
|
35
|
+
cp := (((b0 & u32(0x1F)) << u32(6)) | (b1 & u32(0x3F)));
|
|
39
36
|
out.push(u16(cp));
|
|
40
37
|
i = (i + usize(2));
|
|
41
38
|
},
|
|
42
39
|
((b0 & u32(0xF0)) == u32(0xE0)) => {
|
|
43
|
-
b1 := u32(s.bytes(
|
|
44
|
-
b2 := u32(s.bytes(
|
|
45
|
-
cp
|
|
40
|
+
b1 := u32(s.bytes(i + usize(1)));
|
|
41
|
+
b2 := u32(s.bytes(i + usize(2)));
|
|
42
|
+
cp := ((((b0 & u32(0x0F)) << u32(12)) | ((b1 & u32(0x3F)) << u32(6))) | (b2 & u32(0x3F)));
|
|
46
43
|
out.push(u16(cp));
|
|
47
44
|
i = (i + usize(3));
|
|
48
45
|
},
|
|
49
46
|
true => {
|
|
50
47
|
// 4-byte sequence → surrogate pair
|
|
51
|
-
b1
|
|
52
|
-
b2
|
|
53
|
-
b3
|
|
54
|
-
cp
|
|
48
|
+
b1 := u32(s.bytes(i + usize(1)));
|
|
49
|
+
b2 := u32(s.bytes(i + usize(2)));
|
|
50
|
+
b3 := u32(s.bytes(i + usize(3)));
|
|
51
|
+
cp := (((((b0 & u32(0x07)) << u32(18)) | ((b1 & u32(0x3F)) << u32(12))) | ((b2 & u32(0x3F)) << u32(6))) | (b3 & u32(0x3F)));
|
|
55
52
|
cp2 := (cp - u32(0x10000));
|
|
56
|
-
hi
|
|
57
|
-
lo
|
|
53
|
+
hi := u16((cp2 >> u32(10)) + u32(0xD800));
|
|
54
|
+
lo := u16((cp2 & u32(0x3FF)) + u32(0xDC00));
|
|
58
55
|
out.push(hi);
|
|
59
56
|
out.push(lo);
|
|
60
57
|
i = (i + usize(4));
|
|
61
58
|
}
|
|
62
59
|
);
|
|
63
|
-
};
|
|
60
|
+
});
|
|
64
61
|
out
|
|
65
62
|
});
|
|
66
|
-
|
|
67
|
-
export utf8_to_utf16;
|
|
68
|
-
|
|
63
|
+
export(utf8_to_utf16);
|
|
69
64
|
// ============================================================================
|
|
70
65
|
// UTF-16 → UTF-8
|
|
71
66
|
// ============================================================================
|
|
72
|
-
|
|
73
67
|
/// Convert UTF-16 code units to a UTF-8 `String`.
|
|
74
68
|
///
|
|
75
69
|
/// Decodes surrogate pairs back into supplementary code points.
|
|
76
70
|
/// Throws via `Exception` on unpaired surrogates.
|
|
77
|
-
utf16_to_utf8 :: (fn(data: ArrayList(u16), using(exn : Exception)) -> String)({
|
|
71
|
+
utf16_to_utf8 :: (fn(data : ArrayList(u16), using(exn : Exception)) -> String)({
|
|
78
72
|
out := ArrayList(u8).new();
|
|
79
|
-
i
|
|
80
|
-
while
|
|
73
|
+
i := usize(0);
|
|
74
|
+
while(i < data.len(), {
|
|
81
75
|
w := u32(data.get(i).unwrap());
|
|
82
76
|
cond(
|
|
83
77
|
// Surrogate pair: high surrogate 0xD800-0xDBFF
|
|
84
78
|
((w >= u32(0xD800)) && (w <= u32(0xDBFF))) => {
|
|
85
79
|
cond(
|
|
86
80
|
((i + usize(1)) >= data.len()) => {
|
|
87
|
-
exn.throw(dyn
|
|
81
|
+
exn.throw(dyn(EncodingError.InvalidChar(u8(0))));
|
|
88
82
|
},
|
|
89
83
|
true => ()
|
|
90
84
|
);
|
|
91
|
-
w2 := u32(data.get(
|
|
85
|
+
w2 := u32(data.get(i + usize(1)).unwrap());
|
|
92
86
|
cond(
|
|
93
87
|
((w2 < u32(0xDC00)) || (w2 > u32(0xDFFF))) => {
|
|
94
|
-
exn.throw(dyn
|
|
88
|
+
exn.throw(dyn(EncodingError.InvalidChar(u8(0))));
|
|
95
89
|
},
|
|
96
90
|
true => ()
|
|
97
91
|
);
|
|
98
92
|
cp := ((((w - u32(0xD800)) << u32(10)) + (w2 - u32(0xDC00))) + u32(0x10000));
|
|
99
93
|
// Encode as 4-byte UTF-8
|
|
100
|
-
out.push(u8(
|
|
101
|
-
out.push(u8(
|
|
102
|
-
out.push(u8(
|
|
103
|
-
out.push(u8(
|
|
94
|
+
out.push(u8(u32(0xF0) | (cp >> u32(18))));
|
|
95
|
+
out.push(u8(u32(0x80) | ((cp >> u32(12)) & u32(0x3F))));
|
|
96
|
+
out.push(u8(u32(0x80) | ((cp >> u32(6)) & u32(0x3F))));
|
|
97
|
+
out.push(u8(u32(0x80) | (cp & u32(0x3F))));
|
|
104
98
|
i = (i + usize(2));
|
|
105
99
|
},
|
|
106
100
|
(w < u32(0x80)) => {
|
|
@@ -108,19 +102,18 @@ utf16_to_utf8 :: (fn(data: ArrayList(u16), using(exn : Exception)) -> String)({
|
|
|
108
102
|
i = (i + usize(1));
|
|
109
103
|
},
|
|
110
104
|
(w < u32(0x800)) => {
|
|
111
|
-
out.push(u8(
|
|
112
|
-
out.push(u8(
|
|
105
|
+
out.push(u8(u32(0xC0) | (w >> u32(6))));
|
|
106
|
+
out.push(u8(u32(0x80) | (w & u32(0x3F))));
|
|
113
107
|
i = (i + usize(1));
|
|
114
108
|
},
|
|
115
109
|
true => {
|
|
116
|
-
out.push(u8(
|
|
117
|
-
out.push(u8(
|
|
118
|
-
out.push(u8(
|
|
110
|
+
out.push(u8(u32(0xE0) | (w >> u32(12))));
|
|
111
|
+
out.push(u8(u32(0x80) | ((w >> u32(6)) & u32(0x3F))));
|
|
112
|
+
out.push(u8(u32(0x80) | (w & u32(0x3F))));
|
|
119
113
|
i = (i + usize(1));
|
|
120
114
|
}
|
|
121
115
|
);
|
|
122
|
-
};
|
|
116
|
+
});
|
|
123
117
|
String.from_bytes(out)
|
|
124
118
|
});
|
|
125
|
-
|
|
126
|
-
export utf16_to_utf8;
|
|
119
|
+
export(utf16_to_utf8);
|
package/std/env.yo
CHANGED
|
@@ -3,96 +3,83 @@
|
|
|
3
3
|
//!
|
|
4
4
|
//! Mirrors the layout of Rust's `std::env`. Platform/architecture detection
|
|
5
5
|
//! and child-process spawning live in `std/process`.
|
|
6
|
-
|
|
7
|
-
open
|
|
8
|
-
open
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
{ GlobalAllocator } :: import "./allocator";
|
|
6
|
+
open(import("./collections/array_list"));
|
|
7
|
+
open(import("./string"));
|
|
8
|
+
open(import("./path"));
|
|
9
|
+
{ platform, Platform } :: import("./process");
|
|
10
|
+
{ GlobalAllocator } :: import("./allocator");
|
|
12
11
|
{ malloc, free } :: GlobalAllocator;
|
|
13
|
-
|
|
14
12
|
// === Low-level C interface ===
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
extern(
|
|
14
|
+
"Yo",
|
|
17
15
|
__yo_argc : i32,
|
|
18
16
|
__yo_argv : *(*(u8)),
|
|
19
17
|
__yo_args : [*(u8)]
|
|
20
|
-
;
|
|
21
|
-
|
|
18
|
+
);
|
|
22
19
|
// === Raw command-line arguments ===
|
|
23
|
-
|
|
24
20
|
/// Get raw command-line arguments as a slice of C strings.
|
|
25
21
|
/// The first element is the program name.
|
|
26
|
-
raw_args :: (fn() -> [*(u8)])
|
|
27
|
-
return
|
|
28
|
-
};
|
|
29
|
-
export
|
|
30
|
-
|
|
22
|
+
raw_args :: (fn() -> [*(u8)])({
|
|
23
|
+
return(__yo_args);
|
|
24
|
+
});
|
|
25
|
+
export(raw_args);
|
|
31
26
|
/// Get the number of command-line arguments (including program name).
|
|
32
|
-
argc :: (fn() -> i32)
|
|
33
|
-
return
|
|
34
|
-
};
|
|
35
|
-
export
|
|
36
|
-
|
|
27
|
+
argc :: (fn() -> i32)({
|
|
28
|
+
return(__yo_argc);
|
|
29
|
+
});
|
|
30
|
+
export(argc);
|
|
37
31
|
/// Get the raw `argv` pointer (`*(*(u8))`).
|
|
38
|
-
argv :: (fn() -> *(*(u8)))
|
|
39
|
-
return
|
|
40
|
-
};
|
|
41
|
-
export
|
|
42
|
-
|
|
32
|
+
argv :: (fn() -> *(*(u8)))({
|
|
33
|
+
return(__yo_argv);
|
|
34
|
+
});
|
|
35
|
+
export(argv);
|
|
43
36
|
// === Convenient command-line arguments ===
|
|
44
|
-
|
|
45
37
|
/// Get command-line arguments as an `ArrayList(String)`.
|
|
46
38
|
/// The first element is the program name.
|
|
47
|
-
args :: (fn() -> ArrayList(String))
|
|
39
|
+
args :: (fn() -> ArrayList(String))({
|
|
48
40
|
raw := raw_args();
|
|
49
41
|
i := usize(0);
|
|
50
42
|
len := usize(argc());
|
|
51
|
-
|
|
52
43
|
result := ArrayList(String).with_capacity(len);
|
|
53
|
-
|
|
54
|
-
while i < len, {
|
|
44
|
+
while(i < len, {
|
|
55
45
|
arg_ptr := raw(i);
|
|
56
46
|
arg_str := String.from_cstr(arg_ptr).unwrap();
|
|
57
47
|
result.push(arg_str);
|
|
58
48
|
i = (i + 1);
|
|
59
|
-
};
|
|
60
|
-
return
|
|
61
|
-
};
|
|
62
|
-
export
|
|
63
|
-
|
|
49
|
+
});
|
|
50
|
+
return(result);
|
|
51
|
+
});
|
|
52
|
+
export(args);
|
|
64
53
|
/// Environment variable access and manipulation.
|
|
65
|
-
env :: impl
|
|
66
|
-
{ getenv, setenv } :: import
|
|
67
|
-
|
|
54
|
+
env :: impl({
|
|
55
|
+
{ getenv, setenv } :: import("./libc/stdlib");
|
|
68
56
|
/// Get the value of an environment variable by name.
|
|
69
57
|
/// Returns `.None` if the variable is not set.
|
|
70
|
-
get :: (fn(name : String) -> Option(String))
|
|
58
|
+
get :: (fn(name : String) -> Option(String))({
|
|
71
59
|
name_cstr := name.to_cstr().ptr().unwrap();
|
|
72
60
|
val_ptr := getenv(*(char)(name_cstr));
|
|
73
|
-
match(
|
|
74
|
-
|
|
75
|
-
.
|
|
61
|
+
match(
|
|
62
|
+
val_ptr,
|
|
63
|
+
.None =>.None,
|
|
64
|
+
.Some(ptr) =>.Some(String.from_cstr(*(u8)(ptr)).unwrap())
|
|
76
65
|
)
|
|
77
|
-
};
|
|
78
|
-
export
|
|
79
|
-
|
|
66
|
+
});
|
|
67
|
+
export(get);
|
|
80
68
|
/// Set an environment variable. If `overwrite` is false and the variable
|
|
81
69
|
/// already exists, it is not modified. Returns true on success.
|
|
82
|
-
set :: (fn(name : String, value : String, (overwrite : bool) ?= true) -> bool)
|
|
70
|
+
set :: (fn(name : String, value : String, (overwrite : bool) ?= true) -> bool)({
|
|
83
71
|
name_cstr := name.to_cstr().ptr().unwrap();
|
|
84
72
|
value_cstr := value.to_cstr().ptr().unwrap();
|
|
85
|
-
|
|
86
73
|
result := cond(
|
|
87
74
|
(platform == Platform.Windows) => {
|
|
88
|
-
{ _putenv_s } :: import
|
|
89
|
-
|
|
75
|
+
{ _putenv_s } :: import("./libc/windows");
|
|
90
76
|
cond(
|
|
91
77
|
overwrite =>
|
|
92
78
|
_putenv_s(*(char)(name_cstr), *(char)(value_cstr)),
|
|
93
79
|
true => {
|
|
94
80
|
existing := getenv(*(char)(name_cstr));
|
|
95
|
-
match(
|
|
81
|
+
match(
|
|
82
|
+
existing,
|
|
96
83
|
.Some(_) => int(0),
|
|
97
84
|
.None => _putenv_s(*(char)(name_cstr), *(char)(value_cstr))
|
|
98
85
|
)
|
|
@@ -107,28 +94,23 @@ env :: impl {
|
|
|
107
94
|
setenv(*(char)(name_cstr), *(char)(value_cstr), overwrite_int)
|
|
108
95
|
}
|
|
109
96
|
);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
export env;
|
|
116
|
-
|
|
97
|
+
return(result == int(0));
|
|
98
|
+
});
|
|
99
|
+
export(set);
|
|
100
|
+
});
|
|
101
|
+
export(env);
|
|
117
102
|
/// Get the current working directory as a `Path`.
|
|
118
103
|
/// Returns `Err` with a message on failure.
|
|
119
104
|
cwd :: (fn() -> Result(Path, String))(
|
|
120
105
|
cond(
|
|
121
106
|
(platform == Platform.Windows) => {
|
|
122
|
-
{ GetCurrentDirectoryW, WideCharToMultiByte, CP_UTF8, WCHAR, DWORD } :: import
|
|
123
|
-
|
|
124
|
-
required_size := GetCurrentDirectoryW(ulong(0), .None);
|
|
107
|
+
{ GetCurrentDirectoryW, WideCharToMultiByte, CP_UTF8, WCHAR, DWORD } :: import("./libc/windows");
|
|
108
|
+
required_size := GetCurrentDirectoryW(ulong(0),.None);
|
|
125
109
|
cond(
|
|
126
|
-
(required_size == ulong(0))
|
|
110
|
+
(required_size == ulong(0)) =>.Err(`Failed to get current working directory on Windows`),
|
|
127
111
|
true => {
|
|
128
|
-
wbuf := *(WCHAR)(malloc(
|
|
129
|
-
|
|
130
|
-
actual_size := GetCurrentDirectoryW(required_size, .Some(wbuf));
|
|
131
|
-
|
|
112
|
+
wbuf := *(WCHAR)(malloc(usize(required_size) * usize(2)).unwrap());
|
|
113
|
+
actual_size := GetCurrentDirectoryW(required_size,.Some(wbuf));
|
|
132
114
|
cond(
|
|
133
115
|
(actual_size == ulong(0)) => {
|
|
134
116
|
free(.Some(*(void)(wbuf)));
|
|
@@ -145,15 +127,13 @@ cwd :: (fn() -> Result(Path, String))(
|
|
|
145
127
|
.None,
|
|
146
128
|
.None
|
|
147
129
|
);
|
|
148
|
-
|
|
149
130
|
cond(
|
|
150
131
|
(utf8_size <= i32(0)) => {
|
|
151
132
|
free(.Some(*(void)(wbuf)));
|
|
152
133
|
.Err(`Failed to convert directory path to UTF-8`)
|
|
153
134
|
},
|
|
154
135
|
true => {
|
|
155
|
-
utf8_buf := *(u8)(malloc(
|
|
156
|
-
|
|
136
|
+
utf8_buf := *(u8)(malloc(usize(utf8_size) + usize(1)).unwrap());
|
|
157
137
|
result := WideCharToMultiByte(
|
|
158
138
|
CP_UTF8,
|
|
159
139
|
ulong(0),
|
|
@@ -164,9 +144,7 @@ cwd :: (fn() -> Result(Path, String))(
|
|
|
164
144
|
.None,
|
|
165
145
|
.None
|
|
166
146
|
);
|
|
167
|
-
|
|
168
147
|
free(.Some(*(void)(wbuf)));
|
|
169
|
-
|
|
170
148
|
cond(
|
|
171
149
|
(result <= i32(0)) => {
|
|
172
150
|
free(.Some(*(void)(utf8_buf)));
|
|
@@ -187,14 +165,12 @@ cwd :: (fn() -> Result(Path, String))(
|
|
|
187
165
|
)
|
|
188
166
|
},
|
|
189
167
|
true => {
|
|
190
|
-
{ getcwd } :: import
|
|
191
|
-
|
|
168
|
+
{ getcwd } :: import("./libc/unistd");
|
|
192
169
|
buf_size := usize(4096);
|
|
193
170
|
buf := *(char)(malloc(buf_size).unwrap());
|
|
194
|
-
|
|
195
171
|
result_ptr := getcwd(buf, buf_size);
|
|
196
|
-
|
|
197
|
-
|
|
172
|
+
match(
|
|
173
|
+
result_ptr,
|
|
198
174
|
.None => {
|
|
199
175
|
free(.Some(*(void)(buf)));
|
|
200
176
|
.Err(`Failed to get current working directory`)
|
|
@@ -208,36 +184,31 @@ cwd :: (fn() -> Result(Path, String))(
|
|
|
208
184
|
}
|
|
209
185
|
)
|
|
210
186
|
);
|
|
211
|
-
export
|
|
212
|
-
|
|
187
|
+
export(cwd);
|
|
213
188
|
/// Change the current working directory to `path`.
|
|
214
189
|
/// Returns `Ok(())` on success, or `Err` with a message on failure.
|
|
215
|
-
chdir :: (fn(path: Path) -> Result(unit, String))(
|
|
190
|
+
chdir :: (fn(path : Path) -> Result(unit, String))(
|
|
216
191
|
cond(
|
|
217
192
|
(platform == Platform.Windows) => {
|
|
218
|
-
{ SetCurrentDirectoryA, BOOL } :: import
|
|
219
|
-
|
|
193
|
+
{ SetCurrentDirectoryA, BOOL } :: import("./libc/windows");
|
|
220
194
|
path_str := path.to_string();
|
|
221
195
|
path_cstr := path_str.to_cstr().ptr().unwrap();
|
|
222
196
|
result := SetCurrentDirectoryA(*(char)(path_cstr));
|
|
223
|
-
|
|
224
197
|
cond(
|
|
225
|
-
(result == int(0))
|
|
226
|
-
true
|
|
198
|
+
(result == int(0)) =>.Err(`Failed to change directory to: ${path_str}`),
|
|
199
|
+
true =>.Ok(())
|
|
227
200
|
)
|
|
228
201
|
},
|
|
229
202
|
true => {
|
|
230
|
-
{ chdir : unix_chdir } :: import
|
|
231
|
-
|
|
203
|
+
{ chdir : unix_chdir } :: import("./libc/unistd");
|
|
232
204
|
path_str := path.to_string();
|
|
233
205
|
path_cstr := path_str.to_cstr().ptr().unwrap();
|
|
234
206
|
result := unix_chdir(*(char)(path_cstr));
|
|
235
|
-
|
|
236
207
|
cond(
|
|
237
|
-
(result != int(0))
|
|
238
|
-
true
|
|
208
|
+
(result != int(0)) =>.Err(`Failed to change directory to: ${path_str}`),
|
|
209
|
+
true =>.Ok(())
|
|
239
210
|
)
|
|
240
211
|
}
|
|
241
212
|
)
|
|
242
213
|
);
|
|
243
|
-
export
|
|
214
|
+
export(chdir);
|
package/std/error.yo
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
1
|
//! Standard error handling types and traits for Yo.
|
|
2
|
-
|
|
3
|
-
open
|
|
4
|
-
open import "./fmt";
|
|
5
|
-
|
|
2
|
+
open(import("./string"));
|
|
3
|
+
open(import("./fmt"));
|
|
6
4
|
/// Standard error trait for typed error propagation.
|
|
7
5
|
/// All error types should implement this trait along with `ToString`.
|
|
8
6
|
Error :: trait(
|
|
9
|
-
(source : (fn(self: *(Self)) -> Option(Dyn(SelfTrait)))) ?= ((self)
|
|
10
|
-
|
|
7
|
+
(source : (fn(self : *(Self)) -> Option(Dyn(SelfTrait)))) ?= ((self) ->.None),
|
|
11
8
|
where(Self <: ToString)
|
|
12
9
|
);
|
|
13
|
-
export
|
|
14
|
-
|
|
10
|
+
export(Error);
|
|
15
11
|
/// Type-erased dynamic error type. Wraps any type implementing `Error`.
|
|
16
12
|
AnyError :: Dyn(Error);
|
|
17
|
-
export
|
|
18
|
-
|
|
13
|
+
export(AnyError);
|
|
19
14
|
/// Allows using `String` directly as an error type.
|
|
20
15
|
impl(String, Error());
|
|
21
|
-
|
|
22
|
-
/// Non-resumable exception handling effect module.
|
|
16
|
+
/// Non-resumable exception handling effect record.
|
|
23
17
|
/// Use `throw` to raise an `AnyError` and abort the current computation.
|
|
24
|
-
Exception ::
|
|
18
|
+
Exception :: struct(
|
|
25
19
|
throw : (fn(forall(ResumeType : Type), error : AnyError) -> ResumeType)
|
|
26
20
|
);
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/// Creates a resumable exception module parameterized by the resume type.
|
|
21
|
+
export(Exception);
|
|
22
|
+
/// Creates a resumable exception effect record parameterized by the resume type.
|
|
31
23
|
/// Unlike `Exception`, the handler can return a value to resume the computation.
|
|
32
|
-
ResumableException :: (fn(comptime(ResumeType) : Type) -> comptime(
|
|
33
|
-
|
|
24
|
+
ResumableException :: (fn(comptime(ResumeType) : Type) -> comptime(Type))(
|
|
25
|
+
struct(
|
|
34
26
|
throw : (fn(error : AnyError) -> ResumeType)
|
|
35
27
|
)
|
|
36
28
|
);
|
|
37
|
-
export
|
|
29
|
+
export(ResumableException);
|
package/std/fmt/display.yo
CHANGED
|
@@ -9,16 +9,12 @@
|
|
|
9
9
|
//! )
|
|
10
10
|
//! ));
|
|
11
11
|
//! ```
|
|
12
|
-
|
|
13
|
-
{ Writer } :: import "./writer";
|
|
14
|
-
|
|
12
|
+
{ Writer } :: import("./writer");
|
|
15
13
|
/// Trait for formatting values by writing to a `Writer` rather than allocating a `String`.
|
|
16
14
|
/// Complement to `ToString` for streaming output into an existing buffer.
|
|
17
|
-
|
|
18
|
-
Display :: (fn(comptime(T) : Type) -> comptime(Trait))
|
|
15
|
+
Display :: (fn(comptime(T) : Type) -> comptime(Trait))(
|
|
19
16
|
trait(
|
|
20
|
-
display : (fn(self: *(T), writer: Writer) -> Writer)
|
|
17
|
+
display : (fn(self : *(T), writer : Writer) -> Writer)
|
|
21
18
|
)
|
|
22
|
-
;
|
|
23
|
-
|
|
24
|
-
export Display;
|
|
19
|
+
);
|
|
20
|
+
export(Display);
|
package/std/fmt/index.yo
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
//! Formatted output to stdout and stderr.
|
|
2
|
-
|
|
3
|
-
{
|
|
4
|
-
{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export ToString;
|
|
8
|
-
|
|
2
|
+
{ fwrite, stdout, stderr } :: import("../libc/stdio");
|
|
3
|
+
{ String } :: import("../string");
|
|
4
|
+
{ ToString } :: import("./to_string.yo");
|
|
5
|
+
export(ToString);
|
|
9
6
|
/// Print a value that implements `ToString` to stdout, followed by a newline.
|
|
10
7
|
println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
11
8
|
s := v.to_string();
|
|
@@ -20,8 +17,7 @@ println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
|
20
17
|
);
|
|
21
18
|
fwrite(*(void)(*(u8)("\n")), usize(1), usize(1), stdout);
|
|
22
19
|
});
|
|
23
|
-
export
|
|
24
|
-
|
|
20
|
+
export(println);
|
|
25
21
|
/// Print a value that implements `ToString` to stdout, without a trailing newline.
|
|
26
22
|
print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
27
23
|
s := v.to_string();
|
|
@@ -35,8 +31,7 @@ print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
|
35
31
|
true => ()
|
|
36
32
|
);
|
|
37
33
|
});
|
|
38
|
-
export
|
|
39
|
-
|
|
34
|
+
export(print);
|
|
40
35
|
/// Print a value that implements `ToString` to stderr, followed by a newline.
|
|
41
36
|
eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
42
37
|
s := v.to_string();
|
|
@@ -51,8 +46,7 @@ eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
|
51
46
|
);
|
|
52
47
|
fwrite(*(void)(*(u8)("\n")), usize(1), usize(1), stderr);
|
|
53
48
|
});
|
|
54
|
-
export
|
|
55
|
-
|
|
49
|
+
export(eprintln);
|
|
56
50
|
/// Print a value that implements `ToString` to stderr, without a trailing newline.
|
|
57
51
|
eprint :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
58
52
|
s := v.to_string();
|
|
@@ -66,4 +60,4 @@ eprint :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
|
|
|
66
60
|
true => ()
|
|
67
61
|
);
|
|
68
62
|
});
|
|
69
|
-
export
|
|
63
|
+
export(eprint);
|