From 97007425154eca28ab4e951d828e6e1a9675aa41 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 20 Jun 2022 10:27:27 +0200 Subject: [PATCH] Basic type safe print --- programs/any.kl | 5 +++- programs/os_windows.kl | 54 +++++++++++++++++++++++++++++++++++++++++- typechecking.cpp | 4 ++-- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/programs/any.kl b/programs/any.kl index 86eabf3..2f3ab9d 100644 --- a/programs/any.kl +++ b/programs/any.kl @@ -1,3 +1,4 @@ +#import "os_windows.kl" main :: (argc: int, argv: **char): int test_any() @@ -31,7 +32,9 @@ test_arrays :: () for array2;; *it = 0 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 thing: int diff --git a/programs/os_windows.kl b/programs/os_windows.kl index f5810da..bc487b4 100644 --- a/programs/os_windows.kl +++ b/programs/os_windows.kl @@ -45,7 +45,7 @@ release :: (m: *Memory) m.commit = 0 m.reserve = 0 -print :: (string: String16) +write_console :: (string: String16) handle := GetStdHandle(STD_OUTPUT_HANDLE) WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0) @@ -61,3 +61,55 @@ time :: (): F64 assert(err != 0) result := query->F64 / performance_frequency 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] \ No newline at end of file diff --git a/typechecking.cpp b/typechecking.cpp index 6032fec..ad0c7fc 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -110,11 +110,11 @@ make_sure_types_are_compatible_for_constant_evaluation(Token *pos, Value *a, Val return; } else if(is_pointer(a->type) && is_pointer(b->type)){ - goto fail; + return; } else if(is_typed(a->type) && is_typed(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)); } }