1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Support for const expressions for bools & chars

This commit is contained in:
Brian Fiete 2021-12-31 06:17:57 -05:00
parent 2a98bf00d5
commit 28689853d2
6 changed files with 42 additions and 4 deletions

View file

@ -359,6 +359,11 @@ bool BfIRConstHolder::IsInt(BfTypeCode typeCode)
return (typeCode >= BfTypeCode_Int8) && (typeCode <= BfTypeCode_Char32);
}
bool BfIRConstHolder::IsChar(BfTypeCode typeCode)
{
return (typeCode >= BfTypeCode_Char8) && (typeCode <= BfTypeCode_Char32);
}
bool BfIRConstHolder::IsIntable(BfTypeCode typeCode)
{
return (typeCode >= BfTypeCode_Boolean) && (typeCode <= BfTypeCode_Char32);

View file

@ -907,6 +907,7 @@ public:
void FixTypeCode(BfTypeCode& typeCode);
int GetSize(BfTypeCode typeCode);
static bool IsInt(BfTypeCode typeCode);
static bool IsChar(BfTypeCode typeCode);
static bool IsIntable(BfTypeCode typeCode);
static bool IsSigned(BfTypeCode typeCode);
static bool IsFloat(BfTypeCode typeCode);

View file

@ -7586,6 +7586,8 @@ void BfModule::ResolveGenericParamConstraints(BfGenericParamInstance* genericPar
switch (typeCode)
{
case BfTypeCode_StringId:
case BfTypeCode_Boolean:
case BfTypeCode_Int8:
case BfTypeCode_UInt8:
case BfTypeCode_Int16:
@ -11970,6 +11972,7 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
switch (constant->mTypeCode)
{
case BfTypeCode_Boolean:
case BfTypeCode_Int8:
case BfTypeCode_UInt8:
case BfTypeCode_Int16:

View file

@ -13605,8 +13605,15 @@ StringT<128> BfModule::TypeToString(BfType* resolvedType, BfTypeNameFlags typeNa
void BfModule::VariantToString(StringImpl& str, const BfVariant& variant)
{
switch (variant.mTypeCode)
{
case BfTypeCode_Char8:
{
case BfTypeCode_Boolean:
if (variant.mUInt64 == 0)
str += "false";
else if (variant.mUInt64 == 1)
str += "true";
else
str += StrFormat("%lld", variant.mInt64);
break;
case BfTypeCode_Int8:
case BfTypeCode_UInt8:
case BfTypeCode_Int16:
@ -13614,6 +13621,16 @@ void BfModule::VariantToString(StringImpl& str, const BfVariant& variant)
case BfTypeCode_Int32:
str += StrFormat("%d", variant.mInt32);
break;
case BfTypeCode_Char8:
case BfTypeCode_Char16:
case BfTypeCode_Char32:
if ((variant.mUInt32 >= 32) && (variant.mUInt32 <= 0x7E))
str += StrFormat("'%c'", (char)variant.mUInt32);
else if (variant.mUInt32 <= 0xFF)
str += StrFormat("'\\x%2X'", variant.mUInt32);
else
str += StrFormat("'\\u{%X}'", variant.mUInt32);
break;
case BfTypeCode_UInt32:
str += StrFormat("%lu", variant.mUInt32);
break;

View file

@ -2787,7 +2787,9 @@ BfVariant BfResolvedTypeSet::EvaluateToVariant(LookupContext* ctx, BfExpression*
// Limit the types of constants to prevent duplicate values with different types - we don't want to hash a typeref with an int32
// when the constraint requirement is int64 (but we don't know that at hash time)
if (BfIRConstHolder::IsInt(variant.mTypeCode))
if (BfIRConstHolder::IsChar(variant.mTypeCode))
variant.mTypeCode = BfTypeCode_Char32;
else if (BfIRConstHolder::IsInt(variant.mTypeCode))
variant.mTypeCode = BfTypeCode_Int64;
else if (variant.mTypeCode == BfTypeCode_Float)
{

View file

@ -2232,8 +2232,10 @@ bool BfSystem::DoesLiteralFit(BfTypeCode typeCode, int64 value)
switch (typeCode)
{
case BfTypeCode_Boolean:
return (value >= 0) && (value < 1);
case BfTypeCode_Int8:
return (value >= -0x80) && (value < 0x80);
return (value >= -0x80) && (value < 0x80);
case BfTypeCode_Int16:
return (value >= -0x8000) && (value < 0x8000);
case BfTypeCode_Int32:
@ -2242,10 +2244,13 @@ bool BfSystem::DoesLiteralFit(BfTypeCode typeCode, int64 value)
return true;
case BfTypeCode_UInt8:
case BfTypeCode_Char8:
return (value >= 0) && (value < 0x100);
case BfTypeCode_UInt16:
case BfTypeCode_Char16:
return (value >= 0) && (value < 0x10000);
case BfTypeCode_UInt32:
case BfTypeCode_Char32:
return (value >= 0) && (value < 0x100000000LL);
case BfTypeCode_UInt64:
return (value >= 0);
@ -2267,6 +2272,8 @@ bool BfSystem::DoesLiteralFit(BfTypeCode typeCode, uint64 value)
switch (typeCode)
{
case BfTypeCode_Boolean:
return (value < 1);
case BfTypeCode_Int8:
return (value < 0x80);
case BfTypeCode_Int16:
@ -2277,10 +2284,13 @@ bool BfSystem::DoesLiteralFit(BfTypeCode typeCode, uint64 value)
return true;
case BfTypeCode_UInt8:
case BfTypeCode_Char8:
return (value < 0x100);
case BfTypeCode_UInt16:
case BfTypeCode_Char16:
return (value < 0x10000);
case BfTypeCode_UInt32:
case BfTypeCode_Char32:
return (value < 0x100000000LL);
case BfTypeCode_UInt64:
return true;