From f6a6ea4eb1eef85171e7b051d2a737a4e7747d51 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Tue, 19 Nov 2019 22:54:26 +0800 Subject: [PATCH] Add print --- balloon/abstract_syntax_tree.cpp | 116 ++++++++++++++++++++----------- balloon/balloon_lexer.h | 5 ++ balloon/lib/io.nas | 7 ++ 3 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 balloon/lib/io.nas diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index adf3461..5134364 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -707,18 +707,10 @@ void abstract_syntax_tree::run_root() else if(t.get_type()==__var_string) std::cout<type==__number) - std::cout<number<type==__string) - std::cout<str<type==__number || i->type==__string) + ; else if(i->type==__id) - { - var t=i->call_identifier(); - if(t.get_type()==__var_number) - std::cout<call_identifier(); else if(i->type==__while) { scope.add_new_block_scope(); @@ -745,7 +737,7 @@ void abstract_syntax_tree::run_root() break; } end_time=time(NULL); - std::cout<<"------------------------------------------------------------------------------"<>[Runtime] process exited after "< parameter,var self_func) std::list::iterator para_name=para.children.begin(); std::list::iterator para_value=parameter.begin(); - for(;para_name!=para.children.end();++para_name,++para_value) + + if(self_func.get_name()=="print") { - if(para_value==parameter.end() && para_name!=para.children.end()) + std::string temp_str; + for(std::list::iterator i=parameter.begin();i!=parameter.end();++i) { - exit_type=__lack_parameter; - std::cout<<">>[Runtime-error] line "<get_type()) + { + case __null_type:std::cout<<">>[Runtime-error] cannot print null value."<get_number();break; + case __var_string: + temp_str=i->get_string(); + for(int j=0;j>[Runtime-error] cannot print array value."<>[Runtime-error] cannot print hash value."<>[Runtime-error] cannot print function."<>[Runtime-error] cannot print unknown value."<name); - scope.add_new_var(new_var); - } - if(exit_type==__process_exited_successfully) - { - int _t=blk.run_block(); - // in case the exit_type is not __process_exited_successfully in run_block - // or the function does not have return value; - if(_t!=__return) - ret_stack.push(error_var); + ret.set_type(__var_number); + if(exit_type!=__error_value_type) + ret.set_number(1); + else + ret.set_number(0); + ret_stack.push(ret); } else - ret_stack.push(error_var); + { + for(;para_name!=para.children.end();++para_name,++para_value) + { + if(para_value==parameter.end() && para_name!=para.children.end()) + { + exit_type=__lack_parameter; + std::cout<<">>[Runtime-error] line "<name); + scope.add_new_var(new_var); + } + if(exit_type==__process_exited_successfully) + { + int _t=blk.run_block(); + // in case the exit_type is not __process_exited_successfully in run_block + // or the function does not have return value; + if(_t!=__return) + ret_stack.push(error_var); + } + else + ret_stack.push(error_var); + } //get return scope.pop_last_block_scope(); @@ -901,18 +943,10 @@ int abstract_syntax_tree::run_block() exit_type=__redeclaration; } } - else if(i->type==__number) - std::cout<number<type==__string) - std::cout<str<type==__number || i->type==__string) + ; else if(i->type==__id) - { - var t=i->call_identifier(); - if(t.get_type()==__var_number) - std::cout<call_identifier(); else if(i->type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal) i->assignment(); else if(i->type==__add_operator || i->type==__sub_operator || i->type==__mul_operator || i->type==__div_operator || i->type==__link_operator || i->type==__or_operator || i->type==__and_operator || i->type==__nor_operator) diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index 45b5cf7..e7b5864 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -96,6 +96,11 @@ class resource_file std::cout<<">>[Resource] lack lib file: lib/math.nas ."<>[Resource] lack lib file: lib/io.nas ."<