minor refactoring

This commit is contained in:
2026-07-25 00:23:52 +02:00
parent ad54a00893
commit c7f527e3c3
5 changed files with 49 additions and 40 deletions
+16 -9
View File
@@ -5,7 +5,7 @@ import "@std/strmap"
import "@source/strpool"
keywords std.StaticStringMap(TokenKind) :: strmap.init([
keywords std.StringMap(TokenKind) :: strmap.init([
{ "proc", .proc },
{ "return", .return },
{ "if", .if },
@@ -58,6 +58,9 @@ scan proc(state @mut State, input []u8) void ! (mem.AllocError | strpool.InternE
# comments
if char == '#' {
# fixme(brolang): while currently requires curly braces but this should be legal
#while (cursor < input.len and input[cursor] != '\n') cursor += 1
while cursor < input.len and input[cursor] != '\n' : cursor += 1 {}
continue
}
@@ -96,9 +99,7 @@ scan proc(state @mut State, input []u8) void ! (mem.AllocError | strpool.InternE
has_decimal bool = false
# scan integer part
while cursor < input.len and is_digit(input[cursor]) {
cursor += 1
}
while cursor < input.len and is_digit(input[cursor]) : cursor += 1 {}
# check for decimal point
if cursor < input.len and input[cursor] == '.' {
@@ -110,18 +111,21 @@ scan proc(state @mut State, input []u8) void ! (mem.AllocError | strpool.InternE
if has_decimal and (cursor >= input.len or !is_digit(input[cursor])) {
token :: tokens.items.len
try arraylist.append(tokens, Token{ kind = .invalid, start = start })
try arraylist.append(diagnostics, ScanDiagnostic{ token = token, message = "float must end with a digit" })
try arraylist.append(diagnostics, ScanDiagnostic{
token = token,
message = "float must end with a digit",
})
continue
}
# scan decimal part
while cursor < input.len and is_digit(input[cursor]) : cursor += 1 {}
if has_decimal {
if (has_decimal)
try arraylist.append(tokens, Token{ kind = .float, start = start })
} else {
else
try arraylist.append(tokens, Token{ kind = .int, start = start })
}
continue
}
@@ -184,7 +188,10 @@ scan proc(state @mut State, input []u8) void ! (mem.AllocError | strpool.InternE
# invalid character
token :: tokens.items.len
try arraylist.append(tokens, Token{ kind = .invalid, start = cursor })
try arraylist.append(diagnostics, ScanDiagnostic{ token = token, message = "invalid character" })
try arraylist.append(diagnostics, ScanDiagnostic{
token = token,
message = "invalid character",
})
cursor += 1
}
-23
View File
@@ -1,7 +1,6 @@
import "@std/mem"
import "@std/arraylist"
import "@std/hashmap"
import "@std/testing"
# cross-cutting concern, hence global singleton (owned by main.hon)
strings StringPool = undefined
@@ -64,25 +63,3 @@ get_str proc(pool @StringPool, id StringId) ?[]u8 {
get_id proc(pool @StringPool, str []u8) ?StringId {
return hashmap.get(&pool.ids, str)
}
handles_intern test {
pool StringPool = init(mem.c_allocator)
defer deinit(&pool)
input [5]mut u8 = ['h', 'e', 'l', 'l', 'o']
id :: try intern(&pool, input[..])
duplicate :: try intern(&pool, "hello")
input[0] = 'j'
same_id :: get_id(&pool, "hello")
mutated_id :: get_id(&pool, input[..])
hello_str :: get_str(&pool, id)
invalid_str :: get_str(&pool, id + 1)
try testing.expect_equal(0, id)
try testing.expect_equal(id, duplicate)
try testing.expect_equal(id, same_id?)
try testing.expect_equal(null, mutated_id)
try testing.expect_equal(null, invalid_str)
try testing.expect_equal("hello", hello_str?)
}
+24
View File
@@ -0,0 +1,24 @@
import "@std/mem"
import "@std/testing"
handles_intern test {
pool StringPool = init(mem.c_allocator)
defer deinit(&pool)
input [5]mut u8 = ['h', 'e', 'l', 'l', 'o']
id :: try intern(&pool, input[..])
duplicate :: try intern(&pool, "hello")
input[0] = 'j'
same_id :: get_id(&pool, "hello")
mutated_id :: get_id(&pool, input[..])
hello_str :: get_str(&pool, id)
invalid_str :: get_str(&pool, id + 1)
try testing.expect_equal(0, id)
try testing.expect_equal(id, duplicate)
try testing.expect_equal(id, same_id?)
try testing.expect_equal(null, mutated_id)
try testing.expect_equal(null, invalid_str)
try testing.expect_equal("hello", hello_str?)
}