repl does not write temp file now

This commit is contained in:
ValKmjolnir 2023-09-06 00:12:01 +08:00
parent a0e6047296
commit ec03f0aee0
5 changed files with 59 additions and 10 deletions

View File

@ -8,7 +8,7 @@
#include <winsock.h> #include <winsock.h>
#pragma comment(lib,"ws2_32") #pragma comment(lib,"ws2_32")
class WSAmanager{ class WSAmanager {
private: private:
WSAData data; WSAData data;
public: public:

View File

@ -1,4 +1,5 @@
#include "nasal_err.h" #include "nasal_err.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> // use SetConsoleTextAttribute #include <windows.h> // use SetConsoleTextAttribute
struct for_reset { struct for_reset {
@ -6,7 +7,11 @@ struct for_reset {
for_reset() { for_reset() {
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &scr); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &scr);
} }
} reset_ter_color; static for_reset* singleton() {
static for_reset windows_set;
return &windows_set;
}
};
#endif #endif
std::ostream& back_white(std::ostream& s) { std::ostream& back_white(std::ostream& s) {
@ -57,7 +62,7 @@ std::ostream& white(std::ostream& s) {
std::ostream& reset(std::ostream& s) { std::ostream& reset(std::ostream& s) {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
reset_ter_color.scr.wAttributes); for_reset::singleton()->scr.wAttributes);
#else #else
s << "\033[0m"; s << "\033[0m";
#endif #endif
@ -68,7 +73,22 @@ void flstream::load(const std::string& f) {
if (file==f) { // don't need to load a loaded file if (file==f) { // don't need to load a loaded file
return; return;
} else { } else {
file=f; file = f;
}
if (repl_file_info::instance()->in_repl_mode &&
repl_file_info::instance()->repl_file_name==file) {
const auto& source = repl_file_info::instance()->repl_file_source;
res = {};
size_t pos = 0, last = 0;
while ((pos = source.find("\n", last))!=std::string::npos) {
res.push_back(source.substr(last, pos - last));
last = pos + 1;
}
if (last<source.length()) {
res.push_back(source.substr(last));
}
return;
} }
res.clear(); res.clear();

View File

@ -8,6 +8,18 @@
#include "nasal.h" #include "nasal.h"
struct repl_file_info {
bool in_repl_mode = false;
std::string repl_file_name = "<nasal-repl>";
std::string repl_file_source = "";
// singleton
static repl_file_info* instance() {
static repl_file_info info;
return &info;
}
};
struct span { struct span {
u32 begin_line; u32 begin_line;
u32 begin_column; u32 begin_column;

View File

@ -62,6 +62,14 @@ void lexer::err_char() {
} }
void lexer::open(const std::string& file) { void lexer::open(const std::string& file) {
if (repl_file_info::instance()->in_repl_mode &&
repl_file_info::instance()->repl_file_name==file) {
err.load(file);
filename = file;
res = repl_file_info::instance()->repl_file_source;
return;
}
// check file exsits and it is a regular file // check file exsits and it is a regular file
struct stat buffer; struct stat buffer;
if (stat(file.c_str(), &buffer)==0 && !S_ISREG(buffer.st_mode)) { if (stat(file.c_str(), &buffer)==0 && !S_ISREG(buffer.st_mode)) {

View File

@ -20,16 +20,14 @@ void repl::update_temp_file() {
for(const auto& i : source) { for(const auto& i : source) {
content += i + "\n"; content += i + "\n";
} }
repl_file_info::instance()->repl_file_source = content;
std::ofstream out(".temp.nas", std::ios::binary);
out << content;
} }
bool repl::check_need_more_input() { bool repl::check_need_more_input() {
while(true) { while(true) {
update_temp_file(); update_temp_file();
auto nasal_lexer = std::unique_ptr<lexer>(new lexer); auto nasal_lexer = std::unique_ptr<lexer>(new lexer);
if (nasal_lexer->scan(".temp.nas").geterr()) { if (nasal_lexer->scan("<nasal-repl>").geterr()) {
return false; return false;
} }
@ -67,6 +65,10 @@ void repl::help() {
bool repl::run() { bool repl::run() {
update_temp_file(); update_temp_file();
using clk = std::chrono::high_resolution_clock;
const auto den = clk::duration::period::den;
auto start = clk::now();
auto nasal_lexer = std::unique_ptr<lexer>(new lexer); auto nasal_lexer = std::unique_ptr<lexer>(new lexer);
auto nasal_parser = std::unique_ptr<parse>(new parse); auto nasal_parser = std::unique_ptr<parse>(new parse);
auto nasal_linker = std::unique_ptr<linker>(new linker); auto nasal_linker = std::unique_ptr<linker>(new linker);
@ -74,7 +76,7 @@ bool repl::run() {
auto nasal_codegen = std::unique_ptr<codegen>(new codegen); auto nasal_codegen = std::unique_ptr<codegen>(new codegen);
auto nasal_runtime = std::unique_ptr<vm>(new vm); auto nasal_runtime = std::unique_ptr<vm>(new vm);
if (nasal_lexer->scan(".temp.nas").geterr()) { if (nasal_lexer->scan("<nasal-repl>").geterr()) {
return false; return false;
} }
@ -82,7 +84,7 @@ bool repl::run() {
return false; return false;
} }
if (nasal_linker->link(*nasal_parser, ".temp.nas", true).geterr()) { if (nasal_linker->link(*nasal_parser, "<nasal-repl>", true).geterr()) {
return false; return false;
} }
@ -91,6 +93,8 @@ bool repl::run() {
return false; return false;
} }
auto end = clk::now();
std::clog << "[compile time: " << (end-start).count()*1.0/den << "s]\n";
nasal_runtime->run(*nasal_codegen, *nasal_linker, {}, false); nasal_runtime->run(*nasal_codegen, *nasal_linker, {}, false);
return true; return true;
@ -98,6 +102,9 @@ bool repl::run() {
void repl::execute() { void repl::execute() {
source = {}; source = {};
auto repl_file_handle = repl_file_info::instance();
repl_file_handle->in_repl_mode = true;
std::cout << "Nasal REPL interpreter(experimental).\n"; std::cout << "Nasal REPL interpreter(experimental).\n";
help(); help();
@ -127,9 +134,11 @@ void repl::execute() {
continue; continue;
} }
// run program
if (!run()) { if (!run()) {
source.pop_back(); source.pop_back();
} }
std::cout << "\n"; std::cout << "\n";
} }
} }