Add sizeof() operator for numeric data types
parent
c0cdd84e85
commit
bc572aa52f
|
@ -114,6 +114,46 @@ CBotInstr* CompileExprLitNum(CBotToken* &p, CBotCStack* pStack)
|
||||||
return pStack->Return(nullptr, pStk);
|
return pStack->Return(nullptr, pStk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CBotInstr* CompileSizeOf(CBotToken* &p, CBotCStack* pStack)
|
||||||
|
{
|
||||||
|
CBotToken* pp = p;
|
||||||
|
|
||||||
|
if (!IsOfType(p, TokenTypVar)) return nullptr;
|
||||||
|
if (pp->GetString() == "sizeof" && IsOfType(p, ID_OPENPAR))
|
||||||
|
{
|
||||||
|
CBotCStack* pStk = pStack->TokenStack();
|
||||||
|
|
||||||
|
int value;
|
||||||
|
|
||||||
|
if (IsOfType(p, ID_BYTE)) value = sizeof(signed char);
|
||||||
|
else if (IsOfType(p, ID_SHORT)) value = sizeof(short);
|
||||||
|
else if (IsOfType(p, ID_CHAR)) value = sizeof(uint32_t);
|
||||||
|
else if (IsOfType(p, ID_INT)) value = sizeof(int);
|
||||||
|
else if (IsOfType(p, ID_LONG)) value = sizeof(long);
|
||||||
|
else if (IsOfType(p, ID_FLOAT)) value = sizeof(float);
|
||||||
|
else if (IsOfType(p, ID_DOUBLE)) value = sizeof(double);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p = pp;
|
||||||
|
return pStack->Return(nullptr, pStk);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsOfType(p, ID_CLOSEPAR))
|
||||||
|
{
|
||||||
|
auto inst = new CBotExprLitNum<int>(value);
|
||||||
|
inst->SetToken(pp);
|
||||||
|
|
||||||
|
CBotVar* var = CBotVar::Create("", CBotTypInt);
|
||||||
|
pStk->SetVar(var);
|
||||||
|
return pStack->Return(inst, pStk);
|
||||||
|
}
|
||||||
|
pStk->SetError(CBotErrClosePar, p->GetStart());
|
||||||
|
return pStack->Return(nullptr, pStk);
|
||||||
|
}
|
||||||
|
p = pp;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool CBotExprLitNum<T>::Execute(CBotStack* &pj)
|
bool CBotExprLitNum<T>::Execute(CBotStack* &pj)
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,6 +26,8 @@ namespace CBot
|
||||||
|
|
||||||
CBotInstr* CompileExprLitNum(CBotToken* &p, CBotCStack* pStack);
|
CBotInstr* CompileExprLitNum(CBotToken* &p, CBotCStack* pStack);
|
||||||
|
|
||||||
|
CBotInstr* CompileSizeOf(CBotToken* &p, CBotCStack* pStack);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief A number literal - 5, 1, 2.5, 3.75, etc. or a predefined numerical constant (see CBotToken::DefineNum())
|
* \brief A number literal - 5, 1, 2.5, 3.75, etc. or a predefined numerical constant (see CBotToken::DefineNum())
|
||||||
*
|
*
|
||||||
|
|
|
@ -68,6 +68,13 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack)
|
||||||
if (inst != nullptr || !pStk->IsOk())
|
if (inst != nullptr || !pStk->IsOk())
|
||||||
return pStack->Return(inst, pStk);
|
return pStack->Return(inst, pStk);
|
||||||
|
|
||||||
|
// is it sizeof operator ?
|
||||||
|
inst = CBot::CompileSizeOf(p, pStk);
|
||||||
|
if (inst != nullptr || !pStk->IsOk())
|
||||||
|
{
|
||||||
|
return pStack->Return(inst, pStk);
|
||||||
|
}
|
||||||
|
|
||||||
// is it a variable name?
|
// is it a variable name?
|
||||||
if (p->GetType() == TokenTypVar)
|
if (p->GetType() == TokenTypVar)
|
||||||
{
|
{
|
||||||
|
@ -155,6 +162,13 @@ CBotInstr* CBotParExpr::CompileLitExpr(CBotToken* &p, CBotCStack* pStack)
|
||||||
return pStack->Return(inst, pStk);
|
return pStack->Return(inst, pStk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// is it sizeof operator ?
|
||||||
|
inst = CBot::CompileSizeOf(p, pStk);
|
||||||
|
if (inst != nullptr || !pStk->IsOk())
|
||||||
|
{
|
||||||
|
return pStack->Return(inst, pStk);
|
||||||
|
}
|
||||||
|
|
||||||
// is this a chaine?
|
// is this a chaine?
|
||||||
if (p->GetType() == TokenTypString)
|
if (p->GetType() == TokenTypString)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue