Added $converable / $castable. Simplify and corrected if try/catch parsing. Fix bug with { [A] = 1 }

This commit is contained in:
Christoffer Lerno
2022-07-24 15:01:48 +02:00
committed by Christoffer Lerno
parent 7e0a29ef40
commit 812bd8b3d0
22 changed files with 117 additions and 132 deletions

View File

@@ -88,7 +88,7 @@ struct Allocator
AllocatorFunction function;
}
macro malloc($Type)
macro malloc($Type) @builtin
{
return ($Type*)(mem::alloc($Type.sizeof));
}
@@ -147,7 +147,7 @@ fn void*! realloc_checked(void *ptr, usize new_size, usize alignment = 0)
return thread_allocator.realloc(ptr, new_size, alignment);
}
fn void free(void* ptr)
fn void free(void* ptr) @builtin
{
return thread_allocator.free(ptr)!!;
}

22
lib/std/core/mem_array.c3 Normal file
View File

@@ -0,0 +1,22 @@
// Copyright (c) 2021 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the MIT license
// a copy of which can be found in the LICENSE_STDLIB file.
module std::core::mem::array;
/**
* @require usize.typeid.max / elements > $Type.sizeof
**/
macro alloc($Type, usize elements)
{
$Type* ptr = mem::alloc($Type.sizeof * elements, $alignof($Type));
return ptr[:elements];
}
/**
* @require (usize.typeid.max / elements > $Type.sizeof)
**/
macro make($Type, usize elements)
{
$Type* ptr = mem::calloc($sizeof($Type) * elements, $alignof($Type));
return ptr[:elements];
}

View File

@@ -253,19 +253,6 @@ fn void String.append_char(String* str, char c)
data.chars[data.len++] = c;
}
macro bool is_pointer_char_array($Type)
{
var $Type2 = $Type;
$if ($Type.typeid.kind != TypeKind.POINTER):
return false;
$elif ($Type.typeid.inner.kind != TypeKind.ARRAY):
return false;
$elif ($Type.typeid.inner.inner != char.typeid):
return false;
$else:
return true;
$endif;
}
macro void String.append(String* str, value)
{
@@ -281,9 +268,9 @@ macro void String.append(String* str, value)
$case Char32:
str.append_char32(value);
$default:
$if ($Type.typeid.kind == TypeKind.SIGNED_INT || $Type.typeid.kind == TypeKind.UNSIGNED_INT):
str.append_char32((Char32)value);
$elif (is_pointer_char_array($Type)):
$if ($convertable($Type, Char32)):
str.append_char32(value);
$elif ($convertable($Type, char[])):
str.append_chars(value);
$else:
$assert("Unsupported type for appending");