Skip to content

Commit

Permalink
avoid unnecessary deep copy by using rvalue ref
Browse files Browse the repository at this point in the history
and test file update.
bug fixed in test/lexer.nas
  • Loading branch information
ValKmjolnir committed Jun 24, 2021
1 parent fd57e9a commit 3c9a10d
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 311 deletions.
7 changes: 3 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "nasal.h"

nasal_vm vm;

void help_interact()
{
std::cout
Expand Down Expand Up @@ -31,7 +29,7 @@ void help_cmd()
void info()
{
std::cout
<<">> Nasal interpreter ver 6.5 efficient gc test .\n"
<<">> Nasal interpreter ver 6.5.\n"
<<">> Thanks to https://github.com/andyross/nasal\n"
<<">> Code: https://github.com/ValKmjolnir/Nasal-Interpreter\n"
<<">> Code: https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
Expand Down Expand Up @@ -60,6 +58,7 @@ void execute(std::string& file,std::string& command)
nasal_parse parse;
nasal_import import;
nasal_codegen codegen;
nasal_vm vm;
lexer.openfile(file);
lexer.scanner();
if(lexer.get_error())
Expand Down Expand Up @@ -153,7 +152,7 @@ int main(int argc,const char* argv[])
else if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"-version")))
{
logo();
std::cout<<"Nasal interpreter ver 6.5 efficient gc test\n";
std::cout<<"Nasal interpreter ver 6.5\n";
}
else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-help")))
help_cmd();
Expand Down
33 changes: 27 additions & 6 deletions nasal_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ class nasal_ast
nasal_ast(){line=0;type=ast_null;}
nasal_ast(int l,int t){line=l;type=t;}
nasal_ast(const nasal_ast&);
nasal_ast& operator=(const nasal_ast&);
nasal_ast(nasal_ast&&);
nasal_ast& operator=(const nasal_ast&);
nasal_ast& operator=(nasal_ast&&);
void print_ast(int);
void clear();
void add_child(nasal_ast ast){children.push_back(ast);}
void add_child(nasal_ast&& ast){children.push_back(std::move(ast));}
void set_line(int l){line=l;}
void set_type(int t){type=t;}
void set_str(std::string& s){str=s;}
Expand All @@ -82,6 +84,16 @@ nasal_ast::nasal_ast(const nasal_ast& tmp)
return;
}

nasal_ast::nasal_ast(nasal_ast&& tmp)
{
line=tmp.line;
type=tmp.type;
str.swap(tmp.str);
num =tmp.num;
children.swap(tmp.children);
return;
}

nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
{
line=tmp.line;
Expand All @@ -92,6 +104,16 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
return *this;
}

nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
{
line=tmp.line;
type=tmp.type;
str.swap(tmp.str);
num=tmp.num;
children.swap(tmp.children);
return *this;
}

void nasal_ast::clear()
{
line=0;
Expand All @@ -104,11 +126,10 @@ void nasal_ast::clear()

void nasal_ast::print_ast(int depth)
{
std::string indentation="";
for(int i=0;i<depth;++i)
indentation+="| ";
std::cout<<indentation<<ast_name[type];
if(type==ast_str || type==ast_id || type==ast_dynamic_id || type==ast_callh)
std::cout<<"| ";
std::cout<<ast_name[type];
if(type==ast_str || type==ast_id || type==ast_default_arg || type==ast_dynamic_id || type==ast_callh)
std::cout<<":"<<str;
else if(type==ast_num)
std::cout<<":"<<num;
Expand Down
4 changes: 2 additions & 2 deletions nasal_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ void nasal_codegen::func_gen(nasal_ast& ast)
}
else if(tmp.get_type()==ast_default_arg)
{
calc_gen(tmp.get_children()[1]);
std::string& str=tmp.get_children()[0].get_str();
calc_gen(tmp.get_children()[0]);
std::string& str=tmp.get_str();
regist_string(str);
add_sym(str);
gen(op_defpara,string_table[str]);
Expand Down
25 changes: 10 additions & 15 deletions nasal_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class nasal_import
void die(std::string&,const char*);
bool check_import(nasal_ast&);
bool check_exist(std::string&);
void linker(nasal_ast&,nasal_ast&);
void linker(nasal_ast&,nasal_ast&&);
nasal_ast file_import(nasal_ast&);
nasal_ast load(nasal_ast&);
public:
Expand Down Expand Up @@ -51,21 +51,21 @@ only this kind of node can be recognized as 'import':
return true;
}

bool nasal_import::check_exist(std::string& filename)
bool nasal_import::check_exist(std::string& file)
{
// avoid importing the same file
for(auto& fname:filename_table)
if(filename==fname)
if(file==fname)
return true;
filename_table.push_back(filename);
filename_table.push_back(file);
return false;
}

void nasal_import::linker(nasal_ast& root,nasal_ast& add_root)
void nasal_import::linker(nasal_ast& root,nasal_ast&& add_root)
{
// add children of add_root to the back of root
for(auto& i:add_root.get_children())
root.add_child(i);
root.add_child(std::move(i));
return;
}

Expand All @@ -75,7 +75,7 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
nasal_ast tmp(0,ast_root);

// get filename and set node to ast_null
std::string& filename=node.get_children()[1].get_children()[0].get_str();
std::string filename=node.get_children()[1].get_children()[0].get_str();
node.clear();

// avoid infinite loading loop
Expand All @@ -98,7 +98,7 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
die(filename,"parser");
return tmp;
}
tmp=import_par.get_root();
tmp=std::move(import_par.get_root());
import_par.get_root().clear();
// check if tmp has 'import'
return load(tmp);
Expand All @@ -109,14 +109,9 @@ nasal_ast nasal_import::load(nasal_ast& root)
nasal_ast new_root(0,ast_root);
for(auto& i:root.get_children())
if(check_import(i))
{
nasal_ast tmp=file_import(i);
// add tmp to the back of new_root
linker(new_root,tmp);
}
linker(new_root,file_import(i));
// add root to the back of new_root
linker(new_root,root);
// oops,i think it is not efficient if the root is too ... large?
linker(new_root,std::move(root));
return new_root;
}

Expand Down
67 changes: 33 additions & 34 deletions nasal_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class nasal_parse
std::vector<token> error_token;
int in_function; // count when generating function block,used to check return-expression
int in_loop; // count when generating loop block,used to check break/continue-expression
void die(int,std::string);
void die(int,std::string&&);
void match(int type,const char* err_info="");
bool check_comma(int*);
bool check_multi_def();
Expand Down Expand Up @@ -140,7 +140,7 @@ void nasal_parse::main_process()
<<"check \'(\',\'[\',\'{\',\')\',\']\',\'}\' match or not.\n";
return;
}
void nasal_parse::die(int line,std::string info)
void nasal_parse::die(int line,std::string&& info)
{
++error;
std::cout<<">> [parse] line "<<line<<": "<<info<<".\n";
Expand Down Expand Up @@ -378,21 +378,21 @@ nasal_ast nasal_parse::args_gen()
nasal_ast special_arg(tok_list[ptr].line,ast_null);
if(tok_list[ptr].type==tok_eq)
{
special_arg.add_child(tmp);
match(tok_eq);
special_arg.add_child(calc());
special_arg=std::move(tmp);
special_arg.set_type(ast_default_arg);
special_arg.add_child(calc());
}
else
{
match(tok_ellipsis);
special_arg=tmp;
special_arg=std::move(tmp);
special_arg.set_type(ast_dynamic_id);
}
node.add_child(special_arg);
node.add_child(std::move(special_arg));
}
else
node.add_child(tmp);
node.add_child(std::move(tmp));
if(tok_list[ptr].type==tok_comma)
match(tok_comma);
else if(tok_list[ptr].type==tok_id)// first set of identifier
Expand All @@ -407,9 +407,9 @@ nasal_ast nasal_parse::args_gen()
{
switch(tmp.get_type())
{
case ast_id: args_format+="val";break;
case ast_default_arg: args_format+="val=scalar";break;
case ast_dynamic_id: args_format+="val...";break;
case ast_id: args_format+=tmp.get_str();break;
case ast_default_arg: args_format+=tmp.get_str()+"=val";break;
case ast_dynamic_id: args_format+=tmp.get_str()+"...";break;
}
args_format+=",)"[&tmp==&node.get_children().back()];
}
Expand All @@ -433,7 +433,7 @@ nasal_ast nasal_parse::args_gen()
{
case ast_dynamic_id:
case ast_id: new_name=tmp.get_str();break;
case ast_default_arg:new_name=tmp.get_children()[0].get_str();break;
case ast_default_arg:new_name=tmp.get_str();break;
}
if(argname_table.count(new_name))
die(tmp.get_line(),"parameter's name repeats: "+new_name);
Expand Down Expand Up @@ -518,21 +518,21 @@ nasal_ast nasal_parse::calc()
// trinocular calculation
nasal_ast tmp(tok_list[ptr].line,ast_trino);
match(tok_quesmark);
tmp.add_child(node);
tmp.add_child(std::move(node));
tmp.add_child(calc());
match(tok_colon);
tmp.add_child(calc());
node=tmp;
node=std::move(tmp);
}
else if(tok_eq<=tok_list[ptr].type && tok_list[ptr].type<=tok_lnkeq)
{
check_memory_reachable(node);
// tok_eq~tok_lnkeq is 37 to 42,ast_equal~ast_lnkeq is 21~26
nasal_ast tmp(tok_list[ptr].line,tok_list[ptr].type-tok_eq+ast_equal);
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_list[ptr].type);
tmp.add_child(calc());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand All @@ -542,10 +542,10 @@ nasal_ast nasal_parse::or_expr()
while(tok_list[ptr].type==tok_or)
{
nasal_ast tmp(tok_list[ptr].line,ast_or);
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_or);
tmp.add_child(and_expr());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand All @@ -555,10 +555,10 @@ nasal_ast nasal_parse::and_expr()
while(tok_list[ptr].type==tok_and)
{
nasal_ast tmp(tok_list[ptr].line,ast_and);
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_and);
tmp.add_child(cmp_expr());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand All @@ -569,10 +569,10 @@ nasal_ast nasal_parse::cmp_expr()
{
// tok_cmpeq~tok_geq is 43~48,ast_cmpeq~ast_geq is 27~32
nasal_ast tmp(tok_list[ptr].line,tok_list[ptr].type-tok_cmpeq+ast_cmpeq);
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_list[ptr].type);
tmp.add_child(additive_expr());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand All @@ -588,10 +588,10 @@ nasal_ast nasal_parse::additive_expr()
case tok_sub: tmp.set_type(ast_sub); break;
case tok_link: tmp.set_type(ast_link); break;
}
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_list[ptr].type);
tmp.add_child(multive_expr());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand All @@ -601,10 +601,10 @@ nasal_ast nasal_parse::multive_expr()
while(tok_list[ptr].type==tok_mult || tok_list[ptr].type==tok_div)
{
nasal_ast tmp(tok_list[ptr].line,tok_list[ptr].type-tok_mult+ast_mult);
tmp.add_child(node);
tmp.add_child(std::move(node));
match(tok_list[ptr].type);
tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand Down Expand Up @@ -667,11 +667,10 @@ nasal_ast nasal_parse::scalar()
}
if(is_call(tok_list[ptr].type))
{
nasal_ast tmp=node;
node.clear();
nasal_ast tmp=std::move(node);
node.set_line(tok_list[ptr].line);
node.set_type(ast_call);
node.add_child(tmp);
node.add_child(std::move(tmp));
}
while(is_call(tok_list[ptr].type))
node.add_child(call_scalar());
Expand Down Expand Up @@ -744,9 +743,9 @@ nasal_ast nasal_parse::subvec()
{
nasal_ast tmp(node.get_line(),ast_subvec);
match(tok_colon);
tmp.add_child(node);
tmp.add_child(std::move(node));
tmp.add_child((tok_list[ptr].type==tok_comma || tok_list[ptr].type==tok_rbracket)?nil_gen():calc());
node=tmp;
node=std::move(tmp);
}
return node;
}
Expand Down Expand Up @@ -905,7 +904,7 @@ nasal_ast nasal_parse::for_loop()
match(tok_semi,"expected \';\' in for(;;)");
// conditional expression
if(tok_list[ptr].type==tok_eof)
die(error_line,"expected conditional expression");
die(error_line,"expected conditional expr");
if(tok_list[ptr].type==tok_semi)
node.add_child(null_node_gen());
else
Expand Down Expand Up @@ -976,7 +975,7 @@ nasal_ast nasal_parse::conditional()
tmp.add_child(calc());
match(tok_rcurve);
tmp.add_child(exprs_gen());
node.add_child(tmp);
node.add_child(std::move(tmp));
// end of if-expression
while(tok_list[ptr].type==tok_elsif)
{
Expand All @@ -986,14 +985,14 @@ nasal_ast nasal_parse::conditional()
tmp.add_child(calc());
match(tok_rcurve);
tmp.add_child(exprs_gen());
node.add_child(tmp);
node.add_child(std::move(tmp));
}
if(tok_list[ptr].type==tok_else)
{
nasal_ast tmp(tok_list[ptr].line,ast_else);
match(tok_else);
tmp.add_child(exprs_gen());
node.add_child(tmp);
node.add_child(std::move(tmp));
}
return node;
}
Expand Down
Loading

0 comments on commit 3c9a10d

Please sign in to comment.