implemented very simple repl

This commit is contained in:
Karma Riuk
2025-07-01 18:43:25 +02:00
parent aee7a741b1
commit 69bee723a2
4 changed files with 42 additions and 3 deletions

View File

@@ -1,9 +1,8 @@
#include "token/type.hpp"
#include "repl/repl.hpp"
#include <iostream>
int main() {
token::type eof = token::type::ILLEGAL;
std::cout << eof << std::endl;
repl::start(std::cin, std::cout);
return 0;
}

30
src/repl/repl.cpp Normal file
View 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
View File

@@ -0,0 +1,6 @@
#include <istream>
#include <ostream>
namespace repl {
void start(std::istream&, std::ostream&);
}

View File

@@ -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