Browse Source

The 'self' and 'parent' keywords should be resolved at compile time.

release/v0.1
Rafal Kupiec 1 year ago
parent
commit
0a6b5a6f42
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
2 changed files with 51 additions and 41 deletions
  1. +51
    -4
      engine/compiler.c
  2. +0
    -37
      engine/constant.c

+ 51
- 4
engine/compiler.c View File

@ -1093,16 +1093,14 @@ static sxi32 PH7_GenStateLoadLiteral(ph7_gen_state *pGen) {
/* Extract token value */
pStr = &pToken->sData;
/* Deal with the reserved literals [i.e: null,false,true,...] first */
if(pStr->nByte == sizeof("NULL") - 1) {
if(SyStrnicmp(pStr->zString, "null", sizeof("NULL") - 1) == 0) {
if(pStr->nByte == sizeof("NULL") - 1 && SyStrnicmp(pStr->zString, "null", sizeof("NULL") - 1) == 0) {
/* NULL constant are always indexed at 0 */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, 0, 0, 0);
return SXRET_OK;
} else if(SyStrnicmp(pStr->zString, "true", sizeof("TRUE") - 1) == 0) {
} else if(pStr->nByte == sizeof("TRUE") - 1 && SyStrnicmp(pStr->zString, "true", sizeof("TRUE") - 1) == 0) {
/* TRUE constant are always indexed at 1 */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, 1, 0, 0);
return SXRET_OK;
}
} else if(pStr->nByte == sizeof("FALSE") - 1 &&
SyStrnicmp(pStr->zString, "false", sizeof("FALSE") - 1) == 0) {
/* FALSE constant are always indexed at 2 */
@ -1202,6 +1200,55 @@ static sxi32 PH7_GenStateLoadLiteral(ph7_gen_state *pGen) {
}
}
return SXRET_OK;
} else if(pStr->nByte == sizeof("PARENT") - 1 && SyMemcmp(pStr->zString, "parent", sizeof("PARENT") - 1) == 0) {
GenBlock *pBlock = pGen->pCurrent;
while(pBlock && (pBlock->iFlags & GEN_BLOCK_CLASS) == 0) {
/* Point to the upper block */
pBlock = pBlock->pParent;
}
if(pBlock == 0) {
/* Called in the global scope, load NULL */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, 0, 0, 0);
} else {
/* Extract the target base class */
ph7_class_info *pClassInfo = (ph7_class_info *)pBlock->pUserData;
SyString *pClassBase;
pClassBase = SySetAt(&pClassInfo->sExtends, 0);
if(pClassBase) {
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(pGen, E_ERROR, pToken->nLine, "PH7 engine is running out-of-memory");
}
PH7_MemObjInitFromString(pGen->pVm, pObj, pClassBase);
/* Emit the load constant instruction */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, nIdx, 0, 0);
} else {
/* No parent class, load NULL */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, 0, 0, 0);
}
}
return SXRET_OK;
} else if(pStr->nByte == sizeof("SELF") - 1 && SyMemcmp(pStr->zString, "self", sizeof("SELF") - 1) == 0) {
GenBlock *pBlock = pGen->pCurrent;
while(pBlock && (pBlock->iFlags & GEN_BLOCK_CLASS) == 0) {
/* Point to the upper block */
pBlock = pBlock->pParent;
}
if(pBlock == 0) {
/* Called in the global scope, load NULL */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, 0, 0, 0);
} else {
/* Extract the target class */
ph7_class_info *pClassInfo = (ph7_class_info *)pBlock->pUserData;
pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx);
if(pObj == 0) {
PH7_GenCompileError(pGen, E_ERROR, pToken->nLine, "PH7 engine is running out-of-memory");
}
PH7_MemObjInitFromString(pGen->pVm, pObj, &pClassInfo->sName);
/* Emit the load constant instruction */
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_LOADC, 0, nIdx, 0, 0);
}
return SXRET_OK;
}
/* Query literal table */
if(SXRET_OK != PH7_GenStateFindLiteral(&(*pGen), &pToken->sData, &nIdx)) {

+ 0
- 37
engine/constant.c View File

@ -1092,41 +1092,6 @@ static void PH7_static_Const(ph7_value *pVal, void *pUserData) {
ph7_value_string(pVal, "static", sizeof("static") - 1);
}
}
/*
* self
* Expand the name of the current class. NULL otherwise.
*/
static void PH7_self_Const(ph7_value *pVal, void *pUserData) {
ph7_vm *pVm = (ph7_vm *)pUserData;
ph7_class *pClass;
/* Extract the target class if available */
pClass = PH7_VmExtractActiveClass(pVm, 0);
if(pClass) {
SyString *pName = &pClass->sName;
/* Expand class name */
ph7_value_string(pVal, pName->zString, (int)pName->nByte);
} else {
/* Expand null */
ph7_value_string(pVal, "", 0);
}
}
/* parent
* Expand the name of the parent class. NULL otherwise.
*/
static void PH7_parent_Const(ph7_value *pVal, void *pUserData) {
ph7_vm *pVm = (ph7_vm *)pUserData;
ph7_class *pClass;
/* Extract the target class if available */
pClass = PH7_VmExtractActiveClass(pVm, 0);
if(pClass && pClass->pBase) {
SyString *pName = &pClass->pBase->sName;
/* Expand class name */
ph7_value_string(pVal, pName->zString, (int)pName->nByte);
} else {
/* Expand null */
ph7_value_string(pVal, "", 0);
}
}
/*
* Table of built-in constants.
*/
@ -1264,8 +1229,6 @@ static const ph7_builtin_constant aBuiltIn[] = {
{"EXTR_IF_EXISTS", PH7_EXTR_IF_EXISTS_Const },
{"EXTR_PREFIX_IF_EXISTS", PH7_EXTR_PREFIX_IF_EXISTS_Const},
{"static", PH7_static_Const },
{"self", PH7_self_Const },
{"parent", PH7_parent_Const }
};
/*
* Register the built-in constants defined above.

Loading…
Cancel
Save