📝 move functions from nasal_misc => util

This commit is contained in:
ValKmjolnir 2024-06-02 16:53:03 +08:00
parent c840d70a9c
commit 05605c3570
9 changed files with 161 additions and 148 deletions

View File

@ -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: \

View File

@ -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;
}

View File

@ -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;

View File

@ -1,5 +1,6 @@
#include "nasal_import.h"
#include "symbol_finder.h"
#include "util/util.h"
#include "util/fs.h"
#include <memory>
@ -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);
}

View File

@ -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;
}

View File

@ -1,4 +1,6 @@
#include "natives/builtin.h"
#include "util/util.h"
#include <chrono>
#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

View File

@ -4,15 +4,20 @@
#include <io.h>
#endif
#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif
#include <sys/stat.h>
#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;
}

View File

@ -1,3 +1,121 @@
#include "util/util.h"
namespace nasal::util {}
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";
}
}

View File

@ -1,3 +1,19 @@
#pragma
namespace nasal::util {}
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();
}