implemented very simple repl
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
#include "token/type.hpp"
|
#include "repl/repl.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
token::type eof = token::type::ILLEGAL;
|
repl::start(std::cin, std::cout);
|
||||||
std::cout << eof << std::endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
30
src/repl/repl.cpp
Normal file
30
src/repl/repl.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include "repl.hpp"
|
||||||
|
|
||||||
|
#include "lexer/lexer.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
6
src/repl/repl.hpp
Normal file
6
src/repl/repl.hpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <istream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
namespace repl {
|
||||||
|
void start(std::istream&, std::ostream&);
|
||||||
|
}
|
@@ -13,4 +13,8 @@ namespace token {
|
|||||||
|
|
||||||
token(::token::type t, char c): type(t), literal(1, c) {}
|
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
|
} // namespace token
|
||||||
|
Reference in New Issue
Block a user