diff --git a/src/nasal_err.cpp b/src/nasal_err.cpp index 290ce1d..3ff12c0 100644 --- a/src/nasal_err.cpp +++ b/src/nasal_err.cpp @@ -137,14 +137,15 @@ void error::err( const std::string iden = identation(maxlen); for(u32 line = loc.begin_line; line<=loc.end_line; ++line) { + // skip line 0 if (!line) { continue; } if (loc.begin_line=res.size()) { + continue; + } + + const auto& code = res[line-1]; std::cerr << cyan << leftpad(line, maxlen) << " | " << reset << code << "\n"; // output underline std::cerr << cyan << iden << " | " << reset; diff --git a/src/nasal_lexer.cpp b/src/nasal_lexer.cpp index 9b63ab8..7228c6f 100644 --- a/src/nasal_lexer.cpp +++ b/src/nasal_lexer.cpp @@ -330,6 +330,7 @@ const error& lexer::scan(const std::string& file) { line = 1; column = 0; ptr = 0; + toks = {}; open(file); while(ptrrepl_file_source = content; + info::instance()->repl_file_source = content + " "; } 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) { break; } + auto line = readline("... "); source.back() += "\n" + line; } @@ -56,19 +57,17 @@ bool repl::check_need_more_input() { } void repl::help() { - std::cout << ".h, .help | show help\n"; - std::cout << ".e, .exit | quit the REPL\n"; - std::cout << ".q, .quit | quit the REPL\n"; - std::cout << ".c, .clear | clear the screen\n"; + std::cout << ".h, .help | show help\n"; + std::cout << ".e, .exit | quit the REPL\n"; + std::cout << ".q, .quit | quit the REPL\n"; + std::cout << ".c, .clear | clear the screen\n"; + std::cout << ".s, .source | show source code\n"; std::cout << "\n"; } bool repl::run() { - update_temp_file(); - using clk = std::chrono::high_resolution_clock; - const auto den = clk::duration::period::den; - auto start = clk::now(); + const auto den = clk::duration::period::den; auto nasal_lexer = std::unique_ptr(new lexer); auto nasal_parser = std::unique_ptr(new parse); @@ -76,6 +75,8 @@ bool repl::run() { auto nasal_opt = std::unique_ptr(new optimizer); auto nasal_codegen = std::unique_ptr(new codegen); + auto start = clk::now(); + update_temp_file(); if (nasal_lexer->scan("").geterr()) { return false; } @@ -88,18 +89,18 @@ bool repl::run() { 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()) { return false; } auto end = clk::now(); 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, // because constant strings will be generated again. // 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; } @@ -117,7 +118,9 @@ void repl::execute() { continue; } - if (line == ".e" || line == ".exit" || line == ".q" || line == ".quit") { + if (line == ".e" || line == ".exit") { + break; + } else if (line == ".q" || line == ".quit") { break; } else if (line == ".h" || line == ".help") { help(); @@ -125,6 +128,10 @@ void repl::execute() { } else if (line == ".c" || line == ".clear") { std::cout << "\033c"; continue; + } else if (line == ".s" || line == ".source") { + update_temp_file(); + std::cout << info::instance()->repl_file_source << "\n"; + continue; } else if (line[0] == "."[0]) { std::cout << "no such command \"" << line; std::cout << "\", input \".help\" for help\n"; diff --git a/src/repl.h b/src/repl.h index 3aa5b81..ea677c5 100644 --- a/src/repl.h +++ b/src/repl.h @@ -27,7 +27,7 @@ struct info { class repl { private: std::vector source; - std::unique_ptr runtime; + vm runtime; private: std::string readline(std::string); @@ -37,8 +37,11 @@ private: bool run(); public: - repl(): runtime(std::unique_ptr(new vm)) { - runtime->set_repl_mode_flag(true); + repl() { + // set repl mode + runtime.set_repl_mode_flag(true); + // no detail report info + runtime.set_detail_report_info(false); } void execute(); }; diff --git a/tools/repl.nas b/tools/repl.nas deleted file mode 100644 index 74a3cc2..0000000 --- a/tools/repl.nas +++ /dev/null @@ -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; - } -} \ No newline at end of file