cli: add recoverable endian APIs and qualified std enums
This commit is contained in:
45 files changed
+1205
-89
No files matched your search
@@ -28,7 +28,7 @@ fn_decl = "fn" ident "(" [ param { "," param } ] ")" "->" type [ "from" id
|
||||
block ; (* `trusted` is reserved and rejected, E7006 *)
|
||||
param = ident ":" type ;
|
||||
|
||||
type = int_type | "bool" | "void" | "never" | ident
|
||||
type = int_type | "bool" | "void" | "never" | path
|
||||
| "[" type ";" int_lit "]"
|
||||
| wrapper "<" type ">"
|
||||
| "cap" "<" path ">"
|
||||
@@ -64,7 +64,7 @@ if_expr = "if" expr block [ "else" ( if_expr | block ) ] ;
|
||||
match_expr = "match" expr "{" { arm } "}" ;
|
||||
arm = pattern "=>" ( block | expr "," ) ;
|
||||
pattern = "_" | int_lit (* integer matches only; `_` is required there *)
|
||||
| ident "::" ident [ "(" ident { "," ident } ")" ] (* enum variant: Shape::Rect(w, h); no `_` in enum matches *)
|
||||
| path "::" ident [ "(" ident { "," ident } ")" ] (* enum variant: Shape::Rect(w, h); no `_` in enum matches *)
|
||||
| ident [ "(" ident { "," ident } ")" ] ; (* outcome of a fallible operation: ok(n), closed *)
|
||||
|
||||
select_stmt = "select" "{" { select_arm } "cancelled" [ "(" ident { "," ident } ")" ] "=>" ( block | expr "," ) "}" ;
|
||||
@@ -89,7 +89,7 @@ member = ident | keyword ; (* after `.` a keyword is
|
||||
call_args = [ "<" type ">" ] "(" [ expr { "," expr } ] ")" ;
|
||||
primary = int_lit | "true" | "false" | string
|
||||
| ident [ "{" field_init { "," field_init } [ "," ] "}" ] (* struct literal *)
|
||||
| ident "::" ident (* enum variant; applied by a call *)
|
||||
| path "::" ident (* enum variant; applied by a call *)
|
||||
| "[" expr ";" int_lit "]"
|
||||
| "(" expr ")"
|
||||
| if_expr | match_expr | block ;
|
||||
|
||||
@@ -36,3 +36,38 @@ effects []
|
||||
Unknown module/member or absent import: `E7002`; duplicate import: `E7006`; malformed import or type arguments: `E7001`; wrong argument type/arity: `E7005`; conflicting borrows: `E2101`. No new diagnostic code.
|
||||
|
||||
The example is `tests/conformance/source/quickref/std.aji`; library boundary and algorithm tests live under `tests/conformance/source/std/`.
|
||||
|
||||
## Endian integers
|
||||
|
||||
All functions operate at slice offset zero. Pass a sub-slice to select an offset. Extra bytes are ignored on read and preserved on write. No alignment or heap allocation is required. A short destination is unchanged; a short source yields no integer payload.
|
||||
|
||||
| Width | Read functions (`src: slice<u8>`) | Result | Write functions (`dst: slice_mut<u8>, value: uW`) | Result |
|
||||
|---|---|---|---|---|
|
||||
| 16 | `std.bytes.read_u16_le`, `std.bytes.read_u16_be` | `std.bytes.ReadU16` | `std.bytes.write_u16_le`, `std.bytes.write_u16_be` | `std.bytes.WriteResult` |
|
||||
| 32 | `std.bytes.read_u32_le`, `std.bytes.read_u32_be` | `std.bytes.ReadU32` | `std.bytes.write_u32_le`, `std.bytes.write_u32_be` | `std.bytes.WriteResult` |
|
||||
| 64 | `std.bytes.read_u64_le`, `std.bytes.read_u64_be` | `std.bytes.ReadU64` | `std.bytes.write_u64_le`, `std.bytes.write_u64_be` | `std.bytes.WriteResult` |
|
||||
|
||||
Read enums have exactly `ok(uW)` and `short_input`. `WriteResult` has exactly `ok` and `short_output`. These are ordinary Copy enums, handled with qualified enum patterns; they are not intrinsic outcome matches. They can be passed or returned through user functions. No generic must-use rule is introduced.
|
||||
|
||||
```aji
|
||||
module demo.endian
|
||||
import std.bytes;
|
||||
|
||||
fn main() -> u32
|
||||
effects []
|
||||
{
|
||||
var wire: [u8; 6] = zero;
|
||||
match std.bytes.write_u32_be(mut wire[1..5], 0x12345678) {
|
||||
std.bytes.WriteResult::ok => {}
|
||||
std.bytes.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
match std.bytes.read_u32_be(ref wire[1..5]) {
|
||||
std.bytes.ReadU32::ok(value) => { return value; }
|
||||
std.bytes.ReadU32::short_input => { return 0; }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Missing enum arm: `E7004`; duplicate/wildcard enum arm: `E7006`; unknown type/variant or missing import: `E7002`; wrong width/payload, immutable destination or variant from another enum: `E7005`; conflicting destination borrows: `E2101`. Existing `or trap` only applies to fallible built-in operations, so it is not used with these enums.
|
||||
|
||||
The example is checked in `source/quickref/std_endian.aji`; fixtures for every width/order and all short lengths are in `source/std/endian_u*.aji`.
|
||||
Reference in new issue
Block a user