🐛 fix error report bug in import.cpp

This commit is contained in:
ValKmjolnir 2023-10-24 00:16:48 +08:00
parent f7f4a38b47
commit 9f7484596a
5 changed files with 21 additions and 13 deletions

View File

@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <sstream>
#include <cmath> #include <cmath>
// abbreviation of some useful basic type // abbreviation of some useful basic type

View File

@ -1303,7 +1303,7 @@ void codegen::print(std::ostream& out) {
out << std::hex << "<0x" << func_begin_stack.top() << std::dec << ">;\n"; out << std::hex << "<0x" << func_begin_stack.top() << std::dec << ">;\n";
// avoid two empty lines // avoid two empty lines
if (c.op!=op_newf) { if (c.op!=op_newf) {
out<<"\n"; out << "\n";
} }
func_begin_stack.pop(); func_begin_stack.pop();
func_end_stack.pop(); func_end_stack.pop();
@ -1322,7 +1322,7 @@ void codegen::print(std::ostream& out) {
} }
// output bytecode // output bytecode
out << " " << codestream(c,i) << "\n"; out << " " << codestream(c, i) << "\n";
} }
} }

View File

@ -198,8 +198,10 @@ code_block* linker::import_regular_file(call_expr* node) {
return new code_block({0, 0, 0, 0, filename}); return new code_block({0, 0, 0, 0, filename});
} }
if (check_self_import(filename)) { if (check_self_import(filename)) {
err.err("link", "self-referenced module <" + filename + ">:\n" + err.err("link",
" reference path: " + generate_self_import_path(filename)); "self-referenced module <" + filename + ">:\n" +
" reference path: " + generate_self_import_path(filename)
);
return new code_block({0, 0, 0, 0, filename}); return new code_block({0, 0, 0, 0, filename});
} }
exist(filename); exist(filename);
@ -208,16 +210,19 @@ code_block* linker::import_regular_file(call_expr* node) {
// start importing... // start importing...
if (lex.scan(filename).geterr()) { if (lex.scan(filename).geterr()) {
err.err("link", "error occurred when analysing <" + filename + ">"); err.err("link", "error occurred when analysing <" + filename + ">");
return new code_block({0, 0, 0, 0, filename});
} }
if (par.compile(lex).geterr()) { if (par.compile(lex).geterr()) {
err.err("link", "error occurred when analysing <" + filename + ">"); err.err("link", "error occurred when analysing <" + filename + ">");
return new code_block({0, 0, 0, 0, filename});
} }
auto tmp = par.swap(nullptr);
// check if tmp has 'import' auto parse_result = par.swap(nullptr);
auto res = load(tmp, find(filename));
// check if parse result has 'import'
auto result = load(parse_result, find(filename));
module_load_stack.pop_back(); module_load_stack.pop_back();
return res; return result;
} }
code_block* linker::import_nasal_lib() { code_block* linker::import_nasal_lib() {
@ -239,16 +244,18 @@ code_block* linker::import_nasal_lib() {
err.err("link", err.err("link",
"error occurred when analysing library <" + filename + ">" "error occurred when analysing library <" + filename + ">"
); );
return new code_block({0, 0, 0, 0, filename});
} }
if (par.compile(lex).geterr()) { if (par.compile(lex).geterr()) {
err.err("link", err.err("link",
"error occurred when analysing library <" + filename + ">" "error occurred when analysing library <" + filename + ">"
); );
return new code_block({0, 0, 0, 0, filename});
} }
auto tmp = par.swap(nullptr);
// check if tmp has 'import' auto parse_result = par.swap(nullptr);
return load(tmp, find(filename)); // check if library has 'import' (in fact it should not)
return load(parse_result, find(filename));
} }
std::string linker::generate_module_name(const std::string& file_path) { std::string linker::generate_module_name(const std::string& file_path) {

View File

@ -7,7 +7,7 @@
namespace nasal { namespace nasal {
class optimizer:public ast_visitor { class optimizer: public ast_visitor {
private: private:
void const_string(binary_operator*, string_literal*, string_literal*); void const_string(binary_operator*, string_literal*, string_literal*);
void const_number(binary_operator*, number_literal*, number_literal*); void const_number(binary_operator*, number_literal*, number_literal*);

View File

@ -9,7 +9,7 @@
namespace nasal { namespace nasal {
class symbol_finder:public ast_visitor { class symbol_finder: public ast_visitor {
public: public:
struct symbol_info { struct symbol_info {
std::string name; std::string name;