#include "command.h" #include "astbranch.h" #include "gamestate.h" #include 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.parse( pAst ); }