Unset local for variable, fixes #62
Some checks failed
Build / AerScript (push) Failing after 26s

This commit is contained in:
Rafal Kupiec 2025-08-28 18:38:28 +02:00
parent eead19918d
commit dc5725d1af
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 38 additions and 0 deletions

View File

@ -1860,6 +1860,9 @@ static sxi32 PH7_CompileFor(ph7_gen_state *pGen) {
sxu32 nLine; sxu32 nLine;
sxi32 rc; sxi32 rc;
nLine = pGen->pIn->nLine; nLine = pGen->pIn->nLine;
SyString *pName = NULL;
char *zName = NULL;
/* Jump the 'for' keyword */ /* Jump the 'for' keyword */
pGen->pIn++; pGen->pIn++;
if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & PH7_TK_LPAREN) == 0) { if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & PH7_TK_LPAREN) == 0) {
@ -1879,6 +1882,13 @@ static sxi32 PH7_CompileFor(ph7_gen_state *pGen) {
pGen->pEnd = pEnd; pGen->pEnd = pEnd;
sxu32 nKey = (sxu32)(SX_PTR_TO_INT(pGen->pIn->pUserData)); sxu32 nKey = (sxu32)(SX_PTR_TO_INT(pGen->pIn->pUserData));
if(nKey & (PH7_KEYWORD_AUTO | PH7_KEYWORD_TYPEDEF)) { if(nKey & (PH7_KEYWORD_AUTO | PH7_KEYWORD_TYPEDEF)) {
/* Local for variable, store its name */
pName = &pGen->pIn[2].sData;
zName = SyMemBackendStrDup(&pGen->pVm->sAllocator, pName->zString, pName->nByte);
if(zName == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 engine is running out-of-memory");
}
/* Compile variable */
PH7_CompileVar(&(*pGen)); PH7_CompileVar(&(*pGen));
} }
/* Compile initialization expressions if available */ /* Compile initialization expressions if available */
@ -1976,6 +1986,11 @@ static sxi32 PH7_CompileFor(ph7_gen_state *pGen) {
PH7_VmEmitInstr(pGen->pVm, nLine, PH7_OP_JMP, 0, pForBlock->nFirstInstr, 0, 0); PH7_VmEmitInstr(pGen->pVm, nLine, PH7_OP_JMP, 0, pForBlock->nFirstInstr, 0, 0);
/* Fix all jumps now the destination is resolved */ /* Fix all jumps now the destination is resolved */
PH7_GenStateFixJumps(pForBlock, -1, PH7_VmInstrLength(pGen->pVm)); PH7_GenStateFixJumps(pForBlock, -1, PH7_VmInstrLength(pGen->pVm));
/* If local for loop variable, unset it */
if(zName != NULL) {
/* Emit instruction to unset the variable after its initialization */
PH7_VmEmitInstr(pGen->pVm, nLine, PH7_OP_UNSET, 0, 0, (void*)zName, 0);
}
/* Release the loop block */ /* Release the loop block */
PH7_GenStateLeaveBlock(pGen, 0); PH7_GenStateLeaveBlock(pGen, 0);
/* Statement successfully compiled */ /* Statement successfully compiled */

View File

@ -2292,6 +2292,28 @@ static sxi32 VmByteCodeExec(
} }
break; break;
} }
/*
* UNSET: * * P3
*
* Unset a variable. It takes the variable name indexed at P3 operand.
*/
case PH7_OP_UNSET: {
SyString sName;
VmFrame *pFrame = pVm->pFrame;
SyHashEntry *pEntry;
sxu32 nIdx;
SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3));
/* Find variable in local frame */
pEntry = SyHashGet(&pFrame->hVar, (const void *)sName.zString, sName.nByte);
if(pEntry) {
nIdx = (sxu32)SX_PTR_TO_INT(pEntry->pUserData);
/* Delete variable from local frame */
SyHashDeleteEntry(&pFrame->hVar, (const void *)sName.zString, sName.nByte, 0);
/* Call PH7_VmUnsetMemObj to release the variable */
PH7_VmUnsetMemObj(&(*pVm), nIdx, FALSE);
}
break;
}
/* /*
* LOADC P1 P2 * * LOADC P1 P2 *
* *

View File

@ -1379,6 +1379,7 @@ enum ph7_vm_op {
PH7_OP_IMPORT, /* Import AerScript module */ PH7_OP_IMPORT, /* Import AerScript module */
PH7_OP_INCLUDE, /* Include another source file */ PH7_OP_INCLUDE, /* Include another source file */
PH7_OP_DECLARE, /* Declare a variable */ PH7_OP_DECLARE, /* Declare a variable */
PH7_OP_UNSET, /* Unset variable */
PH7_OP_LOADV, /* Load variable */ PH7_OP_LOADV, /* Load variable */
PH7_OP_LOADC, /* Load constant */ PH7_OP_LOADC, /* Load constant */
PH7_OP_LOAD_IDX, /* Load array entry */ PH7_OP_LOAD_IDX, /* Load array entry */