diff --git a/src/main.cpp b/src/main.cpp index cc3a0c6..1603b08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,8 @@ -#include "token/type.hpp" +#include "repl/repl.hpp" #include int main() { - token::type eof = token::type::ILLEGAL; - std::cout << eof << std::endl; + repl::start(std::cin, std::cout); return 0; } diff --git a/src/repl/repl.cpp b/src/repl/repl.cpp new file mode 100644 index 0000000..55aa3ff --- /dev/null +++ b/src/repl/repl.cpp @@ -0,0 +1,30 @@ +#include "repl.hpp" + +#include "lexer/lexer.hpp" + +#include +#include + +static const std::string PROMPT = ">> "; + +namespace repl { + + void start(std::istream& in, std::ostream& out) { + while (true) { + out << PROMPT; + std::string line; + if (!std::getline(in, line)) + return; + + std::istringstream ss(line); + + lexer::lexer l{ss}; + for (token::token tok = l.next_token(); + tok.type != token::type::END_OF_FILE; + tok = l.next_token()) + out << tok << " "; + out << std::endl; + } + } + +} // namespace repl diff --git a/src/repl/repl.hpp b/src/repl/repl.hpp new file mode 100644 index 0000000..9d2c5d7 --- /dev/null +++ b/src/repl/repl.hpp @@ -0,0 +1,6 @@ +#include +#include + +namespace repl { + void start(std::istream&, std::ostream&); +} diff --git a/src/token/token.hpp b/src/token/token.hpp index 227baac..db18c3b 100644 --- a/src/token/token.hpp +++ b/src/token/token.hpp @@ -13,4 +13,8 @@ namespace token { token(::token::type t, char c): type(t), literal(1, c) {} }; + + inline std::ostream& operator<<(std::ostream& os, token tok) { + return os << tok.type << '(' << tok.literal << ')'; + } } // namespace token