From 580a5b264d140c5d1db5d68c3e87f14809c35a2e Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 27 Jul 2020 02:30:39 -0700 Subject: [PATCH] update --- version3.0/nasal_enum.h | 11 +++++ version3.0/nasal_gc.h | 98 +++++++++++++++++++++++++++++++++++++---- version3.0/nasal_vm.h | 16 +++---- 3 files changed, 109 insertions(+), 16 deletions(-) diff --git a/version3.0/nasal_enum.h b/version3.0/nasal_enum.h index 671fb54..21c1100 100644 --- a/version3.0/nasal_enum.h +++ b/version3.0/nasal_enum.h @@ -170,4 +170,15 @@ void error_info(int line,int error_type,std::string error_str="") return; } +enum runtime_scalar_type +{ + vm_nil=0, + vm_number, + vm_string, + vm_closure, + vm_function, + vm_vector, + vm_hash +}; + #endif \ No newline at end of file diff --git a/version3.0/nasal_gc.h b/version3.0/nasal_gc.h index bbd7419..5a6c90c 100644 --- a/version3.0/nasal_gc.h +++ b/version3.0/nasal_gc.h @@ -3,12 +3,55 @@ #define GC_BLK_SIZE 128 -class nasal_number; -class nasal_string; -class nasal_vector; -class nasal_hash; -class nasal_function; -class nasal_closure; +class nasal_vector +{ +private: + // this int points to the space in nasal_vm_memory_manager + std::vector elems; +public: + nasal_vector() + { + elems.clear(); + return; + } +}; +class nasal_hash +{ +private: + // this int points to the space in nasal_vm_memory_manager + std::map elems; +public: + nasal_hash() + { + elems.clear(); + return; + } +}; +class nasal_function +{ +private: + // this int points to the space in nasal_vm_memory_manager + std::list closures; + nasal_ast function_tree; +public: + nasal_function() + { + closures.clear(); + function_tree.clear(); + return; + } +}; +class nasal_closure +{ +private: + std::map elems; +public: + nasal_closure() + { + elems.clear(); + return; + } +}; class nasal_scalar { @@ -18,10 +61,49 @@ private: public: nasal_scalar() { - type=0; - scalar_ptr=(void*)NULL; + this->type=vm_nil; + this->scalar_ptr=(void*)NULL; return; } + ~nasal_scalar() + { + switch(this->type) + { + case vm_nil:break; + case vm_number: delete (double*)(this->scalar_ptr); break; + case vm_string: delete (std::string*)(this->scalar_ptr); break; + case vm_vector: delete (nasal_vector*)(this->scalar_ptr); break; + case vm_hash: delete (nasal_hash*)(this->scalar_ptr); break; + case vm_function: delete (nasal_function*)(this->scalar_ptr); break; + case vm_closure: delete (nasal_closure*)(this->scalar_ptr); break; + } + return; + } + bool set_type(int nasal_scalar_type) + { + bool ret=true; + this->type=nasal_scalar_type; + switch(nasal_scalar_type) + { + case vm_nil: this->scalar_ptr=(void*)NULL; break; + case vm_number: this->scalar_ptr=(void*)(new double); break; + case vm_string: this->scalar_ptr=(void*)(new std::string); break; + case vm_vector: this->scalar_ptr=(void*)(new nasal_vector); break; + case vm_hash: this->scalar_ptr=(void*)(new nasal_hash); break; + case vm_function: this->scalar_ptr=(void*)(new nasal_function); break; + case vm_closure: this->scalar_ptr=(void*)(new nasal_closure); break; + default: + std::cout<<">> [scalar] error scalar type: "<type=vm_nil; + this->scalar_ptr=(void*)NULL; + ret=false; + break; + } + return ret; + } + // +-*/~ + // = + // unary - ! }; struct gc_unit diff --git a/version3.0/nasal_vm.h b/version3.0/nasal_vm.h index 160f9a5..39fc105 100644 --- a/version3.0/nasal_vm.h +++ b/version3.0/nasal_vm.h @@ -3,26 +3,26 @@ #define MEM_BLK_SIZE 128 -class nasal_vm +class nasal_vm_memory_manager { private: std::queue free_space; std::vector memory; public: - nasal_vm(); - ~nasal_vm(); + nasal_vm_memory_manager(); + ~nasal_vm_memory_manager(); int nas_alloc(); int nas_free(int); int nas_store(int,int); }; -nasal_vm::nasal_vm() +nasal_vm_memory_manager::nasal_vm_memory_manager() { memory.clear(); return; } -nasal_vm::~nasal_vm() +nasal_vm_memory_manager::~nasal_vm_memory_manager() { int size=memory.size(); for(int i=0;i