From 5f75319be9d6ce30066794e880d12ceb85f3888e Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Wed, 28 Oct 2026 19:55:25 +0100 Subject: [PATCH] Remove variable-variable parsing and dynamic local-name dispatch --- engine/compiler.c | 3 ++- engine/parser.c | 24 +++++++----------------- engine/vm.c | 43 +++++++------------------------------------ 3 files changed, 16 insertions(+), 54 deletions(-) diff --git a/engine/compiler.c b/engine/compiler.c index 65b67cd..72fb769 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -1161,6 +1161,7 @@ PH7_PRIVATE sxi32 PH7_CompileVariable(ph7_gen_state *pGen, sxi32 iCompileFlag) { if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & (PH7_TK_ID | PH7_TK_KEYWORD)) == 0) { /* Invalid variable name */ PH7_GenCompileError(pGen, E_ERROR, nLine, "Invalid variable name"); + return SXERR_ABORT; } p3 = 0; SyHashEntry *pEntry; @@ -5023,7 +5024,7 @@ static sxi32 PH7_GenStateEmitExprCode( } else { p3 = pInstr->p3; } - /* POP the last dynamic load instruction */ + /* POP the last load instruction */ (void)PH7_VmPopInstr(pGen->pVm); } } diff --git a/engine/parser.c b/engine/parser.c index 8503b9f..0237903 100644 --- a/engine/parser.c +++ b/engine/parser.c @@ -459,24 +459,14 @@ static sxi32 ExprExtractNode(ph7_gen_state *pGen, ph7_expr_node **ppNode) { pCur++; } else if(pCur->nType & PH7_TK_DOLLAR) { /* Isolate variable */ - while(pCur < pGen->pEnd && (pCur->nType & PH7_TK_DOLLAR)) { - pCur++; /* Variable variable */ - } - if(pCur < pGen->pEnd) { - if(pCur->nType & (PH7_TK_ID | PH7_TK_KEYWORD)) { - /* Variable name */ - pCur++; - } else if(pCur->nType & PH7_TK_OCB /* '{' */) { - pCur++; - /* Dynamic variable name,Collect until the next non nested '}' */ - PH7_DelimitNestedTokens(pCur, pGen->pEnd, PH7_TK_OCB, PH7_TK_CCB, &pCur); - if(pCur < pGen->pEnd) { - pCur++; - } else { - PH7_GenCompileError(pGen, E_ERROR, pNode->pStart->nLine, "Syntax error: Missing closing brace '}'"); - } - } + pCur++; + if(pCur >= pGen->pEnd || (pCur->nType & (PH7_TK_ID | PH7_TK_KEYWORD)) == 0) { + PH7_GenCompileError(pGen, E_ERROR, pNode->pStart->nLine, "Invalid variable name"); + SySetRelease(&pNode->aNodeArgs); + SyMemBackendPoolFree(&pGen->pVm->sAllocator, pNode); + return SXERR_ABORT; } + pCur++; pNode->xCode = PH7_CompileVariable; } else if(pCur->nType & PH7_TK_OCB /* '{' */) { /* Array, assemble tokens */ diff --git a/engine/vm.c b/engine/vm.c index 6f04e3a..1618cf4 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -3331,30 +3331,16 @@ Resume: /* * LOADV: * * P3 * - * Load a variable where it's name is taken from the top of the stack or - * from the P3 operand. + * Load a variable whose name is given by the P3 operand. */ case PH7_OP_LOADV: { ph7_value *pObj; SyString sName; - if(pInstr->p3 == 0) { - /* Take the variable name from the top of the stack */ - if(pTos < pStack) { - goto Abort; - } -# - /* Force a string cast */ - if((pTos->nType & MEMOBJ_STRING) == 0) { - PH7_MemObjToString(pTos); - } - SyStringInitFromBuf(&sName, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob)); - } else { - SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3)); - /* Reserve a room for the target object */ - pTos++; - } + SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3)); + /* Reserve a room for the target object */ + pTos++; /* Extract the requested memory object */ - pObj = VmExtractMemObj(&(*pVm), &sName, pInstr->p3 ? FALSE : TRUE); + pObj = VmExtractMemObj(&(*pVm), &sName, FALSE); if(pObj == 0) { /* Fatal error */ PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Variable '$%z' undeclared (first use in this method/closure)", &sName); @@ -3628,31 +3614,16 @@ Resume: } } break; - } else if(pInstr->p3 == 0) { - /* Take the variable name from the next on the stack */ - if((pTos->nType & MEMOBJ_STRING) == 0) { - /* Force a string cast */ - PH7_MemObjToString(pTos); - } - SyStringInitFromBuf(&sName, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob)); - pTos--; - if(pTos < pStack) { - goto Abort; - } - } else { - SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3)); } + SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3)); /* Extract the desired variable if available */ - pObj = VmExtractMemObj(&(*pVm), &sName, pInstr->p3 ? FALSE : TRUE); + pObj = VmExtractMemObj(&(*pVm), &sName, FALSE); if(pObj == 0) { PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Variable '$%z' undeclared (first use in this method/closure)", &sName); } else if(pObj->iFlags != MEMOBJ_VARIABLE) { PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Cannot re-assign a value of '$%z' statement", &sName); } - if(!pInstr->p3) { - PH7_MemObjRelease(&pTos[1]); - } /* Perform the store operation */ rc = PH7_MemObjSafeStore(pTos, pObj); if(rc != SXRET_OK) {