From b6bd0d4c763be7bcb98ca800933a40c107bec334 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Thu, 31 Oct 2019 00:01:37 +0800 Subject: [PATCH] update --- nasal_runtime.h | 493 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 493 insertions(+) create mode 100644 nasal_runtime.h diff --git a/nasal_runtime.h b/nasal_runtime.h new file mode 100644 index 0000000..b1efd84 --- /dev/null +++ b/nasal_runtime.h @@ -0,0 +1,493 @@ +#ifndef __NASAL_RUNTIME_H__ +#define __NASAL_RUNTIME_H__ + +class var +{ + private: + int type; + std::string name; + double number; + std::string str; + std::list var_list; + std::list var_hash; + abstract_syntax_tree function; + public: + var() + { + type=0; + name=""; + number=0; + str=""; + function.set_clear(); + } + var(const var& p) + { + type=p.type; + name=p.name; + number=p.number; + str=p.str; + function=p.function; + } + var& operator=(const var& p) + { + type=p.type; + name=p.name; + number=p.number; + str=p.str; + function=p.function; + return *this; + } + void print_information() + { + std::cout<<"[ type: ";print_token(type);std::cout<<" ]"; + std::cout<<"[ name: "< p) + { + var_list=p; + return; + } + void append_list(var p) + { + var_list.push_back(p); + return; + } + void set_hash(std::list p) + { + var_hash=p; + return; + } + void append_hash(var p) + { + var_hash.push_back(p); + return; + } + int get_type() + { + return type; + } + std::string get_name() + { + return name; + } + double get_number() + { + return number; + } + std::string get_string() + { + return str; + } + abstract_syntax_tree get_function() + { + return function; + } + std::list get_list() + { + return var_list; + } + std::list get_hash() + { + return var_hash; + } +}; + +class var_scope_manager +{ + private: + std::list> scope_list; + var error_var; + public: + var_scope_manager() + { + scope_list.clear(); + std::string str="__nas_strc_error_ret"; + error_var.set_name(str); + error_var.set_type(__null_type); + return; + } + void set_clear() + { + scope_list.clear(); + return; + } + var& search_var(std::string str) + { + for(std::list>::iterator i=scope_list.begin();i!=scope_list.end();++i) + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->get_name()==str) + return *j; + return error_var; + } + void add_var(var new_var) + { + for(std::list>::iterator i=scope_list.begin();i!=scope_list.end();++i) + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->get_name()==new_var.get_name()) + { + std::cout<<">>[Runtime] redeclaration of var '"<>::iterator i=scope_list.end(); + --i; + i->push_back(new_var); + } + else + std::cout<<">>[Runtime] empty scope list."< new_list; + scope_list.push_back(new_list); + return; + } + void pop_last_scope() + { + if(!scope_list.empty()) + scope_list.pop_back(); + else + std::cout<<">>[Runtime] scope poped empty thing."< parameters; + var error_var; + public: + parameter_list() + { + parameters.clear(); + std::string str="__nas_strc_error_ret"; + error_var.set_name(str); + error_var.set_type(__null_type); + return; + } + parameter_list(const parameter_list& p) + { + parameters=p.parameters; + error_var=p.error_var; + return; + } + void set_clear() + { + parameters.clear(); + return; + } + var& search_var(std::string str) + { + // parameters have nothing to do with scope_list so + // if there is a var whose name appeared in scope_list, + // the var will be called only in this function area. + for(std::list::iterator i=parameters.begin();i!=parameters.end();++i) + if(i->get_name()==str) + return *i; + return error_var; + } + void add_var(var new_var) + { + for(std::list::iterator i=parameters.begin();i!=parameters.end();++i) + if(i->get_name()==new_var.get_name()) + { + std::cout<<">>[Runtime] redeclaration of var '"<>[Runtime] process begins."<>[Runtime] process exited after "<>[Runtime] runtime got the ast-root: "<<((void *)&tree)<<"->"<<((void *)&root)<<"."<::iterator i=tree.get_children().begin();i!=tree.get_children().end();++i) + { + switch(i->get_type()) + { + case __add_operator: + case __sub_operator: + case __mul_operator: + case __div_operator: + case __link_operator: + case __and_operator: + case __or_operator: + case __nor_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_less_or_equal: + case __cmp_more: + case __cmp_more_or_equal: + run_calculation(*i); + break; + case __definition: + run_definition(*i); + break; + case __assignment: + run_assignment(*i); + break; + case __while: + case __for: + case __forindex: + case __foreach: + run_loop(*i); + break; + case __ifelse: + run_if_else(*i); + break; + case __call_function: + run_function(*i); + break; + case __id: + case __hash_search: + case __list_search: + identifier_call(*i,null_para); + break; + case __number: + case __string: + scalar_call(*i); + break; + default: + std::cout<<">>[Debug] error occurred."<::iterator iter=tree.get_children().begin(); + std::string var_name=iter->get_var_name(); + ++iter; + if(iter==tree.get_children().end()) + { + new_var.set_type(__null_type); + new_var.set_name(var_name); + scope.add_var(new_var); + return; + } + else + { + switch(iter->get_type()) + { + case __add_operator: + case __sub_operator: + case __mul_operator: + case __div_operator: + case __link_operator: + case __and_operator: + case __or_operator: + case __nor_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_less_or_equal: + case __cmp_more: + case __cmp_more_or_equal: + new_var=run_calculation(*iter); + break; + case __call_function: + new_var=run_function(*iter); + break; + case __id: + case __hash_search: + case __list_search: + break; + case __number: + case __string: + new_var=scalar_call(*iter); + break; + case __function: + new_var.set_type(__function); + new_var.set_function(*iter); + break; + case __list: + break; + case __hash: + break; + default: + std::cout<<">>[Debug] error occurred."<