summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/functioncount.cpp35
-rw-r--r--src/functioncount.h16
-rw-r--r--src/game.cpp2
-rw-r--r--tests/test.stage4
4 files changed, 56 insertions, 1 deletions
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 @@
1#include "functioncount.h"
2
3#include "gamestate.h"
4
5FunctionCount::FunctionCount()
6{
7}
8
9FunctionCount::~FunctionCount()
10{
11}
12
13void FunctionCount::call( class GameState &gState )
14{
15 Variable v = gState.popDeref();
16 switch( v.getType() )
17 {
18 case Variable::tList:
19 gState.push( (int64_t)v.getList().getSize() );
20 break;
21
22 case Variable::tDictionary:
23 gState.push( (int64_t)v.getHash().getSize() );
24 break;
25
26 case Variable::tString:
27 gState.push( (int64_t)v.getString().getSize() );
28 break;
29
30 default:
31 gState.push( Variable( Variable::tNull ) );
32 break;
33 }
34}
35
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 @@
1#ifndef FUNCTION_COUNT_H
2#define FUNCTION_COUNT_H
3
4#include "function.h"
5
6class FunctionCount : public Function
7{
8public:
9 FunctionCount();
10 virtual ~FunctionCount();
11
12 virtual Bu::String getName() const { return "count"; }
13 virtual void call( class GameState &gState );
14};
15
16#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 @@
9#include "functionstring.h" 9#include "functionstring.h"
10#include "functionjoin.h" 10#include "functionjoin.h"
11#include "functionkeys.h" 11#include "functionkeys.h"
12#include "functioncount.h"
12 13
13Game::Game() 14Game::Game()
14{ 15{
@@ -22,6 +23,7 @@ Game::Game()
22 addFunction( new FunctionString() ); 23 addFunction( new FunctionString() );
23 addFunction( new FunctionJoin() ); 24 addFunction( new FunctionJoin() );
24 addFunction( new FunctionKeys() ); 25 addFunction( new FunctionKeys() );
26 addFunction( new FunctionCount() );
25} 27}
26 28
27Game::~Game() 29Game::~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 <<start>>
24 { 24 {
25 if true then 25 if true then
26 { 26 {
27 getVal(); 27 getVal();
28 } 28 }
29
30 display( count( "global.stuff" ) );
29 } 31 }
30 32
31 enter 33 enter