🔥 delete tools/repl.nas
This commit is contained in:
parent
3e01239722
commit
d0d9ce3b80
|
@ -137,14 +137,15 @@ void error::err(
|
||||||
const std::string iden = identation(maxlen);
|
const std::string iden = identation(maxlen);
|
||||||
|
|
||||||
for(u32 line = loc.begin_line; line<=loc.end_line; ++line) {
|
for(u32 line = loc.begin_line; line<=loc.end_line; ++line) {
|
||||||
|
// skip line 0
|
||||||
if (!line) {
|
if (!line) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loc.begin_line<line && line<loc.end_line) {
|
if (loc.begin_line<line && line<loc.end_line) {
|
||||||
if (line==loc.begin_line+1) {
|
if (line==loc.begin_line+1) {
|
||||||
std::cerr << cyan << iden << " | " << reset << "...\n"
|
std::cerr << cyan << iden << " | " << reset << "...\n";
|
||||||
<< cyan << iden << " | " << reset << "\n";
|
std::cerr << cyan << iden << " | " << reset << "\n";
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -154,7 +155,12 @@ void error::err(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& code=res[line-1];
|
// line out of range
|
||||||
|
if (line-1>=res.size()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& code = res[line-1];
|
||||||
std::cerr << cyan << leftpad(line, maxlen) << " | " << reset << code << "\n";
|
std::cerr << cyan << leftpad(line, maxlen) << " | " << reset << code << "\n";
|
||||||
// output underline
|
// output underline
|
||||||
std::cerr << cyan << iden << " | " << reset;
|
std::cerr << cyan << iden << " | " << reset;
|
||||||
|
|
|
@ -330,6 +330,7 @@ const error& lexer::scan(const std::string& file) {
|
||||||
line = 1;
|
line = 1;
|
||||||
column = 0;
|
column = 0;
|
||||||
ptr = 0;
|
ptr = 0;
|
||||||
|
toks = {};
|
||||||
open(file);
|
open(file);
|
||||||
|
|
||||||
while(ptr<res.size()) {
|
while(ptr<res.size()) {
|
||||||
|
|
33
src/repl.cpp
33
src/repl.cpp
|
@ -21,7 +21,7 @@ void repl::update_temp_file() {
|
||||||
for(const auto& i : source) {
|
for(const auto& i : source) {
|
||||||
content += i + "\n";
|
content += i + "\n";
|
||||||
}
|
}
|
||||||
info::instance()->repl_file_source = content;
|
info::instance()->repl_file_source = content + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool repl::check_need_more_input() {
|
bool repl::check_need_more_input() {
|
||||||
|
@ -49,6 +49,7 @@ bool repl::check_need_more_input() {
|
||||||
if (in_curve<=0 && in_bracket<=0 && in_brace<=0) {
|
if (in_curve<=0 && in_bracket<=0 && in_brace<=0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto line = readline("... ");
|
auto line = readline("... ");
|
||||||
source.back() += "\n" + line;
|
source.back() += "\n" + line;
|
||||||
}
|
}
|
||||||
|
@ -56,19 +57,17 @@ bool repl::check_need_more_input() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void repl::help() {
|
void repl::help() {
|
||||||
std::cout << ".h, .help | show help\n";
|
std::cout << ".h, .help | show help\n";
|
||||||
std::cout << ".e, .exit | quit the REPL\n";
|
std::cout << ".e, .exit | quit the REPL\n";
|
||||||
std::cout << ".q, .quit | quit the REPL\n";
|
std::cout << ".q, .quit | quit the REPL\n";
|
||||||
std::cout << ".c, .clear | clear the screen\n";
|
std::cout << ".c, .clear | clear the screen\n";
|
||||||
|
std::cout << ".s, .source | show source code\n";
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool repl::run() {
|
bool repl::run() {
|
||||||
update_temp_file();
|
|
||||||
|
|
||||||
using clk = std::chrono::high_resolution_clock;
|
using clk = std::chrono::high_resolution_clock;
|
||||||
const auto den = clk::duration::period::den;
|
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);
|
||||||
|
@ -76,6 +75,8 @@ bool repl::run() {
|
||||||
auto nasal_opt = std::unique_ptr<optimizer>(new optimizer);
|
auto nasal_opt = std::unique_ptr<optimizer>(new optimizer);
|
||||||
auto nasal_codegen = std::unique_ptr<codegen>(new codegen);
|
auto nasal_codegen = std::unique_ptr<codegen>(new codegen);
|
||||||
|
|
||||||
|
auto start = clk::now();
|
||||||
|
update_temp_file();
|
||||||
if (nasal_lexer->scan("<nasal-repl>").geterr()) {
|
if (nasal_lexer->scan("<nasal-repl>").geterr()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -88,18 +89,18 @@ bool repl::run() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
nasal_opt->do_optimization(nasal_parser->tree());
|
// nasal_opt->do_optimization(nasal_parser->tree());
|
||||||
if (nasal_codegen->compile(*nasal_parser, *nasal_linker).geterr()) {
|
if (nasal_codegen->compile(*nasal_parser, *nasal_linker).geterr()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end = clk::now();
|
auto end = clk::now();
|
||||||
std::clog << "[compile time: " << (end-start).count()*1000.0/den << " ms]\n";
|
std::clog << "[compile time: " << (end-start).count()*1000.0/den << " ms]\n";
|
||||||
runtime->set_detail_report_info(false);
|
|
||||||
// TODO: gc init stage in this run may cause memory leak,
|
// TODO: gc init stage in this run may cause memory leak,
|
||||||
// because constant strings will be generated again.
|
// because constant strings will be generated again.
|
||||||
// but we could not delete old strings, they maybe still on stack.
|
// but we could not delete old strings, they maybe still on stack.
|
||||||
runtime->run(*nasal_codegen, *nasal_linker, {});
|
runtime.run(*nasal_codegen, *nasal_linker, {});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -117,7 +118,9 @@ void repl::execute() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line == ".e" || line == ".exit" || line == ".q" || line == ".quit") {
|
if (line == ".e" || line == ".exit") {
|
||||||
|
break;
|
||||||
|
} else if (line == ".q" || line == ".quit") {
|
||||||
break;
|
break;
|
||||||
} else if (line == ".h" || line == ".help") {
|
} else if (line == ".h" || line == ".help") {
|
||||||
help();
|
help();
|
||||||
|
@ -125,6 +128,10 @@ void repl::execute() {
|
||||||
} else if (line == ".c" || line == ".clear") {
|
} else if (line == ".c" || line == ".clear") {
|
||||||
std::cout << "\033c";
|
std::cout << "\033c";
|
||||||
continue;
|
continue;
|
||||||
|
} else if (line == ".s" || line == ".source") {
|
||||||
|
update_temp_file();
|
||||||
|
std::cout << info::instance()->repl_file_source << "\n";
|
||||||
|
continue;
|
||||||
} else if (line[0] == "."[0]) {
|
} else if (line[0] == "."[0]) {
|
||||||
std::cout << "no such command \"" << line;
|
std::cout << "no such command \"" << line;
|
||||||
std::cout << "\", input \".help\" for help\n";
|
std::cout << "\", input \".help\" for help\n";
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct info {
|
||||||
class repl {
|
class repl {
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> source;
|
std::vector<std::string> source;
|
||||||
std::unique_ptr<vm> runtime;
|
vm runtime;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string readline(std::string);
|
std::string readline(std::string);
|
||||||
|
@ -37,8 +37,11 @@ private:
|
||||||
bool run();
|
bool run();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
repl(): runtime(std::unique_ptr<vm>(new vm)) {
|
repl() {
|
||||||
runtime->set_repl_mode_flag(true);
|
// set repl mode
|
||||||
|
runtime.set_repl_mode_flag(true);
|
||||||
|
// no detail report info
|
||||||
|
runtime.set_detail_report_info(false);
|
||||||
}
|
}
|
||||||
void execute();
|
void execute();
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
# experimental repl
|
|
||||||
# 2023/8/19 by ValKmjolnir
|
|
||||||
|
|
||||||
var help = func() {
|
|
||||||
println(" .help | show help");
|
|
||||||
println(" .exit | quit the REPL");
|
|
||||||
println(" .quit | quit the REPL");
|
|
||||||
println(" .clear | clear the screen");
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
var content = [];
|
|
||||||
var log_cache = "";
|
|
||||||
|
|
||||||
println("Nasal: This is experimental REPL");
|
|
||||||
println("Tips : \";\" is automatically added at the end of the input line.");
|
|
||||||
help();
|
|
||||||
|
|
||||||
var count_bracket = func(line) {
|
|
||||||
var len = size(line);
|
|
||||||
var count = 0;
|
|
||||||
for(var i = 0; i < len; i += 1) {
|
|
||||||
if (line[i] == "{"[0]) {
|
|
||||||
count += 1;
|
|
||||||
} elsif (line[i] == "}"[0]) {
|
|
||||||
count -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
var line = readline(">>> ");
|
|
||||||
if (!size(line)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (line == ".exit" or line == ".quit") {
|
|
||||||
break;
|
|
||||||
} elsif (line == ".help") {
|
|
||||||
println();
|
|
||||||
help();
|
|
||||||
continue;
|
|
||||||
} elsif (line == ".clear") {
|
|
||||||
print("\ec");
|
|
||||||
continue;
|
|
||||||
} elsif (line[0] == "."[0]) {
|
|
||||||
println("no such command \"", line, "\", input \".help\" for help\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var in_bracket_level = count_bracket(line);
|
|
||||||
while(in_bracket_level > 0) {
|
|
||||||
var temp_line = readline("... ");
|
|
||||||
in_bracket_level += count_bracket(temp_line);
|
|
||||||
line ~= temp_line ~ "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
append(content, line);
|
|
||||||
|
|
||||||
var source = "";
|
|
||||||
foreach(var i; content) {
|
|
||||||
source ~= i ~ ";\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
io.fout(".temp.nas", source);
|
|
||||||
var result = system("nasal .temp.nas > .temp.log");
|
|
||||||
if (result != 0) {
|
|
||||||
pop(content);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var log = io.readfile(".temp.log");
|
|
||||||
if (size(log) and size(log) != size(log_cache)) {
|
|
||||||
println(substr(log, size(log_cache), size(log)), "\n");
|
|
||||||
log_cache = log;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue