diff --git a/.gitignore b/.gitignore index 76ad02e..4869c04 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,8 @@ *.vcxproj *.vcxproj.filters *.vcxproj.user +.vs +x64 # misc nasal diff --git a/README.md b/README.md index 18e565b..746be98 100644 --- a/README.md +++ b/README.md @@ -127,11 +127,11 @@ __PLEASE USE MINGW ON WINDOWS!__ Use g++ on __`Windows`__(`MinGW-w64`) platform. Download MinGW-w64 [__HERE__](https://www.mingw-w64.org/downloads/). (otherwise don't blame me for not reminding YOU 👿 ) -> g++ -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -static +> $(CXX) -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -static -Or use g++/clang++ on __`linux/macOS/Unix`__ platform. +Or use g++/clang++ on __`linux/macOS/Unix`__ platform (we suggest clang). -> [cpp compiler] -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl +> $(CXX) -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl Or using makefile,`mingw32-make` is __`Windows(MinGW-w64)`__ platform's `make`: @@ -143,6 +143,22 @@ on __`linux/macOS/Unix`__: > make nasal +You could choose which compiler you want to use by add this after the command: + +> CXX=clang++ +> +> CXX=g++ +> +> CXX=... + +If you think `-O3` isn't that safe and stable, you could choose: + +> make stable-release +> +> mingw32-make stable-release-mingw + +We are trying to make this project compilable on MSVC, let's wait and see. + ## __How to Use__ First we should learn how to write and run a program using this language, diff --git a/doc/README_zh.md b/doc/README_zh.md index d966a6d..8236842 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -110,11 +110,11 @@ __Windows用户请一定一定一定要使用MinGW编译!__ __`Windows`__(`MinGW-w64`)用户使用g++编译器并使用以下命令来进行编译. 没有编译环境的请在[__这里__](https://www.mingw-w64.org/downloads/)下载MinGW-w64。(编译不出来别怪我没说哦👿) -> g++ -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -static +> $(CXX) -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -static -__`linux/macOS/Unix`__ 用户可以使用g++或者clang++替代下面命令中中括号的部分来进行编译。 +__`linux/macOS/Unix`__ 用户可以使用g++或者clang++替代下面命令中中括号的部分来进行编译(我们建议您使用clang)。 -> [cpp compiler] -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl +> $(CXX) -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl 当然也可以使用makefile,`mingw32-make`是 __`Windows(MinGW-w64)`__ 平台的`make`: @@ -126,6 +126,22 @@ __`linux/macOS/Unix`__ 平台直接使用make即可: > make nasal +你也可以通过在命令后面添加如下的其中一行来指定你想要使用的编译器: + +> CXX=clang++ +> +> CXX=g++ +> +> CXX=... + +如果你觉得`-O3`编译的版本不是那么安全和稳定,你也可以选择生成稳定的版本: + +> make stable-release +> +> mingw32-make stable-release-mingw + +我们现在还在尝试将项目移植到MSVC下,敬请期待。 + ## __使用方法__ 首先我们要通过[__教程__](#教程)知道这个语言的语法以及如何使用这个解释器来运行nasal程序。 diff --git a/makefile b/makefile index 56a4aa3..270d3b9 100644 --- a/makefile +++ b/makefile @@ -16,9 +16,15 @@ SRC=\ nasal.h nasal:$(SRC) - clang++ -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall + $(CXX) -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall nasal.exe:$(SRC) - g++ -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static + $(CXX) -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static + +stable-release:$(SRC) + $(CXX) -std=c++11 -O2 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall +stable-release-mingw:$(SRC) + $(CXX) -std=c++11 -O2 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static + test:nasal @ ./nasal -op -e test/ascii-art.nas @ ./nasal -op -c test/auto_crash.nas diff --git a/module/makefile b/module/makefile index 86bba57..a2778d4 100644 --- a/module/makefile +++ b/module/makefile @@ -1,29 +1,29 @@ .PHONY=clean all mingw-all libfib.so: fib.cpp - clang++ -c -O3 fib.cpp -fPIC -o fib.o - clang++ -shared -o libfib.so fib.o + $(CXX) -c -O3 fib.cpp -fPIC -o fib.o + $(CXX) -shared -o libfib.so fib.o rm fib.o libfib.dll: fib.cpp - g++ -c -O3 fib.cpp -fPIC -o fib.o - g++ -shared -o libfib.dll fib.o + $(CXX) -c -O3 fib.cpp -fPIC -o fib.o + $(CXX) -shared -o libfib.dll fib.o del fib.o libkey.so: keyboard.cpp - clang++ -c -O3 keyboard.cpp -fPIC -o keyboard.o - clang++ -shared -o libkey.so keyboard.o + $(CXX) -c -O3 keyboard.cpp -fPIC -o keyboard.o + $(CXX) -shared -o libkey.so keyboard.o rm keyboard.o libkey.dll: keyboard.cpp - g++ -c -O3 keyboard.cpp -fPIC -o keyboard.o -static - g++ -shared -o libkey.dll keyboard.o -static + $(CXX) -c -O3 keyboard.cpp -fPIC -o keyboard.o -static + $(CXX) -shared -o libkey.dll keyboard.o -static del keyboard.o libnasock.so: nasocket.cpp - clang++ -c -O3 nasocket.cpp -fPIC -o nasocket.o - clang++ -shared -o libnasock.so nasocket.o + $(CXX) -c -O3 nasocket.cpp -fPIC -o nasocket.o + $(CXX) -shared -o libnasock.so nasocket.o rm nasocket.o libnasock.dll: nasocket.cpp - g++ -c -O3 nasocket.cpp -fPIC -o nasocket.o -lwsock32 -static - g++ -shared -o libnasock.dll nasocket.o -lwsock32 -static + $(CXX) -c -O3 nasocket.cpp -fPIC -o nasocket.o -lwsock32 -static + $(CXX) -shared -o libnasock.dll nasocket.o -lwsock32 -static del nasocket.o clean: @@ -31,4 +31,4 @@ clean: all: libfib.so libkey.so libnasock.so @ echo "build done" mingw-all: libfib.dll libkey.dll libnasock.dll - @ echo "build done" \ No newline at end of file + @ echo build done \ No newline at end of file diff --git a/module/nasocket.cpp b/module/nasocket.cpp index 69fb298..b79d0d2 100644 --- a/module/nasocket.cpp +++ b/module/nasocket.cpp @@ -1,5 +1,4 @@ #include "../nasal.h" -#include #ifdef _WIN32 #include diff --git a/nasal.h b/nasal.h index ac5cb46..b05b6b5 100644 --- a/nasal.h +++ b/nasal.h @@ -7,6 +7,11 @@ #include #endif +#ifdef _MSC_VER +#include +#include +#endif + #include #include #include diff --git a/nasal_ast.h b/nasal_ast.h index a87fbb0..db152fa 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -1,4 +1,4 @@ -#ifndef __NASAL_AST_H__ +#ifndef __NASAL_AST_H__ #define __NASAL_AST_H__ enum ast_node diff --git a/nasal_builtin.h b/nasal_builtin.h index 785d63e..e00a184 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -1,4 +1,4 @@ -#ifndef __NASAL_BUILTIN_H__ +#ifndef __NASAL_BUILTIN_H__ #define __NASAL_BUILTIN_H__ #if defined __APPLE__ @@ -1030,16 +1030,28 @@ nasal_ref builtin_waitpid(nasal_ref* local,nasal_gc& gc) } void obj_dir_destructor(void* ptr) { +#ifndef _MSC_VER closedir((DIR*)ptr); +#else + FindClose(ptr); +#endif } nasal_ref builtin_opendir(nasal_ref* local,nasal_gc& gc) { nasal_ref path=local[1]; if(path.type!=vm_str) return builtin_err("opendir","\"path\" must be string"); +#ifdef _MSC_VER + WIN32_FIND_DATA data; + HANDLE p; + p=FindFirstFile(path.str().c_str(),&data); + if(p==INVALID_HANDLE_VALUE) + return builtin_err("opendir","cannot open dir <"+path.str()+"> errno "+std::to_string(errno)); +#else DIR* p=opendir(path.str().c_str()); if(!p) return builtin_err("opendir","cannot open dir <"+path.str()+"> errno "+std::to_string(errno)); +#endif nasal_ref ret=gc.alloc(vm_obj); ret.obj().type=nasal_obj::dir; ret.obj().ptr=(void*)p; @@ -1051,17 +1063,28 @@ nasal_ref builtin_readdir(nasal_ref* local,nasal_gc& gc) nasal_ref handle=local[1]; if(!handle.objchk(nasal_obj::dir)) return builtin_err("readdir","not a valid dir handle"); +#ifdef _MSC_VER + WIN32_FIND_DATA data; + if(!FindNextFile(handle.obj().ptr,&data)) + return nil; + return gc.newstr(data.cFileName); +#else dirent* p=readdir((DIR*)handle.obj().ptr); if(!p) return nil; return gc.newstr(p->d_name); +#endif } nasal_ref builtin_closedir(nasal_ref* local,nasal_gc& gc) { nasal_ref handle=local[1]; if(!handle.objchk(nasal_obj::dir)) return builtin_err("closedir","not a valid dir handle"); +#ifndef _MSC_VER closedir((DIR*)handle.obj().ptr); +#else + FindClose(handle.obj().ptr); +#endif handle.obj().ptr=nullptr; return nil; } diff --git a/nasal_dbg.h b/nasal_dbg.h index 37f6acc..f0aede6 100644 --- a/nasal_dbg.h +++ b/nasal_dbg.h @@ -255,7 +255,7 @@ void nasal_dbg::run( &nasal_dbg::opr_mcallv, &nasal_dbg::opr_mcallh, &nasal_dbg::opr_ret }; - std::vector code; + std::vector code; for(auto& i:gen.get_code()) { code.push_back(oprs[i.op]); diff --git a/nasal_import.h b/nasal_import.h index cf1ab04..9400049 100644 --- a/nasal_import.h +++ b/nasal_import.h @@ -1,6 +1,10 @@ #ifndef __NASAL_IMPORT_H__ #define __NASAL_IMPORT_H__ +#ifdef _MSC_VER +#define F_OK 0 +#endif + class nasal_import { private: diff --git a/nasal_lexer.h b/nasal_lexer.h index ce7fdc8..1d6e643 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -1,6 +1,10 @@ #ifndef __NASAL_LEXER_H__ #define __NASAL_LEXER_H__ +#ifdef _MSC_VER +#define S_ISREG(m) (((m)&0xF000)==0x8000) +#endif + #define ID(c) ((c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')||(c<0)) #define HEX(c) (('0'<=c&&c<='9')||('a'<=c&&c<='f')||('A'<=c && c<='F')) #define OCT(c) ('0'<=c&&c<='7') diff --git a/nasal_vm.h b/nasal_vm.h index a6340a5..e398060 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -732,7 +732,11 @@ inline void nasal_vm::opr_callfv() for(uint32_t i=psize;i=1;--i)// load arguments local[i]=local[i-1]; local[0]=func.local[0];// load "me" @@ -1040,15 +1044,16 @@ void nasal_vm::run( &nasal_vm::opr_mcallv, &nasal_vm::opr_mcallh, &nasal_vm::opr_ret }; - std::vector code; + typedef struct{nafunc ptr;uint32_t type;} naf; + std::vector code; for(auto& i:gen.get_code()) { - code.push_back(oprs[i.op]); + code.push_back({oprs[i.op],i.op}); imm.push_back(i.num); } - while(code[pc]){ - (this->*code[pc])(); - ++count[num]; + while(code[pc].ptr){ + ++count[code[pc].type]; + (this->*code[pc].ptr)(); if(top>=canary) break; ++pc;