@shd101wyy/yo 0.1.12 → 0.1.13

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 (164) hide show
  1. package/README.md +10 -1
  2. package/out/cjs/index.cjs +578 -572
  3. package/out/cjs/yo-cli.cjs +1413 -592
  4. package/out/esm/index.mjs +434 -428
  5. package/out/types/src/build-runner.d.ts +1 -1
  6. package/out/types/src/doc/builder.d.ts +21 -0
  7. package/out/types/src/doc/builder.test.d.ts +1 -0
  8. package/out/types/src/doc/extractor.d.ts +27 -0
  9. package/out/types/src/doc/extractor.test.d.ts +1 -0
  10. package/out/types/src/doc/model.d.ts +87 -0
  11. package/out/types/src/doc/render-html.d.ts +23 -0
  12. package/out/types/src/doc/render-html.test.d.ts +1 -0
  13. package/out/types/src/doc/render-json.d.ts +7 -0
  14. package/out/types/src/doc/render-json.test.d.ts +1 -0
  15. package/out/types/src/doc/render-markdown.d.ts +13 -0
  16. package/out/types/src/doc/render-markdown.test.d.ts +1 -0
  17. package/out/types/src/doc/sections.d.ts +7 -0
  18. package/out/types/src/doc/sections.test.d.ts +1 -0
  19. package/out/types/src/doc-command.d.ts +10 -0
  20. package/out/types/src/env.d.ts +1 -0
  21. package/out/types/src/evaluator/builtins/build.d.ts +17 -0
  22. package/out/types/src/evaluator/context.d.ts +1 -0
  23. package/out/types/src/expr.d.ts +2 -1
  24. package/out/types/src/token.d.ts +4 -0
  25. package/out/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +2 -1
  27. package/scripts/build-site.ts +461 -0
  28. package/std/alg/hash.yo +12 -24
  29. package/std/allocator.yo +21 -29
  30. package/std/async.yo +4 -2
  31. package/std/build.yo +188 -42
  32. package/std/cli/arg_parser.yo +37 -1
  33. package/std/collections/array_list.yo +8 -20
  34. package/std/collections/btree_map.yo +15 -20
  35. package/std/collections/deque.yo +13 -15
  36. package/std/collections/hash_map.yo +15 -54
  37. package/std/collections/hash_set.yo +14 -50
  38. package/std/collections/linked_list.yo +6 -23
  39. package/std/collections/priority_queue.yo +15 -17
  40. package/std/crypto/md5.yo +10 -9
  41. package/std/crypto/random.yo +14 -12
  42. package/std/crypto/sha256.yo +9 -8
  43. package/std/encoding/base64.yo +13 -15
  44. package/std/encoding/hex.yo +14 -10
  45. package/std/encoding/html.yo +14 -11
  46. package/std/encoding/html_char_utils.yo +14 -11
  47. package/std/encoding/html_entities.yo +7 -8
  48. package/std/encoding/json.yo +36 -19
  49. package/std/encoding/punycode.yo +21 -18
  50. package/std/encoding/toml.yo +23 -9
  51. package/std/encoding/utf16.yo +16 -9
  52. package/std/error.yo +10 -23
  53. package/std/fmt/display.yo +15 -17
  54. package/std/fmt/index.yo +6 -27
  55. package/std/fmt/to_string.yo +4 -10
  56. package/std/fmt/writer.yo +33 -34
  57. package/std/fs/dir.yo +37 -30
  58. package/std/fs/file.yo +77 -75
  59. package/std/fs/metadata.yo +25 -25
  60. package/std/fs/temp.yo +21 -27
  61. package/std/fs/types.yo +20 -23
  62. package/std/fs/walker.yo +26 -31
  63. package/std/gc.yo +5 -0
  64. package/std/glob/index.yo +3 -0
  65. package/std/http/client.yo +25 -11
  66. package/std/http/http.yo +20 -0
  67. package/std/http/index.yo +1 -0
  68. package/std/io/reader.yo +4 -8
  69. package/std/io/writer.yo +4 -7
  70. package/std/libc/assert.yo +2 -2
  71. package/std/libc/ctype.yo +1 -2
  72. package/std/libc/dirent.yo +1 -2
  73. package/std/libc/errno.yo +1 -2
  74. package/std/libc/fcntl.yo +2 -2
  75. package/std/libc/float.yo +1 -2
  76. package/std/libc/limits.yo +1 -2
  77. package/std/libc/math.yo +2 -0
  78. package/std/libc/signal.yo +1 -2
  79. package/std/libc/stdatomic.yo +1 -2
  80. package/std/libc/stdint.yo +4 -3
  81. package/std/libc/stdio.yo +2 -0
  82. package/std/libc/stdlib.yo +2 -0
  83. package/std/libc/string.yo +2 -0
  84. package/std/libc/sys/stat.yo +1 -2
  85. package/std/libc/time.yo +2 -2
  86. package/std/libc/unistd.yo +2 -0
  87. package/std/libc/wctype.yo +1 -2
  88. package/std/libc/windows.yo +2 -2
  89. package/std/log/index.yo +11 -26
  90. package/std/net/addr.yo +18 -11
  91. package/std/net/dns.yo +3 -2
  92. package/std/net/errors.yo +16 -2
  93. package/std/net/tcp.yo +25 -22
  94. package/std/net/udp.yo +14 -12
  95. package/std/os/env.yo +23 -18
  96. package/std/os/signal.yo +31 -19
  97. package/std/path.yo +23 -74
  98. package/std/prelude.yo +284 -113
  99. package/std/process.yo +23 -37
  100. package/std/regex/compiler.yo +3 -5
  101. package/std/regex/flags.yo +11 -11
  102. package/std/regex/index.yo +2 -13
  103. package/std/regex/match.yo +3 -5
  104. package/std/regex/node.yo +6 -6
  105. package/std/regex/parser.yo +2 -4
  106. package/std/regex/unicode.yo +5 -5
  107. package/std/regex/vm.yo +5 -5
  108. package/std/string/index.yo +2 -1
  109. package/std/string/rune.yo +25 -1
  110. package/std/string/string.yo +31 -19
  111. package/std/string/unicode.yo +14 -15
  112. package/std/sync/channel.yo +18 -28
  113. package/std/sync/cond.yo +4 -0
  114. package/std/sync/mutex.yo +4 -1
  115. package/std/sync/once.yo +17 -19
  116. package/std/sync/rwlock.yo +19 -22
  117. package/std/sync/waitgroup.yo +21 -23
  118. package/std/sys/advise.yo +4 -4
  119. package/std/sys/bufio/buf_reader.yo +19 -16
  120. package/std/sys/bufio/buf_writer.yo +14 -11
  121. package/std/sys/clock.yo +4 -4
  122. package/std/sys/constants.yo +5 -5
  123. package/std/sys/copy.yo +9 -8
  124. package/std/sys/dir.yo +9 -8
  125. package/std/sys/dns.yo +8 -8
  126. package/std/sys/errors.yo +35 -6
  127. package/std/sys/events.yo +3 -3
  128. package/std/sys/externs.yo +3 -3
  129. package/std/sys/fallocate.yo +4 -4
  130. package/std/sys/fcntl.yo +8 -6
  131. package/std/sys/file.yo +7 -8
  132. package/std/sys/future.yo +1 -3
  133. package/std/sys/iov.yo +4 -4
  134. package/std/sys/lock.yo +7 -7
  135. package/std/sys/mmap.yo +7 -8
  136. package/std/sys/path.yo +4 -7
  137. package/std/sys/perm.yo +8 -8
  138. package/std/sys/pipe.yo +9 -8
  139. package/std/sys/process.yo +8 -8
  140. package/std/sys/seek.yo +4 -4
  141. package/std/sys/signal.yo +4 -4
  142. package/std/sys/signals.yo +4 -4
  143. package/std/sys/socket.yo +4 -4
  144. package/std/sys/socketpair.yo +4 -4
  145. package/std/sys/sockinfo.yo +4 -4
  146. package/std/sys/statfs.yo +8 -8
  147. package/std/sys/statx.yo +4 -4
  148. package/std/sys/sysinfo.yo +4 -4
  149. package/std/sys/tcp.yo +8 -8
  150. package/std/sys/temp.yo +9 -8
  151. package/std/sys/time.yo +8 -8
  152. package/std/sys/timer.yo +7 -8
  153. package/std/sys/tty.yo +13 -10
  154. package/std/sys/udp.yo +8 -8
  155. package/std/sys/umask.yo +4 -4
  156. package/std/sys/unix.yo +5 -5
  157. package/std/testing/bench.yo +21 -10
  158. package/std/thread.yo +18 -9
  159. package/std/time/datetime.yo +12 -14
  160. package/std/time/duration.yo +12 -14
  161. package/std/time/instant.yo +13 -16
  162. package/std/time/sleep.yo +9 -8
  163. package/std/url/index.yo +3 -19
  164. package/std/worker.yo +10 -18
@@ -1,14 +1,14 @@
1
- // std/encoding/json.yo - JSON parsing and serialisation
2
- //
3
- // Provides a dynamically-typed JSON value tree.
4
- //
5
- // Example:
6
- // { json_parse, json_stringify, JsonValue } :: import "std/encoding/json";
7
- // { Exception } :: import "std/error";
8
- //
9
- // given(exn) := Exception(throw : ((err) -> { escape (); }));
10
- // v := json_parse(`{"x": 1}`);
11
- // println(json_stringify(v));
1
+ //! JSON parsing and serialization.
2
+ //!
3
+ //! Provides a dynamically-typed JSON value tree.
4
+ //!
5
+ //! Example:
6
+ //! { json_parse, json_stringify, JsonValue } :: import "std/encoding/json";
7
+ //! { Exception } :: import "std/error";
8
+ //!
9
+ //! given(exn) := Exception(throw : ((err) -> { escape (); }));
10
+ //! v := json_parse(`{"x": 1}`);
11
+ //! println(json_stringify(v));
12
12
 
13
13
  open import "../string";
14
14
  { ArrayList } :: import "../collections/array_list";
@@ -20,12 +20,19 @@ open import "../string";
20
20
  // JsonError
21
21
  // ============================================================================
22
22
 
23
+ /// JSON parsing error type.
23
24
  JsonError :: enum(
25
+ /// Encountered an unexpected character at the given position.
24
26
  UnexpectedChar(ch: u8, pos: usize),
27
+ /// Input ended unexpectedly.
25
28
  UnexpectedEnd,
29
+ /// Failed to parse a numeric literal.
26
30
  InvalidNumber,
31
+ /// Invalid escape sequence in a string.
27
32
  InvalidEscape,
33
+ /// Invalid Unicode escape in a string.
28
34
  InvalidUnicode,
35
+ /// Other error with a descriptive message.
29
36
  Other(msg: String)
30
37
  );
31
38
 
@@ -50,12 +57,19 @@ export JsonError;
50
57
  // JsonValue
51
58
  // ============================================================================
52
59
 
60
+ /// Dynamically-typed JSON value tree.
53
61
  JsonValue :: enum(
62
+ /// JSON null.
54
63
  Null,
64
+ /// JSON boolean.
55
65
  Bool(value: bool),
66
+ /// JSON number (IEEE 754 double).
56
67
  Number(value: f64),
68
+ /// JSON string.
57
69
  Str(value: String),
70
+ /// JSON array of values.
58
71
  Array(items: ArrayList(Self)),
72
+ /// JSON object with ordered key-value pairs.
59
73
  Object(keys: ArrayList(String), values: ArrayList(Self))
60
74
  );
61
75
 
@@ -63,13 +77,14 @@ JsonValue :: enum(
63
77
  // JsonKV - key/value pair convenience struct for Object
64
78
  // ============================================================================
65
79
 
80
+ /// Key-value pair for JSON object entries.
66
81
  JsonKV :: struct(
67
82
  key : String,
68
83
  value : JsonValue
69
84
  );
70
85
 
71
86
  impl(JsonValue,
72
- // Get a field from an Object value. Returns .None if not an Object or key missing.
87
+ /// Get a field from an Object value by key. Returns `.None` if not an Object or key is missing.
73
88
  get : (fn(self: Self, key: String) -> Option(JsonValue))(
74
89
  match(self,
75
90
  .Object(keys, values) => {
@@ -88,7 +103,7 @@ impl(JsonValue,
88
103
  )
89
104
  ),
90
105
 
91
- // Get an element from an Array value by index.
106
+ /// Get an element from an Array value by index.
92
107
  at : (fn(self: Self, index: usize) -> Option(JsonValue))(
93
108
  match(self,
94
109
  .Array(items) => items.get(index),
@@ -96,7 +111,7 @@ impl(JsonValue,
96
111
  )
97
112
  ),
98
113
 
99
- // Extract a bool.
114
+ /// Extract the boolean value, or `.None` if not a `Bool`.
100
115
  as_bool : (fn(self: Self) -> Option(bool))(
101
116
  match(self,
102
117
  .Bool(b) => .Some(b),
@@ -104,7 +119,7 @@ impl(JsonValue,
104
119
  )
105
120
  ),
106
121
 
107
- // Extract a number.
122
+ /// Extract the numeric value, or `.None` if not a `Number`.
108
123
  as_number : (fn(self: Self) -> Option(f64))(
109
124
  match(self,
110
125
  .Number(n) => .Some(n),
@@ -112,7 +127,7 @@ impl(JsonValue,
112
127
  )
113
128
  ),
114
129
 
115
- // Extract a string.
130
+ /// Extract the string value, or `.None` if not a `Str`.
116
131
  as_string : (fn(self: Self) -> Option(String))(
117
132
  match(self,
118
133
  .Str(s) => .Some(s),
@@ -120,7 +135,7 @@ impl(JsonValue,
120
135
  )
121
136
  ),
122
137
 
123
- // Extract an array.
138
+ /// Extract the array items, or `.None` if not an `Array`.
124
139
  as_array : (fn(self: Self) -> Option(ArrayList(JsonValue)))(
125
140
  match(self,
126
141
  .Array(items) => .Some(items),
@@ -128,7 +143,7 @@ impl(JsonValue,
128
143
  )
129
144
  ),
130
145
 
131
- // Extract object entries as ArrayList(JsonKV).
146
+ /// Extract object entries as `ArrayList(JsonKV)`, or `.None` if not an `Object`.
132
147
  as_object : (fn(self: Self) -> Option(ArrayList(JsonKV)))(
133
148
  match(self,
134
149
  .Object(keys, values) => {
@@ -468,6 +483,7 @@ _parse_value :: (fn(p: _Parser) -> Result(JsonValue, JsonError))({
468
483
  // Public API
469
484
  // ============================================================================
470
485
 
486
+ /// Parse a JSON string into a `JsonValue` tree. Throws via `Exception` on invalid input.
471
487
  json_parse :: (fn(s: str, using(exn : Exception)) -> JsonValue)({
472
488
  p := _Parser.new(s);
473
489
  match(_parse_value(p),
@@ -558,13 +574,14 @@ _stringify_into :: (fn(value: JsonValue, out: ArrayList(u8)) -> unit)({
558
574
  );
559
575
  });
560
576
 
577
+ /// Serialize a `JsonValue` to a compact JSON string.
561
578
  json_stringify :: (fn(value: JsonValue) -> String)({
562
579
  out := ArrayList(u8).new();
563
580
  _stringify_into(value, out);
564
581
  String.from_bytes(out)
565
582
  });
566
583
 
567
- // Serialise with pretty-printing (delegates to compact for now).
584
+ /// Serialize a `JsonValue` to a pretty-printed JSON string.
568
585
  json_stringify_pretty :: (fn(value: JsonValue, _indent: usize) -> String)(
569
586
  json_stringify(value)
570
587
  );
@@ -1,14 +1,14 @@
1
- // Punycode codec (RFC 3492)
2
- //
3
- // Provides punycode encoding/decoding and IDN hostname conversion.
4
- //
5
- // Example:
6
- // { punycode_decode, punycode_encode, to_unicode, to_ascii } :: import "std/encoding/punycode";
7
- //
8
- // encoded := punycode_encode(`München`);
9
- // decoded := punycode_decode(encoded);
10
- // ascii_domain := to_ascii(`münchen.de`); // "xn--mnchen-3ya.de"
11
- // unicode_domain := to_unicode(ascii_domain); // "münchen.de"
1
+ //! Punycode encoding/decoding (RFC 3492) for internationalized domain names.
2
+ //!
3
+ //! Provides punycode encoding/decoding and IDN hostname conversion.
4
+ //!
5
+ //! Example:
6
+ //! { punycode_decode, punycode_encode, to_unicode, to_ascii } :: import "std/encoding/punycode";
7
+ //!
8
+ //! encoded := punycode_encode(`München`);
9
+ //! decoded := punycode_decode(encoded);
10
+ //! ascii_domain := to_ascii(`münchen.de`); // "xn--mnchen-3ya.de"
11
+ //! unicode_domain := to_unicode(ascii_domain); // "münchen.de"
12
12
 
13
13
  open import "../string";
14
14
  { ArrayList } :: import "../collections/array_list";
@@ -122,8 +122,9 @@ _string_to_codepoints :: (fn(s: String) -> ArrayList(i32))({
122
122
  cps
123
123
  });
124
124
 
125
- // Decode a punycode-encoded string (without the xn-- prefix).
126
- // Returns .Some(decoded) on success, .None on error.
125
+ /// Decode a punycode-encoded string (without the `xn--` prefix).
126
+ ///
127
+ /// Returns `.Some(decoded)` on success, `.None` on invalid input.
127
128
  punycode_decode :: (fn(input: String) -> Option(String))({
128
129
  (bytes : ArrayList(u8)) = input.as_bytes();
129
130
  (input_len : i32) = i32(bytes.len());
@@ -220,7 +221,7 @@ punycode_decode :: (fn(input: String) -> Option(String))({
220
221
  .Some(String.from_bytes(result_bytes))
221
222
  });
222
223
 
223
- // Encode a Unicode string to punycode (without the xn-- prefix).
224
+ /// Encode a Unicode string to punycode (without the `xn--` prefix).
224
225
  punycode_encode :: (fn(input: String) -> String)({
225
226
  (cps : ArrayList(i32)) = _string_to_codepoints(input);
226
227
  (cp_count : i32) = i32(cps.len());
@@ -300,8 +301,9 @@ punycode_encode :: (fn(input: String) -> String)({
300
301
  String.from_bytes(out)
301
302
  });
302
303
 
303
- // Convert an IDN hostname to Unicode display form.
304
- // Splits on '.', decodes xn-- labels, keeps original on failure.
304
+ /// Convert an IDN hostname to Unicode display form.
305
+ ///
306
+ /// Splits on `.`, decodes `xn--` labels via punycode, and keeps original labels on failure.
305
307
  to_unicode :: (fn(hostname: String) -> String)({
306
308
  (parts : ArrayList(String)) = hostname.split(`.`);
307
309
  (result : String) = ``;
@@ -331,8 +333,9 @@ to_unicode :: (fn(hostname: String) -> String)({
331
333
  result
332
334
  });
333
335
 
334
- // Convert a Unicode hostname to ASCII punycode form.
335
- // Non-ASCII labels get xn-- prefix.
336
+ /// Convert a Unicode hostname to ASCII-compatible encoding.
337
+ ///
338
+ /// Non-ASCII labels are punycode-encoded with the `xn--` prefix.
336
339
  to_ascii :: (fn(hostname: String) -> String)({
337
340
  (parts : ArrayList(String)) = hostname.split(`.`);
338
341
  (result : String) = ``;
@@ -1,28 +1,35 @@
1
- // std/encoding/toml.yo - Basic TOML parser
2
- //
3
- // Parses a subset of TOML into a TomlValue tree.
4
- // Supports: strings, integers, booleans, table sections, comments.
5
- //
6
- // Example:
7
- // { toml_parse, TomlValue } :: import "std/encoding/toml";
8
- // // Or via the encoding index:
9
- // { toml_parse, TomlValue } :: import "std/encoding";
1
+ //! Basic TOML parser for configuration files.
2
+ //!
3
+ //! Parses a subset of TOML into a `TomlValue` tree.
4
+ //! Supports: strings, integers, booleans, table sections, comments.
5
+ //!
6
+ //! Example:
7
+ //! { toml_parse, TomlValue } :: import "std/encoding/toml";
8
+ //! // Or via the encoding index:
9
+ //! { toml_parse, TomlValue } :: import "std/encoding";
10
10
 
11
11
  open import "../string";
12
12
  { ArrayList } :: import "../collections/array_list";
13
13
 
14
+ /// TOML value type — the result of parsing a TOML document.
14
15
  TomlValue :: enum(
16
+ /// A TOML string value.
15
17
  Str(value: String),
18
+ /// A TOML integer value.
16
19
  Int(value: i64),
20
+ /// A TOML boolean value.
17
21
  Bool(value: bool),
22
+ /// A TOML table (ordered key-value map).
18
23
  Table(keys: ArrayList(String), values: ArrayList(Self))
19
24
  );
20
25
 
21
26
  impl(TomlValue,
27
+ /// Create a new empty table.
22
28
  new_table : (fn() -> Self)(
23
29
  Self.Table(keys: ArrayList(String).new(), values: ArrayList(TomlValue).new())
24
30
  ),
25
31
 
32
+ /// Look up a value by key in a table. Returns `.None` if not a table or key is missing.
26
33
  get : (fn(self: Self, key: String) -> Option(TomlValue))(
27
34
  match(self,
28
35
  .Table(keys, values) => {
@@ -41,10 +48,12 @@ impl(TomlValue,
41
48
  )
42
49
  ),
43
50
 
51
+ /// Check whether the table contains the given key.
44
52
  has_key : (fn(self: Self, key: String) -> bool)(
45
53
  self.get(key).is_some()
46
54
  ),
47
55
 
56
+ /// Insert or update a key-value pair in the table.
48
57
  set : (fn(self: Self, key: String, value: TomlValue) -> unit)(
49
58
  match(self,
50
59
  .Table(keys, values) => {
@@ -72,6 +81,7 @@ impl(TomlValue,
72
81
  )
73
82
  ),
74
83
 
84
+ /// Return the number of keys in the table, or `0` if not a table.
75
85
  table_len : (fn(self: Self) -> usize)(
76
86
  match(self,
77
87
  .Table(keys, _) => keys.len(),
@@ -79,14 +89,17 @@ impl(TomlValue,
79
89
  )
80
90
  ),
81
91
 
92
+ /// Extract the string value, or `.None` if not a `Str`.
82
93
  as_string : (fn(self: Self) -> Option(String))(
83
94
  match(self, .Str(s) => .Some(s), _ => .None)
84
95
  ),
85
96
 
97
+ /// Extract the integer value, or `.None` if not an `Int`.
86
98
  as_int : (fn(self: Self) -> Option(i64))(
87
99
  match(self, .Int(n) => .Some(n), _ => .None)
88
100
  ),
89
101
 
102
+ /// Extract the boolean value, or `.None` if not a `Bool`.
90
103
  as_bool : (fn(self: Self) -> Option(bool))(
91
104
  match(self, .Bool(b) => .Some(b), _ => .None)
92
105
  )
@@ -129,6 +142,7 @@ _parse_toml_value :: (fn(raw: String) -> Result(TomlValue, String))({
129
142
  .Err(`Unknown value: ${trimmed}`)
130
143
  });
131
144
 
145
+ /// Parse a TOML string into a `TomlValue` tree. Returns `Result(TomlValue, String)`.
132
146
  toml_parse :: (fn(input: String) -> Result(TomlValue, String))({
133
147
  root_keys := ArrayList(String).new();
134
148
  root_vals := ArrayList(TomlValue).new();
@@ -1,12 +1,12 @@
1
- // std/encoding/utf16.yo - UTF-8 <-> UTF-16 conversion
2
- //
3
- // Converts between Yo's UTF-8 String and UTF-16LE/BE (as ArrayList(u16)).
4
- //
5
- // Example:
6
- // { utf8_to_utf16, utf16_to_utf8 } :: import "std/encoding/utf16";
7
- //
8
- // words := utf8_to_utf16("hello");
9
- // s := utf16_to_utf8(words);
1
+ //! UTF-8 to UTF-16 conversion and vice versa.
2
+ //!
3
+ //! Converts between Yo's UTF-8 `str` and UTF-16 code units (`ArrayList(u16)`).
4
+ //!
5
+ //! Example:
6
+ //! { utf8_to_utf16, utf16_to_utf8 } :: import "std/encoding/utf16";
7
+ //!
8
+ //! words := utf8_to_utf16("hello");
9
+ //! s := utf16_to_utf8(words);
10
10
 
11
11
  open import "../string";
12
12
  { ArrayList } :: import "../collections/array_list";
@@ -17,6 +17,9 @@ open import "../string";
17
17
  // UTF-8 → UTF-16
18
18
  // ============================================================================
19
19
 
20
+ /// Convert a UTF-8 string to an `ArrayList(u16)` of UTF-16 code units.
21
+ ///
22
+ /// Handles BMP characters directly and encodes supplementary characters as surrogate pairs.
20
23
  utf8_to_utf16 :: (fn(s: str) -> ArrayList(u16))({
21
24
  out := ArrayList(u16).new();
22
25
  i := usize(0);
@@ -64,6 +67,10 @@ export utf8_to_utf16;
64
67
  // UTF-16 → UTF-8
65
68
  // ============================================================================
66
69
 
70
+ /// Convert UTF-16 code units to a UTF-8 `String`.
71
+ ///
72
+ /// Decodes surrogate pairs back into supplementary code points.
73
+ /// Throws via `Exception` on unpaired surrogates.
67
74
  utf16_to_utf8 :: (fn(data: ArrayList(u16), using(exn : Exception)) -> String)({
68
75
  out := ArrayList(u8).new();
69
76
  i := usize(0);
package/std/error.yo CHANGED
@@ -1,14 +1,10 @@
1
- // std/error.yo - Error trait
2
- //
3
- // Provides a standard Error trait for typed error propagation.
4
- // All error types in the standard library should implement this trait.
1
+ //! Standard error handling types and traits for Yo.
2
+
5
3
  open import "./string";
6
4
  open import "./fmt";
7
5
 
8
- // ============================================================================
9
- // Error trait
10
- // ============================================================================
11
-
6
+ /// Standard error trait for typed error propagation.
7
+ /// All error types should implement this trait along with `ToString`.
12
8
  Error :: trait(
13
9
  (source : (fn(self: *(Self)) -> Option(Dyn(SelfTrait)))) ?= ((self) -> .None),
14
10
 
@@ -16,32 +12,23 @@ Error :: trait(
16
12
  );
17
13
  export Error;
18
14
 
15
+ /// Type-erased dynamic error type. Wraps any type implementing `Error`.
19
16
  AnyError :: Dyn(Error);
20
17
  export AnyError;
21
18
 
22
- // ============================================================================
23
- // Error implementations for standard types
24
- // ============================================================================
25
-
19
+ /// Allows using `String` directly as an error type.
26
20
  impl(String, Error());
27
21
 
28
- /// === Exception ===
29
- // This is a non-resumable exception handling.
30
- //
31
- // Example (function that may throw):
32
- //
33
- // safe_div :: (fn(x: i32, y: i32, using(exn : Exception)) -> i32)(
34
- // cond(
35
- // (y == i32(0)) => exn.throw(dyn `division by zero`)),
36
- // true => (x / y)
37
- // )
38
- // );
22
+ /// Non-resumable exception handling effect module.
23
+ /// Use `throw` to raise an `AnyError` and abort the current computation.
39
24
  Exception :: module(
40
25
  throw : (fn(forall(ResumeType : Type), error : AnyError) -> ResumeType)
41
26
  );
42
27
  export Exception;
43
28
 
44
29
 
30
+ /// Creates a resumable exception module parameterized by the resume type.
31
+ /// Unlike `Exception`, the handler can return a value to resume the computation.
45
32
  ResumableException :: (fn(comptime(ResumeType) : Type) -> comptime(Module))(
46
33
  module(
47
34
  throw : (fn(error : AnyError) -> ResumeType)
@@ -1,25 +1,23 @@
1
- // std/fmt/display.yo - Display trait
2
- //
3
- // The Display trait provides a standard way to format a value for
4
- // human-readable output using a Writer. It complements ToString
5
- // (which always returns a String) by streaming into an existing Writer.
6
- //
7
- // Example:
8
- // impl(MyType, Display(
9
- // display : (fn(self: *(Self), writer: Writer) -> Writer)(
10
- // writer.write_str("MyType(").write_i64(i64(self.value)).write_str(")")
11
- // )
12
- // ));
1
+ //! Display trait for streaming formatted output to a Writer.
2
+ //!
3
+ //! # Example
4
+ //!
5
+ //! ```rust
6
+ //! impl(MyType, Display(
7
+ //! display : (fn(self: *(Self), writer: Writer) -> Writer)(
8
+ //! writer.write_str("MyType(").write_i64(i64(self.value)).write_str(")")
9
+ //! )
10
+ //! ));
11
+ //! ```
13
12
 
14
13
  { Writer } :: import "./writer";
15
14
 
16
- // ============================================================================
17
- // Display trait
18
- // ============================================================================
15
+ /// Trait for formatting values by writing to a `Writer` rather than allocating a `String`.
16
+ /// Complement to `ToString` for streaming output into an existing buffer.
19
17
 
20
- Display :: (fn(comptime(T) : Type) -> comptime(Type))
18
+ Display :: (fn(comptime(T) : Type) -> comptime(Trait))
21
19
  trait(
22
- display :: (fn(self: *(T), writer: Writer) -> Writer)
20
+ display : (fn(self: *(T), writer: Writer) -> Writer)
23
21
  )
24
22
  ;
25
23
 
package/std/fmt/index.yo CHANGED
@@ -1,16 +1,12 @@
1
+ //! Formatted output to stdout and stderr.
2
+
1
3
  { fwrite, stdout, stderr } :: import "../libc/stdio";
2
4
  { String } :: import "../string";
3
5
  { ToString } :: import "./to_string.yo";
4
6
 
5
7
  export ToString;
6
8
 
7
- /**
8
- * Print a value that implements ToString to stdout, with a newline.
9
- *
10
- * Example:
11
- * println(42); // prints "42\n"
12
- * println("hello"); // prints "hello\n"
13
- */
9
+ /// Print a value that implements `ToString` to stdout, followed by a newline.
14
10
  println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
15
11
  s := v.to_string();
16
12
  str_bytes := s.as_bytes();
@@ -26,13 +22,7 @@ println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
26
22
  });
27
23
  export println;
28
24
 
29
- /**
30
- * Print a value that implements ToString to stdout, without a newline.
31
- *
32
- * Example:
33
- * print("hello ");
34
- * print("world"); // prints "hello world"
35
- */
25
+ /// Print a value that implements `ToString` to stdout, without a trailing newline.
36
26
  print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
37
27
  s := v.to_string();
38
28
  str_bytes := s.as_bytes();
@@ -47,12 +37,7 @@ print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
47
37
  });
48
38
  export print;
49
39
 
50
- /**
51
- * Print a value that implements ToString to stderr, with a newline.
52
- *
53
- * Example:
54
- * eprintln("Error: something went wrong");
55
- */
40
+ /// Print a value that implements `ToString` to stderr, followed by a newline.
56
41
  eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
57
42
  s := v.to_string();
58
43
  str_bytes := s.as_bytes();
@@ -68,13 +53,7 @@ eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
68
53
  });
69
54
  export eprintln;
70
55
 
71
- /**
72
- * Print a value that implements ToString to stderr, without a newline.
73
- *
74
- * Example:
75
- * eprint("Warning: ");
76
- * eprintln("be careful");
77
- */
56
+ /// Print a value that implements `ToString` to stderr, without a trailing newline.
78
57
  eprint :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
79
58
  s := v.to_string();
80
59
  str_bytes := s.as_bytes();
@@ -1,16 +1,10 @@
1
+ //! ToString trait and implementations for all primitive types.
2
+
1
3
  { String, rune } :: import "../string";
2
4
  { snprintf } :: import "../libc/stdio";
3
5
 
4
- /**
5
- * ToString - A trait for converting values to String representation.
6
- *
7
- * This is similar to:
8
- * - Rust's `std::fmt::Display` / `ToString` trait
9
- * - Go's `fmt.Stringer` interface
10
- * - Java's `Object.toString()` method
11
- *
12
- * Types implementing this trait can be used with `println`, `print`, and `format`.
13
- */
6
+ /// Trait for converting values to their `String` representation.
7
+ /// Types implementing this trait can be used with `println`, `print`, and template strings.
14
8
  ToString :: trait(
15
9
  to_string : (fn(self: *(Self)) -> String)
16
10
  );