hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 40f085ca4dc79fe84b928b0f581fd663e425fcea
parent abb20ec9fcdc1746a861f0ec5306b05a12992708
Author: Sebastian <sebastian@sebsite.pw>
Date:   Wed, 23 Aug 2023 23:39:40 -0400

hare::types: precompute builtin hashes

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/types/+test.ha | 29+++++++++++++++++++++++++++++
Mhare/types/builtins.ha | 39+++++++++++++++++----------------------
2 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/hare/types/+test.ha b/hare/types/+test.ha @@ -4,6 +4,7 @@ // (c) 2021 Drew DeVault <sir@cmpwn.com> // (c) 2021 Ember Sawady <ecs@d2evs.net> // (c) 2022 Sebastian <sebastian@sebsite.pw> +use fmt; use hare::ast; use hare::lex; use hare::parse; @@ -369,3 +370,31 @@ fn resolve( assert(htype.flags == 0); assert((htype.repr as alias).secondary == of); }; + +@test fn builtins() void = { + const builtins = [ + (&builtin_bool, "bool"), + (&builtin_f32, "f32"), + (&builtin_f64, "f64"), + (&builtin_i8, "i8"), + (&builtin_i16, "i16"), + (&builtin_i32, "i32"), + (&builtin_i64, "i64"), + (&builtin_opaque, "opaque"), + (&builtin_rune, "rune"), + (&builtin_u8, "u8"), + (&builtin_u16, "u16"), + (&builtin_u32, "u32"), + (&builtin_u64, "u64"), + (&builtin_void, "void"), + ]; + for (let i = 0z; i < len(builtins); i += 1) { + const expected = hash(builtins[i].0); + const actual = builtins[i].0.id; + if (expected != actual) { + fmt::errorfln("expected type {} to have ID {}, but got {}", + builtins[i].1, expected, actual)!; + abort(); + }; + }; +}; diff --git a/hare/types/builtins.ha b/hare/types/builtins.ha @@ -1,10 +1,14 @@ // License: MPL-2.0 // (c) 2021 Drew DeVault <sir@cmpwn.com> +// IDs are precomputed, but a test verifies that they match what they would be +// if computed at runtime + // [[_type]] representation of bool. export const builtin_bool: _type = _type { repr = builtin::BOOL, sz = 1, _align = 1, + id = 292984781, ... }; @@ -12,6 +16,7 @@ export const builtin_bool: _type = _type { export const builtin_f32: _type = _type { repr = builtin::F32, sz = 4, _align = 4, + id = 3950255460, ... }; @@ -19,6 +24,7 @@ export const builtin_f32: _type = _type { export const builtin_f64: _type = _type { repr = builtin::F64, sz = 8, _align = 8, + id = 1568378015, ... }; @@ -26,6 +32,7 @@ export const builtin_f64: _type = _type { export const builtin_i8: _type = _type { repr = builtin::I8, sz = 1, _align = 1, + id = 3312558843, ... }; @@ -33,6 +40,7 @@ export const builtin_i8: _type = _type { export const builtin_i16: _type = _type { repr = builtin::I16, sz = 2, _align = 2, + id = 930681398, ... }; @@ -40,6 +48,7 @@ export const builtin_i16: _type = _type { export const builtin_i32: _type = _type { repr = builtin::I32, sz = 4, _align = 4, + id = 2037165609, ... }; @@ -47,6 +56,7 @@ export const builtin_i32: _type = _type { export const builtin_i64: _type = _type { repr = builtin::I64, sz = 8, _align = 8, + id = 1399468992, ... }; @@ -54,6 +64,7 @@ export const builtin_i64: _type = _type { export const builtin_opaque: _type = _type { repr = builtin::OPAQUE, sz = SIZE_UNDEFINED, _align = SIZE_UNDEFINED, + id = 461893804, ... }; @@ -61,6 +72,7 @@ export const builtin_opaque: _type = _type { export const builtin_rune: _type = _type { repr = builtin::RUNE, sz = 4, _align = 4, + id = 2374983655, ... }; @@ -68,6 +80,7 @@ export const builtin_rune: _type = _type { export const builtin_u8: _type = _type { repr = builtin::U8, sz = 1, _align = 1, + id = 1906196061, ... }; @@ -75,6 +88,7 @@ export const builtin_u8: _type = _type { export const builtin_u16: _type = _type { repr = builtin::U16, sz = 2, _align = 2, + id = 2206074632, ... }; @@ -82,6 +96,7 @@ export const builtin_u16: _type = _type { export const builtin_u32: _type = _type { repr = builtin::U32, sz = 4, _align = 4, + id = 4119164483, ... }; @@ -89,6 +104,7 @@ export const builtin_u32: _type = _type { export const builtin_u64: _type = _type { repr = builtin::U64, sz = 8, _align = 8, + id = 3481467866, ... }; @@ -96,27 +112,6 @@ export const builtin_u64: _type = _type { export const builtin_void: _type = _type { repr = builtin::VOID, sz = 0, _align = 0, + id = 2543892678, ... }; - -@init fn init() void = { - const builtins = [ - &builtin_bool, - &builtin_f32, - &builtin_f64, - &builtin_i8, - &builtin_i16, - &builtin_i32, - &builtin_i64, - &builtin_opaque, - &builtin_rune, - &builtin_u8, - &builtin_u16, - &builtin_u32, - &builtin_u64, - &builtin_void, - ]; - for (let i = 0z; i < len(builtins); i += 1) { - builtins[i].id = hash(builtins[i]); - }; -};