update
This commit is contained in:
parent
b701637db6
commit
3b50f251c9
|
@ -66,7 +66,7 @@ void par_func()
|
|||
return;
|
||||
}
|
||||
|
||||
void ast_func()
|
||||
void ast_print()
|
||||
{
|
||||
lexer.scanner(resource.get_file());
|
||||
if(!lexer.get_error())
|
||||
|
@ -153,7 +153,7 @@ int main()
|
|||
else if(command=="par")
|
||||
par_func();
|
||||
else if(command=="ast")
|
||||
ast_func();
|
||||
ast_print();
|
||||
else if(command=="run")
|
||||
runtime_start();
|
||||
else if(command=="logo")
|
||||
|
|
|
@ -31,10 +31,15 @@ class nasal_function
|
|||
private:
|
||||
// this int points to the space in nasal_vm::garbage_collector_memory
|
||||
int closure_addr;
|
||||
nasal_ast function_tree;
|
||||
nasal_ast argument_list;
|
||||
nasal_ast function_expr;
|
||||
public:
|
||||
nasal_function();
|
||||
~nasal_function();
|
||||
void set_closure_addr(int);
|
||||
int get_closure_addr();
|
||||
void set_arguments(nasal_ast&);
|
||||
void set_run_block(nasal_ast&);
|
||||
};
|
||||
class nasal_closure
|
||||
{
|
||||
|
@ -111,7 +116,7 @@ public:
|
|||
int mem_alloc(); // memory gives a new space
|
||||
int mem_free(int); // give space back to memory
|
||||
int mem_change(int,int); // change value in memory space
|
||||
int mem_store(int,int); // init value in memory space
|
||||
int mem_init(int,int); // init value in memory space
|
||||
int mem_get(int); // get value in memory space
|
||||
};
|
||||
|
||||
|
@ -167,13 +172,21 @@ nasal_hash::~nasal_hash()
|
|||
}
|
||||
void nasal_hash::add_elem(std::string key,int memory_address)
|
||||
{
|
||||
if(elems.find(key)==elems.end())
|
||||
elems[key]=memory_address;
|
||||
else
|
||||
std::cout<<">> [vm] nasal_hash::add_elem: "<<key<<" already exists."<<std::endl;
|
||||
return;
|
||||
}
|
||||
void nasal_hash::del_elem(std::string key)
|
||||
{
|
||||
if(elems.find(key)!=elems.end())
|
||||
{
|
||||
nasal_vm.mem_free(elems[key]);
|
||||
elems.erase(key);
|
||||
}
|
||||
else
|
||||
std::cout<<">> [vm] nasal_hash::del_elem: "<<key<<" does not exist."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -181,13 +194,34 @@ void nasal_hash::del_elem(std::string key)
|
|||
nasal_function::nasal_function()
|
||||
{
|
||||
closure_addr=-1;
|
||||
function_tree.clear();
|
||||
argument_list.clear();
|
||||
function_expr.clear();
|
||||
return;
|
||||
}
|
||||
nasal_function::~nasal_function()
|
||||
{
|
||||
nasal_vm.del_reference(closure_addr);
|
||||
function_tree.clear();
|
||||
argument_list.clear();
|
||||
function_expr.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_function::set_closure_addr(int address)
|
||||
{
|
||||
closure_addr=address;
|
||||
return;
|
||||
}
|
||||
int nasal_function::get_closure_addr()
|
||||
{
|
||||
return this->closure_addr;
|
||||
}
|
||||
void nasal_function::set_arguments(nasal_ast& node)
|
||||
{
|
||||
argument_list=node;
|
||||
return;
|
||||
}
|
||||
void nasal_function::set_run_block(nasal_ast& node)
|
||||
{
|
||||
function_expr=node;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -209,7 +243,7 @@ void nasal_closure::add_scope()
|
|||
int hash_address=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(hash_address).set_type(vm_hash);
|
||||
int scope_memory_address=nasal_vm.mem_alloc();
|
||||
nasal_vm.mem_store(scope_memory_address,hash_address);
|
||||
nasal_vm.mem_init(scope_memory_address,hash_address);
|
||||
this->elems.push_back(scope_memory_address);
|
||||
return;
|
||||
}
|
||||
|
@ -1025,7 +1059,7 @@ int nasal_virtual_machine::mem_change(int memory_address,int value_space)
|
|||
}
|
||||
return 1;
|
||||
}
|
||||
int nasal_virtual_machine::mem_store(int memory_address,int value_space)
|
||||
int nasal_virtual_machine::mem_init(int memory_address,int value_space)
|
||||
{
|
||||
// this progress is used to init a memory space
|
||||
// be careful! this process doesn't check if this mem_space is in use.
|
||||
|
|
|
@ -391,6 +391,11 @@ nasal_ast nasal_parse::func_gen()
|
|||
node.add_child(args_list_gen());
|
||||
++ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
nasal_ast null_argument_list;
|
||||
node.add_child(null_argument_list);
|
||||
}
|
||||
if(ptr>=tok_list_size)
|
||||
{
|
||||
error_info(error_line,lack_left_brace);
|
||||
|
|
|
@ -18,6 +18,12 @@ private:
|
|||
int global_scope_address;
|
||||
nasal_ast root;
|
||||
// process functions are private
|
||||
int number_generation(nasal_ast&);
|
||||
int string_generation(nasal_ast&);
|
||||
int vector_generation(nasal_ast&);
|
||||
int hash_generation(nasal_ast&);
|
||||
int function_generation(nasal_ast&);
|
||||
|
||||
int main_progress();
|
||||
int block_progress();
|
||||
int loop_progress();
|
||||
|
@ -58,6 +64,78 @@ void nasal_runtime::run()
|
|||
nasal_vm.del_reference(global_scope_address);
|
||||
return;
|
||||
}
|
||||
|
||||
// private functions
|
||||
int nasal_runtime::number_generation(nasal_ast& node)
|
||||
{
|
||||
int new_addr=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(new_addr).set_type(vm_number);
|
||||
nasal_vm.gc_get(new_addr).set_number(trans_string_to_number(node.get_str()));
|
||||
return new_addr;
|
||||
}
|
||||
int nasal_runtime::string_generation(nasal_ast& node)
|
||||
{
|
||||
int new_addr=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(new_addr).set_type(vm_string);
|
||||
nasal_vm.gc_get(new_addr).set_string(node.get_str());
|
||||
return new_addr;
|
||||
}
|
||||
int nasal_runtime::vector_generation(nasal_ast& node)
|
||||
{
|
||||
int new_addr=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(new_addr).set_type(vm_vector);
|
||||
nasal_vector& ref_of_this_vector=nasal_vm.gc_get(new_addr).get_vector();
|
||||
|
||||
int node_children_size=node.get_children().size();
|
||||
for(int i=0;i<node_children_size;++i)
|
||||
{
|
||||
int new_memory_space=nasal_vm.mem_alloc();
|
||||
ref_of_this_vector.add_elem(new_memory_space);
|
||||
switch(node.get_children()[i].get_type())
|
||||
{
|
||||
case ast_number: nasal_vm.mem_init(new_memory_space,number_generation(node.get_children()[i]));break;
|
||||
case ast_string: nasal_vm.mem_init(new_memory_space,string_generation(node.get_children()[i]));break;
|
||||
case ast_vector: nasal_vm.mem_init(new_memory_space,vector_generation(node.get_children()[i]));break;
|
||||
case ast_hash: nasal_vm.mem_init(new_memory_space,hash_generation(node.get_children()[i]));break;
|
||||
case ast_function: nasal_vm.mem_init(new_memory_space,function_generation(node.get_children()[i]));break;
|
||||
}
|
||||
}
|
||||
return new_addr;
|
||||
}
|
||||
int nasal_runtime::hash_generation(nasal_ast& node)
|
||||
{
|
||||
int new_addr=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(new_addr).set_type(vm_hash);
|
||||
nasal_hash& ref_of_this_hash=nasal_vm.gc_get(new_addr).get_hash();
|
||||
|
||||
int node_children_size=node.get_children().size();
|
||||
for(int i=0;i<node_children_size;++i)
|
||||
{
|
||||
int new_memory_space=nasal_vm.mem_alloc();
|
||||
std::string hash_member_name=node.get_children()[i].get_children()[0].get_str();
|
||||
ref_of_this_hash.add_elem(hash_member_name,new_memory_space);
|
||||
nasal_ast& ref_of_hash_member_value=node.get_children()[i].get_children()[1];
|
||||
switch(ref_of_hash_member_value.get_type())
|
||||
{
|
||||
case ast_number: nasal_vm.mem_init(new_memory_space,number_generation(ref_of_hash_member_value));break;
|
||||
case ast_string: nasal_vm.mem_init(new_memory_space,string_generation(ref_of_hash_member_value));break;
|
||||
case ast_vector: nasal_vm.mem_init(new_memory_space,vector_generation(ref_of_hash_member_value));break;
|
||||
case ast_hash: nasal_vm.mem_init(new_memory_space,hash_generation(ref_of_hash_member_value));break;
|
||||
case ast_function: nasal_vm.mem_init(new_memory_space,function_generation(ref_of_hash_member_value));break;
|
||||
}
|
||||
}
|
||||
return new_addr;
|
||||
}
|
||||
int nasal_runtime::function_generation(nasal_ast& node)
|
||||
{
|
||||
int new_addr=nasal_vm.gc_alloc();
|
||||
nasal_vm.gc_get(new_addr).set_type(vm_function);
|
||||
nasal_function& ref_of_this_function=nasal_vm.gc_get(new_addr).get_func();
|
||||
|
||||
ref_of_this_function.set_arguments(node.get_children()[0]);
|
||||
ref_of_this_function.set_run_block(node.get_children()[1]);
|
||||
return new_addr;
|
||||
}
|
||||
int nasal_runtime::main_progress()
|
||||
{
|
||||
int ret_state=rt_exit_without_error;
|
||||
|
|
Loading…
Reference in New Issue