#include "token.h" #include "number.h" #include #include Token::Token() : eType( tUninitialized ), sVal( 0 ) { } Token::Token( Type eType ) : eType( eType ), sVal( 0 ) { } Token::Token( Type eType, Bu::String *s ) : eType( eType ), sVal( s ) { } Token::Token( Type eType, Number *n ) : eType( eType ), nVal( n ) { } Token::Token( const Token &rSrc ) : eType( rSrc.eType ), sVal( rSrc.sVal ) { Token &rMod = const_cast(rSrc); rMod.sVal = 0; } Token::~Token() { switch( eType ) { case tNumber: delete nVal; break; case tVariable: case tCommand: case tString: delete sVal; break; default: break; } } Token &Token::operator=( const Token &rhs ) { switch( eType ) { case tNumber: delete nVal; break; case tVariable: case tCommand: case tString: delete sVal; break; default: break; } eType = rhs.eType; sVal = rhs.sVal; Token &rMod = const_cast(rhs); rMod.sVal = 0; return *this; } Bu::Formatter &operator<<( Bu::Formatter &f, Token::Type eType ) { switch( eType ) { case Token::tNumber: return f << "num"; case Token::tVariable: return f << "var"; case Token::tCommand: return f << "cmd"; case Token::tPlus: return f << "+"; case Token::tMinus: return f << "-"; case Token::tDivide: return f << "/"; case Token::tMultiply: return f << "*"; case Token::tModulus: return f << "%"; case Token::tOpenParen: return f << "("; case Token::tCloseParen: return f << ")"; case Token::tEquals: return f << "="; case Token::tString: return f << "str"; case Token::tEndOfLine: return f << "eol"; case Token::tEndOfInput: return f << "eoi"; case Token::tNegate: return f << "neg"; case Token::tUninitialized: return f << "<->"; case Token::tComputedValue: return f << "cmp"; default: return f << "???"; } } Bu::Formatter &operator<<( Bu::Formatter &f, const Token &t ) { f << t.eType; if( t.eType == Token::tNumber ) f << "(" << (t.nVal?t.nVal->toString():"") << ")"; else if( t.eType == Token::tString || t.eType == Token::tVariable ) f << "(" << (t.sVal?*(t.sVal):"") << ")"; return f; }