Allow key and value declaration inside foreach() loop.
The build was successful. Details

This commit is contained in:
Rafal Kupiec 2019-05-01 11:38:28 +02:00
parent a726ab795a
commit 1d6822c1e2
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
1 changed files with 49 additions and 17 deletions

View File

@ -1811,7 +1811,9 @@ static sxi32 PH7_CompileForeach(ph7_gen_state *pGen) {
ph7_foreach_info *pInfo; ph7_foreach_info *pInfo;
sxu32 nFalseJump; sxu32 nFalseJump;
VmInstr *pInstr; VmInstr *pInstr;
char *zName;
sxu32 nLine; sxu32 nLine;
sxu32 nKey;
sxi32 rc; sxi32 rc;
nLine = pGen->pIn->nLine; nLine = pGen->pIn->nLine;
/* Jump the 'foreach' keyword */ /* Jump the 'foreach' keyword */
@ -1851,6 +1853,20 @@ static sxi32 PH7_CompileForeach(ph7_gen_state *pGen) {
} }
pTmp = pGen->pEnd; pTmp = pGen->pEnd;
pGen->pEnd = pCur; pGen->pEnd = pCur;
nKey = (sxu32)(SX_PTR_TO_INT(pGen->pIn->pUserData));
if(nKey & PH7_KEYWORD_TYPEDEF) {
/* Hack to compile variable */
pGen->pEnd->nType = PH7_TK_SEMI;
if(pGen->pIn[3].nType != PH7_TK_SEMI) {
PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Improper key declaration");
}
/* Extract variable name */
zName = SyMemBackendStrDup(&pGen->pVm->sAllocator, pGen->pIn[2].sData.zString, pGen->pIn[2].sData.nByte);
/* Compile variable declaration */
PH7_CompileVar(&(*pGen));
/* Record key name */
SyStringInitFromBuf(&pInfo->sKey, zName, SyStrlen(zName));
} else {
rc = PH7_CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator); rc = PH7_CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
if(rc == SXERR_ABORT) { if(rc == SXERR_ABORT) {
/* Expression handler request an operation abort [i.e: Out-of-memory] */ /* Expression handler request an operation abort [i.e: Out-of-memory] */
@ -1861,6 +1877,7 @@ static sxi32 PH7_CompileForeach(ph7_gen_state *pGen) {
/* Record key name */ /* Record key name */
SyStringInitFromBuf(&pInfo->sKey, pInstr->p3, SyStrlen((const char *)pInstr->p3)); SyStringInitFromBuf(&pInfo->sKey, pInstr->p3, SyStrlen((const char *)pInstr->p3));
} }
}
pCur++; /* Jump the array operator */ pCur++; /* Jump the array operator */
pGen->pIn = pCur; pGen->pIn = pCur;
pGen->pEnd = pTmp; pGen->pEnd = pTmp;
@ -1885,6 +1902,20 @@ static sxi32 PH7_CompileForeach(ph7_gen_state *pGen) {
/* Swap token streams */ /* Swap token streams */
pTmp = pGen->pEnd; pTmp = pGen->pEnd;
pGen->pEnd = pCur; pGen->pEnd = pCur;
nKey = (sxu32)(SX_PTR_TO_INT(pGen->pIn->pUserData));
if(nKey & PH7_KEYWORD_TYPEDEF) {
/* Hack to compile variable */
pGen->pEnd->nType = PH7_TK_SEMI;
if(pGen->pIn[3].nType != PH7_TK_SEMI) {
PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Improper value declaration");
}
/* Extract variable name */
zName = SyMemBackendStrDup(&pGen->pVm->sAllocator, pGen->pIn[2].sData.zString, pGen->pIn[2].sData.nByte);
/* Compile variable declaration */
PH7_CompileVar(&(*pGen));
/* Record value name */
SyStringInitFromBuf(&pInfo->sValue, zName, SyStrlen(zName));
} else {
/* Compile the expression holding the value name */ /* Compile the expression holding the value name */
rc = PH7_CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator); rc = PH7_CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
if(rc == SXERR_ABORT) { if(rc == SXERR_ABORT) {
@ -1896,6 +1927,7 @@ static sxi32 PH7_CompileForeach(ph7_gen_state *pGen) {
/* Record value name */ /* Record value name */
SyStringInitFromBuf(&pInfo->sValue, pInstr->p3, SyStrlen((const char *)pInstr->p3)); SyStringInitFromBuf(&pInfo->sValue, pInstr->p3, SyStrlen((const char *)pInstr->p3));
} }
}
if(pGen->pIn < pCur) { if(pGen->pIn < pCur) {
PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Unexpected token '%z'", &pGen->pIn->sData); PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Unexpected token '%z'", &pGen->pIn->sData);
} }