Browse Source

Implement the implicit 'auto' type.

An implicitly typed local variable is strongly typed just as if it had been declared the type, but the compiler determines the type. The following two declarations of $i are functionally equivalent:
int $i = 10; // Explicitly typed.
auto $i = 10; // Implicitly typed.
release/v0.1
Rafal Kupiec 1 year ago
parent
commit
78c416c6b8
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
2 changed files with 18 additions and 12 deletions
  1. +5
    -1
      engine/compiler.c
  2. +13
    -11
      engine/vm.c

+ 5
- 1
engine/compiler.c View File

@ -2466,6 +2466,8 @@ static sxi32 PH7_CompileVar(ph7_gen_state *pGen) {
PH7_VmEmitInstr(pGen->pVm, pGen->pIn->nLine, PH7_OP_DONE, (rc != SXERR_EMPTY ? 1 : 0), 1, 0, 0);
/* Restore default bytecode container */
PH7_VmSetByteCodeContainer(pGen->pVm, pInstrContainer);
} else if(nType == MEMOBJ_NULL) {
PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Implicitly-typed variable '$%z' must be initialized", pName);
}
/* Set static variable type */
sStatic.iFlags = nType;
@ -2492,6 +2494,8 @@ static sxi32 PH7_CompileVar(ph7_gen_state *pGen) {
} else {
PH7_VmEmitInstr(pGen->pVm, nLine, PH7_OP_POP, 1, 0, 0, 0);
}
} else if(nType == MEMOBJ_NULL) {
PH7_GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Implicitly-typed variable '$%z' must be initialized", pName);
} else {
pGen->pIn += 2; /* Jump the dollar '$' sign and variable name */
}
@ -4926,7 +4930,7 @@ static ProcLangConstruct PH7_GenStateGetStatementHandler(
SyToken *pLookahead /* Look-ahead token */
) {
sxu32 n = 0;
if((nKeywordID & PH7_KEYWORD_TYPEDEF) != 0 || nKeywordID == PH7_KEYWORD_STATIC) {
if((nKeywordID & PH7_KEYWORD_AUTO) != 0 || (nKeywordID & PH7_KEYWORD_TYPEDEF) != 0 || nKeywordID == PH7_KEYWORD_STATIC) {
if(nKeywordID == PH7_KEYWORD_STATIC && pLookahead && (pLookahead->nType & PH7_TK_OP)) {
const ph7_expr_op *pOp = (const ph7_expr_op *)pLookahead->pUserData;
if(pOp && pOp->iOp == EXPR_OP_DC /*::*/) {

+ 13
- 11
engine/vm.c View File

@ -2421,20 +2421,22 @@ static sxi32 VmByteCodeExec(
PH7_VmThrowError(&(*pVm), PH7_CTX_ERR,
"Redeclaration of ‘$%z’ variable", &sName);
}
if(pInstr->iP2 & MEMOBJ_MIXED && (pInstr->iP2 & MEMOBJ_HASHMAP) == 0) {
pObj->nType = MEMOBJ_MIXED | MEMOBJ_VOID;
} else {
if(pInstr->iP2 & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap;
pMap = PH7_NewHashmap(&(*pVm), 0, 0);
if(pMap == 0) {
PH7_VmMemoryError(&(*pVm));
if(pInstr->iP2 != MEMOBJ_NULL) {
if(pInstr->iP2 & MEMOBJ_MIXED && (pInstr->iP2 & MEMOBJ_HASHMAP) == 0) {
pObj->nType = MEMOBJ_MIXED | MEMOBJ_VOID;
} else {
if(pInstr->iP2 & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap;
pMap = PH7_NewHashmap(&(*pVm), 0, 0);
if(pMap == 0) {
PH7_VmMemoryError(&(*pVm));
}
pObj->x.pOther = pMap;
}
pObj->x.pOther = pMap;
MemObjSetType(pObj, pInstr->iP2);
}
MemObjSetType(pObj, pInstr->iP2);
pTos->nIdx = SXU32_HIGH; /* Mark as constant */
}
pTos->nIdx = SXU32_HIGH; /* Mark as constant */
}
break;
}

Loading…
Cancel
Save