Get rid of switch: endswitch construct

This commit is contained in:
Rafal Kupiec 2018-07-28 22:45:20 +02:00
parent ef2ea60a60
commit 3f1397ed93
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 5 additions and 60 deletions

View File

@ -4871,7 +4871,7 @@ static sxi32 PH7_CompileTry(ph7_gen_state *pGen) {
* Compile a switch block. * Compile a switch block.
* (See block-comment below for more information) * (See block-comment below for more information)
*/ */
static sxi32 GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 iTokenDelim, sxu32 *pBlockStart) { static sxi32 GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 *pBlockStart) {
sxi32 rc = SXRET_OK; sxi32 rc = SXRET_OK;
while(pGen->pIn < pGen->pEnd && (pGen->pIn->nType & (PH7_TK_SEMI/*';'*/ | PH7_TK_COLON/*':'*/)) == 0) { while(pGen->pIn < pGen->pEnd && (pGen->pIn->nType & (PH7_TK_SEMI/*';'*/ | PH7_TK_COLON/*':'*/)) == 0) {
/* Unexpected token */ /* Unexpected token */
@ -4884,8 +4884,7 @@ static sxi32 GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 iTokenDelim,
pGen->pIn++; pGen->pIn++;
/* First instruction to execute in this block. */ /* First instruction to execute in this block. */
*pBlockStart = PH7_VmInstrLength(pGen->pVm); *pBlockStart = PH7_VmInstrLength(pGen->pVm);
/* Compile the block until we hit a case/default/endswitch keyword /* Compile the block until we hit a case/default keyword or the '}' token */
* or the '}' token */
for(;;) { for(;;) {
if(pGen->pIn >= pGen->pEnd) { if(pGen->pIn >= pGen->pEnd) {
/* No more input to process */ /* No more input to process */
@ -4894,15 +4893,6 @@ static sxi32 GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 iTokenDelim,
rc = SXRET_OK; rc = SXRET_OK;
if((pGen->pIn->nType & PH7_TK_KEYWORD) == 0) { if((pGen->pIn->nType & PH7_TK_KEYWORD) == 0) {
if(pGen->pIn->nType & PH7_TK_CCB /*'}' */) { if(pGen->pIn->nType & PH7_TK_CCB /*'}' */) {
if(iTokenDelim != PH7_TK_CCB) {
/* Unexpected token */
rc = PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Unexpected token '%z'",
&pGen->pIn->sData);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
}
/* FALL THROUGH */
}
rc = SXERR_EOF; rc = SXERR_EOF;
break; break;
} }
@ -4913,19 +4903,6 @@ static sxi32 GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 iTokenDelim,
if(nKwrd == PH7_TKWRD_CASE || nKwrd == PH7_TKWRD_DEFAULT) { if(nKwrd == PH7_TKWRD_CASE || nKwrd == PH7_TKWRD_DEFAULT) {
break; break;
} }
if(nKwrd == PH7_TKWRD_ENDSWITCH /* endswitch; */) {
if(iTokenDelim != PH7_TK_KEYWORD) {
/* Unexpected token */
rc = PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Unexpected token '%z'",
&pGen->pIn->sData);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
}
/* FALL THROUGH */
}
/* Block compiled */
break;
}
} }
/* Compile block */ /* Compile block */
rc = PH7_CompileBlock(&(*pGen), 0); rc = PH7_CompileBlock(&(*pGen), 0);
@ -5011,7 +4988,6 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
GenBlock *pSwitchBlock; GenBlock *pSwitchBlock;
SyToken *pTmp, *pEnd; SyToken *pTmp, *pEnd;
ph7_switch *pSwitch; ph7_switch *pSwitch;
sxu32 nToken;
sxu32 nLine; sxu32 nLine;
sxi32 rc; sxi32 rc;
nLine = pGen->pIn->nLine; nLine = pGen->pIn->nLine;
@ -5066,7 +5042,7 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
pGen->pIn = &pEnd[1]; pGen->pIn = &pEnd[1];
pGen->pEnd = pTmp; pGen->pEnd = pTmp;
if(pGen->pIn >= pGen->pEnd || &pGen->pIn[1] >= pGen->pEnd || if(pGen->pIn >= pGen->pEnd || &pGen->pIn[1] >= pGen->pEnd ||
(pGen->pIn->nType & (PH7_TK_OCB/*'{'*/ | PH7_TK_COLON/*:*/)) == 0) { (pGen->pIn->nType & PH7_TK_OCB/*'{'*/) == 0) {
pTmp = pGen->pIn; pTmp = pGen->pIn;
if(pTmp >= pGen->pEnd) { if(pTmp >= pGen->pEnd) {
pTmp--; pTmp--;
@ -5078,13 +5054,6 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
} }
goto Synchronize; goto Synchronize;
} }
/* Set the delimiter token */
if(pGen->pIn->nType & PH7_TK_COLON) {
nToken = PH7_TK_KEYWORD;
/* Stop compilation when the 'endswitch;' keyword is seen */
} else {
nToken = PH7_TK_CCB; /* '}' */
}
pGen->pIn++; /* Jump the leading curly braces/colons */ pGen->pIn++; /* Jump the leading curly braces/colons */
/* Create the switch blocks container */ /* Create the switch blocks container */
pSwitch = (ph7_switch *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(ph7_switch)); pSwitch = (ph7_switch *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(ph7_switch));
@ -5111,33 +5080,11 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
break; break;
} }
if((pGen->pIn->nType & PH7_TK_KEYWORD) == 0) { if((pGen->pIn->nType & PH7_TK_KEYWORD) == 0) {
if(nToken != PH7_TK_CCB || (pGen->pIn->nType & PH7_TK_CCB /*}*/) == 0) {
/* Unexpected token */
rc = PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Switch: Unexpected token '%z'",
&pGen->pIn->sData);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
}
/* FALL THROUGH */
}
/* Block compiled */ /* Block compiled */
break; break;
} }
/* Extract the keyword */ /* Extract the keyword */
nKwrd = SX_PTR_TO_INT(pGen->pIn->pUserData); nKwrd = SX_PTR_TO_INT(pGen->pIn->pUserData);
if(nKwrd == PH7_TKWRD_ENDSWITCH /* endswitch; */) {
if(nToken != PH7_TK_KEYWORD) {
/* Unexpected token */
rc = PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Switch: Unexpected token '%z'",
&pGen->pIn->sData);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
}
/* FALL THROUGH */
}
/* Block compiled */
break;
}
if(nKwrd == PH7_TKWRD_DEFAULT) { if(nKwrd == PH7_TKWRD_DEFAULT) {
/* /*
* According to the PHP language reference manual * According to the PHP language reference manual
@ -5153,7 +5100,7 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
} }
pGen->pIn++; /* Jump the 'default' keyword */ pGen->pIn++; /* Jump the 'default' keyword */
/* Compile the default block */ /* Compile the default block */
rc = GenStateCompileSwitchBlock(pGen, nToken, &pSwitch->nDefault); rc = GenStateCompileSwitchBlock(pGen, &pSwitch->nDefault);
if(rc == SXERR_ABORT) { if(rc == SXERR_ABORT) {
return SXERR_ABORT; return SXERR_ABORT;
} else if(rc == SXERR_EOF) { } else if(rc == SXERR_EOF) {
@ -5171,7 +5118,7 @@ static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen) {
return SXERR_ABORT; return SXERR_ABORT;
} }
/* Compile the case block */ /* Compile the case block */
rc = GenStateCompileSwitchBlock(pGen, nToken, &sCase.nStart); rc = GenStateCompileSwitchBlock(pGen, &sCase.nStart);
/* Insert in the switch container */ /* Insert in the switch container */
SySetPut(&pSwitch->aCaseExpr, (const void *)&sCase); SySetPut(&pSwitch->aCaseExpr, (const void *)&sCase);
if(rc == SXERR_ABORT) { if(rc == SXERR_ABORT) {

View File

@ -580,7 +580,6 @@ static sxu32 KeywordCode(const char *z, int n) {
} ph7_token; } ph7_token;
static ph7_token pTokenLookup[] = { static ph7_token pTokenLookup[] = {
{"extends", PH7_TKWRD_EXTENDS}, {"extends", PH7_TKWRD_EXTENDS},
{"endswitch", PH7_TKWRD_ENDSWITCH},
{"switch", PH7_TKWRD_SWITCH}, {"switch", PH7_TKWRD_SWITCH},
{"print", PH7_TKWRD_PRINT}, {"print", PH7_TKWRD_PRINT},
{"int", PH7_TKWRD_INT}, {"int", PH7_TKWRD_INT},

View File

@ -1478,7 +1478,6 @@ enum ph7_expr_id {
* Using them as variable names is generally OK, but could lead to confusion. * Using them as variable names is generally OK, but could lead to confusion.
*/ */
#define PH7_TKWRD_EXTENDS 1 /* extends */ #define PH7_TKWRD_EXTENDS 1 /* extends */
#define PH7_TKWRD_ENDSWITCH 2 /* endswitch */
#define PH7_TKWRD_SWITCH 3 /* switch */ #define PH7_TKWRD_SWITCH 3 /* switch */
#define PH7_TKWRD_PRINT 4 /* print */ #define PH7_TKWRD_PRINT 4 /* print */
#define PH7_TKWRD_INTERFACE 5 /* interface */ #define PH7_TKWRD_INTERFACE 5 /* interface */