1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00

Better array size fix with missing commas

This commit is contained in:
Brian Fiete 2020-10-12 10:22:42 -07:00
parent 9332693132
commit d24f169181
2 changed files with 21 additions and 13 deletions

View file

@ -12100,13 +12100,13 @@ void BfExprEvaluator::Visit(BfObjectCreateExpression* objCreateExpr)
int dimensions = 1; int dimensions = 1;
bool commaExpected = false;
if (arrayTypeRef->mParams.size() != 0) if (arrayTypeRef->mParams.size() != 0)
{ {
auto intType = mModule->ResolveTypeDef(mModule->mSystem->mTypeIntPtr); auto intType = mModule->ResolveTypeDef(mModule->mSystem->mTypeIntPtr);
for (int argIdx = 0; argIdx < (int)arrayTypeRef->mParams.size(); argIdx++) for (auto arg : arrayTypeRef->mParams)
{ {
auto arg = arrayTypeRef->mParams[argIdx];
if (auto tokenNode = BfNodeDynCastExact<BfTokenNode>(arg)) if (auto tokenNode = BfNodeDynCastExact<BfTokenNode>(arg))
{ {
if (tokenNode->GetToken() == BfToken_Comma) if (tokenNode->GetToken() == BfToken_Comma)
@ -12114,11 +12114,19 @@ void BfExprEvaluator::Visit(BfObjectCreateExpression* objCreateExpr)
if (isRawArrayAlloc) if (isRawArrayAlloc)
{ {
mModule->Fail("Sized arrays cannot be multidimensional.", tokenNode); mModule->Fail("Sized arrays cannot be multidimensional.", tokenNode);
continue;
} }
dimensions++;
if (dimensions == 5)
{
mModule->Fail("Too many array dimensions, consider using a jagged array.", tokenNode);
}
commaExpected = false;
continue; continue;
} }
} }
auto expr = BfNodeDynCast<BfExpression>(arg); auto expr = BfNodeDynCast<BfExpression>(arg);
if ((isRawArrayAlloc) && (!dimLengthVals.IsEmpty())) if ((isRawArrayAlloc) && (!dimLengthVals.IsEmpty()))
{ {
@ -12126,15 +12134,6 @@ void BfExprEvaluator::Visit(BfObjectCreateExpression* objCreateExpr)
continue; continue;
} }
if (argIdx != 0)
{
dimensions++;
if (dimensions == 5)
{
mModule->Fail("Too many array dimensions, consider using a jagged array.", arg);
}
}
dimLengthRefs.Add(expr); dimLengthRefs.Add(expr);
BfTypedValue dimLength; BfTypedValue dimLength;
@ -12160,11 +12159,18 @@ void BfExprEvaluator::Visit(BfObjectCreateExpression* objCreateExpr)
dimLength = mModule->Cast(expr, dimLength, intType, castFlags); dimLength = mModule->Cast(expr, dimLength, intType, castFlags);
} }
if (commaExpected)
{
mModule->AssertErrorState();
continue;
}
if (!dimLength) if (!dimLength)
{ {
dimLength = mModule->GetDefaultTypedValue(intType); dimLength = mModule->GetDefaultTypedValue(intType);
} }
dimLengthVals.push_back(dimLength.mValue); dimLengthVals.push_back(dimLength.mValue);
commaExpected = true;
} }
} }

View file

@ -3,6 +3,8 @@ namespace Tests
{ {
class Arrays class Arrays
{ {
public static float[,] GetArray() => new .[,] ( ( 0, 1, 2, 3), ( 10, 11, 12, 13 ), ( 20, 21, 22, 23 ) );
struct StructA struct StructA
{ {
public int16 mA = 11; public int16 mA = 11;