optimize codes

This commit is contained in:
ValKmjolnir 2022-04-12 18:26:54 +08:00
parent b6f174e869
commit 022460755f
6 changed files with 11 additions and 12 deletions

View File

@ -135,7 +135,7 @@ private:
std::string _str; std::string _str;
std::vector<nasal_ast> _child; std::vector<nasal_ast> _child;
public: public:
nasal_ast(const uint32_t l=0,const uint32_t t=ast_null):_line(l),_type(t){} nasal_ast(const uint32_t l=0,const uint32_t t=ast_null):_line(l),_type(t),_num(0){}
nasal_ast(const nasal_ast&); nasal_ast(const nasal_ast&);
nasal_ast(nasal_ast&&); nasal_ast(nasal_ast&&);
void print(int,bool); void print(int,bool);
@ -162,13 +162,12 @@ public:
inline std::vector<nasal_ast>& child(){return _child;} inline std::vector<nasal_ast>& child(){return _child;}
}; };
nasal_ast::nasal_ast(const nasal_ast& tmp) nasal_ast::nasal_ast(const nasal_ast& tmp):
_str(tmp._str),_child(tmp._child)
{ {
_line=tmp._line; _line=tmp._line;
_type=tmp._type; _type=tmp._type;
_num =tmp._num; _num =tmp._num;
_str =tmp._str;
_child=tmp._child;
} }
nasal_ast::nasal_ast(nasal_ast&& tmp) nasal_ast::nasal_ast(nasal_ast&& tmp)

View File

@ -88,7 +88,7 @@ nas_native(builtin_platform);
nas_native(builtin_gc); nas_native(builtin_gc);
nas_native(builtin_md5); nas_native(builtin_md5);
nasal_ref builtin_err(const char* func_name,std::string info) nasal_ref builtin_err(const char* func_name,const std::string info)
{ {
std::cerr<<"[vm] "<<func_name<<": "<<info<<".\n"; std::cerr<<"[vm] "<<func_name<<": "<<info<<".\n";
return {vm_none}; return {vm_none};

View File

@ -247,7 +247,7 @@ private:
void block_gen(const nasal_ast&); void block_gen(const nasal_ast&);
void ret_gen(const nasal_ast&); void ret_gen(const nasal_ast&);
public: public:
nasal_codegen(nasal_err& e):nerr(e){} nasal_codegen(nasal_err& e):fileindex(0),nerr(e),file(nullptr){}
void compile(const nasal_parse&,const nasal_import&); void compile(const nasal_parse&,const nasal_import&);
void print_op(uint32_t); void print_op(uint32_t);
void print(); void print();

View File

@ -131,7 +131,7 @@ struct nasal_func
std::vector<nasal_ref> upvalue; // closure std::vector<nasal_ref> upvalue; // closure
std::unordered_map<std::string,size_t> keys; // parameter name table, size_t begins from 1 std::unordered_map<std::string,size_t> keys; // parameter name table, size_t begins from 1
nasal_func():dynpara(-1){} nasal_func():dynpara(-1),entry(0),psize(0),lsize(0){}
void clear(); void clear();
}; };
@ -151,6 +151,7 @@ struct nasal_obj
{ {
enum obj_type enum obj_type
{ {
null,
file=1, file=1,
dir, dir,
dylib, dylib,
@ -166,7 +167,7 @@ struct nasal_obj
typedef void (*dest)(void*); typedef void (*dest)(void*);
dest destructor; dest destructor;
nasal_obj():ptr(nullptr),destructor(nullptr){} nasal_obj():type(obj_type::null),ptr(nullptr),destructor(nullptr){}
~nasal_obj(){clear();} ~nasal_obj(){clear();}
void clear() void clear()
{ {

View File

@ -93,12 +93,11 @@ struct token
uint32_t column; uint32_t column;
uint32_t type; uint32_t type;
std::string str; std::string str;
token(uint32_t l=0,uint32_t c=0,uint32_t t=tok_null,std::string s="") token(uint32_t l=0,uint32_t c=0,uint32_t t=tok_null,const std::string s=""):str(s)
{ {
line=l; line=l;
column=c; column=c;
type=t; type=t;
str=s;
} }
}; };
@ -119,7 +118,7 @@ private:
std::string num_gen(); std::string num_gen();
std::string str_gen(); std::string str_gen();
public: public:
nasal_lexer(nasal_err& e):nerr(e){} nasal_lexer(nasal_err& e):line(0),column(0),ptr(0),nerr(e){}
void scan(const std::string&); void scan(const std::string&);
void print(); void print();
const std::vector<token>& get_tokens() const {return tokens;} const std::vector<token>& get_tokens() const {return tokens;}

View File

@ -98,7 +98,7 @@ private:
nasal_ast break_expr(); nasal_ast break_expr();
nasal_ast ret_expr(); nasal_ast ret_expr();
public: public:
nasal_parse(nasal_err& e):nerr(e){} nasal_parse(nasal_err& e):ptr(0),in_func(0),in_loop(0),tokens(nullptr),nerr(e){}
void print(){root.print(0);} void print(){root.print(0);}
void compile(const nasal_lexer&); void compile(const nasal_lexer&);
nasal_ast& ast(){return root;} nasal_ast& ast(){return root;}