✨ add cmakelists.txt
This commit is contained in:
parent
f914678311
commit
92abad3384
|
@ -47,6 +47,9 @@ nasal.exe
|
||||||
.vscode
|
.vscode
|
||||||
dump
|
dump
|
||||||
|
|
||||||
|
# build dir
|
||||||
|
build
|
||||||
|
|
||||||
# macOS special cache directory
|
# macOS special cache directory
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
|
|
@ -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})
|
10
main.cpp
10
main.cpp
|
@ -70,7 +70,11 @@ void err() {
|
||||||
std::exit(1);
|
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;
|
using clk=std::chrono::high_resolution_clock;
|
||||||
const auto den=clk::duration::period::den;
|
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();
|
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();
|
gen.compile(parse, ld).chkerr();
|
||||||
if (cmd&VM_CODE) {
|
if (cmd&VM_CODE) {
|
||||||
gen.print();
|
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) {
|
} else if (cmd&VM_TIME || cmd&VM_EXEC) {
|
||||||
ctx.run(gen, ld, argv, cmd&VM_DETAIL);
|
ctx.run(gen, ld, argv, cmd&VM_DETAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get running time
|
||||||
if (cmd&VM_TIME) {
|
if (cmd&VM_TIME) {
|
||||||
f64 tm=(clk::now()-start).count()*1.0/den;
|
f64 tm=(clk::now()-start).count()*1.0/den;
|
||||||
std::clog<<"process exited after "<<tm<<"s.\n\n";
|
std::clog<<"process exited after "<<tm<<"s.\n\n";
|
||||||
|
|
|
@ -72,12 +72,15 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
noecho_input this_window;
|
noecho_input this_window;
|
||||||
|
|
||||||
var nas_getch(var* args, usize size, gc* ngc) {
|
var nas_getch(var* args, usize size, gc* ngc) {
|
||||||
return var::num((double)this_window.noecho_getch());
|
return var::num((double)this_window.noecho_getch());
|
||||||
}
|
}
|
||||||
|
|
||||||
var nas_kbhit(var* args, usize size, gc* ngc) {
|
var nas_kbhit(var* args, usize size, gc* ngc) {
|
||||||
return var::num((double)this_window.noecho_kbhit());
|
return var::num((double)this_window.noecho_kbhit());
|
||||||
}
|
}
|
||||||
|
|
||||||
var nas_noblock(var* args, usize size, gc* ngc) {
|
var nas_noblock(var* args, usize size, gc* ngc) {
|
||||||
if (this_window.noecho_kbhit()) {
|
if (this_window.noecho_kbhit()) {
|
||||||
return var::num((double)this_window.noecho_getch());
|
return var::num((double)this_window.noecho_getch());
|
||||||
|
|
6
nasal.h
6
nasal.h
|
@ -134,10 +134,12 @@ f64 oct2f(const char* str) {
|
||||||
return ret;
|
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.
|
// it is not platform independent, and may have strange output.
|
||||||
// so we write a new function here to convert str to number manually.
|
// 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 dec2f(const char* str) {
|
||||||
f64 ret=0,negative=1,num_pow=0;
|
f64 ret=0,negative=1,num_pow=0;
|
||||||
while('0'<=*str && *str<='9') {
|
while('0'<=*str && *str<='9') {
|
||||||
|
|
|
@ -151,6 +151,7 @@ public:
|
||||||
|
|
||||||
ast(ast&&) = default;
|
ast(ast&&) = default;
|
||||||
ast& operator=(ast&&) = default;
|
ast& operator=(ast&&) = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void print(u32, bool, std::vector<string>&) const;
|
void print(u32, bool, std::vector<string>&) const;
|
||||||
|
|
||||||
|
@ -165,13 +166,16 @@ public:
|
||||||
ast(const span& s, const u32 t)
|
ast(const span& s, const u32 t)
|
||||||
: loc(s), nd_type(t), nd_num(0), nd_str("") {}
|
: loc(s), nd_type(t), nd_num(0), nd_str("") {}
|
||||||
|
|
||||||
|
public:
|
||||||
void dump() const;
|
void dump() const;
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
public:
|
||||||
ast& operator[](usize n) {return nd_child[n];}
|
ast& operator[](usize n) {return nd_child[n];}
|
||||||
const ast& operator[](usize n) const {return nd_child[n];}
|
const ast& operator[](usize n) const {return nd_child[n];}
|
||||||
usize size() const {return nd_child.size();}
|
usize size() const {return nd_child.size();}
|
||||||
|
|
||||||
|
public:
|
||||||
void add(ast&& node) {nd_child.push_back(std::move(node));}
|
void add(ast&& node) {nd_child.push_back(std::move(node));}
|
||||||
void set_begin(const u32, const u32);
|
void set_begin(const u32, const u32);
|
||||||
void set_end(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_str(const string& s) {nd_str=s;}
|
||||||
void set_num(const f64 n) {nd_num=n;}
|
void set_num(const f64 n) {nd_num=n;}
|
||||||
|
|
||||||
|
public:
|
||||||
u32 line() const {return loc.end_line;}
|
u32 line() const {return loc.end_line;}
|
||||||
u32 type() const {return nd_type;}
|
u32 type() const {return nd_type;}
|
||||||
f64 num() const {return nd_num;}
|
f64 num() const {return nd_num;}
|
||||||
|
|
|
@ -152,7 +152,8 @@ var builtin_split(var* local,gc& ngc) {
|
||||||
ngc.temp=nil;
|
ngc.temp=nil;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
usize last=0,pos=s.find(deli,0);
|
usize last=0;
|
||||||
|
usize pos=s.find(deli, 0);
|
||||||
while(pos!=string::npos) {
|
while(pos!=string::npos) {
|
||||||
if (pos>last) {
|
if (pos>last) {
|
||||||
vec.push_back(ngc.newstr(s.substr(last, pos-last)));
|
vec.push_back(ngc.newstr(s.substr(last, pos-last)));
|
||||||
|
|
|
@ -298,12 +298,16 @@ void codegen::func_gen(const ast& node) {
|
||||||
}
|
}
|
||||||
regist_str(str);
|
regist_str(str);
|
||||||
switch(tmp.type()) {
|
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:
|
case ast_default:
|
||||||
calc_gen(tmp[0]);
|
calc_gen(tmp[0]);
|
||||||
gen(op_deft, str_table[str], tmp.line());
|
gen(op_deft, str_table[str], tmp.line());
|
||||||
break;
|
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);
|
add_sym(str);
|
||||||
}
|
}
|
||||||
|
@ -609,8 +613,8 @@ void codegen::cond_gen(const ast& node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void codegen::loop_gen(const ast& node) {
|
void codegen::loop_gen(const ast& node) {
|
||||||
continue_ptr.push_front(std::vector<i32>());
|
continue_ptr.push_front({});
|
||||||
break_ptr.push_front(std::vector<i32>());
|
break_ptr.push_front({});
|
||||||
switch(node.type()) {
|
switch(node.type()) {
|
||||||
case ast_while: while_gen(node); break;
|
case ast_while: while_gen(node); break;
|
||||||
case ast_for: for_gen(node); break;
|
case ast_for: for_gen(node); break;
|
||||||
|
|
|
@ -35,7 +35,8 @@ public:
|
||||||
|
|
||||||
std::vector<string> dbg::parse(const string& cmd) {
|
std::vector<string> dbg::parse(const string& cmd) {
|
||||||
std::vector<string> res;
|
std::vector<string> res;
|
||||||
usize last=0,pos=cmd.find(" ",0);
|
usize last=0;
|
||||||
|
usize pos=cmd.find(" ", 0);
|
||||||
while(pos!=string::npos) {
|
while(pos!=string::npos) {
|
||||||
if (pos>last) {
|
if (pos>last) {
|
||||||
res.push_back(cmd.substr(last, pos-last));
|
res.push_back(cmd.substr(last, pos-last));
|
||||||
|
@ -200,8 +201,8 @@ void dbg::interact() {
|
||||||
void dbg::run(
|
void dbg::run(
|
||||||
const codegen& gen,
|
const codegen& gen,
|
||||||
const linker& linker,
|
const linker& linker,
|
||||||
const std::vector<string>& argv)
|
const std::vector<string>& argv
|
||||||
{
|
) {
|
||||||
verbose=true;
|
verbose=true;
|
||||||
fsize=linker.filelist().size();
|
fsize=linker.filelist().size();
|
||||||
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);
|
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);
|
||||||
|
|
|
@ -352,6 +352,7 @@ void nas_obj::file_dtor() {
|
||||||
}
|
}
|
||||||
fclose((FILE*)ptr);
|
fclose((FILE*)ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nas_obj::dir_dtor() {
|
void nas_obj::dir_dtor() {
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
closedir((DIR*)ptr);
|
closedir((DIR*)ptr);
|
||||||
|
@ -359,6 +360,7 @@ void nas_obj::dir_dtor() {
|
||||||
FindClose(ptr);
|
FindClose(ptr);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void nas_obj::dylib_dtor() {
|
void nas_obj::dylib_dtor() {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
FreeLibrary((HMODULE)ptr);
|
FreeLibrary((HMODULE)ptr);
|
||||||
|
|
|
@ -42,7 +42,8 @@ public:
|
||||||
linker::linker(error& e): show_path(false), lib_loaded(false), err(e) {
|
linker::linker(error& e): show_path(false), lib_loaded(false), err(e) {
|
||||||
char sep=is_windows()? ';':':';
|
char sep=is_windows()? ';':':';
|
||||||
string PATH=getenv("PATH");
|
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) {
|
while(pos!=string::npos) {
|
||||||
string dirpath=PATH.substr(last, pos-last);
|
string dirpath=PATH.substr(last, pos-last);
|
||||||
if (dirpath.length()) {
|
if (dirpath.length()) {
|
||||||
|
|
|
@ -107,7 +107,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
void die(const span&,string);
|
void die(const span&,string);
|
||||||
void next() {++ptr;};
|
void next() {++ptr;}
|
||||||
void match(tok type, const char* info=nullptr);
|
void match(tok type, const char* info=nullptr);
|
||||||
bool lookahead(tok);
|
bool lookahead(tok);
|
||||||
bool is_call(tok);
|
bool is_call(tok);
|
||||||
|
|
|
@ -1073,7 +1073,8 @@ void vm::run(
|
||||||
const codegen& gen,
|
const codegen& gen,
|
||||||
const linker& linker,
|
const linker& linker,
|
||||||
const std::vector<string>& argv,
|
const std::vector<string>& argv,
|
||||||
const bool detail) {
|
const bool detail
|
||||||
|
) {
|
||||||
verbose=detail;
|
verbose=detail;
|
||||||
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);
|
init(gen.strs(), gen.nums(), gen.codes(), linker.filelist(), argv);
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
|
|
Loading…
Reference in New Issue