Merge pull request #58 from ValKmjolnir/develop

🎨 format & adjust cmake for windows build
This commit is contained in:
ValK 2024-12-27 00:57:39 +08:00 committed by GitHub
commit 4062c40bb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 226 additions and 205 deletions

3
.gitignore vendored
View File

@ -2,7 +2,8 @@
build/ build/
out/ out/
dist/ dist/
cmake-build-*/ cmake-build-*
cmake-windows-*
# IDE and editor files # IDE and editor files
.vscode/ .vscode/

View File

@ -103,9 +103,13 @@ target_link_libraries(mat module-used-object)
add_library(nasock SHARED ${CMAKE_SOURCE_DIR}/module/nasocket.cpp) add_library(nasock SHARED ${CMAKE_SOURCE_DIR}/module/nasocket.cpp)
target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR}/src) target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR}/src)
if (WIN32)
target_link_libraries(nasock ws2_32)
endif()
target_link_libraries(nasock module-used-object) target_link_libraries(nasock module-used-object)
# Add web library # Add web library, not for MSVC now
if (NOT MSVC)
add_library(nasal-web SHARED add_library(nasal-web SHARED
src/nasal_web.cpp src/nasal_web.cpp
${NASAL_OBJECT_SOURCE_FILE} ${NASAL_OBJECT_SOURCE_FILE}
@ -116,3 +120,4 @@ set_target_properties(nasal-web PROPERTIES
CXX_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON VISIBILITY_INLINES_HIDDEN ON
) )
endif()

View File

@ -88,7 +88,7 @@ Make sure thread model is `posix thread model`, otherwise no thread library exis
### __Windows (Visual Studio)__ ### __Windows (Visual Studio)__
There is a [__CMakelists.txt__](./CMakeLists.txt) to create project. There is a [__CMakelists.txt__](./CMakeLists.txt) to create project. Recently we find how to build it with command line tools: [__Build Nasal-Interpreter on Windows]__(./doc/windows-build.md).
### __Linux / macOS / Unix__ ### __Linux / macOS / Unix__

View File

@ -81,6 +81,7 @@ Windows 平台的预览版解释器现在还没配置相关流水线,
### __Windows 平台 (Vistual Studio)__ ### __Windows 平台 (Vistual Studio)__
项目提供了 [__CMakeLists.txt__](../CMakeLists.txt) 用于在`Visual Studio`中创建项目。 项目提供了 [__CMakeLists.txt__](../CMakeLists.txt) 用于在`Visual Studio`中创建项目。
最近我们总结了命令行编译的方法 [__如何在 Windows 平台编译 Nasal-Interpreter__](./windows-build.md)。
### __Linux / macOS / Unix 平台__ ### __Linux / macOS / Unix 平台__

27
doc/windows-build.md Normal file
View File

@ -0,0 +1,27 @@
# Build Nasal-Interpreter on Windows
## MSVC / Visual Studio
Need CMake and Visual Studio 2022. Remember to add MSBuild.exe to Path.
Valid on powershell:
```sh
mkdir cmake-windows-msvc
cd cmake-windows-msvc
cmake .. -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 17 2022"
MSbuild.exe nasal.sln /p:Configuration=Release /p:Platform=x64
```
## MingW-W64
Need CMake and MingW-W64. Remember to add MingW-W64 bin to Path.
Valid on powershell:
```sh
mkdir cmake-windows-mingw
cd cmake-windows-mingw
cmake .. -DCMAKE_BUILD_TYPE=Release -G "MinGW Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
mingw32-make.exe -j6
```

View File

@ -317,41 +317,4 @@ std::ostream& operator<<(std::ostream& out, var& ref) {
return out; return out;
} }
bool var::object_check(const std::string& name) const {
return is_ghost() && ghost().type_name == name && ghost().pointer;
}
var var::none() {
return {vm_type::vm_none, static_cast<u64>(0)};
}
var var::nil() {
return {vm_type::vm_nil, static_cast<u64>(0)};
}
var var::ret(u64 pc) {
return {vm_type::vm_ret, pc};
}
var var::cnt(i64 n) {
return {vm_type::vm_cnt, n};
}
var var::num(f64 n) {
return {vm_type::vm_num, n};
}
var var::gcobj(nas_val* p) {
return {p->type, p};
}
var var::addr(var* p) {
return {vm_type::vm_addr, p};
}
var nas_err(const std::string& error_function_name, const std::string& info) {
std::cerr << "[vm] " << error_function_name << ": " << info << "\n";
return var::none();
}
} }

View File

@ -104,13 +104,27 @@ public:
public: public:
// create new var object // create new var object
static var none(); static var none() {
static var nil(); return var(vm_type::vm_none, static_cast<u64>(0));
static var ret(u64); }
static var cnt(i64); static var nil() {
static var num(f64); return var(vm_type::vm_nil, static_cast<u64>(0));
static var gcobj(nas_val*); }
static var addr(var*); static var ret(u64 pc) {
return var(vm_type::vm_ret, pc);
}
static var cnt(i64 n) {
return var(vm_type::vm_cnt, n);
}
static var num(f64 n) {
return var(vm_type::vm_num, n);
}
static var gcobj(nas_val* p) {
return var(p->type, p);
}
static var addr(var* p) {
return var(vm_type::vm_addr, p);
}
public: public:
// get value // get value
@ -162,7 +176,8 @@ public:
// number and string can be translated to each other // number and string can be translated to each other
f64 to_num() const; f64 to_num() const;
std::string to_str(); std::string to_str();
bool object_check(const std::string&) const; inline bool object_check(const std::string&) const;
friend std::ostream& operator<<(std::ostream&, var&);
}; };
struct nas_vec { struct nas_vec {
@ -174,6 +189,7 @@ struct nas_vec {
auto size() const { return elems.size(); } auto size() const { return elems.size(); }
var get_value(const i32); var get_value(const i32);
var* get_memory(const i32); var* get_memory(const i32);
friend std::ostream& operator<<(std::ostream&, nas_vec&);
}; };
struct nas_hash { struct nas_hash {
@ -185,6 +201,7 @@ struct nas_hash {
auto size() const { return elems.size(); } auto size() const { return elems.size(); }
var get_value(const std::string&); var get_value(const std::string&);
var* get_memory(const std::string&); var* get_memory(const std::string&);
friend std::ostream& operator<<(std::ostream&, nas_hash&);
}; };
struct nas_func { struct nas_func {
@ -206,6 +223,7 @@ struct nas_func {
parameter_size(0), local_size(0), parameter_size(0), local_size(0),
dynamic_parameter_name("") {} dynamic_parameter_name("") {}
void clear(); void clear();
friend std::ostream& operator<<(std::ostream&, nas_func&);
}; };
struct nas_upval { struct nas_upval {
@ -250,6 +268,7 @@ public:
~nas_ghost() { clear(); } ~nas_ghost() { clear(); }
void set(const std::string&, destructor, marker, void*); void set(const std::string&, destructor, marker, void*);
void clear(); void clear();
friend std::ostream& operator<<(std::ostream&, const nas_ghost&);
public: public:
const auto& get_ghost_name() const { return type_name; } const auto& get_ghost_name() const { return type_name; }
@ -290,6 +309,7 @@ struct nas_co {
delete[] ctx.stack; delete[] ctx.stack;
} }
void clear(); void clear();
friend std::ostream& operator<<(std::ostream&, const nas_co&);
}; };
struct nas_map { struct nas_map {
@ -304,21 +324,21 @@ public:
var get_value(const std::string&); var get_value(const std::string&);
var* get_memory(const std::string&); var* get_memory(const std::string&);
friend std::ostream& operator<<(std::ostream&, nas_map&);
}; };
std::ostream& operator<<(std::ostream&, nas_vec&);
std::ostream& operator<<(std::ostream&, nas_hash&);
std::ostream& operator<<(std::ostream&, nas_func&);
std::ostream& operator<<(std::ostream&, nas_map&);
std::ostream& operator<<(std::ostream&, const nas_ghost&);
std::ostream& operator<<(std::ostream&, const nas_co&);
std::ostream& operator<<(std::ostream&, var&);
const var zero = var::num(0); const var zero = var::num(0);
const var one = var::num(1); const var one = var::num(1);
const var nil = var::nil(); const var nil = var::nil();
inline bool var::object_check(const std::string& name) const {
return is_ghost() && ghost().type_name == name && ghost().pointer;
}
// use to print error log and return error value // use to print error log and return error value
var nas_err(const std::string&, const std::string&); static var nas_err(const std::string& func, const std::string& info) {
std::cerr << "[vm] " << func << ": " << info << "\n";
return var::none();
}
} }

View File

@ -536,13 +536,13 @@ void vm::die(const std::string& str) {
if (!ngc.cort) { if (!ngc.cort) {
// in main context, exit directly // in main context, exit directly
std::exit(1); std::exit(1);
} else { }
// in coroutine, shut down the coroutine and return to main context // in coroutine, shut down the coroutine and return to main context
ctx.pc = 0; // mark coroutine 'dead' ctx.pc = 0; // mark coroutine 'dead'
ngc.context_reserve(); // switch context to main ngc.context_reserve(); // switch context to main
ctx.top[0] = nil; // generate return value 'nil' ctx.top[0] = nil; // generate return value 'nil'
} }
}
void vm::run(const codegen& gen, void vm::run(const codegen& gen,
const linker& linker, const linker& linker,

View File

@ -4,9 +4,7 @@ use std.io;
use std.unix; use std.unix;
var SEEK_SET = io.SEEK_SET; var SEEK_SET = io.SEEK_SET;
var SEEK_CUR = io.SEEK_CUR; var SEEK_CUR = io.SEEK_CUR;
var SEEK_END = io.SEEK_END; var SEEK_END = io.SEEK_END;
var new = func(filename, mode = "r") { var new = func(filename, mode = "r") {

View File

@ -37,7 +37,9 @@ var to_char = func(number) {
} }
var to_num = func(character) { var to_num = func(character) {
return __temp_contains(__char_to_num, character)? __char_to_num[character]:-1; return __temp_contains(__char_to_num, character)
? __char_to_num[character]
: -1;
} }
var __string_split_with_empty_substr = func(separator, str) { var __string_split_with_empty_substr = func(separator, str) {

View File

@ -48,14 +48,15 @@ func() {
func { func {
var co = coroutine.create(func() { var co = coroutine.create(func() {
var (a, b) = coroutine.yield(a + b); var (a, b) = coroutine.yield(a + b);
println("coroutine.yield get ",a," ",b); println("[0] coroutine.yield get ", a, " ", b);
(a, b) = coroutine.yield(a + b); (a, b) = coroutine.yield(a + b);
println("coroutine.yield get ",a," ",b); println("[0] coroutine.yield get ", a, " ", b);
return "end"; return "end";
}); });
for(var i = 0; i < 5; i += 1) for(var i = 0; i < 5; i += 1)
println("coroutine.resume get ",coroutine.resume(co,i,i+1)); println("[0] coroutine.resume get ", coroutine.resume(co, i, i + 1));
print("\n");
}(); }();
# test crash in coroutines # test crash in coroutines
@ -63,15 +64,16 @@ var co=coroutine.create(func {
var b = func() { b() } var b = func() { b() }
coroutine.yield(b); coroutine.yield(b);
b(); b();
coroutine.yield(0); coroutine.yield(0); # unreachable
}); });
println("coroutine yield: ",coroutine.resume(co)); println("[1] coroutine yield: ", coroutine.resume(co));
println("coroutine state:\e[32m ",coroutine.status(co),"\e[0m"); println("[1] coroutine state after yield:\e[32m ", coroutine.status(co), "\e[0m");
println("coroutine error: ",coroutine.resume(co)); println("[1] coroutine stackoverflow error: ", coroutine.resume(co));
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m"); println("[1] coroutine state after error:\e[91m ", coroutine.status(co), "\e[0m");
println("coroutine yield: ",coroutine.resume(co)); println("[1] coroutine yield after error: ", coroutine.resume(co));
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m"); println("[1] coroutine state after error:\e[91m ", coroutine.status(co), "\e[0m");
print("\n");
var co = coroutine.create(func { var co = coroutine.create(func {
var a = 1; var a = 1;
@ -82,13 +84,13 @@ var co = coroutine.create(func {
coroutine.yield(b()); coroutine.yield(b());
}); });
println("coroutine yield: ",coroutine.resume(co)); println("[2] coroutine yield: ", coroutine.resume(co));
println("coroutine state:\e[32m ",coroutine.status(co),"\e[0m"); println("[2] coroutine state:\e[32m ", coroutine.status(co), "\e[0m");
println("coroutine error: ",coroutine.resume(co)); println("[2] coroutine error: ", coroutine.resume(co));
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m"); println("[2] coroutine state:\e[91m ", coroutine.status(co), "\e[0m");
println("coroutine yield: ",coroutine.resume(co)); println("[2] coroutine yield: ", coroutine.resume(co));
println("coroutine state:\e[91m ",coroutine.status(co),"\e[0m"); println("[2] coroutine state:\e[91m ", coroutine.status(co), "\e[0m");
println("ok"); println("[2] ok\n");
# pressure test # pressure test
for(var t = 0; t < 10; t += 1) { for(var t = 0; t < 10; t += 1) {
@ -112,7 +114,8 @@ for(var t=0;t<10;t+=1) {
print(" ", bar.bar(rate), " ", print(" ", bar.bar(rate), " ",
padding.leftpad(str(int(rate*100)),3), "% | ", padding.leftpad(str(int(rate*100)),3), "% | ",
str(1e3 * int(counter / tm.elapsedMSec())), str(1e3 * int(counter / tm.elapsedMSec())),
" tasks/s \r"); " tasks/s \r"
);
} }
} }
@ -121,5 +124,6 @@ for(var t=0;t<10;t+=1) {
consumer(); consumer();
println(" ", bar.bar(1), " 100% | ", println(" ", bar.bar(1), " 100% | ",
str(int(1e3 * counter / tm.elapsedMSec())), str(int(1e3 * counter / tm.elapsedMSec())),
" tasks/s "); " tasks/s "
);
} }