Browse Source

This commit contains several changes:

* print() is now treated as standard function
 * get rid of echo() function
 * fix test suite
and it fixes #26.
pull/44/head
Rafal Kupiec 3 years ago
parent
commit
4bbdc20174
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
6 changed files with 25 additions and 133 deletions
  1. +21
    -89
      engine/compiler.c
  2. +0
    -2
      engine/lexer.c
  3. +2
    -2
      engine/parser.c
  4. +1
    -37
      engine/vm.c
  5. +0
    -2
      include/ph7int.h
  6. +1
    -1
      tests/utf8_variables.aer

+ 21
- 89
engine/compiler.c View File

@ -1144,66 +1144,30 @@ PH7_PRIVATE sxi32 PH7_CompileLangConstruct(ph7_gen_state *pGen, sxi32 iCompileFl
pName = &pGen->pIn->sData;
nKeyID = (sxu32)SX_PTR_TO_INT(pGen->pIn->pUserData);
pGen->pIn++; /* Jump the language construct keyword */
if(nKeyID == PH7_TKWRD_ECHO) {
SyToken *pTmp, *pNext = 0;
/* Compile arguments one after one */
pTmp = pGen->pEnd;
/* Symisc eXtension to the PHP programming language:
* 'echo' can be used in the context of a function which
* mean that the following expression is valid:
* fopen('file.txt','r') or echo "IO error";
*/
PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, 1 /* Boolean true index */, 0, 0);
while(SXRET_OK == PH7_GetNextExpr(pGen->pIn, pTmp, &pNext)) {
if(pGen->pIn < pNext) {
pGen->pEnd = pNext;
rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_RDONLY_LOAD/* Do not create variable if inexistant */, 0);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
}
if(rc != SXERR_EMPTY) {
/* Ticket 1433-008: Optimization #1: Consume input directly
* without the overhead of a function call.
* This is a very powerful optimization that improve
* performance greatly.
*/
PH7_VmEmitInstr(pGen->pVm, PH7_OP_CONSUME, 1, 0, 0, 0);
}
}
/* Jump trailing commas */
while(pNext < pTmp && (pNext->nType & PH7_TK_COMMA)) {
pNext++;
}
pGen->pIn = pNext;
}
/* Restore token stream */
pGen->pEnd = pTmp;
} else {
sxi32 nArg = 0;
sxu32 nIdx = 0;
rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_RDONLY_LOAD/* Do not create variable if inexistant */, 0);
if(rc == SXERR_ABORT) {
sxi32 nArg = 0;
sxu32 nIdx = 0;
rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_RDONLY_LOAD/* Do not create variable if non-existent */, 0);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
} else if(rc != SXERR_EMPTY) {
nArg = 1;
}
if(SXRET_OK != GenStateFindLiteral(&(*pGen), pName, &nIdx)) {
ph7_value *pObj;
/* Emit the call instruction */
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "Fatal, PH7 engine is running out of memory");
SXUNUSED(iCompileFlag); /* cc warning */
return SXERR_ABORT;
} else if(rc != SXERR_EMPTY) {
nArg = 1;
}
if(SXRET_OK != GenStateFindLiteral(&(*pGen), pName, &nIdx)) {
ph7_value *pObj;
/* Emit the call instruction */
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(&(*pGen), E_ERROR, 1, "Fatal, PH7 engine is running out of memory");
SXUNUSED(iCompileFlag); /* cc warning */
return SXERR_ABORT;
}
PH7_MemObjInitFromString(pGen->pVm, pObj, pName);
/* Install in the literal table */
GenStateInstallLiteral(&(*pGen), pObj, nIdx);
}
/* Emit the call instruction */
PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, nIdx, 0, 0);
PH7_VmEmitInstr(pGen->pVm, PH7_OP_CALL, nArg, 0, 0, 0);
PH7_MemObjInitFromString(pGen->pVm, pObj, pName);
/* Install in the literal table */
GenStateInstallLiteral(&(*pGen), pObj, nIdx);
}
/* Emit the call instruction */
PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, nIdx, 0, 0);
PH7_VmEmitInstr(pGen->pVm, PH7_OP_CALL, nArg, 0, 0, 0);
/* Node successfully compiled */
return SXRET_OK;
}
@ -2589,37 +2553,6 @@ static sxi32 PH7_CompileHalt(ph7_gen_state *pGen) {
PH7_VmEmitInstr(pGen->pVm, PH7_OP_HALT, nExpr, 0, 0, 0);
return SXRET_OK;
}
/*
* Compile the 'echo' language construct.
*/
static sxi32 PH7_CompileEcho(ph7_gen_state *pGen) {
SyToken *pTmp, *pNext = 0;
sxi32 rc;
/* Jump the 'echo' keyword */
pGen->pIn++;
/* Compile arguments one after one */
pTmp = pGen->pEnd;
while(SXRET_OK == PH7_GetNextExpr(pGen->pIn, pTmp, &pNext)) {
if(pGen->pIn < pNext) {
pGen->pEnd = pNext;
rc = PH7_CompileExpr(&(*pGen), EXPR_FLAG_RDONLY_LOAD/* Do not create variable if inexistant */, 0);
if(rc == SXERR_ABORT) {
return SXERR_ABORT;
} else if(rc != SXERR_EMPTY) {
/* Emit the consume instruction */
PH7_VmEmitInstr(pGen->pVm, PH7_OP_CONSUME, 1, 0, 0, 0);
}
}
/* Jump trailing commas */
while(pNext < pTmp && (pNext->nType & PH7_TK_COMMA)) {
pNext++;
}
pGen->pIn = pNext;
}
/* Restore token stream */
pGen->pEnd = pTmp;
return SXRET_OK;
}
/*
* Compile the static statement.
* According to the PHP language reference
@ -5378,7 +5311,6 @@ PH7_PRIVATE ProcNodeConstruct PH7_GetNodeHandler(sxu32 nNodeType) {
* PHP Language construct table.
*/
static const LangConstruct aLangConstruct[] = {
{ PH7_TKWRD_ECHO, PH7_CompileEcho }, /* echo language construct */
{ PH7_TKWRD_IF, PH7_CompileIf }, /* if statement */
{ PH7_TKWRD_FOR, PH7_CompileFor }, /* for statement */
{ PH7_TKWRD_WHILE, PH7_CompileWhile }, /* while statement */


+ 0
- 2
engine/lexer.c View File

@ -581,13 +581,11 @@ static sxu32 KeywordCode(const char *z, int n) {
static ph7_token pTokenLookup[] = {
{"extends", PH7_TKWRD_EXTENDS},
{"switch", PH7_TKWRD_SWITCH},
{"print", PH7_TKWRD_PRINT},
{"int", PH7_TKWRD_INT},
{"require_once", PH7_TKWRD_REQONCE},
{"require", PH7_TKWRD_REQUIRE},
{"return", PH7_TKWRD_RETURN},
{"namespace", PH7_TKWRD_NAMESPACE},
{"echo", PH7_TKWRD_ECHO},
{"object", PH7_TKWRD_OBJECT},
{"throw", PH7_TKWRD_THROW},
{"bool", PH7_TKWRD_BOOL},


+ 2
- 2
engine/parser.c View File

@ -329,8 +329,8 @@ PH7_PRIVATE void PH7_DelimitNestedTokens(SyToken *pIn, SyToken *pEnd, sxu32 nTok
* or method names. Using them as variable names is generally OK, but could lead to confusion.
*/
PH7_PRIVATE int PH7_IsLangConstruct(sxu32 nKeyID, sxu8 bCheckFunc) {
if(nKeyID == PH7_TKWRD_ECHO || nKeyID == PH7_TKWRD_PRINT || nKeyID == PH7_TKWRD_INCLUDE
|| nKeyID == PH7_TKWRD_INCONCE || nKeyID == PH7_TKWRD_REQUIRE || nKeyID == PH7_TKWRD_REQONCE
if(nKeyID == PH7_TKWRD_INCLUDE || nKeyID == PH7_TKWRD_INCONCE
|| nKeyID == PH7_TKWRD_REQUIRE || nKeyID == PH7_TKWRD_REQONCE
) {
return TRUE;
}


+ 1
- 37
engine/vm.c View File

@ -8621,39 +8621,6 @@ static int vm_builtin_uniqid(ph7_context *pCtx, int nArg, ph7_value **apArg) {
* Status:
* Stable.
*/
/*
* void echo($string...)
* Output one or more messages.
* Parameters
* $string
* Message to output.
* Return
* NULL.
*/
static int vm_builtin_echo(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zData;
int nDataLen = 0;
ph7_vm *pVm;
int i, rc;
/* Point to the target VM */
pVm = pCtx->pVm;
/* Output */
for(i = 0 ; i < nArg ; ++i) {
zData = ph7_value_to_string(apArg[i], &nDataLen);
if(nDataLen > 0) {
rc = pVm->sVmConsumer.xConsumer((const void *)zData, (unsigned int)nDataLen, pVm->sVmConsumer.pUserData);
if(pVm->sVmConsumer.xConsumer != VmObConsumer) {
/* Increment output length */
pVm->nOutputLen += nDataLen;
}
if(rc == SXERR_ABORT) {
/* Output consumer callback request an operation abort */
return PH7_ABORT;
}
}
}
return SXRET_OK;
}
/*
* int print($string...)
* Output one or more messages.
@ -8661,7 +8628,7 @@ static int vm_builtin_echo(ph7_context *pCtx, int nArg, ph7_value **apArg) {
* $string
* Message to output.
* Return
* 1 always.
* NULL.
*/
static int vm_builtin_print(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zData;
@ -8685,8 +8652,6 @@ static int vm_builtin_print(ph7_context *pCtx, int nArg, ph7_value **apArg) {
}
}
}
/* Return 1 */
ph7_result_int(pCtx, 1);
return SXRET_OK;
}
/*
@ -11620,7 +11585,6 @@ static const ph7_builtin_func aVmFunc[] = {
#endif /* PH7_DISABLE_HASH_FUNC */
#endif /* PH7_DISABLE_BUILTIN_FUNC */
/* Language constructs functions */
{ "echo", vm_builtin_echo },
{ "print", vm_builtin_print },
{ "exit", vm_builtin_exit },
{ "eval", vm_builtin_eval },


+ 0
- 2
include/ph7int.h View File

@ -1479,7 +1479,6 @@ enum ph7_expr_id {
*/
#define PH7_TKWRD_EXTENDS 1 /* extends */
#define PH7_TKWRD_SWITCH 3 /* switch */
#define PH7_TKWRD_PRINT 4 /* print */
#define PH7_TKWRD_INTERFACE 5 /* interface */
/* The number '8' is reserved for PH7_TK_ID */
#define PH7_TKWRD_REQONCE 9 /* require_once */
@ -1510,7 +1509,6 @@ enum ph7_expr_id {
#define PH7_TKWRD_AS 33 /* as */
#define PH7_TKWRD_CONTINUE 34 /* continue */
#define PH7_TKWRD_EXIT 35 /* exit */
#define PH7_TKWRD_ECHO 37 /* echo */
#define PH7_TKWRD_IMPLEMENTS 39 /* implements */
#define PH7_TKWRD_INCONCE 40 /* include_once */
#define PH7_TKWRD_INCLUDE 41 /* include */


+ 1
- 1
tests/utf8_variables.aer View File

@ -9,7 +9,7 @@ class Main {
}
private function ダウンロード(){
echo $this->概要 . "\n";
print($this->概要 . "\n");
}
private function isUTF8($str) {


Loading…
Cancel
Save