Token.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. void Token::setType(TokenType type)
  51. {
  52. this->type = &type;
  53. }
  54. TokenType Token::getType() const
  55. {
  56. return *type;
  57. }
  58. std::ostream& operator<<(std::ostream& stream, const Token& t)
  59. {
  60. stream << t.getLine() << " ";
  61. stream << t.getType();
  62. string s = t.getString();
  63. if(s.size() != 0)
  64. {
  65. stream << "(\"" << s << "\")";
  66. }
  67. if(t.getType() == Tokens::FLOAT)
  68. {
  69. stream << '(' << t.getFloat() << ')';
  70. }
  71. return stream;
  72. }