mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fix to "typeid.kind". Conversion unsigned int -> enum fixed. @autoimport -> @builtin. Comparison macros. Bump to 0.2.21
This commit is contained in:
committed by
Christoffer Lerno
parent
036859c0c8
commit
b313bec69d
@@ -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
|
||||
{
|
||||
|
||||
76
lib/std/core/builtin_comparison.c3
Normal file
76
lib/std/core/builtin_comparison.c3
Normal 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;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -67,12 +67,12 @@ const QUAD_MIN_EXP = -16481;
|
||||
const QUAD_EPSILON = 1.92592994438723585305597794258492732e-34;
|
||||
*/
|
||||
|
||||
macro max(x, y) @autoimport
|
||||
macro max(x, y) @builtin
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
macro min(x, y) @autoimport
|
||||
macro min(x, y) @builtin
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
@@ -23,26 +23,6 @@
|
||||
module std::priorityqueue<Type>;
|
||||
import std::array::list;
|
||||
|
||||
|
||||
// Helper macros to allow arbitrary non-primitive types to be comparable
|
||||
private macro bool less(Type x, Type y)
|
||||
{
|
||||
$if ($defined(Type.less)):
|
||||
return x.less(y);
|
||||
$else:
|
||||
return x < y;
|
||||
$endif;
|
||||
}
|
||||
|
||||
private macro bool greater(Type x, Type y)
|
||||
{
|
||||
$if ($defined(Type.greater)):
|
||||
return x.greater(y);
|
||||
$else:
|
||||
return x > y;
|
||||
$endif;
|
||||
}
|
||||
|
||||
define Heap = List<Type>;
|
||||
|
||||
struct PriorityQueue
|
||||
|
||||
Reference in New Issue
Block a user