From 33b6c34a20545222088f645bbc3cc5610f7cc782 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 8 Feb 2012 09:29:50 -0700 Subject: Added function count(). --- src/functioncount.cpp | 35 +++++++++++++++++++++++++++++++++++ src/functioncount.h | 16 ++++++++++++++++ src/game.cpp | 2 ++ tests/test.stage | 4 +++- 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/functioncount.cpp create mode 100644 src/functioncount.h diff --git a/src/functioncount.cpp b/src/functioncount.cpp new file mode 100644 index 0000000..f879238 --- /dev/null +++ b/src/functioncount.cpp @@ -0,0 +1,35 @@ +#include "functioncount.h" + +#include "gamestate.h" + +FunctionCount::FunctionCount() +{ +} + +FunctionCount::~FunctionCount() +{ +} + +void FunctionCount::call( class GameState &gState ) +{ + Variable v = gState.popDeref(); + switch( v.getType() ) + { + case Variable::tList: + gState.push( (int64_t)v.getList().getSize() ); + break; + + case Variable::tDictionary: + gState.push( (int64_t)v.getHash().getSize() ); + break; + + case Variable::tString: + gState.push( (int64_t)v.getString().getSize() ); + break; + + default: + gState.push( Variable( Variable::tNull ) ); + break; + } +} + diff --git a/src/functioncount.h b/src/functioncount.h new file mode 100644 index 0000000..24f9e4e --- /dev/null +++ b/src/functioncount.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_COUNT_H +#define FUNCTION_COUNT_H + +#include "function.h" + +class FunctionCount : public Function +{ +public: + FunctionCount(); + virtual ~FunctionCount(); + + virtual Bu::String getName() const { return "count"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 19a31f7..05c5eca 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -9,6 +9,7 @@ #include "functionstring.h" #include "functionjoin.h" #include "functionkeys.h" +#include "functioncount.h" Game::Game() { @@ -22,6 +23,7 @@ Game::Game() addFunction( new FunctionString() ); addFunction( new FunctionJoin() ); addFunction( new FunctionKeys() ); + addFunction( new FunctionCount() ); } Game::~Game() diff --git a/tests/test.stage b/tests/test.stage index cc765a4..dc7ad7e 100644 --- a/tests/test.stage +++ b/tests/test.stage @@ -24,8 +24,10 @@ situation <> { if true then { - getVal(); + getVal(); } + + display( count( "global.stuff" ) ); } enter -- cgit v1.2.3