diff --git a/main.cpp b/main.cpp index 8260488..0086a4b 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,6 @@ void help_interact() std::cout <<">> [ ] input a file name to execute. \n" <<">> [help ] show help. \n" - <<">> [clear] clear the screen. \n" <<">> [lex ] view tokens. \n" <<">> [ast ] view abstract syntax tree. \n" <<">> [code ] view byte code. \n" @@ -20,10 +19,10 @@ void help_cmd() #ifdef _WIN32 <<"use command \'chcp 65001\' if want to use unicode.\n" #endif - <<"nasal | use interactive interpreter.\n" - <<"nasal -h -help | get help.\n" - <<"nasal -v -version | get version of nasal interpreter.\n" - <<"nasal filename | execute script file.\n"; + <<"nasal | use interactive interpreter.\n" + <<"nasal -h, --help | get help.\n" + <<"nasal -v, --version | get version of nasal interpreter.\n" + <<"nasal filename | execute script file.\n"; return; } void info() @@ -125,14 +124,6 @@ void interact() std::cin>>command; if(command=="help") help_interact(); - else if(command=="clear") - { -#ifdef _WIN32 - system("cls"); -#else - int rs=system("clear"); -#endif - } else if(command=="logo") logo(); else if(command=="exit") @@ -148,9 +139,9 @@ int main(int argc,const char* argv[]) std::string command,file="null"; if(argc==1) interact(); - else if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"-version"))) + else if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version"))) logo(); - else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-help"))) + else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help"))) help_cmd(); else if(argc==2 && argv[1][0]!='-') { diff --git a/nasal_codegen.h b/nasal_codegen.h index ffa4ebd..d1a9261 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -170,16 +170,19 @@ struct opcode { uint8_t op; uint32_t num; - opcode(uint8_t _op=op_nop,uint32_t _num=0) + uint32_t line; + opcode(uint8_t _op=op_nop,uint32_t _num=0,uint32_t _line=0) { op=_op; num=_num; + line=_line; return; } opcode& operator=(const opcode& tmp) { op=tmp.op; num=tmp.num; + line=tmp.line; return *this; } }; @@ -206,7 +209,7 @@ private: void add_sym(std::string&); int local_find(std::string&); int global_find(std::string&); - void gen(uint8_t,uint32_t); + void gen(uint8_t,uint32_t,uint32_t); void num_gen(nasal_ast&); void str_gen(nasal_ast&); void vec_gen(nasal_ast&); @@ -311,21 +314,21 @@ int nasal_codegen::global_find(std::string& name) return -1; } -void nasal_codegen::gen(uint8_t op,uint32_t num) +void nasal_codegen::gen(uint8_t op,uint32_t num,uint32_t line) { - exec_code.push_back({op,num}); + exec_code.push_back({op,num,line}); return; } void nasal_codegen::num_gen(nasal_ast& ast) { double num=ast.get_num(); - if(num==0)gen(op_pzero,0); - else if(num==1)gen(op_pone,0); + if(num==0)gen(op_pzero,0,ast.get_line()); + else if(num==1)gen(op_pone,0,ast.get_line()); else { regist_number(num); - gen(op_pnum,number_table[num]); + gen(op_pnum,number_table[num],ast.get_line()); } return; } @@ -334,7 +337,7 @@ void nasal_codegen::str_gen(nasal_ast& ast) { std::string& str=ast.get_str(); regist_string(str); - gen(op_pstr,string_table[str]); + gen(op_pstr,string_table[str],ast.get_line()); return; } @@ -342,19 +345,19 @@ void nasal_codegen::vec_gen(nasal_ast& ast) { for(auto& node:ast.get_children()) calc_gen(node); - gen(op_newv,ast.get_children().size()); + gen(op_newv,ast.get_children().size(),ast.get_line()); return; } void nasal_codegen::hash_gen(nasal_ast& ast) { - gen(op_newh,0); + gen(op_newh,0,ast.get_line()); for(auto& node:ast.get_children()) { calc_gen(node.get_children()[1]); std::string& str=node.get_children()[0].get_str(); regist_string(str); - gen(op_happ,string_table[str]); + gen(op_happ,string_table[str],node.get_line()); } return; } @@ -362,9 +365,9 @@ void nasal_codegen::hash_gen(nasal_ast& ast) void nasal_codegen::func_gen(nasal_ast& ast) { int newfunc_label=exec_code.size(); - gen(op_newf,0); + gen(op_newf,0,ast.get_line()); int local_label=exec_code.size(); - gen(op_intl,0); + gen(op_intl,0,ast.get_line()); local.push_back(std::vector()); // add special keyword 'me' into symbol table @@ -378,7 +381,7 @@ void nasal_codegen::func_gen(nasal_ast& ast) add_sym(me); } - gen(op_offset,0); + gen(op_offset,0,ast.get_line()); for(auto& i:local) exec_code.back().num+=i.size(); // generate parameter list @@ -389,7 +392,7 @@ void nasal_codegen::func_gen(nasal_ast& ast) std::string& str=tmp.get_str(); regist_string(str); add_sym(str); - gen(op_para,string_table[str]); + gen(op_para,string_table[str],tmp.get_line()); } else if(tmp.get_type()==ast_default_arg) { @@ -397,19 +400,19 @@ void nasal_codegen::func_gen(nasal_ast& ast) std::string& str=tmp.get_str(); regist_string(str); add_sym(str); - gen(op_defpara,string_table[str]); + gen(op_defpara,string_table[str],tmp.get_line()); } else if(tmp.get_type()==ast_dynamic_id) { std::string& str=tmp.get_str(); regist_string(str); add_sym(str); - gen(op_dynpara,string_table[str]); + gen(op_dynpara,string_table[str],tmp.get_line()); } } exec_code[newfunc_label].num=exec_code.size()+1; int jmp_ptr=exec_code.size(); - gen(op_jmp,0); + gen(op_jmp,0,0); nasal_ast& block=ast.get_children()[1]; block_gen(block); @@ -419,8 +422,8 @@ void nasal_codegen::func_gen(nasal_ast& ast) if(!block.get_children().size() || block.get_children().back().get_type()!=ast_ret) { - gen(op_pnil,0); - gen(op_ret,0); + gen(op_pnil,0,block.get_line()); + gen(op_ret,0,block.get_line()); } exec_code[jmp_ptr].num=exec_code.size(); return; @@ -451,7 +454,7 @@ void nasal_codegen::call_id(nasal_ast& ast) for(int i=0;builtin_func[i].name;++i) if(builtin_func[i].name==str) { - gen(op_callb,i); + gen(op_callb,i,ast.get_line()); if(local.empty()) die("builtin functions work in a local scope.",ast.get_line()); return; @@ -459,13 +462,13 @@ void nasal_codegen::call_id(nasal_ast& ast) int index=local_find(str); if(index>=0) { - gen(op_calll,index); + gen(op_calll,index,ast.get_line()); return; } index=global_find(str); if(index>=0) { - gen(op_callg,index); + gen(op_callg,index,ast.get_line()); return; } die("undefined symbol \""+str+"\".",ast.get_line()); @@ -476,7 +479,7 @@ void nasal_codegen::call_hash(nasal_ast& ast) { std::string& str=ast.get_str(); regist_string(str); - gen(op_callh,string_table[str]); + gen(op_callh,string_table[str],ast.get_line()); return; } @@ -486,42 +489,42 @@ void nasal_codegen::call_vec(nasal_ast& ast) if(ast.get_children().size()==1 && ast.get_children()[0].get_type()!=ast_subvec) { calc_gen(ast.get_children()[0]); - gen(op_callv,0); + gen(op_callv,0,ast.get_children()[0].get_line()); return; } - gen(op_slcbegin,0); + gen(op_slcbegin,0,ast.get_line()); for(auto& tmp:ast.get_children()) { if(tmp.get_type()!=ast_subvec) { calc_gen(tmp); - gen(op_slc,0); + gen(op_slc,0,tmp.get_line()); } else { calc_gen(tmp.get_children()[0]); calc_gen(tmp.get_children()[1]); - gen(op_slc2,0); + gen(op_slc2,0,tmp.get_line()); } } - gen(op_slcend,0); + gen(op_slcend,0,ast.get_line()); return; } void nasal_codegen::call_func(nasal_ast& ast) { if(!ast.get_children().size()) - gen(op_callfv,0); + gen(op_callfv,0,ast.get_line()); else if(ast.get_children()[0].get_type()==ast_hashmember) { hash_gen(ast); - gen(op_callfh,0); + gen(op_callfh,0,ast.get_line()); } else { for(auto& node:ast.get_children()) calc_gen(node); - gen(op_callfv,ast.get_children().size()); + gen(op_callfv,ast.get_children().size(),ast.get_line()); } return; } @@ -564,13 +567,13 @@ void nasal_codegen::mcall_id(nasal_ast& ast) int index=local_find(str); if(index>=0) { - gen(op_mcalll,index); + gen(op_mcalll,index,ast.get_line()); return; } index=global_find(str); if(index>=0) { - gen(op_mcallg,index); + gen(op_mcallg,index,ast.get_line()); return; } die("undefined symbol \""+str+"\".",ast.get_line()); @@ -580,7 +583,7 @@ void nasal_codegen::mcall_id(nasal_ast& ast) void nasal_codegen::mcall_vec(nasal_ast& ast) { calc_gen(ast.get_children()[0]); - gen(op_mcallv,0); + gen(op_mcallv,0,ast.get_line()); return; } @@ -588,7 +591,7 @@ void nasal_codegen::mcall_hash(nasal_ast& ast) { std::string& str=ast.get_str(); regist_string(str); - gen(op_mcallh,string_table[str]); + gen(op_mcallh,string_table[str],ast.get_line()); return; } @@ -597,7 +600,7 @@ void nasal_codegen::single_def(nasal_ast& ast) std::string& str=ast.get_children()[0].get_str(); add_sym(str); calc_gen(ast.get_children()[1]); - local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str)); + local.empty()?gen(op_loadg,global_find(str),ast.get_line()):gen(op_loadl,local_find(str),ast.get_line()); return; } void nasal_codegen::multi_def(nasal_ast& ast) @@ -612,7 +615,7 @@ void nasal_codegen::multi_def(nasal_ast& ast) calc_gen(vals[i]); std::string& str=ids[i].get_str(); add_sym(str); - local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str)); + local.empty()?gen(op_loadg,global_find(str),ids[i].get_line()):gen(op_loadl,local_find(str),ids[i].get_line()); } } else @@ -620,12 +623,12 @@ void nasal_codegen::multi_def(nasal_ast& ast) calc_gen(ast.get_children()[1]); for(int i=0;i