Basic type safe print

This commit is contained in:
Krzosa Karol
2022-06-20 10:27:27 +02:00
parent 99b0ba2673
commit 9700742515
3 changed files with 59 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
#import "os_windows.kl"
main :: (argc: int, argv: **char): int main :: (argc: int, argv: **char): int
test_any() test_any()
@@ -31,7 +32,9 @@ test_arrays :: ()
for array2;; *it = 0 for array2;; *it = 0
for i := 0, i < length_of(array1), i+=1;; assert(0 == array1[i]) for i := 0, i < length_of(array1), i+=1;; assert(0 == array1[i])
test_array_any(10, 20, 30) print("Thing % %", {30, 20})
test_array_any(10, {20, 30})
// test_array_any(10, 20, 30) // @fix
Some_Struct :: struct Some_Struct :: struct
thing: int thing: int

View File

@@ -45,7 +45,7 @@ release :: (m: *Memory)
m.commit = 0 m.commit = 0
m.reserve = 0 m.reserve = 0
print :: (string: String16) write_console :: (string: String16)
handle := GetStdHandle(STD_OUTPUT_HANDLE) handle := GetStdHandle(STD_OUTPUT_HANDLE)
WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0) WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0)
@@ -61,3 +61,55 @@ time :: (): F64
assert(err != 0) assert(err != 0)
result := query->F64 / performance_frequency result := query->F64 / performance_frequency
return result return result
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
itoa :: (value: S64, result: *U8, base: S64): *U8
// check that the base if valid
if (base < 2) || (base > 36)
*result = 0 // '
return result
ptr := result
ptr1 := result
tmp_char: U8
tmp_value: S64
for value != 0
tmp_value = value
value /= base
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]
// Apply negative sign
if tmp_value < 0
*ptr++ = '- // '
*ptr-- = 0
for ptr1 < ptr
tmp_char = *ptr
*ptr-- = *ptr1
*ptr1++ = tmp_char
return result
print :: (string: String, args: ..)
buffer: [1024]U8
buffer_len: S64
arg_counter := 0
for i := 0, i < length_of(string), i+=1
if string[i] == '% // '
assert(arg_counter < length_of(args), "Passing too many [%] to a print lambda")
arg := args[arg_counter++]
if arg.type == S64
value := *(arg.data->*S64)
itoa_buff: [64]U8
p := itoa(value, &itoa_buff[0], 10)
for *p != 0
buffer[buffer_len++] = *p++
else;; assert(false)
else
buffer[buffer_len++] = string[i]

View File

@@ -110,11 +110,11 @@ make_sure_types_are_compatible_for_constant_evaluation(Token *pos, Value *a, Val
return; return;
} }
else if(is_pointer(a->type) && is_pointer(b->type)){ else if(is_pointer(a->type) && is_pointer(b->type)){
goto fail; return;
} }
else if(is_typed(a->type) && is_typed(b->type)){ else if(is_typed(a->type) && is_typed(b->type)){
if(a->type != b->type){ if(a->type != b->type){
fail: compiler_error(pos, "Type mismatch in make_sure_types_are_compatible_for_constant_evaluation - left: %Q right: %Q", typestring(a->type), typestring(b->type)); compiler_error(pos, "Type mismatch in make_sure_types_are_compatible_for_constant_evaluation - left: %Q right: %Q", typestring(a->type), typestring(b->type));
} }
} }