#include "Token.h" #include /** * Constructos, initialize the type, the string contents, and the integer * contents. */ Token::Token(const Type t, const string &s) : type(t), int_val(0), str_val(s) { } Token::Token(const Type t, const int i) : type(t), int_val(i), str_val("") { } Token::Token(const Token * const tok) : type(tok->type), int_val(tok->int_val), str_val(tok->str_val) { } /** * Destructor, auto-filled. */ Token::~Token(void) { } /** * Convert a token to a string. */ string &Token::appendToString(string &str) const { stringstream ss(str); ss << type << "("; if(type == INT) { ss << int_val << ")"; } else { ss << str_val << ")"; } str.assign(ss.str()); return str; } /** * Overload output stream functions to support tokens and token types. */ ostream &operator<<(ostream &out, const Token * const token) { string ostr(""); return out << token->appendToString(ostr); } ostream &operator<<(ostream &out, const Token &token) { string ostr(""); return out << token.appendToString(ostr); } ostream &operator<<(ostream &is, const Token::Type type) { switch(type) { case Token::STRING: is << "STRING"; break; case Token::INT: is << "INT"; break; case Token::VARNAME: is << "VARNAME"; break; case Token::FUNCTION: is << "FUNCTION"; break; case Token::OPERATOR: is << "OPERATOR"; break; } return is; }