Token.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Token.h"
  2. #include "../Exception.h"
  3. #include <iostream>
  4. #include "TokenType.h"
  5. Token::Token(TokenType type, int line) : type(type)
  6. {
  7. this->line = line;
  8. f = 0.0f;
  9. s = "";
  10. }
  11. Token::~Token()
  12. {
  13. }
  14. void Token::setFloat(float f)
  15. {
  16. if(type != Tokens::FLOAT)
  17. {
  18. throw Exception("token contains no float");
  19. }
  20. this->f = f;
  21. }
  22. void Token::setString(string s)
  23. {
  24. if(type != Tokens::TEXT && type != Tokens::LABEL && type != Tokens::VAR)
  25. {
  26. throw Exception("token contains no string");
  27. }
  28. this->s.assign(s);
  29. }
  30. float Token::getFloat() const
  31. {
  32. return f;
  33. }
  34. bool Token::getBool() const
  35. {
  36. if(type == Tokens::TRUE)
  37. {
  38. return true;
  39. }
  40. return false;
  41. }
  42. string Token::getString() const
  43. {
  44. return s;
  45. }
  46. int Token::getLine() const
  47. {
  48. return line;
  49. }
  50. TokenType Token::getType() const
  51. {
  52. return type;
  53. }
  54. std::ostream& operator<<(std::ostream& stream, const Token& t)
  55. {
  56. stream << t.getLine() << " ";
  57. stream << t.getType();
  58. string s = t.getString();
  59. if(s.size() != 0)
  60. {
  61. stream << "(\"" << s << "\")";
  62. }
  63. if(t.getType() == Tokens::FLOAT)
  64. {
  65. stream << '(' << t.getFloat() << ')';
  66. }
  67. return stream;
  68. }