1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "Token.h"
- #include "../Exception.h"
- #include <iostream>
- #include "TokenType.h"
- Token::Token(TokenType type, int line) : type(&type)
- {
- this->line = line;
- f = 0.0f;
- s = "";
- }
- Token::~Token()
- {
- }
- void Token::setFloat(float f)
- {
- if((*type) != Tokens::FLOAT)
- {
- throw Exception("token contains no float");
- }
- this->f = f;
- }
- void Token::setString(string s)
- {
- if(*type != Tokens::TEXT && *type != Tokens::LABEL && *type != Tokens::VAR)
- {
- throw Exception("token contains no string");
- }
- this->s.assign(s);
- }
- float Token::getFloat() const
- {
- return f;
- }
- bool Token::getBool() const
- {
- if(*type == Tokens::TRUE)
- {
- return true;
- }
- return false;
- }
- string Token::getString() const
- {
- return s;
- }
- int Token::getLine() const
- {
- return line;
- }
- void Token::setType(TokenType type)
- {
- this->type = &type;
- }
- TokenType Token::getType() const
- {
- return *type;
- }
- std::ostream& operator<<(std::ostream& stream, const Token& t)
- {
- stream << t.getLine() << " ";
- stream << t.getType();
- string s = t.getString();
- if(s.size() != 0)
- {
- stream << "(\"" << s << "\")";
- }
- if(t.getType() == Tokens::FLOAT)
- {
- stream << '(' << t.getFloat() << ')';
- }
- return stream;
- }
|