Extend "var" to allow type inference on variables.

This commit is contained in:
Christoffer Lerno
2022-10-13 09:26:15 +02:00
committed by Christoffer Lerno
parent 5e184f04e7
commit 5d9a7ab0a6
8 changed files with 155 additions and 27 deletions

View File

@@ -28,16 +28,16 @@ fault VarCastResult
**/
macro void @scope(&variable; @body) @builtin
{
$typeof(variable) temp = variable;
var temp = variable;
defer variable = temp;
@body();
}
macro void @swap(&a, &b) @builtin
{
$typeof(a) temp = a;
a = b;
b = temp;
var temp = a;
a = b;
b = temp;
}
/**
@@ -95,11 +95,11 @@ macro void unreachable($string = "Unreachable statement reached.") @builtin @nor
macro bitcast(expr, $Type) @builtin
{
var $size = (usz)($sizeof(expr));
$assert($size == $Type.sizeof, "Cannot bitcast between types of different size.");
$Type x = void;
mem::copy(&x, &expr, $size, $Type.alignof, $alignof(expr));
return x;
var $size = (usz)($sizeof(expr));
$assert($size == $Type.sizeof, "Cannot bitcast between types of different size.");
$Type x = void;
mem::copy(&x, &expr, $size, $Type.alignof, $alignof(expr));
return x;
}
/**

View File

@@ -80,7 +80,7 @@ macro min(x, ...) @builtin
$if ($vacount == 1):
return less(x, $vaarg(0)) ? x : $vaarg(0);
$else:
$typeof(x) result = x;
var result = x;
$for (var $i = 0; $i < $vacount; $i++):
if (less($vaarg($i), result))
{
@@ -96,7 +96,7 @@ macro max(x, ...) @builtin
$if ($vacount == 1):
return greater(x, $vaarg(0)) ? x : $vaarg(0);
$else:
$typeof(x) result = x;
var result = x;
$for (var $i = 0; $i < $vacount; $i++):
if (greater($vaarg($i), result))
{