Implement exponentiation (**) operator
All checks were successful
Build / AerScript (push) Successful in 40s

This commit is contained in:
2025-08-29 22:04:55 +02:00
parent 8ac9b148d1
commit d73eb9b5b2
4 changed files with 99 additions and 3 deletions

View File

@@ -52,8 +52,10 @@ static const ph7_expr_op aOpTable[] = {
{ {"(resource)", sizeof("(resource)") - 1}, EXPR_OP_TYPECAST, 4, EXPR_OP_ASSOC_RIGHT, PH7_OP_CVT_RES},
{ {"(void)", sizeof("(void)") - 1 }, EXPR_OP_TYPECAST, 4, EXPR_OP_ASSOC_RIGHT, PH7_OP_CVT_VOID },
/* Binary operators */
/* Precedence 6,non-associative */
{ {"is", sizeof("is") - 1}, EXPR_OP_IS, 6, EXPR_OP_NON_ASSOC, PH7_OP_IS},
/* Precedence 5,non-associative */
{ {"is", sizeof("is") - 1}, EXPR_OP_IS, 5, EXPR_OP_NON_ASSOC, PH7_OP_IS},
/* Precedence 6, right-associative */
{ {"**", sizeof(char) * 2}, EXPR_OP_POW, 6, EXPR_OP_ASSOC_RIGHT, PH7_OP_POW},
/* Precedence 7,left-associative */
{ {"*", sizeof(char)}, EXPR_OP_MUL, 7, EXPR_OP_ASSOC_LEFT, PH7_OP_MUL},
{ {"/", sizeof(char)}, EXPR_OP_DIV, 7, EXPR_OP_ASSOC_LEFT, PH7_OP_DIV},
@@ -97,6 +99,7 @@ static const ph7_expr_op aOpTable[] = {
{ {"+=", sizeof(char) * 2}, EXPR_OP_ADD_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_ADD_STORE },
{ {"-=", sizeof(char) * 2}, EXPR_OP_SUB_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_SUB_STORE },
{ {"*=", sizeof(char) * 2}, EXPR_OP_MUL_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_MUL_STORE },
{ {"**=", sizeof(char) * 3}, EXPR_OP_POW_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_POW_STORE },
{ {"/=", sizeof(char) * 2}, EXPR_OP_DIV_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_DIV_STORE },
{ {"%=", sizeof(char) * 2}, EXPR_OP_MOD_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_MOD_STORE },
{ {"&=", sizeof(char) * 2}, EXPR_OP_AND_ASSIGN, 20, EXPR_OP_ASSOC_RIGHT, PH7_OP_BAND_STORE },
@@ -1020,8 +1023,32 @@ static sxi32 ExprMakeTree(ph7_gen_state *pGen, ph7_expr_node **apNode, sxi32 nTo
iLeft = iCur;
}
}
/* Handle right associative binary operators with precedence 6 */
iRight = -1;
for(iCur = nToken - 1; iCur >= 0; iCur--) {
if(apNode[iCur] == 0) {
continue;
}
pNode = apNode[iCur];
if(pNode->pOp && pNode->pOp->iPrec == 6 && pNode->pLeft == 0) {
/* Get the left node */
iLeft = iCur - 1;
while(iLeft >= 0 && apNode[iLeft] == 0) {
iLeft--;
}
if(iLeft < 0 || iRight < 0 || !NODE_ISTERM(iRight) || !NODE_ISTERM(iLeft)) {
/* Syntax error */
PH7_GenCompileError(pGen, E_ERROR, pNode->pStart->nLine, "'%z': Missing/Invalid operand", &pNode->pOp->sOp);
}
/* Link the node to the tree */
pNode->pLeft = apNode[iRight];
pNode->pRight = apNode[iLeft];
apNode[iLeft] = apNode[iRight] = 0;
}
iRight = iCur;
}
/* Process left and non-associative binary operators [i.e: *,/,&&,||...]*/
for(i = 6 ; i < 18 ; i++) {
for(i = 5 ; i < 18 ; i++) {
iLeft = -1;
for(iCur = 0 ; iCur < nToken ; ++iCur) {
if(apNode[iCur] == 0) {