Fix to "typeid.kind". Conversion unsigned int -> enum fixed. @autoimport -> @builtin. Comparison macros. Bump to 0.2.21

This commit is contained in:
Christoffer Lerno
2022-07-22 17:04:30 +02:00
committed by Christoffer Lerno
parent 036859c0c8
commit b313bec69d
15 changed files with 210 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2021 Christoffer Lerno. All rights reserved.
// Copyright (c) 2021-2022 Christoffer Lerno and contributors. 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::builtin;
@@ -20,14 +20,14 @@ fault VarCastResult
*
* @param variable `the variable to store and restore`
**/
macro void @scope(&variable; @body) @autoimport
macro void @scope(&variable; @body) @builtin
{
$typeof(variable) temp = variable;
defer variable = temp;
@body();
}
macro void @swap(&a, &b) @autoimport
macro void @swap(&a, &b) @builtin
{
$typeof(a) temp = a;
a = b;
@@ -41,7 +41,7 @@ macro void @swap(&a, &b) @autoimport
* @param $Type `the type to convert to`
* @return `The variant.ptr converted to its type.`
**/
macro varcast(variant v, $Type) @autoimport
macro varcast(variant v, $Type) @builtin
{
if (v.type != $Type.typeid) return VarCastResult.TYPE_MISMATCH!;
return ($Type*)v.ptr;
@@ -56,7 +56,7 @@ struct CallstackElement
char* file;
uint line;
}
fn void panic(char* message, char *file, char *function, uint line) @autoimport
fn void panic(char* message, char *file, char *function, uint line) @builtin
{
CallstackElement* stack = $$stacktrace();
$if ($defined(libc::stderr) && $defined(libc::fprintf)):
@@ -82,12 +82,14 @@ fn void panic(char* message, char *file, char *function, uint line) @autoimport
$$trap();
}
macro unreachable($string = "Unreachable statement reached.") @autoimport @noreturn
macro unreachable($string = "Unreachable statement reached.") @builtin @noreturn
{
panic($string, $$FILE, $$FUNC, $$LINE);
$$unreachable();
}
/*
enum TypeKind
{

View File

@@ -0,0 +1,76 @@
// Copyright (c) 2021-2022 Christoffer Lerno and contributors. 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::builtin;
/**
* @require is_comparable_value(a) && is_comparable_value(b)
**/
macro less(a, b) @builtin
{
$if ($defined(a.less)):
return a.less(b);
$elif ($defined(a.compare_to)):
return a.compare_to(b) < 0;
$else:
return a < b;
$endif;
}
/**
* @require is_comparable_value(a) && is_comparable_value(b)
**/
macro less_eq(a, b) @builtin
{
$if ($defined(a.less)):
return !b.less(a);
$elif ($defined(a.compare_to)):
return a.compare_to(b) <= 0;
$else:
return a <= b;
$endif;
}
/**
* @require is_comparable_value(a) && is_comparable_value(b)
**/
macro greater(a, b) @builtin
{
$if ($defined(a.less)):
return b.less(a);
$elif ($defined(a.compare_to)):
return a.compare_to(b) > 0;
$else:
return a > b;
$endif;
}
/**
* @require is_comparable_value(a) && is_comparable_value(b)
**/
macro greater_eq(a, b) @builtin
{
$if ($defined(a.less)):
return !a.less(b);
$elif ($defined(a.compare_to)):
return a.compare_to(b) >= 0;
$else:
return a >= b;
$endif;
}
/**
* @require is_equatable_value(a) && is_equatable_value(b) `values must be equatable`
**/
macro bool equals(a, b) @builtin
{
$if ($defined(a.equals)):
return a.equals(b);
$elif ($defined(a.compare_to)):
return a.compare_to(b) == 0;
$elif ($defined(a.less)):
return !a.less(b) && !b.less(a);
$else:
return a == b;
$endif;
}

View File

@@ -77,6 +77,49 @@ macro variant_to_int(variant v, $Type)
}
}
macro bool is_numerical($Type)
{
var $kind = $Type.typeid.kind;
$if ($kind == TypeKind.DISTINCT):
return is_numerical($Type.typeid.inner);
$else:
return $kind == TypeKind.SIGNED_INT || $kind == TypeKind.UNSIGNED_INT || $kind == TypeKind.FLOAT
|| $kind == TypeKind.VECTOR;
$endif;
}
macro bool is_comparable($Type)
{
var $kind = $Type.typeid.kind;
$if ($kind == TypeKind.DISTINCT):
return is_comparable($Type.typeid.inner);
$else:
return $kind == TypeKind.SIGNED_INT || $kind == TypeKind.UNSIGNED_INT || $kind == TypeKind.FLOAT
|| $kind == TypeKind.VECTOR || $kind == TypeKind.BOOL || $kind == TypeKind.POINTER
|| $kind == TypeKind.ENUM;
$endif;
}
macro bool is_equatable_value(value)
{
$if ($defined(value.less) || $defined(value.compare_to) || $defined(value.equals)):
return true;
$else:
return is_comparable($typeof(value));
$endif;
}
macro bool is_comparable_value(value)
{
$if ($defined(value.less) || $defined(value.compare_to)):
return true;
$else:
return is_comparable($typeof(value));
$endif;
}
enum TypeKind : char
{
VOID,