diff --git a/src/CBot/CBotInstr/CBotExprLitNum.cpp b/src/CBot/CBotInstr/CBotExprLitNum.cpp index 9842cf57..61978bf0 100644 --- a/src/CBot/CBotInstr/CBotExprLitNum.cpp +++ b/src/CBot/CBotInstr/CBotExprLitNum.cpp @@ -114,6 +114,46 @@ CBotInstr* CompileExprLitNum(CBotToken* &p, CBotCStack* pStack) 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(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 bool CBotExprLitNum::Execute(CBotStack* &pj) { diff --git a/src/CBot/CBotInstr/CBotExprLitNum.h b/src/CBot/CBotInstr/CBotExprLitNum.h index 92cd302d..8d7edcc1 100644 --- a/src/CBot/CBotInstr/CBotExprLitNum.h +++ b/src/CBot/CBotInstr/CBotExprLitNum.h @@ -26,6 +26,8 @@ namespace CBot 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()) * diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 24bdaafe..9572ff5e 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -68,6 +68,13 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) if (inst != nullptr || !pStk->IsOk()) 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? if (p->GetType() == TokenTypVar) { @@ -155,6 +162,13 @@ CBotInstr* CBotParExpr::CompileLitExpr(CBotToken* &p, CBotCStack* pStack) 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? if (p->GetType() == TokenTypString) {