⚡ repl does not write temp file now
This commit is contained in:
parent
a0e6047296
commit
ec03f0aee0
|
@ -8,7 +8,7 @@
|
|||
#include <winsock.h>
|
||||
#pragma comment(lib,"ws2_32")
|
||||
|
||||
class WSAmanager{
|
||||
class WSAmanager {
|
||||
private:
|
||||
WSAData data;
|
||||
public:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "nasal_err.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h> // use SetConsoleTextAttribute
|
||||
struct for_reset {
|
||||
|
@ -6,7 +7,11 @@ struct for_reset {
|
|||
for_reset() {
|
||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &scr);
|
||||
}
|
||||
} reset_ter_color;
|
||||
static for_reset* singleton() {
|
||||
static for_reset windows_set;
|
||||
return &windows_set;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
std::ostream& back_white(std::ostream& s) {
|
||||
|
@ -57,7 +62,7 @@ std::ostream& white(std::ostream& s) {
|
|||
std::ostream& reset(std::ostream& s) {
|
||||
#ifdef _WIN32
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
|
||||
reset_ter_color.scr.wAttributes);
|
||||
for_reset::singleton()->scr.wAttributes);
|
||||
#else
|
||||
s << "\033[0m";
|
||||
#endif
|
||||
|
@ -68,7 +73,22 @@ void flstream::load(const std::string& f) {
|
|||
if (file==f) { // don't need to load a loaded file
|
||||
return;
|
||||
} 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();
|
||||
|
|
|
@ -8,6 +8,18 @@
|
|||
|
||||
#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 {
|
||||
u32 begin_line;
|
||||
u32 begin_column;
|
||||
|
|
|
@ -62,6 +62,14 @@ void lexer::err_char() {
|
|||
}
|
||||
|
||||
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
|
||||
struct stat buffer;
|
||||
if (stat(file.c_str(), &buffer)==0 && !S_ISREG(buffer.st_mode)) {
|
||||
|
|
21
src/repl.cpp
21
src/repl.cpp
|
@ -20,16 +20,14 @@ void repl::update_temp_file() {
|
|||
for(const auto& i : source) {
|
||||
content += i + "\n";
|
||||
}
|
||||
|
||||
std::ofstream out(".temp.nas", std::ios::binary);
|
||||
out << content;
|
||||
repl_file_info::instance()->repl_file_source = content;
|
||||
}
|
||||
|
||||
bool repl::check_need_more_input() {
|
||||
while(true) {
|
||||
update_temp_file();
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -67,6 +65,10 @@ void repl::help() {
|
|||
bool repl::run() {
|
||||
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_parser = std::unique_ptr<parse>(new parse);
|
||||
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_runtime = std::unique_ptr<vm>(new vm);
|
||||
|
||||
if (nasal_lexer->scan(".temp.nas").geterr()) {
|
||||
if (nasal_lexer->scan("<nasal-repl>").geterr()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -82,7 +84,7 @@ bool repl::run() {
|
|||
return false;
|
||||
}
|
||||
|
||||
if (nasal_linker->link(*nasal_parser, ".temp.nas", true).geterr()) {
|
||||
if (nasal_linker->link(*nasal_parser, "<nasal-repl>", true).geterr()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -91,6 +93,8 @@ bool repl::run() {
|
|||
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);
|
||||
|
||||
return true;
|
||||
|
@ -98,6 +102,9 @@ bool repl::run() {
|
|||
|
||||
void repl::execute() {
|
||||
source = {};
|
||||
auto repl_file_handle = repl_file_info::instance();
|
||||
repl_file_handle->in_repl_mode = true;
|
||||
|
||||
std::cout << "Nasal REPL interpreter(experimental).\n";
|
||||
help();
|
||||
|
||||
|
@ -127,9 +134,11 @@ void repl::execute() {
|
|||
continue;
|
||||
}
|
||||
|
||||
// run program
|
||||
if (!run()) {
|
||||
source.pop_back();
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue