add cmakelists.txt

This commit is contained in:
ValKmjolnir 2023-05-06 21:12:40 +08:00
parent f914678311
commit 92abad3384
19 changed files with 725 additions and 669 deletions

3
.gitignore vendored
View File

@ -47,6 +47,9 @@ nasal.exe
.vscode
dump
# build dir
build
# macOS special cache directory
.DS_Store

27
CMakeLists.txt Normal file
View File

@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.10)
project(nasal VERSION 10.1)
# -std=c++14 -Wshadow -Wall
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wshadow -Wall")
# generate release executables
set(CMAKE_BUILD_TYPE "Release")
add_library(fib SHARED ${CMAKE_SOURCE_DIR}/module/fib.cpp)
target_include_directories(fib PRIVATE ${CMAKE_SOURCE_DIR})
add_library(key SHARED ${CMAKE_SOURCE_DIR}/module/keyboard.cpp)
target_include_directories(key PRIVATE ${CMAKE_SOURCE_DIR})
add_library(mat SHARED ${CMAKE_SOURCE_DIR}/module/matrix.cpp)
target_include_directories(mat PRIVATE ${CMAKE_SOURCE_DIR})
add_library(nasock SHARED ${CMAKE_SOURCE_DIR}/module/nasocket.cpp)
target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR})
add_executable(nasal main.cpp)
target_link_libraries(nasal dl)
target_include_directories(nasal PRIVATE ${CMAKE_SOURCE_DIR})

View File

@ -70,7 +70,11 @@ void err() {
std::exit(1);
}
void execute(const string& file, const std::vector<string>& argv, const u32 cmd) {
void execute(
const string& file,
const std::vector<string>& argv,
const u32 cmd
) {
using clk=std::chrono::high_resolution_clock;
const auto den=clk::duration::period::den;
@ -96,7 +100,7 @@ void execute(const string& file, const std::vector<string>& argv, const u32 cmd)
parse.tree().dump();
}
// code generator gets parser's ast and linker's import file list to generate code
// code generator gets parser's ast and import file list to generate code
gen.compile(parse, ld).chkerr();
if (cmd&VM_CODE) {
gen.print();
@ -109,6 +113,8 @@ void execute(const string& file, const std::vector<string>& argv, const u32 cmd)
} else if (cmd&VM_TIME || cmd&VM_EXEC) {
ctx.run(gen, ld, argv, cmd&VM_DETAIL);
}
// get running time
if (cmd&VM_TIME) {
f64 tm=(clk::now()-start).count()*1.0/den;
std::clog<<"process exited after "<<tm<<"s.\n\n";

View File

@ -72,12 +72,15 @@ public:
};
noecho_input this_window;
var nas_getch(var* args, usize size, gc* ngc) {
return var::num((double)this_window.noecho_getch());
}
var nas_kbhit(var* args, usize size, gc* ngc) {
return var::num((double)this_window.noecho_kbhit());
}
var nas_noblock(var* args, usize size, gc* ngc) {
if (this_window.noecho_kbhit()) {
return var::num((double)this_window.noecho_getch());

View File

@ -134,10 +134,12 @@ f64 oct2f(const char* str) {
return ret;
}
// we have the same reason not using atof here just as andy's interpreter does.
// 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.
// so we write a new function here to convert str to number manually.
// but this also makes 0.1+0.2==0.3, not another result that you may get in other languages.
// but this also makes 0.1+0.2==0.3,
// not another result that you may get in other languages.
f64 dec2f(const char* str) {
f64 ret=0,negative=1,num_pow=0;
while('0'<=*str && *str<='9') {

View File

@ -151,6 +151,7 @@ public:
ast(ast&&) = default;
ast& operator=(ast&&) = default;
private:
void print(u32, bool, std::vector<string>&) const;
@ -165,13 +166,16 @@ public:
ast(const span& s, const u32 t)
: loc(s), nd_type(t), nd_num(0), nd_str("") {}
public:
void dump() const;
void clear();
public:
ast& operator[](usize n) {return nd_child[n];}
const ast& operator[](usize n) const {return nd_child[n];}
usize size() const {return nd_child.size();}
public:
void add(ast&& node) {nd_child.push_back(std::move(node));}
void set_begin(const u32, const u32);
void set_end(const u32, const u32);
@ -179,6 +183,7 @@ public:
void set_str(const string& s) {nd_str=s;}
void set_num(const f64 n) {nd_num=n;}
public:
u32 line() const {return loc.end_line;}
u32 type() const {return nd_type;}
f64 num() const {return nd_num;}

View File

@ -152,7 +152,8 @@ var builtin_split(var* local,gc& ngc) {
ngc.temp=nil;
return res;
}
usize last=0,pos=s.find(deli,0);
usize last=0;
usize pos=s.find(deli, 0);
while(pos!=string::npos) {
if (pos>last) {
vec.push_back(ngc.newstr(s.substr(last, pos-last)));

View File

@ -298,12 +298,16 @@ void codegen::func_gen(const ast& node) {
}
regist_str(str);
switch(tmp.type()) {
case ast_id:gen(op_para,str_table[str],tmp.line());break;
case ast_id:
gen(op_para, str_table[str], tmp.line());
break;
case ast_default:
calc_gen(tmp[0]);
gen(op_deft, str_table[str], tmp.line());
break;
case ast_dynamic:gen(op_dyn,str_table[str],tmp.line());break;
case ast_dynamic:
gen(op_dyn, str_table[str], tmp.line());
break;
}
add_sym(str);
}
@ -609,8 +613,8 @@ void codegen::cond_gen(const ast& node) {
}
void codegen::loop_gen(const ast& node) {
continue_ptr.push_front(std::vector<i32>());
break_ptr.push_front(std::vector<i32>());
continue_ptr.push_front({});
break_ptr.push_front({});
switch(node.type()) {
case ast_while: while_gen(node); break;
case ast_for: for_gen(node); break;

View File

@ -35,7 +35,8 @@ public:
std::vector<string> dbg::parse(const string& cmd) {
std::vector<string> res;
usize last=0,pos=cmd.find(" ",0);
usize last=0;
usize pos=cmd.find(" ", 0);
while(pos!=string::npos) {
if (pos>last) {
res.push_back(cmd.substr(last, pos-last));
@ -200,8 +201,8 @@ void dbg::interact() {
void dbg::run(
const codegen& gen,
const linker& linker,
const std::vector<string>& argv)
{
const std::vector<string>& argv
) {
verbose=true;
fsize=linker.filelist().size();
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);

View File

@ -352,6 +352,7 @@ void nas_obj::file_dtor() {
}
fclose((FILE*)ptr);
}
void nas_obj::dir_dtor() {
#ifndef _MSC_VER
closedir((DIR*)ptr);
@ -359,6 +360,7 @@ void nas_obj::dir_dtor() {
FindClose(ptr);
#endif
}
void nas_obj::dylib_dtor() {
#ifdef _WIN32
FreeLibrary((HMODULE)ptr);

View File

@ -42,7 +42,8 @@ public:
linker::linker(error& e): show_path(false), lib_loaded(false), err(e) {
char sep=is_windows()? ';':':';
string PATH=getenv("PATH");
usize last=0,pos=PATH.find(sep,0);
usize last=0;
usize pos=PATH.find(sep, 0);
while(pos!=string::npos) {
string dirpath=PATH.substr(last, pos-last);
if (dirpath.length()) {

View File

@ -107,7 +107,7 @@ private:
};
void die(const span&,string);
void next() {++ptr;};
void next() {++ptr;}
void match(tok type, const char* info=nullptr);
bool lookahead(tok);
bool is_call(tok);

View File

@ -1073,7 +1073,8 @@ void vm::run(
const codegen& gen,
const linker& linker,
const std::vector<string>& argv,
const bool detail) {
const bool detail
) {
verbose=detail;
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);
#ifndef _MSC_VER