mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Cleanup use of macro inspection to use @typekind and @typeid macros.
This commit is contained in:
@@ -100,7 +100,7 @@ fn Object* new_int(int128 i, Allocator* allocator)
|
||||
macro Object* new_enum(e, Allocator* allocator)
|
||||
{
|
||||
Object* o = allocator.new(Object);
|
||||
*o = { .i = (int128)e, .allocator = allocator, .type = $typeof(e).typeid };
|
||||
*o = { .i = (int128)e, .allocator = allocator, .type = @typeid(e) };
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ bitstruct UInt128LE : uint128 @littleendian
|
||||
macro read(bytes, $Type)
|
||||
{
|
||||
char[] s;
|
||||
$switch ($typeof(bytes).kindof)
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
s = (*bytes)[:$Type.sizeof];
|
||||
$default:
|
||||
@@ -110,7 +110,7 @@ macro read(bytes, $Type)
|
||||
macro write(x, bytes, $Type)
|
||||
{
|
||||
char[] s;
|
||||
$switch ($typeof(bytes).kindof)
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
s = (*bytes)[:$Type.sizeof];
|
||||
$default:
|
||||
@@ -146,7 +146,7 @@ macro is_bitorder($Type)
|
||||
|
||||
macro bool is_array_or_sub_of_char(bytes)
|
||||
{
|
||||
$switch ($typeof(bytes).kindof)
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
var $Inner = $typefrom($typeof(bytes).inner);
|
||||
$if $Inner.kindof == ARRAY:
|
||||
@@ -164,7 +164,7 @@ macro bool is_array_or_sub_of_char(bytes)
|
||||
|
||||
macro bool is_arrayptr_or_sub_of_char(bytes)
|
||||
{
|
||||
$switch ($typeof(bytes).kindof)
|
||||
$switch (@typekind(bytes))
|
||||
$case POINTER:
|
||||
var $Inner = $typefrom($typeof(bytes).inner);
|
||||
$if $Inner.kindof == ARRAY:
|
||||
|
||||
@@ -184,12 +184,12 @@ macro bool is_same($TypeA, $TypeB)
|
||||
|
||||
macro bool @has_same(#a, #b, ...)
|
||||
{
|
||||
var $type_a = $typeof(#a).typeid;
|
||||
$if $type_a != $typeof(#b).typeid:
|
||||
var $type_a = @typeid(#a);
|
||||
$if $type_a != @typeid(#b):
|
||||
return false;
|
||||
$endif
|
||||
$for (var $i = 0; $i < $vacount; $i++)
|
||||
$if $typeof($vaexpr($i)).typeid != $type_a:
|
||||
$if @typeid($vaexpr($i)) != $type_a:
|
||||
return false;
|
||||
$endif
|
||||
$endfor
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
module std::core::values;
|
||||
|
||||
macro typeid @typeid(#value) @builtin => $typeof(#value).typeid;
|
||||
macro TypeKind @typekind(#value) @builtin => $typeof(#value).kindof;
|
||||
macro bool @typeis(#value, $Type) @builtin => $typeof(#value).typeid == $Type.typeid;
|
||||
/**
|
||||
|
||||
@@ -51,7 +51,7 @@ fault IoError
|
||||
**/
|
||||
macro String! readline(stream = io::stdin(), Allocator* allocator = mem::heap())
|
||||
{
|
||||
bool $is_stream = $typeof(stream).typeid == InStream*.typeid;
|
||||
bool $is_stream = @typeid(stream) == InStream*.typeid;
|
||||
$if $is_stream:
|
||||
$typeof(&stream.read_byte) func = &stream.read_byte;
|
||||
char val = func((void*)stream)!;
|
||||
@@ -133,7 +133,7 @@ macro usz! fprintn(out, x = "")
|
||||
usz len = fprint(out, x)!;
|
||||
out.write_byte('\n')!;
|
||||
$switch
|
||||
$case $typeof(out).typeid == OutStream*.typeid:
|
||||
$case @typeid(out) == OutStream*.typeid:
|
||||
if (&out.flush) out.flush()!;
|
||||
$case $defined(out.flush):
|
||||
out.flush()!;
|
||||
|
||||
@@ -177,7 +177,7 @@ const char[*] MAX_VARS @private = { [2] = 3, [4] = 5, [8] = 10 };
|
||||
|
||||
/**
|
||||
* @require @is_instream(stream)
|
||||
* @require $typeof(x_ptr).kindof == POINTER && $typeof(x_ptr).inner.kindof.is_int()
|
||||
* @require @typekind(x_ptr) == POINTER && $typeof(x_ptr).inner.kindof.is_int()
|
||||
**/
|
||||
macro usz! read_varint(stream, x_ptr)
|
||||
{
|
||||
@@ -214,7 +214,7 @@ macro usz! read_varint(stream, x_ptr)
|
||||
}
|
||||
/**
|
||||
* @require @is_outstream(stream)
|
||||
* @require $typeof(x).kindof.is_int()
|
||||
* @require @typekind(x).is_int()
|
||||
**/
|
||||
macro usz! write_varint(stream, x)
|
||||
{
|
||||
|
||||
@@ -156,7 +156,7 @@ macro atan2(x, y)
|
||||
**/
|
||||
macro sincos(x, y)
|
||||
{
|
||||
$if $typeof(y[0]).typeid == float.typeid:
|
||||
$if @typeid(y[0]) == float.typeid:
|
||||
return _sincosf(x, y);
|
||||
$else
|
||||
return _sincos(x, y);
|
||||
@@ -168,7 +168,7 @@ macro sincos(x, y)
|
||||
**/
|
||||
macro atan(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _atanf(x);
|
||||
$else
|
||||
return _atan(x);
|
||||
@@ -180,7 +180,7 @@ macro atan(x)
|
||||
**/
|
||||
macro atanh(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _atanhf(x);
|
||||
$else
|
||||
return _atanh(x);
|
||||
@@ -192,7 +192,7 @@ macro atanh(x)
|
||||
**/
|
||||
macro acos(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _acosf(x);
|
||||
$else
|
||||
return _acos(x);
|
||||
@@ -204,7 +204,7 @@ macro acos(x)
|
||||
**/
|
||||
macro acosh(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _acoshf(x);
|
||||
$else
|
||||
return _acosh(x);
|
||||
@@ -216,7 +216,7 @@ macro acosh(x)
|
||||
**/
|
||||
macro asin(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _asinf(x);
|
||||
$else
|
||||
return _asin(x);
|
||||
@@ -228,7 +228,7 @@ macro asin(x)
|
||||
**/
|
||||
macro asinh(x)
|
||||
{
|
||||
$if $typeof(x).typeid == float.typeid:
|
||||
$if @typeid(x) == float.typeid:
|
||||
return _asinhf(x);
|
||||
$else
|
||||
return _asinh(x);
|
||||
|
||||
@@ -244,13 +244,13 @@ macro double __math_oflow(ulong sign) => __math_xflow(sign, 0x1p-767);
|
||||
|
||||
macro __math_xflow(sign, v)
|
||||
{
|
||||
$typeof(v) temp;
|
||||
$typeof(v) temp @noinit;
|
||||
@volatile_store(temp, (sign ? -v : v) * v);
|
||||
return temp;
|
||||
}
|
||||
|
||||
macro force_eval_add(x, v)
|
||||
{
|
||||
$typeof(x) temp;
|
||||
$typeof(x) temp @noinit;
|
||||
@volatile_store(temp, x + v);
|
||||
}
|
||||
@@ -217,7 +217,7 @@ return v;
|
||||
var view_proj = m1.mul(m2);
|
||||
var invert = view_proj.invert();
|
||||
// Create quaternion from source point
|
||||
$if $typeof(v[0]).typeid == float.typeid:
|
||||
$if @typeid(v[0]) == float.typeid:
|
||||
Quaternionf quat = { v.x, v.y, v.z, 1 };
|
||||
$else
|
||||
Quaternion quat = { v.x, v.y, v.z, 1 };
|
||||
|
||||
@@ -69,7 +69,7 @@ fn void seeder(char[] input, char[] out_buffer)
|
||||
|
||||
macro uint hash(value) @local
|
||||
{
|
||||
return fnv32a::encode(&&bitcast(value, char[$typeof(value).sizeof]));
|
||||
return fnv32a::encode(&&bitcast(value, char[$sizeof(value)]));
|
||||
}
|
||||
|
||||
fn char[8 * 4] entropy()
|
||||
|
||||
@@ -4,7 +4,7 @@ module std::sort;
|
||||
* Perform a binary search over the sorted array and return the index
|
||||
* in [0, array.len) where x would be inserted or cmp(i) is true and cmp(j) is true for j in [i, array.len).
|
||||
* @require $defined(list[0]) && $defined(list.len) "The list must be indexable"
|
||||
* @require $or($typeof(cmp).typeid == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
* @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
**/
|
||||
macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
{
|
||||
@@ -13,7 +13,7 @@ macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
for (usz j = len; i < j;)
|
||||
{
|
||||
usz half = i + (j - i) / 2;
|
||||
$if $typeof(cmp).typeid == void*.typeid:
|
||||
$if @typeid(cmp) == void*.typeid:
|
||||
switch
|
||||
{
|
||||
case greater(list[half], x): j = half;
|
||||
@@ -22,7 +22,7 @@ macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
}
|
||||
$else
|
||||
$switch
|
||||
$case $typeof(cmp).params[0] == $typeof(list[0]).typeid:
|
||||
$case $typeof(cmp).params[0] == @typeid(list[0]):
|
||||
int res = cmp(list[half], x);
|
||||
$default:
|
||||
int res = cmp(&list[half], &x);
|
||||
|
||||
@@ -4,7 +4,7 @@ import std::sort::qs;
|
||||
/**
|
||||
* Sort list using the quick sort algorithm.
|
||||
* @require $defined(list[0]) && $defined(list.len) "The list must be indexable and support .len or .len()"
|
||||
* @require $or($typeof(cmp).typeid == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
* @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values"
|
||||
**/
|
||||
macro quicksort(list, cmp = null) @builtin
|
||||
{
|
||||
@@ -31,7 +31,7 @@ def Stack = StackElementItem[64] @private;
|
||||
fn void qsort(Type list, isz low, isz high, Comparer cmp)
|
||||
{
|
||||
var $no_cmp = Comparer.typeid == void*.typeid;
|
||||
var $cmp_by_value = $and(!$no_cmp, Comparer.params[0] == $typeof(list[0]).typeid);
|
||||
var $cmp_by_value = $and(!$no_cmp, Comparer.params[0] == @typeid(list[0]));
|
||||
if (low >= 0 && high >= 0 && low < high)
|
||||
{
|
||||
Stack stack;
|
||||
|
||||
@@ -19,7 +19,7 @@ macro bool @is_comparer(#cmp, #list)
|
||||
$if $params[0] != $params[1]:
|
||||
return false;
|
||||
$else
|
||||
var $element = $typeof(#list[0]).typeid;
|
||||
var $element = @typeid(#list[0]);
|
||||
$switch
|
||||
$case $element == $params[0]:
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user