stela/interpreter.hpp

72 lines
1.3 KiB
C++

#pragma once
#include <vector>
#include "lexer.hpp"
#include "parser.hpp"
namespace stela {
class Command;
class Interpreter {
public:
Interpreter()
: commands()
, lexer(*this)
, parser(lexer, *this)
, m_location(0)
{}
inline int parse()
{
this->m_location = 0;
return this->parser.parse();
}
inline void clear()
{
this->m_location = 0;
this->commands.clear();
}
std::string to_string() const;
/// Switch scanner input stream. Default is standard input (std::cin).
/// It will also reset AST.
inline void switch_input_stream(std::istream* is)
{
this->lexer.switch_streams(is, nullptr);
this->commands.clear();
}
friend class Parser;
friend class Lexer;
private:
inline void add_command(const Command& command)
{
this->commands.push_back(command);
}
inline void increase_location(unsigned int location)
{
this->m_location += location;
std::cout << "increase_location(): "
<< location << ", total = " << this->m_location << '\n';
}
inline unsigned int location() const
{
return this->m_location;
}
private:
Lexer lexer;
Parser parser;
std::vector<Command> commands;
unsigned int m_location;
};
}