cli: add bounded std copy/search and checked arithmetic

This commit is contained in:
dev committed 2026-10-06 15:40:57 +03:00
1 parent a2ca293506
commit 6397a317e2
36 files changed
+1533 -7

No files matched your search

@@ -0,0 +1,33 @@
; expect: run
; result: {"variant":"overflow","payload":[]}
; phase: 0
; spec: docs/spec/16-standard-library.md §2.1.1; docs/spec/07a-air-instructions.md
module test.bounded_arithmetic
air 0
lang 0.1
features [effects]
type @U64Result = enum { ok(u64), overflow }
fn @main() -> @U64Result
effects []
entry:
%a = const.u64 18446744073709551615
%b = const.u64 1
%result = call @checked_add(%a, %b)
ret %result
fn @checked_add(%a: u64, %b: u64) -> @U64Result
effects []
entry:
%maximum = const.u64 18446744073709551615
%remaining = sub.checked.u64 %maximum, %a
%overflows = gt.u64 %b, %remaining
cbr %overflows, @failure(), @success()
failure:
%error = enum.make @U64Result.overflow()
ret %error
success:
%sum = add.checked.u64 %a, %b
%ok = enum.make @U64Result.ok(%sum)
ret %ok
@@ -0,0 +1,16 @@
; expect: reject E1001
; phase: 0
; spec: docs/spec/16-standard-library.md §2.1.1; docs/spec/07a-air-instructions.md
module test.bounded_payload
air 0
lang 0.1
features []
type @U64Result = enum { ok(u64), overflow }
fn @main() -> @U64Result
effects []
entry:
%wrong = const.bool true
%result = enum.make @U64Result.ok(%wrong)
ret %result
+64
View File
@@ -0,0 +1,64 @@
; expect: run
; result: [5,5,5,165]
; phase: 1
; spec: docs/spec/16-standard-library.md §2.4; docs/spec/07a-air-instructions.md
module test.bounded_copy
air 0
lang 0.1
features [arrays, effects, ownership]
type @WriteResult = enum { ok, short_output }
fn @main() -> array<u8, 4>
effects []
entry:
%src_storage = local.alloc array<u8, 3>
%dst_storage = local.alloc array<u8, 4>
%five = const.u8 5
%marker = const.u8 165
%src_value = array.splat array<u8, 3> %five
%dst_value = array.splat array<u8, 4> %marker
local.store %src_storage, %src_value
local.store %dst_storage, %dst_value
%r = borrow.ref %src_storage
%src = slice.make %r
%m = borrow.mut %dst_storage
%dst = slice.make %m
%zero = const.usize 0
%two = const.usize 2
%short_dst = slice.sub %dst, %zero, %two
%short = call @copy(%short_dst, %src)
borrow.end %short_dst
%first = borrow.index %dst, %zero
%untouched = load %first
borrow.end %first
%same = eq.u8 %untouched, %marker
assert %same
enum.switch @WriteResult %short, ok -> @bad, short_output -> @copy_all
bad:
trap
copy_all:
%full = call @copy(%dst, %src)
enum.switch @WriteResult %full, ok -> @done, short_output -> @bad
done:
borrow.end %dst
borrow.end %m
borrow.end %src
borrow.end %r
%result = local.load %dst_storage
ret %result
fn @copy(%dst: slice_mut<u8>, %src: slice<u8>) -> @WriteResult
effects []
entry:
%capacity = slice.len %dst
%count = slice.len %src
%short = lt.usize %capacity, %count
cbr %short, @failure(), @success()
failure:
%error = enum.make @WriteResult.short_output()
ret %error
success:
slice.copy %dst, %src
%ok = enum.make @WriteResult.ok()
ret %ok