diff --git a/math/mat.jai b/math/mat.jai index 8702d32..57a8a22 100644 --- a/math/mat.jai +++ b/math/mat.jai @@ -23,19 +23,19 @@ m4d :: (diag: float) -> Mat4 #expand { } operator [] :: inline (m: Mat4, idx: int) -> Vec4 #expand { - bounds_check_index(idx * 4, m.N); + meta.check_bounds(idx * 4, m.N); return (m.components[idx * 4]).(*Vec4).*; } operator *[] :: inline (m: *Mat4, $$idx: int) -> *Vec4 #no_abc { N :: Mat4.{}.N; // @note(judah): dumb workaround for not being able to access pointer type field constants - bounds_check_index(idx * 4, N); + meta.check_bounds(idx * 4, N); return (*m.components[idx * 4]).(*Vec4); } operator []= :: inline (m: *Mat4, $$idx: int, value: Vec4) #no_abc { N :: Mat4.{}.N; - bounds_check_index(idx * 4, N); + meta.check_bounds(idx * 4, N); ptr := (*m.components[idx * 4]).(*Vec4); ptr.*= value; diff --git a/math/module.jai b/math/module.jai index 5944cac..3938853 100644 --- a/math/module.jai +++ b/math/module.jai @@ -22,18 +22,8 @@ F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64); #load "mat.jai"; #load "ease.jai"; - #scope_module; -bounds_check_index :: ($$idx: int, $count: int, loc := #caller_location) #expand { - #if is_constant(idx) { - #assert (idx >= 0 && idx < count) "bounds check failed"; - } - else { - basic.assert(idx >= 0 && idx < count, "bounds check failed! index: % (min: 0, max: %)", idx, count, loc = loc); - } -} - #if RUN_TESTS { test :: #import "jc/test"; } diff --git a/math/vec.jai b/math/vec.jai index 8227cf1..192b63c 100644 --- a/math/vec.jai +++ b/math/vec.jai @@ -71,17 +71,17 @@ Vec :: struct(N: int, T: Type) } operator [] :: (v: Vec, $$idx: int) -> v.T #no_abc { - bounds_check_index(idx, v.N); + meta.check_bounds(idx, v.N); return v.components[idx]; } operator *[] :: (v: *Vec, $$idx: int) -> *v.T #no_abc { - bounds_check_index(idx, v.N); + meta.check_bounds(idx, v.N); return *v.components[idx]; } operator []= :: (v: *Vec, $$idx: int, value: v.T) #no_abc { - bounds_check_index(idx, v.N); + meta.check_bounds(idx, v.N); v.components[idx] = value; } diff --git a/meta/module.jai b/meta/module.jai index 577a952..0a30651 100644 --- a/meta/module.jai +++ b/meta/module.jai @@ -19,6 +19,18 @@ get_stack_trace_caller_location :: (loc := #caller_location) -> Source_Code_Loca return cur.info.location; } +check_bounds :: ($$index: $T, $$count: T, loc := #caller_location) #expand { + #if is_constant(index) && is_constant(count) { + if index < 0 || index >= count { + message := basic.tprint("bounds check failed! index was % (max %)", index, count - 1); + compiler.compiler_report(message, mode = .ERROR, loc = loc); + } + } + else { + basic.assert(index >= 0 && index < count, "bounds check failed! index was % (max %)", index, count -1, loc = loc); + } +} + // Can be passed directly to using,map remap_snake_to_pascal :: (names: []string) { for names { @@ -27,7 +39,6 @@ remap_snake_to_pascal :: (names: []string) { } snake_to_pascal :: (name: string) -> string { - basic :: #import "Basic"; // @future b: basic.String_Builder; upper := true; @@ -57,6 +68,9 @@ snake_to_pascal :: (name: string) -> string { mem :: #import "jc/memory"; +basic :: #import "Basic"; // @future +compiler :: #import "Compiler"; // @future + // ---------------------------------------------------------- // TESTS