From 86fd5169151cbde0c49646359ec4bc6c9051ba68 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Wed, 9 Sep 2020 02:51:31 -0700 Subject: [PATCH] update --- version3.0/nasal.h | 1 + version3.0/nasal_builtin.h | 37 +++++++ version3.0/nasal_gc.h | 73 ++++++++++---- version3.0/nasal_parse.h | 5 +- version3.0/nasal_runtime.h | 199 ++++++++++++++++++++++++++----------- 5 files changed, 233 insertions(+), 82 deletions(-) create mode 100644 version3.0/nasal_builtin.h diff --git a/version3.0/nasal.h b/version3.0/nasal.h index d4ec429..ae18326 100644 --- a/version3.0/nasal.h +++ b/version3.0/nasal.h @@ -24,5 +24,6 @@ #include "nasal_parse.h" #include "nasal_gc.h" #include "nasal_runtime.h" +#include "nasal_builtin.h" #endif diff --git a/version3.0/nasal_builtin.h b/version3.0/nasal_builtin.h new file mode 100644 index 0000000..96e7cc7 --- /dev/null +++ b/version3.0/nasal_builtin.h @@ -0,0 +1,37 @@ +#ifndef __NASAL_BUILTIN_H__ +#define __NASAL_BUILTIN_H__ + +int nasal_runtime::builtin_print(int local_scope_addr) +{ + int vector_value_addr=-1; + if(local_scope_addr>=0) + vector_value_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("dyn"); + if(vector_value_addr<0 || nasal_vm.gc_get(vector_value_addr).get_type()!=vm_vector) + { + std::cout<<">> [runtime] builtin_print: cannot find values or wrong value type."< memory_manager_memory; public: ~nasal_virtual_machine(); + void clear(); int gc_alloc(); // garbage collector gives a new space nasal_scalar& gc_get(int); // get scalar that stored in gc int add_reference(int); @@ -166,6 +159,11 @@ public: */ nasal_virtual_machine nasal_vm; nasal_scalar nasal_scalar_calculator; +// error values set here,if defined before nasal_vm,SIGSEGV will occur. +nasal_vector error_vector; +nasal_hash error_hash; +nasal_function error_function; +nasal_closure error_closure; /*functions of nasal_vector*/ nasal_vector::nasal_vector() @@ -337,7 +335,6 @@ void nasal_hash::deepcopy(nasal_hash& tmp) /*functions of nasal_function*/ nasal_function::nasal_function() { - is_builtin=false; closure_addr=-1; argument_list.clear(); function_expr.clear(); @@ -351,17 +348,10 @@ nasal_function::~nasal_function() function_expr.clear(); return; } -void nasal_function::set_builtin_func() -{ - is_builtin=true; - return; -} -bool nasal_function::check_builtin() -{ - return is_builtin; -} void nasal_function::set_closure_addr(int value_address) { + if(closure_addr>=0) + nasal_vm.del_reference(closure_addr); closure_addr=value_address; return; } @@ -423,8 +413,39 @@ void nasal_closure::del_scope() { if(this->elems.empty()) return; + // make closure here + // functions in different scope but the same closure will get different copies of closures + // functions in the same scope will get the same copy of closure,when running these functions,one closure will be changed,make sure you are using it correctly! + // please notice this,otherwise your programme may not work correctly + int closure=nasal_vm.gc_alloc(); + nasal_vm.gc_get(closure).set_type(vm_closure); + nasal_closure& new_closure=nasal_vm.gc_get(closure).get_closure(); + new_closure.deepcopy(*this); + + std::vector func_name; + for(std::map::iterator i=new_closure.elems.back().begin();i!=new_closure.elems.back().end();++i) + { + int value_addr=nasal_vm.mem_get(i->second); + if(nasal_vm.gc_get(value_addr).get_type()==vm_function) + { + func_name.push_back(i->first); + nasal_vm.mem_free(i->second); // mem_free will delete the reference of mem_get(i->second) + } + } + for(int i=0;i::iterator i=this->elems.back().begin();i!=this->elems.back().end();++i) + { + int value_addr=nasal_vm.mem_get(i->second); + if(nasal_vm.gc_get(value_addr).get_type()==vm_function) + { + nasal_vm.gc_get(value_addr).get_func().set_closure_addr(closure); + nasal_vm.add_reference(closure); + } nasal_vm.mem_free(i->second); + } + nasal_vm.del_reference(closure); this->elems.pop_back(); return; } @@ -1365,6 +1386,22 @@ nasal_virtual_machine::~nasal_virtual_machine() memory_manager_memory.clear(); return; } +void nasal_virtual_machine::clear() +{ + int gc_mem_size=garbage_collector_memory.size(); + int mm_mem_size=memory_manager_memory.size(); + for(int i=0;ierror=0; + this->global_scope_address=nasal_vm.gc_alloc(); nasal_vm.gc_get(global_scope_address).set_type(vm_closure); nasal_vm.gc_get(global_scope_address).get_closure().add_scope(); - begin_time=std::time(NULL); - returned_statement=main_progress(); - end_time=std::time(NULL); + + time_t begin_time=std::time(NULL); + int returned_statement=main_progress(); + time_t end_time=std::time(NULL); + nasal_vm.gc_get(global_scope_address).get_closure().del_scope(); nasal_vm.del_reference(global_scope_address); - std::cout<<">> [runtime] process exited after "<> [runtime] process exited after "<=0) - { - // codes here make closure - nasal_vm.add_reference(local_scope_addr); - ref_of_this_function.set_closure_addr(local_scope_addr); - } - else - { - int new_closure_addr=nasal_vm.gc_alloc(); - nasal_vm.gc_get(new_closure_addr).set_type(vm_closure); - ref_of_this_function.set_closure_addr(new_closure_addr); - } + + int new_closure=nasal_vm.gc_alloc(); + nasal_vm.gc_get(new_closure).set_type(vm_closure); + ref_of_this_function.set_closure_addr(new_closure); return new_addr; } int nasal_runtime::main_progress() @@ -203,7 +197,8 @@ int nasal_runtime::main_progress() case ast_conditional:ret_state=conditional_progress(root.get_children()[i],-1,false);break; case ast_while:case ast_for:case ast_forindex:case ast_foreach: ret_state=loop_progress(root.get_children()[i],-1,false);break; - case ast_number:case ast_string:break; + case ast_number:case ast_string:case ast_function:break; + case ast_vector:case ast_hash: case ast_call: case ast_add_equal:case ast_sub_equal:case ast_mult_equal:case ast_div_equal:case ast_link_equal: case ast_unary_sub:case ast_unary_not: @@ -239,7 +234,10 @@ int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr,bool allo nasal_vm.gc_get(local_scope_addr).get_closure().add_scope(); } else + { nasal_vm.add_reference(local_scope_addr); + nasal_vm.gc_get(local_scope_addr).get_closure().add_scope(); + } int expr_number=node.get_children().size(); int process_returned_value_addr=-1; for(int i=0;i> [runtime] return expression is not allowed here."<> [runtime] block_progress: return expression is not allowed here."<> [runtime] before_for_loop: cannot use this expression before for-loop."<> [runtime] before_for_loop: cannot use this expression before for-loop."<=0) + return value_address; + } + if(value_address<0) + { std::cout<<">> [runtime] call_nasal_scalar: cannot find value named \'"<=0) + { + nasal_vm.add_reference(last_call_hash_addr); + run_closure.add_new_value("me",last_call_hash_addr); + } nasal_ast& argument_format=reference_of_func.get_arguments(); if(!node.get_children().size()) { @@ -1002,7 +1021,15 @@ int nasal_runtime::call_function(nasal_ast& node,int base_value_addr,int last_ca } } } - block_progress(reference_of_func.get_run_block(),run_closure_addr,true); + int ret_state=block_progress(reference_of_func.get_run_block(),run_closure_addr,true); + if(ret_state==rt_break || ret_state==rt_continue) + { + std::cout<<">> [runtime] call_function: break and continue are not allowed to be used here."<=0) @@ -1017,9 +1044,44 @@ int nasal_runtime::call_function(nasal_ast& node,int base_value_addr,int last_ca } return ret_value_addr; } +int nasal_runtime::call_builtin_function(nasal_ast& node,int local_scope_addr) +{ + int ret_value_addr=-1; + int builtin_num=-1; + std::string builtin_name=node.get_str(); + for(int i=0;i=0) + mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(node.get_str()); + if(mem_address<0) + mem_address=nasal_vm.gc_get(global_scope_address).get_closure().get_mem_address(node.get_str()); + if(mem_address<0) + { + std::cout<<">> [runtime] call_scalar_mem: cannot find value named \'"<=0) mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(node.get_children()[0].get_str()); if(mem_address<0) @@ -1134,16 +1196,35 @@ int nasal_runtime::calculation(nasal_ast& node,int local_scope_addr) // after this process, a new address(in nasal_vm.garbage_collector_memory) will be returned int ret_address=-1; int calculation_type=node.get_type(); - if(calculation_type==ast_number) + if(calculation_type==ast_nil) + { + ret_address=nasal_vm.gc_alloc(); + nasal_vm.gc_get(ret_address).set_type(vm_nil); + } + else if(calculation_type==ast_number) ret_address=number_generation(node); else if(calculation_type==ast_string) ret_address=string_generation(node); + else if(calculation_type==ast_identifier) + { + if(local_scope_addr>=0) + ret_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address(node.get_str()); + if(ret_address<0) + ret_address=nasal_vm.gc_get(global_scope_address).get_closure().get_value_address(node.get_str()); + if(ret_address<0) + { + std::cout<<">> [runtime] calculation: cannot find value named \'"<> [runtime] calculation: this expression cannot be calculated."<> [runtime] calculation: this expression cannot be calculated.expression type:"<> [runtime] definition: must use vector in multi-definition."<