Token.cpp 1.2 KB

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