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

Added <= support for 'for less'

This commit is contained in:
Brian Fiete 2019-10-05 11:27:30 -07:00
parent f4c252048e
commit 7e55369f92
2 changed files with 14 additions and 7 deletions

View file

@ -2803,7 +2803,7 @@ BfForEachStatement* BfReducer::CreateForEachStatement(BfAstNode* node, bool hasT
if (auto nextNode = BfNodeDynCast<BfTokenNode>(mVisitorPos.GetNext()))
{
if (nextNode->mToken == BfToken_LParen)
if ((nextNode->mToken == BfToken_LParen) || (nextNode->mToken == BfToken_LessEquals))
{
mVisitorPos.MoveNext();
auto tupleNode = CreateTupleExpression(nextNode);
@ -2816,7 +2816,7 @@ BfForEachStatement* BfReducer::CreateForEachStatement(BfAstNode* node, bool hasT
auto name = ExpectIdentifierAfter(forEachStatement, "variable name");
MEMBER_SET_CHECKED(forEachStatement, mVariableName, name);
}
auto inToken = ExpectTokenAfter(forEachStatement, BfToken_In, BfToken_LChevron);
auto inToken = ExpectTokenAfter(forEachStatement, BfToken_In, BfToken_LChevron, BfToken_LessEquals);
MEMBER_SET_CHECKED(forEachStatement, mInToken, inToken);
auto expr = CreateExpressionAfter(forEachStatement);
MEMBER_SET_CHECKED(forEachStatement, mCollectionExpression, expr);
@ -2930,7 +2930,7 @@ BfStatement* BfReducer::CreateForStatement(BfAstNode* node)
if (tokenNode != NULL)
{
int token = tokenNode->GetToken();
if ((token == BfToken_In) || (token == BfToken_LChevron))
if ((token == BfToken_In) || (token == BfToken_LChevron) || (token == BfToken_LessEquals))
{
mVisitorPos.mReadPos = startReadIdx;
return CreateForEachStatement(node, true);

View file

@ -5219,10 +5219,13 @@ void BfModule::DoForLess(BfForEachStatement* forEachStmt)
if (varType == NULL)
varType = GetPrimitiveType(BfTypeCode_IntPtr);
if (!varType->IsIntegral())
BfType* checkType = varType;
if (checkType->IsTypedPrimitive())
checkType = checkType->GetUnderlyingType();
if (!checkType->IsIntegral())
{
Fail(StrFormat("Cannot iterate over '%s' in for-less statements, only integer types are allowed", TypeToString(varType).c_str()), forEachStmt->mVariableTypeRef);
varType = GetPrimitiveType(BfTypeCode_Int32);
varType = GetPrimitiveType(BfTypeCode_IntPtr);
if (didInference)
target = GetDefaultTypedValue(varType);
}
@ -5283,6 +5286,9 @@ void BfModule::DoForLess(BfForEachStatement* forEachStmt)
// Soldier on
target = GetDefaultTypedValue(varType);
}
if (forEachStmt->mInToken->mToken == BfToken_LessEquals)
conditionValue = mBfIRBuilder->CreateCmpLTE(localVal, target.mValue, varType->IsSigned());
else
conditionValue = mBfIRBuilder->CreateCmpLT(localVal, target.mValue, varType->IsSigned());
mBfIRBuilder->CreateCondBr(conditionValue, bodyBB, endBB);
ValueScopeEnd(valueScopeStart);
@ -5331,7 +5337,8 @@ void BfModule::DoForLess(BfForEachStatement* forEachStmt)
void BfModule::Visit(BfForEachStatement* forEachStmt)
{
if ((forEachStmt->mInToken != NULL) && (forEachStmt->mInToken->GetToken() == BfToken_LChevron))
if ((forEachStmt->mInToken != NULL) &&
((forEachStmt->mInToken->GetToken() == BfToken_LChevron) || (forEachStmt->mInToken->GetToken() == BfToken_LessEquals)))
{
DoForLess(forEachStmt);
return;