mutable decl syntax change

This commit is contained in:
2026-08-05 21:19:41 +02:00
parent 88b94197c9
commit 081a3fd5df
74 changed files with 870 additions and 733 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ reserve func($T type, list @mut ArrayList(T), min_capacity usize) void ! mem.All
return
}
new_capacity usize = 8
new_capacity usize := 8
if list.capacity >= 8 {
half usize :: divtrunc!(list.capacity, 2)
if list.capacity > maxval!(usize) - half {
+3 -3
View File
@@ -2,7 +2,7 @@ import "@std/mem"
import "@std/testing"
handles_append test {
list ArrayList(i32) = init(mem.c_allocator)
list ArrayList(i32) := init(mem.c_allocator)
defer deinit(&list)
try append(&list, 42)
@@ -12,7 +12,7 @@ handles_append test {
}
handles_clear test {
list ArrayList(i32) = init(mem.c_allocator)
list ArrayList(i32) := init(mem.c_allocator)
defer deinit(&list)
try append(&list, 42)
@@ -22,7 +22,7 @@ handles_clear test {
}
handles_reserve test {
list ArrayList(i32) = init(mem.c_allocator)
list ArrayList(i32) := init(mem.c_allocator)
defer deinit(&list)
try reserve(&list, 10)
+1 -1
View File
@@ -11,7 +11,7 @@ print func($format []u8, $Args type, args Args) void {
}
hide write func(_ ?@mut anyopaque, handle io.Handle, bytes []u8) usize ! io.WriteError {
request usize = bytes.len
request usize := bytes.len
maximum usize :: usize(maxval!(c_long))
if request > maximum {
request = maximum
+1 -1
View File
@@ -14,7 +14,7 @@ init func(
$E, $V type,
values meta.EnumFieldStruct(E, ?V, some!(null)),
) EnumMap(E, V) {
map EnumMap(E, V) = undefined
map EnumMap(E, V) := undefined
match typeinfo!(E) {
.enum |info|: inline for info.fields |field, i| {
+1 -1
View File
@@ -8,7 +8,7 @@ TestEnum :: enum(u8) {
}
handles_sparse_enum_get test {
names EnumMap(TestEnum, []u8) = init({
names EnumMap(TestEnum, []u8) := init({
ident = "identifier",
int = "integer",
})
+5 -5
View File
@@ -59,7 +59,7 @@ get func(
if (map.count == 0) return null
hash :: normalize(hash_key(key))
idx usize = hash & (map.entries.len - 1)
idx usize := hash & (map.entries.len - 1)
while true {
entry :: map.entries[idx]
@@ -98,7 +98,7 @@ put func(
if (entry.hash == 0) continue
# find an empty slot
idx usize = entry.hash & (new_entries.len - 1)
idx usize := entry.hash & (new_entries.len - 1)
while new_entries[idx].hash != 0 {
idx = (idx + 1) & (new_entries.len - 1)
}
@@ -112,7 +112,7 @@ put func(
# put new entry
hash :: normalize(hash_key(key))
idx usize = hash & (map.entries.len - 1)
idx usize := hash & (map.entries.len - 1)
while true {
entry :: map.entries[idx]
@@ -144,8 +144,8 @@ hide normalize func(hash usize) usize {
#! FNV-1a hash implementation.
#! note: vulnerable to collision attacks.
hide str_hash func(key []u8) usize {
hash u32 = 2166136261 # offset basis
prime u32 = 16777619
hash u32 := 2166136261 # offset basis
prime u32 := 16777619
for key |byte| {
product u64 :: u64(hash xor u32(byte)) * prime
+1 -1
View File
@@ -2,7 +2,7 @@ import "@std/mem"
import "@std/testing"
handles_put_and_get test {
map StringHashMap(u32) = init(mem.c_allocator)
map StringHashMap(u32) := init(mem.c_allocator)
defer deinit(&map)
try put(&map, "key", 42)
+3 -3
View File
@@ -47,7 +47,7 @@ writer func(file File) Writer {
}
hide system_read func(_ ?@mut anyopaque, handle Handle, buffer []mut u8) usize ! ReadError {
request usize = buffer.len
request usize := buffer.len
maximum usize :: usize(maxval!(c_long))
if request > maximum {
request = maximum
@@ -69,7 +69,7 @@ hide system_read func(_ ?@mut anyopaque, handle Handle, buffer []mut u8) usize !
}
hide system_write func(_ ?@mut anyopaque, handle Handle, bytes []u8) usize ! WriteError {
request usize = bytes.len
request usize := bytes.len
maximum usize :: usize(maxval!(c_long))
if request > maximum {
request = maximum
@@ -91,7 +91,7 @@ hide system_write func(_ ?@mut anyopaque, handle Handle, bytes []u8) usize ! Wri
}
hide system_open func(_ ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenError {
flags c_int = c.O_RDONLY
flags c_int := c.O_RDONLY
match mode {
.read_only: flags = c.O_RDONLY
.write_only: flags = c.O_WRONLY
+19 -19
View File
@@ -73,7 +73,7 @@ write func(output Writer, bytes []u8) usize ! WriteError {
}
write_all func(output Writer, bytes []u8) void ! WriteError {
offset usize = 0
offset usize := 0
while offset < bytes.len {
count usize :: write(output, bytes[offset..]) catch |err| {
return err
@@ -129,12 +129,12 @@ print func(output Writer, $format []u8, $Args type, args Args) void ! WriteError
}
hide write_integer_signed func(output Writer, value i64, base u64, uppercase bool) void ! WriteError {
buffer [65]mut u8 = undefined
end usize = buffer.len
current i64 = value
buffer [65]mut u8 := undefined
end usize := buffer.len
current i64 := value
while true {
digit_value i64 :: rem!(current, i64(base))
digit u8 = 0
digit u8 := 0
if digit_value < 0 {
digit = u8(-digit_value)
} else {
@@ -162,9 +162,9 @@ hide write_integer_signed func(output Writer, value i64, base u64, uppercase boo
}
hide write_integer_unsigned func(output Writer, value u64, base u64, uppercase bool) void ! WriteError {
buffer [65]mut u8 = undefined
end usize = buffer.len
current u64 = value
buffer [65]mut u8 := undefined
end usize := buffer.len
current u64 := value
while true {
digit u8 :: u8(rem!(current, base))
end -= 1
@@ -206,11 +206,11 @@ hide FormatToken :: struct {
}
hide parse_format func($N usize, $format []u8, $Args type) [N]mut FormatToken {
tokens [N]mut FormatToken = undefined
tokens [N]mut FormatToken := undefined
for (usize(0))..format.len |index| {
tokens[index] = FormatToken {kind = .unused, start = 0, end = 0, field = ""}
}
field_count usize = 0
field_count usize := 0
match typeinfo!(Args) {
.record |record|: {
if !record.is_tuple {
@@ -221,10 +221,10 @@ hide parse_format func($N usize, $format []u8, $Args type) [N]mut FormatToken {
else: compile_error!("io.print arguments must be a tuple")
}
token_count usize = 0
argument_count usize = 0
literal_start usize = 0
cursor usize = 0
token_count usize := 0
argument_count usize := 0
literal_start usize := 0
cursor usize := 0
while cursor < format.len {
byte :: format[cursor]
if byte == '{' {
@@ -243,8 +243,8 @@ hide parse_format func($N usize, $format []u8, $Args type) [N]mut FormatToken {
literal_start = cursor
continue
}
kind FormatTokenKind = .default
width usize = 2
kind FormatTokenKind := .default
width usize := 2
if next != '}' {
if cursor + 2 >= format.len or format[cursor + 2] != '}' {
compile_error!("io.print format expects a one-character specifier")
@@ -353,8 +353,8 @@ hide write_integer func(output Writer, $T type, value T, base u64, uppercase boo
hide write_float func(output Writer, $T type, value T, scientific bool) void ! WriteError {
match typeinfo!(T) {
.float: {
buffer [64]mut u8 = undefined
count c_int = 0
buffer [64]mut u8 := undefined
count c_int := 0
if sizeof!(T) == 4 {
if scientific {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.8e", value)
@@ -401,7 +401,7 @@ hide write_character func(output Writer, $T type, value T) void ! WriteError {
if minval!(T) < 0 or maxval!(T) > 255 {
compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
}
buffer [1]u8 = [u8(value)]
buffer [1]u8 := [u8(value)]
try write_all(output, buffer[..])
}
.distinct |backing|: if scalar_or_distinct_type(backing) {
+11 -11
View File
@@ -46,7 +46,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
return .out_of_memory
}
memory ?*mut u8 = raw_alloc(allocator, count * element_size, alignof!(T))
memory ?*mut u8 := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
return pointer[..count]
@@ -75,13 +75,13 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
return .out_of_memory
}
old_memory ?*mut u8 = null
old_size usize = 0
old_memory ?*mut u8 := null
old_size usize := 0
if memory.len != 0 {
old_memory = ptrcast!(u8, memory.ptr)
old_size = memory.len * element_size
}
resized ?*mut u8 = raw_realloc(
resized ?*mut u8 := raw_realloc(
allocator,
old_memory,
old_size,
@@ -119,15 +119,15 @@ empty func($T type) []mut T {
return empty_slice(T, 0)
}
hide empty_storage [1]mut u64 = [0]
hide empty_storage [1]mut u64 := [0]
hide malloc_alignment usize :: 16 # ponytail: aarch64-macos libc malloc alignment assumption.
hide power_of_two func(value usize) bool {
if (value == 0) return false
current usize = value
current usize := value
while current > 1 {
half usize = divtrunc!(current, 2)
half usize := divtrunc!(current, 2)
if (half * 2 != current) return false
current = half
}
@@ -141,8 +141,8 @@ hide c_alloc func(_ ?@mut anyopaque, size usize, alignment usize) ?*mut u8 {
return ptrcast!(u8, c.malloc(c_ulong(size)))
}
memory [1]mut ?*mut anyopaque = [null]
status c_int = c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size))
memory [1]mut ?*mut anyopaque := [null]
status c_int := c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size))
if (status != 0) return null
return ptrcast!(u8, memory[0])
@@ -161,9 +161,9 @@ hide c_realloc func(_ ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 = c_alloc(null, new_size, alignment)
new_memory ?*mut u8 := c_alloc(null, new_size, alignment)
if new_memory |new_bytes| {
copy_size usize = old_size
copy_size usize := old_size
if (new_size < copy_size) copy_size = new_size
memcopy!(new_bytes[..copy_size], old_memory[..copy_size])
c.free(old_memory)
+3 -3
View File
@@ -46,9 +46,9 @@ TypeInfo :: union(enum) {
EnumFieldStruct func($E, $Field type, $default ?Field) type {
match typeinfo!(E) {
.enum |info|: {
names [info.fields.len]mut []u8 = undefined
field_types [info.fields.len]mut type = undefined
defaults [info.fields.len]mut ?Field = undefined
names [info.fields.len]mut []u8 := undefined
field_types [info.fields.len]mut type := undefined
defaults [info.fields.len]mut ?Field := undefined
inline for info.fields |field, index| {
names[index] = field.name
field_types[index] = Field
+2 -2
View File
@@ -42,7 +42,7 @@ distinct_reflection_exposes_immediate_backing test {
enum_field_struct_defaults test {
names TestNames = {
names TestNames := {
ident = "identifier",
int = "integer",
}
@@ -60,7 +60,7 @@ enum_field_struct_defaults test {
try testing.expect(false)
}
empty TestNames = {}
empty TestNames := {}
if field!(empty, "ident") |_| {
try testing.expect(false)
}
+8 -8
View File
@@ -19,8 +19,8 @@ init func($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
compile_error!("static string map has too many entries")
}
keys [N]mut []u8 = undefined
values [N]mut V = undefined
keys [N]mut []u8 := undefined
values [N]mut V := undefined
# assert no duplicate keys
for entries |entry, i| {
@@ -37,7 +37,7 @@ init func($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
}
if N == 0 {
len_indexes [0]u32 = undefined
len_indexes [0]u32 := undefined
return StaticStringMap(V){
keys = keys[..],
values = values[..],
@@ -51,7 +51,7 @@ init func($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
for 1..N |i| {
key :: keys[i]
value :: values[i]
j usize = i
j usize := i
while j > 0 and keys[j - 1].len > key.len : j -= 1 {
keys[j] = keys[j - 1]
values[j] = values[j - 1]
@@ -62,8 +62,8 @@ init func($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
min_len u32 :: u32(keys[0].len)
max_len u32 :: u32(keys[N - 1].len)
len_indexes [usize(max_len) + 1]mut u32 = undefined
entry_index usize = 0
len_indexes [usize(max_len) + 1]mut u32 := undefined
entry_index usize := 0
for 0..=usize(max_len) |length| {
while entry_index < N and keys[entry_index].len < length : entry_index += 1 {}
len_indexes[length] = u32(entry_index)
@@ -81,10 +81,10 @@ init func($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
get func($V type, map @StaticStringMap(V), key []u8) ?V {
if (map.keys.len == 0 or key.len > maxval!(u32)) return null
length u32 = u32(key.len)
length u32 := u32(key.len)
if (length < map.min_len or length > map.max_len) return null
idx usize = usize(map.len_indexes[usize(length)])
idx usize := usize(map.len_indexes[usize(length)])
while idx < map.keys.len : idx += 1 {
candidate :: map.keys[idx]
if (candidate.len != key.len) return null