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

Fixed operator precedence issue

This commit is contained in:
Brian Fiete 2020-07-02 23:34:17 -07:00
parent 9f04792baa
commit 2bfc9d2d98
2 changed files with 17 additions and 5 deletions

View file

@ -1069,12 +1069,14 @@ static String DbgNodeToString(BfAstNode* astNode)
{ {
String str; String str;
str += "( "; str += "( ";
str += "(";
str += DbgNodeToString(condExpr->mConditionExpression); str += DbgNodeToString(condExpr->mConditionExpression);
str += ") ? ("; str += ") ? (";
str += DbgNodeToString(condExpr->mTrueExpression); str += DbgNodeToString(condExpr->mTrueExpression);
str += ") : ("; str += ") : (";
str += DbgNodeToString(condExpr->mFalseExpression); str += DbgNodeToString(condExpr->mFalseExpression);
str += ")"; str += ")";
str += " )";
return str; return str;
} }
@ -2180,7 +2182,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
unaryOpExpr->mOp = unaryOp; unaryOpExpr->mOp = unaryOp;
unaryOpExpr->mOpToken = tokenNode; unaryOpExpr->mOpToken = tokenNode;
ReplaceNode(tokenNode, unaryOpExpr); ReplaceNode(tokenNode, unaryOpExpr);
unaryOpExpr->mExpression = CreateExpressionAfter(unaryOpExpr, rhsCreateExprFlags); // Don't attempt binary or unary operations- they will always be lower precedence
unaryOpExpr->mExpression = CreateExpressionAfter(unaryOpExpr, (CreateExprFlags)(rhsCreateExprFlags | CreateExprFlags_EarlyExit));
if (unaryOpExpr->mExpression == NULL) if (unaryOpExpr->mExpression == NULL)
return NULL; return NULL;
MoveNode(unaryOpExpr->mExpression, unaryOpExpr); MoveNode(unaryOpExpr->mExpression, unaryOpExpr);
@ -2314,6 +2317,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
if (token == BfToken_Question) if (token == BfToken_Question)
{ {
if ((createExprFlags & CreateExprFlags_EarlyExit) != 0)
return exprLeft;
auto conditionExpr = mAlloc->Alloc<BfConditionalExpression>(); auto conditionExpr = mAlloc->Alloc<BfConditionalExpression>();
ReplaceNode(exprLeft, conditionExpr); ReplaceNode(exprLeft, conditionExpr);
conditionExpr->mConditionExpression = exprLeft; conditionExpr->mConditionExpression = exprLeft;
@ -2340,6 +2345,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
if ((token == BfToken_Case) && ((createExprFlags & CreateStmtFlags_NoCaseExpr) == 0)) if ((token == BfToken_Case) && ((createExprFlags & CreateStmtFlags_NoCaseExpr) == 0))
{ {
if ((createExprFlags & CreateExprFlags_EarlyExit) != 0)
return exprLeft;
// If we have a ".Member case <XXX>" expression, that is an invalid construct. We bail out here // If we have a ".Member case <XXX>" expression, that is an invalid construct. We bail out here
// because it allows the ".Member" to autocomplete because we will treat it as a full expression instead // because it allows the ".Member" to autocomplete because we will treat it as a full expression instead
// of making it the target of an illegal expression // of making it the target of an illegal expression
@ -2581,6 +2588,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
BfBinaryOp binOp = BfTokenToBinaryOp(tokenNode->GetToken()); BfBinaryOp binOp = BfTokenToBinaryOp(tokenNode->GetToken());
if (binOp != BfBinaryOp_None) if (binOp != BfBinaryOp_None)
{ {
if ((createExprFlags & CreateExprFlags_EarlyExit) != 0)
return exprLeft;
auto binOpExpression = mAlloc->Alloc<BfBinaryOperatorExpression>(); auto binOpExpression = mAlloc->Alloc<BfBinaryOperatorExpression>();
ReplaceNode(exprLeft, binOpExpression); ReplaceNode(exprLeft, binOpExpression);
binOpExpression->mLeft = exprLeft; binOpExpression->mLeft = exprLeft;
@ -2605,6 +2614,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
auto assignmentOp = BfTokenToAssignmentOp(tokenNode->GetToken()); auto assignmentOp = BfTokenToAssignmentOp(tokenNode->GetToken());
if (assignmentOp != BfAssignmentOp_None) if (assignmentOp != BfAssignmentOp_None)
{ {
if ((createExprFlags & CreateExprFlags_EarlyExit) != 0)
return exprLeft;
if ((createExprFlags & CreateExprFlags_NoAssignment) != 0) if ((createExprFlags & CreateExprFlags_NoAssignment) != 0)
return exprLeft; return exprLeft;

View file

@ -25,7 +25,8 @@ public:
CreateExprFlags_ExitOnBang = 0x40, CreateExprFlags_ExitOnBang = 0x40,
CreateExprFlags_ExitOnParenExpr = 0x80, CreateExprFlags_ExitOnParenExpr = 0x80,
CreateExprFlags_NoCheckBinOpPrecedence = 0x100, CreateExprFlags_NoCheckBinOpPrecedence = 0x100,
CreateExprFlags_BreakOnCascade = 0x200 CreateExprFlags_BreakOnCascade = 0x200,
CreateExprFlags_EarlyExit = 0x400 // Don't attempt binary or ternary operations
}; };
enum CreateStmtFlags enum CreateStmtFlags