From 3b1659e7eecdb9253e12ed7271bbee34cbbe2ab1 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 3 Dec 2023 21:14:14 +0800 Subject: [PATCH] :sparkles: add namespace fs --- src/io_lib.cpp | 6 +----- src/main.cpp | 14 +++++++++++++- src/nasal.h | 24 +++++++++++++++++++++++- src/nasal_import.cpp | 16 ++++++---------- src/nasal_import.h | 2 +- src/nasal_lexer.cpp | 6 +----- src/nasal_lexer.h | 1 - src/nasal_misc.cpp | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/io_lib.cpp b/src/io_lib.cpp index bbe57d8..28cb508 100644 --- a/src/io_lib.cpp +++ b/src/io_lib.cpp @@ -1,9 +1,5 @@ #include "io_lib.h" -#ifdef _MSC_VER -#define F_OK 0 // fuck msc -#endif - #include namespace nasal { @@ -47,7 +43,7 @@ var builtin_exists(context* ctx, gc* ngc) { if (!filename.is_str()) { return zero; } - return access(filename.str().c_str(), F_OK)!=-1? one:zero; + return fs::exists(filename.str())? one:zero; } var builtin_open(context* ctx, gc* ngc) { diff --git a/src/main.cpp b/src/main.cpp index 1867f67..d9e7588 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,7 @@ const u32 VM_DEBUG = 1<<6; const u32 VM_SYMINFO = 1<<7; const u32 VM_PROFILE = 1<<8; const u32 VM_PROF_ALL = 1<<9; +const u32 VM_REF_FILE = 1<<10; std::ostream& help(std::ostream& out) { out @@ -53,6 +54,7 @@ std::ostream& help(std::ostream& out) { << " -e, --exec | execute directly.\n" << " -t, --time | show execute time.\n" << " -d, --detail | get detail info.\n" + << " -f, --ref-file | get referenced files.\n" << " -dbg, --debug | debug mode.\n" << " --prof | show profiling result, available in debug mode.\n" << " --prof-all | show profiling result of all files," @@ -131,6 +133,14 @@ void execute( // linker gets parser's ast and load import files to this ast ld.link(parse, file, cmd&VM_DETAIL).chkerr(); + if (cmd&VM_REF_FILE) { + if (ld.get_file_list().size()) { + std::cout << "referenced file(s):\n"; + } + for(const auto& file: ld.get_file_list()) { + std::cout << " " << file << "\n"; + } + } // optimizer does simple optimization on ast auto opt = std::unique_ptr(new nasal::optimizer); @@ -211,7 +221,9 @@ i32 main(i32 argc, const char* argv[]) { {"--debug", VM_DEBUG}, {"-dbg", VM_DEBUG}, {"--prof", VM_PROFILE}, - {"--prof-all", VM_PROF_ALL} + {"--prof-all", VM_PROF_ALL}, + {"-f", VM_REF_FILE}, + {"--ref-file", VM_REF_FILE} }; u32 cmd = 0; std::string filename = ""; diff --git a/src/nasal.h b/src/nasal.h index ad651b5..ea69761 100644 --- a/src/nasal.h +++ b/src/nasal.h @@ -41,7 +41,6 @@ const u32 STACK_DEPTH = 4096; f64 hex2f(const char*); f64 oct2f(const char*); - // we have the same reason not using atof here // just as andy's interpreter does. // it is not platform independent, and may have strange output. @@ -55,4 +54,27 @@ i32 utf8_hdchk(const char); std::string chrhex(const char); std::string rawstr(const std::string&, const usize maxlen = 0); +namespace fs { + +class path { +private: + std::string file_system_path; + +public: + path(const path&) = default; + path(const std::string& file_path): file_system_path(file_path) {} + path& operator/(const path&); + const char* c_str() const { + return file_system_path.c_str(); + } + std::string str() const { + return file_system_path; + } +}; + +bool exists(const path&); +bool is_regular(const path&); + +} + } \ No newline at end of file diff --git a/src/nasal_import.cpp b/src/nasal_import.cpp index b0fe98f..0d06ea7 100644 --- a/src/nasal_import.cpp +++ b/src/nasal_import.cpp @@ -4,14 +4,10 @@ #include #include -#ifdef _MSC_VER -#define F_OK 0 // fuck msc -#endif - namespace nasal { linker::linker(): show_path_flag(false), library_loaded(false), this_file("") { - const auto seperator= is_windows()? ';':':'; + const auto seperator = is_windows()? ';':':'; const auto PATH = std::string(getenv("PATH")); usize last = 0, position = PATH.find(seperator, 0); while(position!=std::string::npos) { @@ -48,17 +44,17 @@ std::string linker::get_path(expr* node) { std::string linker::find_real_file_path( const std::string& filename, const span& location) { // first add file name itself into the file path - std::vector path_list = {filename}; + std::vector path_list = {filename}; // generate search path from environ path for(const auto& p : envpath) { - path_list.push_back(p + (is_windows()? "\\":"/") + filename); + path_list.push_back(fs::path(p)/filename); } // search file for(const auto& path : path_list) { - if (access(path.c_str(), F_OK)!=-1) { - return path; + if (fs::exists(path)) { + return path.str(); } } @@ -78,7 +74,7 @@ std::string linker::find_real_file_path( } auto path_list_info = std::string(""); for(const auto& path : path_list) { - path_list_info += " -> " + path + "\n"; + path_list_info += " -> " + path.str() + "\n"; } err.err("link", "in <" + location.file + ">: " + diff --git a/src/nasal_import.h b/src/nasal_import.h index 6dd1e2a..692c922 100644 --- a/src/nasal_import.h +++ b/src/nasal_import.h @@ -29,7 +29,7 @@ private: error err; std::vector imported_files; std::vector module_load_stack; - std::vector envpath; + std::vector envpath; private: bool import_check(expr*); diff --git a/src/nasal_lexer.cpp b/src/nasal_lexer.cpp index e6a12f7..55b2490 100644 --- a/src/nasal_lexer.cpp +++ b/src/nasal_lexer.cpp @@ -76,11 +76,7 @@ void lexer::open(const std::string& file) { } // check file exsits and it is a regular file -#ifdef _MSC_VER - #define S_ISREG(m) (((m)&0xF000)==0x8000) -#endif - struct stat buffer; - if (stat(file.c_str(), &buffer)==0 && !S_ISREG(buffer.st_mode)) { + if (!fs::is_regular(file)) { err.err("lexer", "<"+file+"> is not a regular file"); err.chkerr(); } diff --git a/src/nasal_lexer.h b/src/nasal_lexer.h index 2b5bd38..4c57f8e 100644 --- a/src/nasal_lexer.h +++ b/src/nasal_lexer.h @@ -10,7 +10,6 @@ #include #include #include -#include #include "nasal.h" #include "nasal_err.h" diff --git a/src/nasal_misc.cpp b/src/nasal_misc.cpp index 183ec9e..5797ab2 100644 --- a/src/nasal_misc.cpp +++ b/src/nasal_misc.cpp @@ -1,5 +1,12 @@ #include "nasal.h" +#ifndef _MSC_VER +#include +#else +#include +#endif +#include + namespace nasal { bool is_windows() { @@ -234,4 +241,32 @@ std::string rawstr(const std::string& str, const usize maxlen) { return ret; } +namespace fs { + +path& path::operator/(const path& another) { + this->file_system_path += is_windows()? "\\":"/"; + this->file_system_path += another.file_system_path; + return *this; +} + +bool exists(const path& file_path) { +#ifdef _MSC_VER + #define F_OK 0 // fuck msc +#endif + return access(file_path.c_str(), F_OK)==0; +} + +bool is_regular(const path& file_path) { +#ifdef _MSC_VER + #define S_ISREG(m) (((m)&0xF000)==0x8000) +#endif + struct stat buffer; + if (stat(file_path.c_str(), &buffer)!=0) { + return false; + } + return S_ISREG(buffer.st_mode); +} + +} + }