From 4ccbce36ce43ff22e6047d9cc01c66eaea063aea Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Tue, 3 Mar 2020 10:05:28 +0800 Subject: [PATCH] update --- version2.0/nasal.h | 2 - version2.0/nasal_gc.h | 194 ++++++++++++++++++++++++++++++++++++- version2.0/nasal_lexer.h | 8 +- version2.0/nasal_parse.h | 1 + version2.0/nasal_runtime.h | 106 ++++++++++---------- 5 files changed, 250 insertions(+), 61 deletions(-) diff --git a/version2.0/nasal.h b/version2.0/nasal.h index 46d3cef..bc0520d 100644 --- a/version2.0/nasal.h +++ b/version2.0/nasal.h @@ -17,9 +17,7 @@ #include "nasal_ast.h" #include "nasal_lexer.h" #include "nasal_parse.h" -#include "nasal_scalar.h" #include "nasal_gc.h" -#include "nasal_sym.h" #include "nasal_runtime.h" #endif \ No newline at end of file diff --git a/version2.0/nasal_gc.h b/version2.0/nasal_gc.h index 0e4b09d..e8a4cac 100644 --- a/version2.0/nasal_gc.h +++ b/version2.0/nasal_gc.h @@ -1,6 +1,49 @@ #ifndef __NASAL_GC_H__ #define __NASAL_GC_H__ +class nasal_function +{ + private: + std::list > local_scope; + abstract_syntax_tree function_root; + public: + nasal_function(); + ~nasal_function(); + void set_clear(); + void set_statement_block(abstract_syntax_tree&); + nasal_function& operator=(const nasal_function&); + std::list >& get_local_scope(); + abstract_syntax_tree& get_statement_block(); +}; + +class nasal_scalar +{ + private: + int type; + std::string var_string; + double var_number; + std::vector var_array; + std::map var_hash; + nasal_function var_func; + public: + nasal_scalar(); + nasal_scalar(const nasal_scalar&); + nasal_scalar& operator=(const nasal_scalar&); + void set_clear(); + void set_type(int); + void set_number(double); + void set_string(std::string&); + void vector_push(int); + int vector_pop(); + void hash_add_new_member(std::string,int); + void hash_delete_member(std::string); + int hash_get_member(std::string); + int get_type(); + double get_number(); + std::string get_string(); + nasal_function& get_function(); +}; + struct gc_unit { // collected: If gc collected this item,it'll be set to true.Otherwise it is false. @@ -68,6 +111,8 @@ class gc_manager for(int i=0;i> [Gc] collected "; @@ -144,7 +189,154 @@ class gc_manager } }; gc_manager nasal_gc; -// this object is used in "nasal_sym.h" and "nasal_runtime.h" +// this object is used in "nasal_runtime.h" // because there must be only one gc when running a program +nasal_function::nasal_function() +{ + local_scope.clear(); + function_root.set_clear(); + return; +} +nasal_function::~nasal_function() +{ + local_scope.clear(); + function_root.set_clear(); + return; +} +void nasal_function::set_clear() +{ + local_scope.clear(); + function_root.set_clear(); + return; +} +void nasal_function::set_statement_block(abstract_syntax_tree& func_block) +{ + function_root=func_block; + return; +} +nasal_function& nasal_function::operator=(const nasal_function& tmp) +{ + local_scope=tmp.local_scope; + function_root=tmp.function_root; + return *this; +} +std::list >& nasal_function::get_local_scope() +{ + return local_scope; +} +abstract_syntax_tree& nasal_function::get_statement_block() +{ + return function_root; +} + +nasal_scalar::nasal_scalar() +{ + type=scalar_nil; + var_string=""; + var_number=0; + var_array.clear(); + var_hash.clear(); + return; +} +nasal_scalar::nasal_scalar(const nasal_scalar& tmp) +{ + type=tmp.type; + var_string=tmp.var_string; + var_number=tmp.var_number; + var_array =tmp.var_array; + var_hash =tmp.var_hash; + var_func =tmp.var_func; + return; +} +nasal_scalar& nasal_scalar::operator=(const nasal_scalar& tmp) +{ + type=tmp.type; + var_string=tmp.var_string; + var_number=tmp.var_number; + var_array =tmp.var_array; + var_hash =tmp.var_hash; + var_func =tmp.var_func; + return *this; +} +void nasal_scalar::set_clear() +{ + type=scalar_nil; + var_string.clear(); + var_number=0; + var_array.clear(); + var_hash.clear(); + var_func.set_clear(); + return; +} +void nasal_scalar::set_type(int tmp_type) +{ + // scalar_function is the last enum in enum::scalar_type + type=tmp_type>scalar_function? scalar_nil:tmp_type; + return; +} +void nasal_scalar::set_number(double tmp_number) +{ + var_number=tmp_number; + return; +} +void nasal_scalar::set_string(std::string& tmp_str) +{ + var_string=tmp_str; + return; +} +void nasal_scalar::vector_push(int addr) +{ + var_array.push_back(addr); + return; +} +int nasal_scalar::vector_pop() +{ + int ret=var_array.back(); + var_array.pop_back(); + return ret; +} +void nasal_scalar::hash_add_new_member(std::string member_name,int addr) +{ + // if hash has a new function member + // this function will get a value named 'me' in its own scope + // and the 'me' points to the hash that has the function + if(var_hash.find(member_name)!=var_hash.end()) + std::cout<<">> [Runtime] "<> [Lexer-error] line "<> [Lexer] line "<> [Lexer-error] line "<> [Lexer] line "<> [Lexer-error] line "<> [Lexer] line "<> [Lexer-error] line "<str<<"\'."<> [Lexer] line "<str<<"\'."<get_token(); if((this_token.type==__sub_operator) || (this_token.type==__nor_operator)) { + // unary calculation calc_node.set_clear(); calc_node.set_node_line(this_token.line); calc_node.set_node_type(this_token.type); diff --git a/version2.0/nasal_runtime.h b/version2.0/nasal_runtime.h index 162838f..309b24f 100644 --- a/version2.0/nasal_runtime.h +++ b/version2.0/nasal_runtime.h @@ -5,7 +5,7 @@ class nasal_runtime { private: // local hash_map will be used when running - sym_hash_map global_scope; + std::list > global_scope; // see detail of each enum type in function error_interrupt(const int) enum runtime_error_type @@ -14,26 +14,26 @@ class nasal_runtime __incorrect_head_of_func, __stack_overflow, }; - void error_interrupt (const int); - void delete_total_scope(std::list >&); - void delete_last_scope (std::list >&); - void func_proc (std::list >&,abstract_syntax_tree&); - void call_identifier (abstract_syntax_tree&); - void calculation (abstract_syntax_tree&); - void assignment (abstract_syntax_tree&); - void definition (abstract_syntax_tree&); - void loop (abstract_syntax_tree&); - void conditional (abstract_syntax_tree&); + void error_interrupt (const int); + void vector_generation (abstract_syntax_tree&); + void hash_generation (abstract_syntax_tree&); + void call_identifier (abstract_syntax_tree&); + void calculation (abstract_syntax_tree&); + void assignment (abstract_syntax_tree&); + void definition (abstract_syntax_tree&); + void loop_expr (abstract_syntax_tree&); + void conditional (abstract_syntax_tree&); + void func_proc (std::list >&,abstract_syntax_tree&); public: nasal_runtime() { - global_scope.set_clear(); + global_scope.clear(); nasal_gc.gc_init(); return; } ~nasal_runtime() { - global_scope.set_clear(); + global_scope.clear(); nasal_gc.gc_init(); return; } @@ -56,24 +56,36 @@ void nasal_runtime::error_interrupt(const int type) return; } -void nasal_runtime::delete_total_scope(std::list >& scope) +void nasal_runtime::vector_generation(abstract_syntax_tree& node) { - for(std::list >::iterator i=scope.begin();i!=scope.end();++i) - for(std::map::iterator j=i->begin();j!=i->end();++j) - nasal_gc.reference_delete(j->second); - scope.clear(); return; } - -void nasal_runtime::delete_last_scope(std::list >& scope) +void nasal_runtime::hash_generation(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::call_identifier(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::calculation(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::assignment(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::definition(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::loop_expr(abstract_syntax_tree& node) +{ + return; +} +void nasal_runtime::conditional(abstract_syntax_tree& node) { - if(scope.empty()) - return; - std::list >::iterator iter=scope.end(); - --iter; - for(std::map::iterator i=iter->begin();i!=iter->end();++i) - nasal_gc.reference_delete(i->second); - scope.pop_back(); return; } @@ -92,11 +104,11 @@ void nasal_runtime::func_proc(std::list >& local_scope ; // only number or string else if(node_type==__id) - ; + this->call_identifier(*iter); else if(node_type==__vector) - ; + this->vector_generation(*iter); else if(node_type==__hash) - ; + this->hash_generation(*iter); else if(node_type==__function) ; else if(node_type==__add_operator || node_type==__sub_operator || @@ -114,15 +126,7 @@ void nasal_runtime::func_proc(std::list >& local_scope ; else if(node_type==__conditional) ; - else if(node_type==__while) - ; - else if(node_type==__for) - ; - else if(node_type==__foreach) - ; - else if(node_type==__forindex) - ; - else if(node_type==__return) + else if((node_type==__while) || (node_type==__for) || (node_type==__foreach) || (node_type==__forindex)) ; } return; @@ -132,7 +136,7 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root) { time_t begin_time,end_time; begin_time=std::time(NULL); - global_scope.set_clear(); + global_scope.clear(); nasal_gc.gc_init(); if(root.get_node_type()!=__root) { @@ -146,16 +150,16 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root) if(node_type==__number || node_type==__string) ; else if(node_type==__id) - { - for(std::list::iterator i=iter->get_children().begin();i!=iter->get_children().end();++i) - ; - } + this->call_identifier(*iter); else if(node_type==__vector) - ; + this->vector_generation(*iter); else if(node_type==__hash) - ; + this->hash_generation(*iter); else if(node_type==__function) - ; + { + nasal_scalar temp_function; + temp_function.set_type(scalar_function); + } else if(node_type==__add_operator || node_type==__sub_operator || node_type==__mul_operator || node_type==__div_operator || node_type==__link_operator || @@ -171,13 +175,7 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root) ; else if(node_type==__conditional) ; - else if(node_type==__while) - ; - else if(node_type==__for) - ; - else if(node_type==__foreach) - ; - else if(node_type==__forindex) + else if((node_type==__while) || (node_type==__for) || (node_type==__foreach) || (node_type==__forindex)) ; }