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

Added auto-constructors (ie: 'struct Vec : this(float x, float y);')

This commit is contained in:
Brian Fiete 2021-01-02 12:54:05 -08:00
parent ae0f3c5ebb
commit 9d1a5d9f3d
7 changed files with 155 additions and 9 deletions

View file

@ -8420,6 +8420,14 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
if (baseTypeIdx > 0)
{
if (auto tokenNode = BfNodeDynCast<BfTokenNode>(nextNode))
{
if (tokenNode->mToken == BfToken_Semicolon)
{
break;
}
}
BfTokenNode* commaToken = NULL;
if (typeDeclaration->mGenericParams != NULL)
{
@ -8438,6 +8446,32 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
baseClassCommas.push_back(commaToken);
}
if (auto tokenNode = BfNodeDynCast<BfTokenNode>(mVisitorPos.GetNext()))
{
if (tokenNode->mToken == BfToken_This)
{
mVisitorPos.MoveNext();
auto ctorDecl = mAlloc->Alloc<BfAutoConstructorDeclaration>();
BfDeferredAstSizedArray<BfParameterDeclaration*> params(ctorDecl->mParams, mAlloc);
BfDeferredAstSizedArray<BfTokenNode*> commas(ctorDecl->mCommas, mAlloc);
ctorDecl->mReturnType = NULL;
ReplaceNode(tokenNode, ctorDecl);
MEMBER_SET(ctorDecl, mThisToken, tokenNode);
ParseMethod(ctorDecl, &params, &commas);
if (typeDeclaration->mAutoCtor == NULL)
{
MEMBER_SET(typeDeclaration, mAutoCtor, ctorDecl);
}
else
{
Fail("Only one auto-constructor is allowed", ctorDecl);
AddErrorNode(ctorDecl);
}
continue;
}
}
auto baseType = CreateTypeRefAfter(typeDeclaration);
if (baseType == NULL)
break;
@ -8879,7 +8913,7 @@ BfTokenNode* BfReducer::ParseMethodParams(BfAstNode* node, SizedArrayImpl<BfPara
(token == BfToken_Delegate) || (token == BfToken_Function) ||
(token == BfToken_Params) || (token == BfToken_LParen) ||
(token == BfToken_Var) || (token == BfToken_LBracket) ||
(token == BfToken_DotDotDot)))
(token == BfToken_ReadOnly) || (token == BfToken_DotDotDot)))
{
// These get picked up below
}
@ -8938,7 +8972,7 @@ BfTokenNode* BfReducer::ParseMethodParams(BfAstNode* node, SizedArrayImpl<BfPara
}
else
{
if ((token != BfToken_Out) && (token != BfToken_Ref) && (token != BfToken_Mut) && (token != BfToken_Params))
if ((token != BfToken_Out) && (token != BfToken_Ref) && (token != BfToken_Mut) && (token != BfToken_Params) && (token != BfToken_ReadOnly))
{
Fail("Invalid token", tokenNode);
return NULL;
@ -9341,6 +9375,9 @@ bool BfReducer::ParseMethod(BfMethodDeclaration* methodDeclaration, SizedArrayIm
endToken = NULL;
}
if (auto autoCtorDecl = BfNodeDynCast<BfAutoConstructorDeclaration>(ctorDecl))
return true;
if ((endToken != NULL) && (endToken->GetToken() == BfToken_Semicolon))
{
MEMBER_SET_CHECKED_BOOL(methodDeclaration, mEndSemicolon, endToken);