From 05605c357091517f918e0b0efe737cc92a4d6971 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 2 Jun 2024 16:53:03 +0800 Subject: [PATCH] :memo: move functions from nasal_misc => util --- makefile | 9 ++- src/main.cpp | 5 +- src/nasal.h | 14 ----- src/nasal_import.cpp | 7 ++- src/nasal_misc.cpp | 123 +--------------------------------------- src/natives/builtin.cpp | 6 +- src/util/fs.cpp | 7 ++- src/util/util.cpp | 120 ++++++++++++++++++++++++++++++++++++++- src/util/util.h | 18 +++++- 9 files changed, 161 insertions(+), 148 deletions(-) diff --git a/makefile b/makefile index 305a89e..ffbf9ad 100644 --- a/makefile +++ b/makefile @@ -92,7 +92,7 @@ build: build/main.o: $(NASAL_HEADER) src/main.cpp | build $(CXX) $(CXXFLAGS) src/main.cpp -o build/main.o -build/nasal_misc.o: src/nasal.h src/nasal_misc.cpp | build +build/nasal_misc.o: src/nasal.h src/util/util.h src/nasal_misc.cpp | build $(CXX) $(CXXFLAGS) src/nasal_misc.cpp -o build/nasal_misc.o build/cli.o: src/cli/cli.h src/cli/cli.cpp | build @@ -101,7 +101,7 @@ build/cli.o: src/cli/cli.h src/cli/cli.cpp | build build/util.o: src/util/util.h src/util/util.cpp | build $(CXX) $(CXXFLAGS) src/util/util.cpp -o build/util.o -build/fs.o: src/nasal.h src/util/fs.h src/util/fs.cpp | build +build/fs.o: src/nasal.h src/util/util.h src/util/fs.h src/util/fs.cpp | build $(CXX) $(CXXFLAGS) src/util/fs.cpp -o build/fs.o build/repl.o: $(NASAL_HEADER) src/repl/repl.h src/repl/repl.cpp | build @@ -121,6 +121,7 @@ build/nasal_import.o: \ src/nasal_ast.h\ src/nasal_lexer.h\ src/nasal_parse.h\ + src/util/util.h\ src/util/fs.h\ src/nasal_import.h src/nasal_import.cpp | build $(CXX) $(CXXFLAGS) src/nasal_import.cpp -o build/nasal_import.o @@ -143,7 +144,9 @@ build/builtin.o: \ src/nasal.h\ src/nasal_type.h\ src/nasal_gc.h\ - src/natives/builtin.h src/natives/builtin.cpp | build + src/util/util.h\ + src/natives/builtin.h\ + src/natives/builtin.cpp | build $(CXX) $(CXXFLAGS) src/natives/builtin.cpp -o build/builtin.o build/coroutine.o: \ diff --git a/src/main.cpp b/src/main.cpp index f1eaca2..f72ae4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include "nasal_vm.h" #include "nasal_dbg.h" +#include "util/util.h" #include "repl/repl.h" #include "cli/cli.h" @@ -66,7 +67,7 @@ std::ostream& logo(std::ostream& out) { << " \\_\\ \\/ \\__,_|___/\\__,_|_|\n" << "\n" << "ver : " << __nasver__ - << " " << nasal::get_platform() << " " << nasal::get_arch() + << " " << nasal::util::get_platform() << " " << nasal::util::get_arch() << " (" << __DATE__ << " " << __TIME__ << ")\n" << "std : c++ " << __cplusplus << "\n" << "core : " << std::thread::hardware_concurrency() << " core(s)\n" @@ -95,7 +96,7 @@ std::ostream& version(std::ostream& out) { } out << "nasal version " << __nasver__; - out << " " << nasal::get_platform() << " " << nasal::get_arch(); + out << " " << nasal::util::get_platform() << " " << nasal::util::get_arch(); out << " (" << __DATE__ << " " << __TIME__ << ")\n"; return out; } diff --git a/src/nasal.h b/src/nasal.h index 3820844..7d0c256 100644 --- a/src/nasal.h +++ b/src/nasal.h @@ -23,20 +23,6 @@ using f64 = double; namespace nasal { -bool is_windows(); -bool is_linux(); -bool is_macos(); -bool is_x86(); -bool is_amd64(); -bool is_x86_64(); -bool is_arm(); -bool is_aarch64(); -bool is_ia64(); -bool is_powerpc(); -bool is_superh(); -const char* get_platform(); -const char* get_arch(); - // virtual machine stack depth, both global depth and value stack depth const u32 VM_STACK_DEPTH = UINT16_MAX; diff --git a/src/nasal_import.cpp b/src/nasal_import.cpp index 294bd91..4093516 100644 --- a/src/nasal_import.cpp +++ b/src/nasal_import.cpp @@ -1,5 +1,6 @@ #include "nasal_import.h" #include "symbol_finder.h" +#include "util/util.h" #include "util/fs.h" #include @@ -8,7 +9,7 @@ namespace nasal { linker::linker(): show_path_flag(false), this_file("") { - const auto seperator = is_windows()? ';':':'; + const auto seperator = util::is_windows()? ';':':'; const auto PATH = std::string(getenv("PATH")); usize last = 0, position = PATH.find(seperator, 0); while(position!=std::string::npos) { @@ -31,7 +32,7 @@ std::string linker::get_path(expr* node) { for(auto i : path) { file_relative_path += i->get_name(); if (i!=path.back()) { - file_relative_path += (is_windows()? "\\":"/"); + file_relative_path += (util::is_windows()? "\\":"/"); } } return file_relative_path + ".nas"; @@ -61,7 +62,7 @@ std::string linker::find_real_file_path(const std::string& filename, // we will find lib.nas in nasal std directory if (filename=="lib.nas") { - return is_windows()? + return util::is_windows()? find_real_file_path("std\\lib.nas", location): find_real_file_path("std/lib.nas", location); } diff --git a/src/nasal_misc.cpp b/src/nasal_misc.cpp index 24428b0..4702e01 100644 --- a/src/nasal_misc.cpp +++ b/src/nasal_misc.cpp @@ -1,127 +1,8 @@ #include "nasal.h" - -#ifdef _MSC_VER -#pragma warning (disable:4996) -#endif +#include "util/util.h" namespace nasal { -bool is_windows() { -#if defined(_WIN32) || defined(_WIN64) - return true; -#else - return false; -#endif -} - -bool is_linux() { -#if defined __linux__ - return true; -#else - return false; -#endif -} - -bool is_macos() { -#if defined __APPLE__ - return true; -#else - return false; -#endif -} - -bool is_x86() { -#if defined(__i386__) || defined(_M_IX86) - return true; -#else - return false; -#endif -} - -bool is_amd64() { -#if defined(__amd64__) || defined(_M_X64) - return true; -#else - return false; -#endif -} - -bool is_x86_64() { - return is_amd64(); -} - -bool is_arm() { -#if defined(__arm__) || defined(_M_ARM) - return true; -#else - return false; -#endif -} - -bool is_aarch64() { -#if defined(__aarch64__) || defined(_M_ARM64) - return true; -#else - return false; -#endif -} - -bool is_ia64() { -#if defined(__ia64__) - return true; -#else - return false; -#endif -} - -bool is_powerpc() { -#if defined(__powerpc__) - return true; -#else - return false; -#endif -} - -bool is_superh() { -#if defined(__sh__) - return true; -#else - return false; -#endif -} - -const char* get_platform() { - if (is_windows()) { - return "windows"; - } else if (is_linux()) { - return "linux"; - } else if (is_macos()) { - return "macOS"; - } - return "unknown"; -} - -const char* get_arch() { - if (is_x86()) { - return "x86"; - } else if (is_x86_64()) { - return "x86-64"; - } else if (is_amd64()) { - return "amd64"; - } else if (is_arm()) { - return "arm"; - } else if (is_aarch64()) { - return "aarch64"; - } else if (is_ia64()) { - return "ia64"; - } else if (is_powerpc()) { - return "powerpc"; - } else if (is_superh()) { - return "superh"; - } - return "unknown"; -} - f64 hex_to_f64(const char* str) { f64 ret = 0; for(; *str; ++str) { @@ -244,7 +125,7 @@ std::string rawstr(const std::string& str, const usize maxlen) { std::string ret(""); for(auto i : str) { // windows doesn't output unicode normally, so we output the hex - if (is_windows() && i<=0) { + if (util::is_windows() && i<=0) { ret += "\\x" + char_to_hex(i); continue; } diff --git a/src/natives/builtin.cpp b/src/natives/builtin.cpp index 39da951..79c0f76 100644 --- a/src/natives/builtin.cpp +++ b/src/natives/builtin.cpp @@ -1,4 +1,6 @@ #include "natives/builtin.h" +#include "util/util.h" + #include #ifdef _WIN32 @@ -463,11 +465,11 @@ var builtin_sleep(context* ctx, gc* ngc) { } var builtin_platform(context* ctx, gc* ngc) { - return ngc->newstr(get_platform()); + return ngc->newstr(util::get_platform()); } var builtin_arch(context* ctx, gc* ngc) { - return ngc->newstr(get_arch()); + return ngc->newstr(util::get_arch()); } // md5 related functions diff --git a/src/util/fs.cpp b/src/util/fs.cpp index 941ec9f..1ae0fae 100644 --- a/src/util/fs.cpp +++ b/src/util/fs.cpp @@ -4,15 +4,20 @@ #include #endif +#ifdef _MSC_VER +#pragma warning (disable:4996) +#endif + #include +#include "util/util.h" #include "util/fs.h" #include "nasal.h" namespace nasal::fs { path& path::operator/(const path& another) { - this->file_system_path += is_windows()? "\\":"/"; + this->file_system_path += util::is_windows()? "\\":"/"; this->file_system_path += another.file_system_path; return *this; } diff --git a/src/util/util.cpp b/src/util/util.cpp index 8e1ec44..cf027fc 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -1,3 +1,121 @@ #include "util/util.h" -namespace nasal::util {} \ No newline at end of file +namespace nasal::util { + +bool is_windows() { +#if defined(_WIN32) || defined(_WIN64) + return true; +#else + return false; +#endif +} + +bool is_linux() { +#if defined __linux__ + return true; +#else + return false; +#endif +} + +bool is_macos() { +#if defined __APPLE__ + return true; +#else + return false; +#endif +} + +bool is_x86() { +#if defined(__i386__) || defined(_M_IX86) + return true; +#else + return false; +#endif +} + +bool is_amd64() { +#if defined(__amd64__) || defined(_M_X64) + return true; +#else + return false; +#endif +} + +bool is_x86_64() { + return is_amd64(); +} + +bool is_arm() { +#if defined(__arm__) || defined(_M_ARM) + return true; +#else + return false; +#endif +} + +bool is_aarch64() { +#if defined(__aarch64__) || defined(_M_ARM64) + return true; +#else + return false; +#endif +} + +bool is_ia64() { +#if defined(__ia64__) + return true; +#else + return false; +#endif +} + +bool is_powerpc() { +#if defined(__powerpc__) + return true; +#else + return false; +#endif +} + +bool is_superh() { +#if defined(__sh__) + return true; +#else + return false; +#endif +} + +const char* get_platform() { + if (is_windows()) { + return "windows"; + } else if (is_linux()) { + return "linux"; + } else if (is_macos()) { + return "macOS"; + } + return "unknown"; +} + +const char* get_arch() { + if (is_x86()) { + return "x86"; + } else if (is_x86_64()) { + return "x86-64"; + } else if (is_amd64()) { + return "amd64"; + } else if (is_arm()) { + return "arm"; + } else if (is_aarch64()) { + return "aarch64"; + } else if (is_ia64()) { + return "ia64"; + } else if (is_powerpc()) { + return "powerpc"; + } else if (is_superh()) { + return "superh"; + } + return "unknown"; +} + +} \ No newline at end of file diff --git a/src/util/util.h b/src/util/util.h index 4724961..551b264 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -1,3 +1,19 @@ #pragma -namespace nasal::util {} \ No newline at end of file +namespace nasal::util { + +bool is_windows(); +bool is_linux(); +bool is_macos(); +bool is_x86(); +bool is_amd64(); +bool is_x86_64(); +bool is_arm(); +bool is_aarch64(); +bool is_ia64(); +bool is_powerpc(); +bool is_superh(); +const char* get_platform(); +const char* get_arch(); + +} \ No newline at end of file