replace leading _ for private symbols with keyword hide

This commit is contained in:
2026-07-14 20:19:01 +02:00
parent 5157cf3bcc
commit 267947e79d
31 changed files with 125220 additions and 122709 deletions
+19 -19
View File
@@ -20,17 +20,17 @@ Token :: struct {
kind Kind
}
_is_alpha func(value u8) bool {
hide is_alpha func(value u8) bool {
return value == '_' or
value >= 'a' and value <= 'z' or
value >= 'A' and value <= 'Z'
}
_is_digit func(value u8) bool {
hide is_digit func(value u8) bool {
return value >= '0' and value <= '9'
}
_word_kind func(word []u8) Kind {
hide word_kind func(word []u8) Kind {
# ponytail: enough keywords for the demo; add the full language set when a parser needs it.
if mem.eql(word, "func") or mem.eql(word, "void") {
return .keyword
@@ -38,7 +38,7 @@ _word_kind func(word []u8) Kind {
return .identifier
}
_append func(tokens @mut std.ArrayList(Token), kind Kind, start, end usize) void ! mem.AllocError {
hide append_token func(tokens @mut std.ArrayList(Token), kind Kind, start, end usize) void ! mem.AllocError {
try arraylist.append(tokens, Token {
start = start,
length = end - start,
@@ -54,19 +54,19 @@ lex func(source []u8, tokens @mut std.ArrayList(Token)) void ! mem.AllocError {
if value == ' ' or value == '\t' or value == '\r' {
cursor += 1
} else if value == '\n' {
try _append(tokens, .newline, cursor, cursor + 1)
try append_token(tokens, .newline, cursor, cursor + 1)
cursor += 1
} else if value == '#' {
while cursor < source.len and source[cursor] != '\n' : cursor += 1 {}
} else if _is_alpha(value) {
} else if is_alpha(value) {
start usize :: cursor
cursor += 1
while cursor < source.len and (_is_alpha(source[cursor]) or _is_digit(source[cursor])) : cursor += 1 {}
try _append(tokens, _word_kind(source[start..cursor]), start, cursor)
} else if _is_digit(value) {
while cursor < source.len and (is_alpha(source[cursor]) or is_digit(source[cursor])) : cursor += 1 {}
try append_token(tokens, word_kind(source[start..cursor]), start, cursor)
} else if is_digit(value) {
start usize :: cursor
while cursor < source.len and _is_digit(source[cursor]) : cursor += 1 {}
try _append(tokens, .integer, start, cursor)
while cursor < source.len and is_digit(source[cursor]) : cursor += 1 {}
try append_token(tokens, .integer, start, cursor)
} else if value == '"' {
start usize :: cursor
cursor += 1
@@ -78,9 +78,9 @@ lex func(source []u8, tokens @mut std.ArrayList(Token)) void ! mem.AllocError {
}
if cursor < source.len and source[cursor] == '"' {
cursor += 1
try _append(tokens, .string, start, cursor)
try append_token(tokens, .string, start, cursor)
} else {
try _append(tokens, .invalid, start, cursor)
try append_token(tokens, .invalid, start, cursor)
}
} else {
start usize :: cursor
@@ -88,14 +88,14 @@ lex func(source []u8, tokens @mut std.ArrayList(Token)) void ! mem.AllocError {
if value == ':' and cursor < source.len and source[cursor] == ':' {
cursor += 1
}
try _append(tokens, .punctuation, start, cursor)
try append_token(tokens, .punctuation, start, cursor)
}
}
try _append(tokens, .eof, cursor, cursor)
try append_token(tokens, .eof, cursor, cursor)
return
}
_kind_name func(kind Kind) *c_char {
hide kind_name func(kind Kind) *c_char {
return match kind {
.invalid: "invalid"
.eof: "eof"
@@ -108,8 +108,8 @@ _kind_name func(kind Kind) *c_char {
}
}
_print_token func(source []u8, token Token) void {
_ = c.printf("%-11s", _kind_name(token.kind))
hide print_token func(source []u8, token Token) void {
_ = c.printf("%-11s", kind_name(token.kind))
if token.length != 0 {
_ = c.printf(" `")
i usize = 0
@@ -149,7 +149,7 @@ main func() i32 {
}
for tokens.items |token| {
_print_token(source, token)
print_token(source, token)
}
return 0
}
+7 -7
View File
@@ -74,7 +74,7 @@ write_all func(writer Writer, bytes []u8) void ! WriteError {
return
}
_system_read func(_ ?*mut anyopaque, stream ReadStream, buffer []mut u8) usize ! ReadError {
hide system_read func(_ ?*mut anyopaque, stream ReadStream, buffer []mut u8) usize ! ReadError {
request usize = buffer.len
maximum usize :: usize(maxval!(c_long))
if request > maximum {
@@ -91,7 +91,7 @@ _system_read func(_ ?*mut anyopaque, stream ReadStream, buffer []mut u8) usize !
}
}
_system_write func(_ ?*mut anyopaque, stream WriteStream, bytes []u8) usize ! WriteError {
hide system_write func(_ ?*mut anyopaque, stream WriteStream, bytes []u8) usize ! WriteError {
fd c_int :: c_int(stream)
request usize = bytes.len
maximum usize :: usize(maxval!(c_long))
@@ -109,14 +109,14 @@ _system_write func(_ ?*mut anyopaque, stream WriteStream, bytes []u8) usize ! Wr
}
}
_system_vtable IoVTable :: IoVTable {
read = _system_read,
write = _system_write,
hide system_vtable IoVTable :: IoVTable {
read = system_read,
write = system_write,
}
_system func() Io {
hide system func() Io {
return Io {
context = none,
vtable = &_system_vtable,
vtable = &system_vtable,
}
}
+24 -24
View File
@@ -41,25 +41,25 @@ eql func($T type, left, right []T) bool {
return true
}
_empty_storage [1]mut u64 = [0]
hide empty_storage [1]mut u64 = [0]
_empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&_empty_storage).ptr)
hide empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr)
return pointer[..count]
}
empty func($T type) []mut T {
return _empty_slice(T, 0)
return empty_slice(T, 0)
}
alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
if count == 0 {
return _empty_slice(T, 0)
return empty_slice(T, 0)
}
element_size usize :: sizeof!(T)
if element_size == 0 {
return _empty_slice(T, count)
return empty_slice(T, count)
}
if count > divtrunc!(maxval!(usize), element_size) {
return .out_of_memory
@@ -79,12 +79,12 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
}
if new_count == 0 {
free(allocator, memory)
return _empty_slice(T, 0)
return empty_slice(T, 0)
}
element_size usize :: sizeof!(T)
if element_size == 0 {
return _empty_slice(T, new_count)
return empty_slice(T, new_count)
}
if new_count > divtrunc!(maxval!(usize), element_size) {
return .out_of_memory
@@ -116,9 +116,9 @@ free func($T type, allocator Allocator, memory []mut T) void {
}
}
_malloc_alignment usize :: 16 # ponytail: aarch64-macos libc malloc alignment assumption.
hide malloc_alignment usize :: 16 # ponytail: aarch64-macos libc malloc alignment assumption.
_power_of_two func(value usize) bool {
hide power_of_two func(value usize) bool {
if value == 0 {
return false
}
@@ -135,12 +135,12 @@ _power_of_two func(value usize) bool {
return true
}
_c_alloc func(_ ?*mut anyopaque, size usize, alignment usize) ?*mut u8 {
if _power_of_two(alignment) == false {
hide c_alloc func(_ ?*mut anyopaque, size usize, alignment usize) ?*mut u8 {
if power_of_two(alignment) == false {
return none
}
if alignment <= _malloc_alignment {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.malloc(c_ulong(size)))
}
@@ -153,8 +153,8 @@ _c_alloc func(_ ?*mut anyopaque, size usize, alignment usize) ?*mut u8 {
return ptrcast!(u8, memory[0])
}
_c_realloc func(_ ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
if _power_of_two(alignment) == false {
hide c_realloc func(_ ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
if power_of_two(alignment) == false {
return none
}
@@ -164,11 +164,11 @@ _c_realloc func(_ ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usi
}
if memory |old_memory| {
if alignment <= _malloc_alignment {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 = _c_alloc(none, new_size, alignment)
new_memory ?*mut u8 = c_alloc(none, new_size, alignment)
if new_memory |new_bytes| {
copy_size usize = old_size
if new_size < copy_size {
@@ -183,20 +183,20 @@ _c_realloc func(_ ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usi
return new_memory
}
return _c_alloc(none, new_size, alignment)
return c_alloc(none, new_size, alignment)
}
_c_free func(_ ?*mut anyopaque, memory ?*mut u8, _ usize, _ usize) void {
hide c_free func(_ ?*mut anyopaque, memory ?*mut u8, _ usize, _ usize) void {
c.free(memory)
}
_c_vtable AllocatorVTable :: AllocatorVTable {
alloc = _c_alloc,
realloc = _c_realloc,
free = _c_free,
hide c_vtable AllocatorVTable :: AllocatorVTable {
alloc = c_alloc,
realloc = c_realloc,
free = c_free,
}
c_allocator Allocator :: Allocator {
context = none,
vtable = &_c_vtable,
vtable = &c_vtable,
}