summaryrefslogtreecommitdiff
path: root/src/command.cpp
blob: a11424641cc91963f7464390e2400b62ed3ba213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "command.h"

#include "astbranch.h"
#include "gamestate.h"

#include <bu/sio.h>
using namespace Bu;

Command::Command() :
	pAst( NULL )
{
}

Command::~Command()
{
	delete pAst;
}

void Command::addLiteral( const Bu::String &sValue )
{
	lChunks.append( Chunk( true, sValue ) );
}

void Command::addParam( const Bu::String &sValue )
{
	lChunks.append( Chunk( false, sValue ) );
}

Bu::StringList Command::getParamList() const
{
	Bu::StringList lRev;
	for( ChunkList::const_iterator i = lChunks.begin(); i; i++ )
	{
		if( !(*i).bLiteral )
			lRev.prepend( (*i).sValue );
	}

	return lRev;
}

void Command::setAst( class AstBranch *pAst )
{
	this->pAst = pAst;
}

void Command::print()
{
	sio << "command:";
	for( ChunkList::iterator i = lChunks.begin(); i; i++ )
	{
		if( (*i).bLiteral )
			sio << " \"" << (*i).sValue << "\"";
		else
			sio << " " << (*i).sValue;
	}

	sio << *pAst << sio.nl;
}

bool Command::matches( const Bu::StringList &lCmd )
{
	ChunkList::iterator iChunk = lChunks.begin();
	Bu::StringList::const_iterator iCmd = lCmd.begin();

	for( ; iChunk && iCmd; iChunk++, iCmd++ )
	{
		if( (*iChunk).bLiteral )
		{
			if( (*iChunk).sValue != *iCmd )
				return false;
		}
	}

	if( (bool)iChunk != (bool)iCmd )
		return false;

	return true;
}

void Command::exec( class GameState &gState, const Bu::StringList &lCmd )
{
	ChunkList::iterator iChunk = lChunks.begin();
	Bu::StringList::const_iterator iCmd = lCmd.begin();

	for( ; iChunk && iCmd; iChunk++, iCmd++ )
	{
		if( !(*iChunk).bLiteral )
		{
			gState.push( Variable( *iCmd ) );
		}
	}

	gState.run( pAst );
}