The complete source code of IdealIRC http://www.idealirc.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
idealirc/Script/Tokens.cpp

494 lines
9.7 KiB

#include "Tokens.h"
struct MapTokenPriv
{
MapTokenPriv(int line, int col);
MapTokenPriv(MapTokenPriv&& other);
~MapTokenPriv() = default;
Expression subscript;
};
struct ListConstructPriv
{
ListConstructPriv() = default;
ListConstructPriv(ListConstructPriv&& other);
~ListConstructPriv() = default;
std::vector<Expression> items;
};
struct DispatchTokenPriv
{
DispatchTokenPriv(int line, int col);
DispatchTokenPriv(DispatchTokenPriv&& other);
~DispatchTokenPriv() = default;
Scope parameters;
};
struct FunctionTokenPriv
{
explicit FunctionTokenPriv(const std::string& identifier_, int line, int col);
FunctionTokenPriv(FunctionTokenPriv&& other);
~FunctionTokenPriv() = default;
const std::string identifier;
Scope parameters;
};
struct ExpressionPriv
{
ExpressionPriv() = default;
ExpressionPriv(ExpressionPriv&& other);
~ExpressionPriv() = default;
std::vector<std::unique_ptr<TokenBase>> tokens;
};
struct ScopePriv
{
ScopePriv() = default;
ScopePriv(ScopePriv&& other);
~ScopePriv() = default;
std::vector<Expression> expressions;
};
struct ConditionalScopeTokenPriv
{
ConditionalScopeTokenPriv(int line, int col, Expression&& condition);
ConditionalScopeTokenPriv(ConditionalScopeTokenPriv&& other);
~ConditionalScopeTokenPriv() = default;
ConditionalScope scope;
};
MapToken::~MapToken() = default;
DispatchToken::~DispatchToken() = default;
FunctionToken::~FunctionToken() = default;
Expression::~Expression() = default;
Scope::~Scope() = default;
ConditionalScopeToken::~ConditionalScopeToken() = default;
/*
* Constructors for privates
*/
MapTokenPriv::MapTokenPriv(int line, int col)
: subscript(line, col)
{}
MapTokenPriv::MapTokenPriv(MapTokenPriv&& other)
: subscript(std::move(other.subscript))
{}
ListConstructPriv::ListConstructPriv(ListConstructPriv&& other)
: items(std::move(other.items))
{}
DispatchTokenPriv::DispatchTokenPriv(int line, int col)
: parameters(line, col)
{}
DispatchTokenPriv::DispatchTokenPriv(DispatchTokenPriv&& other)
: parameters(std::move(other.parameters))
{}
FunctionTokenPriv::FunctionTokenPriv(const std::string& identifier_, int line, int col)
: identifier(identifier_)
, parameters(line, col)
{}
FunctionTokenPriv::FunctionTokenPriv(FunctionTokenPriv&& other)
: identifier(other.identifier)
, parameters(std::move(other.parameters))
{}
ExpressionPriv::ExpressionPriv(ExpressionPriv&& other)
: tokens(std::move(other.tokens))
{}
ScopePriv::ScopePriv(ScopePriv&& other)
: expressions(std::move(other.expressions))
{}
ConditionalScopeTokenPriv::ConditionalScopeTokenPriv(int line, int col, Expression&& condition)
: scope(line, col, std::move(condition))
{}
/*
* Various constructors
*/
TokenBase::TokenBase(TokenType type, int line, int col)
: m_type(type)
, m_line(line)
, m_col(col)
{}
TokenBase::TokenBase(TokenBase&& other)
: m_type(other.m_type)
, m_line(other.m_line)
, m_col(other.m_col)
{}
Token::Token(const std::string& token, TokenType type, int line, int col)
: TokenBase(type, line, col)
, m_token(token)
{}
Token::Token(Token&& other)
: TokenBase(std::move(other))
, m_token(std::move(other.m_token))
{}
BoolToken::BoolToken(bool val, int line, int col)
: TokenBase(TokenType::BoolLiteral, line, col)
, m_val(val)
{}
BoolToken::BoolToken(BoolToken&& other)
: TokenBase(std::move(other))
, m_val(other.m_val)
{}
MapToken::MapToken(const std::string& token, int line, int col, bool isRef)
: Token(token, isRef ? TokenType::Reference : TokenType::MapKeyword, line, col)
, mp(new MapTokenPriv(line, col))
{}
MapToken::MapToken(MapToken&& other)
: Token(std::move(other))
, mp(std::move(other.mp))
{}
DispatchToken::DispatchToken(const std::string& token, int line, int col)
: Token(token, TokenType::Dispatch, line, col)
, mp(new DispatchTokenPriv(line, col))
{}
DispatchToken::DispatchToken(DispatchToken&& other)
: Token(std::move(other))
, mp(std::move(other.mp))
{}
FunctionToken::FunctionToken(const std::string& identifier, int line, int col)
: TokenBase(TokenType::Function, line, col)
, mp(new FunctionTokenPriv(identifier, line, col))
{}
FunctionToken::FunctionToken(FunctionToken&& other)
: TokenBase(std::move(other))
, mp(std::move(other.mp))
{}
Expression::Expression(int line, int col)
: TokenBase(TokenType::Expression, line, col)
, mp(new ExpressionPriv)
{}
Expression::Expression(Expression&& other)
: TokenBase(std::move(other))
, mp(std::move(other.mp))
{}
Scope::Scope(int line, int col)
: mp(new ScopePriv)
, m_line(line)
, m_col(col)
{}
Scope::Scope(std::pair<int, int> lineCol)
: mp(new ScopePriv)
, m_line(lineCol.first)
, m_col(lineCol.second)
{}
Scope::Scope(Scope&& other)
: mp(std::move(other.mp))
, m_line(other.m_line)
, m_col(other.m_col)
{}
ImperativeScope::ImperativeScope(int line, int col, ImperativeBlockType type, const std::string& identifier, Scope&& params)
: Scope(line, col)
, m_type(type)
, m_id(identifier)
, m_params(std::move(params))
{}
ConditionalScope::ConditionalScope(int line, int col, Expression&& condition)
: Scope(line, col)
, m_cond(std::move(condition))
{}
ConditionalScopeToken::ConditionalScopeToken(TokenType type, int line, int col, Expression&& condition)
: TokenBase(type, line, col)
, mp(new ConditionalScopeTokenPriv(line, col, std::move(condition)))
{}
/*
* Implementation
*
* TokenBase
*/
std::pair<int, int> TokenBase::lineCol() const
{
return std::make_pair(m_line, m_col);
}
TokenType TokenBase::type() const
{
return m_type;
}
/*
* Implementation
*
* Token
*/
const std::string& Token::token() const
{
return m_token;
}
/*
* Implementation
*
* BoolToken
*/
bool BoolToken::value() const
{
return m_val;
}
/*
* Implementation
*
* MapToken
*/
Expression& MapToken::subscript()
{
return mp->subscript;
}
const Expression& MapToken::subscript() const
{
return mp->subscript;
}
/*
* Implementation
*
* ListConstruct
*/
Expression& ListConstruct::addItem(int line, int col)
{
mp->items.emplace_back(line, col);
return mp->items.back();
}
/*
* Implementation
*
* DispatchToken
*/
Scope& DispatchToken::parameters()
{
return mp->parameters;
}
/*
* Implementation
*
* FunctionToken
*/
Scope& FunctionToken::parameters()
{
return mp->parameters;
}
const Scope& FunctionToken::parameters() const
{
return mp->parameters;
}
const std::string& FunctionToken::identifier() const
{
return mp->identifier;
}
/*
* Implementation
*
* ConditionalScopeToken
*/
ConditionalScope& ConditionalScopeToken::scope() const
{
return mp->scope;
}
/*
* Implementation
*
* Expression
*/
void Expression::addToken(const std::string& token, TokenType type, int line, int col)
{
mp->tokens.emplace_back(new Token(token, type, line, col));
}
void Expression::addBool(bool val, int line, int col)
{
mp->tokens.emplace_back(new BoolToken(val, line, col));
}
MapToken& Expression::addMapToken(const std::string& token, int line, int col, bool isRef)
{
mp->tokens.emplace_back(new MapToken(token, line, col, isRef));
return *(static_cast<MapToken*>(mp->tokens.back().get()));
}
DispatchToken& Expression::addDispatchToken(const std::string& token, int line, int col)
{
mp->tokens.emplace_back(new DispatchToken(token, line, col));
return *(static_cast<DispatchToken*>(mp->tokens.back().get()));
}
Expression& Expression::addExpression(int line, int col)
{
mp->tokens.emplace_back(new Expression(line, col));
return *(static_cast<Expression*>(mp->tokens.back().get()));
}
FunctionToken& Expression::addFunction(const std::string& identifier, int line, int col)
{
mp->tokens.emplace_back(new FunctionToken(identifier, line, col));
return *(static_cast<FunctionToken*>(mp->tokens.back().get()));
}
ConditionalScopeToken& Expression::addIfScope(int line, int col, Expression&& condition)
{
mp->tokens.emplace_back(new ConditionalScopeToken(TokenType::CondIf, line, col, std::move(condition)));
return *(static_cast<ConditionalScopeToken*>(mp->tokens.back().get()));
}
ConditionalScopeToken& Expression::addElseScope(int line, int col)
{
Expression dummy(0, 0);
mp->tokens.emplace_back(new ConditionalScopeToken(TokenType::CondElse, line, col, std::move(dummy)));
return *(static_cast<ConditionalScopeToken*>(mp->tokens.back().get()));
}
ConditionalScopeToken& Expression::addWhileScope(int line, int col, Expression&& condition)
{
mp->tokens.emplace_back(new ConditionalScopeToken(TokenType::CondWhile, line, col, std::move(condition)));
return *(static_cast<ConditionalScopeToken*>(mp->tokens.back().get()));
}
Expression::Iterator Expression::begin() const
{
return mp->tokens.begin();
}
Expression::Iterator Expression::end() const
{
return mp->tokens.end();
}
size_t Expression::count() const
{
return mp->tokens.size();
}
void Expression::eraseBack()
{
if (!mp->tokens.empty())
mp->tokens.erase(mp->tokens.end() - 1);
}
TokenBase& Expression::operator[](size_t idx) const
{
return *(mp->tokens.at(idx));
}
TokenBase& Expression::back() const
{
return *(mp->tokens.back());
}
/*
* Implementation
*
* Scope
*/
std::pair<int, int> Scope::getLineCol() const
{
return std::make_pair(m_line, m_col);
}
Expression& Scope::addStatement(Expression&& expr)
{
mp->expressions.push_back(std::move(expr));
return mp->expressions.back();
}
std::size_t Scope::countStatements() const
{
return mp->expressions.size();
}
std::vector<Expression>::iterator Scope::begin() const
{
return mp->expressions.begin();
}
std::vector<Expression>::iterator Scope::end() const
{
return mp->expressions.end();
}
/*
* Implementation
*
* ImperativeScope
*/
const std::string& ImperativeScope::identifier() const
{
return m_id;
}
const Scope& ImperativeScope::params() const
{
return m_params;
}
ImperativeBlockType ImperativeScope::type() const
{
return m_type;
}
/*
* Implementation
*
* ConditionalScope
*/
const Expression& ConditionalScope::condition() const
{
return m_cond;
}