This commit is contained in:
Valk Richard Li 2020-03-09 17:12:21 +08:00 committed by GitHub
parent 710fff48dd
commit e9ccdf3bc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 19 deletions

View File

@ -230,6 +230,10 @@ ast begins in root node:
statement_2 statement_2
... ...
note: interpreter in flightgear can recognize 1*(2+(var a=2))
but this type of expression is meaningless
so this interpreter does not recognize this.
source code: source code:
0xdeadbeef; 0xdeadbeef;
'str'; 'str';

View File

@ -122,6 +122,7 @@ class gc_manager
// free_space list is used to store space that is not in use. // free_space list is used to store space that is not in use.
std::list<int> free_space; std::list<int> free_space;
std::vector<gc_unit> memory; std::vector<gc_unit> memory;
bool error_occurred;
public: public:
void gc_init() void gc_init()
{ {
@ -131,6 +132,7 @@ class gc_manager
memory.swap(tmp_vec); memory.swap(tmp_vec);
// clear the memory capacity by using tmp_vec.~vector<gc_unit>() // clear the memory capacity by using tmp_vec.~vector<gc_unit>()
free_space.clear(); free_space.clear();
error_occurred=false;
return; return;
} }
int gc_alloc() int gc_alloc()
@ -156,28 +158,29 @@ class gc_manager
// get the reference of the scalar // get the reference of the scalar
return memory[addr].elem; return memory[addr].elem;
} }
bool place_check(const int place) bool place_check(const int addr)
{ {
// check if this place is in memory // check if this place is in memory
// and this place is uncollected // and this place is uncollected
// this function is often used when an identifier is calling a space in memory // this function is often used when an identifier is calling a space in memory
return (place<memory.size()) && (!memory[place].collected); return (0<=addr) && (addr<memory.size()) && (!memory[addr].collected);
} }
void reference_add(const int place) void reference_add(const int addr)
{ {
if(place<memory.size()) if((0<=addr) && (addr<memory.size()) && (!memory[addr].collected))
++memory[place].refcnt; ++memory[addr].refcnt;
else else
{ {
std::cout<<">> [Gc] fatal error: unexpected memory place "; std::cout<<">> [Gc] fatal error: unexpected memory place ";
prt_hex(place); prt_hex(addr);
std::cout<<" ."<<std::endl; std::cout<<" ."<<std::endl;
error_occurred=true;
} }
return; return;
} }
void reference_delete(const int addr) void reference_delete(const int addr)
{ {
if(addr<memory.size()) if((0<=addr) && (addr<memory.size()) && (!memory[addr].collected))
{ {
--memory[addr].refcnt; --memory[addr].refcnt;
if(!memory[addr].refcnt) if(!memory[addr].refcnt)
@ -201,6 +204,7 @@ class gc_manager
std::cout<<">> [Gc] fatal error: unexpected memory address: "; std::cout<<">> [Gc] fatal error: unexpected memory address: ";
prt_hex(addr); prt_hex(addr);
std::cout<<" ."<<std::endl; std::cout<<" ."<<std::endl;
error_occurred=true;
} }
return; return;
} }
@ -226,6 +230,10 @@ class gc_manager
std::cout<<std::endl; std::cout<<std::endl;
return; return;
} }
bool check_error()
{
return error_occurred;
}
}; };
gc_manager nasal_gc; gc_manager nasal_gc;
// this object is used in "nasal_runtime.h" // this object is used in "nasal_runtime.h"

View File

@ -54,7 +54,7 @@ void nasal_runtime::error_interrupt(const int type)
switch (type) switch (type)
{ {
case __incorrect_head_of_tree: case __incorrect_head_of_tree:
std::cout<<error_head<<"the abstract syntax tree's root type is not \'__root\'.please make sure that lib is loaded."<<std::endl;break; std::cout<<error_head<<"lib hasn\'t been loaded."<<std::endl;break;
case __incorrect_head_of_func: case __incorrect_head_of_func:
std::cout<<error_head<<"called identifier is not a function."<<std::endl;break; std::cout<<error_head<<"called identifier is not a function."<<std::endl;break;
case __stack_overflow: case __stack_overflow:
@ -84,6 +84,7 @@ int nasal_runtime::vector_generation(std::list<std::map<std::string,int> >& loca
{ {
int addr=nasal_gc.gc_alloc(); int addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(addr).set_type(scalar_vector); nasal_gc.get_scalar(addr).set_type(scalar_vector);
std::list<abstract_syntax_tree>::iterator call_node=node.get_children().end();
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i) for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
{ {
int var_type=i->get_node_type(); int var_type=i->get_node_type();
@ -99,11 +100,21 @@ int nasal_runtime::vector_generation(std::list<std::map<std::string,int> >& loca
nasal_gc.get_scalar(addr).get_vector().vec_push(function_generation(local_scope,*i)); nasal_gc.get_scalar(addr).get_vector().vec_push(function_generation(local_scope,*i));
else if(var_type==__id) else if(var_type==__id)
nasal_gc.get_scalar(addr).get_vector().vec_push(call_identifier(local_scope,*i)); nasal_gc.get_scalar(addr).get_vector().vec_push(call_identifier(local_scope,*i));
else if(var_type==__add_operator || var_type==__sub_operator || var_type==__mul_operator || var_type==__div_operator || var_type==__link_operator ||
var_type==__cmp_equal || var_type==__cmp_less || var_type==__cmp_more || var_type==__cmp_not_equal || var_type==__cmp_less_or_equal || var_type==__cmp_more_or_equal ||
var_type==__and_operator || var_type==__or_operator || var_type==__ques_mark ||
var_type==__equal || var_type==__add_equal || var_type==__sub_equal || var_type==__div_equal || var_type==__mul_equal || var_type==__link_equal)
nasal_gc.get_scalar(addr).get_vector().vec_push(calculation(local_scope,*i));
else else
{ {
; call_node=i;
break;
} }
} }
for(;call_node!=node.get_children().end();++call_node)
{
;
}
return addr; return addr;
} }
int nasal_runtime::hash_generation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node) int nasal_runtime::hash_generation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node)
@ -111,22 +122,26 @@ int nasal_runtime::hash_generation(std::list<std::map<std::string,int> >& local_
int addr=nasal_gc.gc_alloc(); int addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(addr).set_type(scalar_hash); nasal_gc.get_scalar(addr).set_type(scalar_hash);
nasal_gc.get_scalar(addr).get_hash().set_self_addr(addr); nasal_gc.get_scalar(addr).get_hash().set_self_addr(addr);
std::list<abstract_syntax_tree>::iterator call_node=node.get_children().end();
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i) for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
{ {
if(i->get_node_type()!=__hash_member) if(i->get_node_type()!=__hash_member)
{
call_node=i;
break; break;
}
else else
{ {
std::string member_name=i->get_children().front().get_var_string(); std::string member_name=i->get_children().front().get_var_string();
int var_type=i->get_children().back().get_node_type(); int var_type=i->get_children().back().get_node_type();
if(var_type==__number) if(var_type==__number)
; nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,number_generation(*i));
else if(var_type==__string) else if(var_type==__string)
; nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,string_generation(*i));
else if(var_type==__vector) else if(var_type==__vector)
; nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,vector_generation(local_scope,*i));
else if(var_type==__hash) else if(var_type==__hash)
; nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,hash_generation(local_scope,*i));
else if(var_type==__function) else if(var_type==__function)
{ {
// hash's function must get a parent_hash_addr // hash's function must get a parent_hash_addr
@ -134,12 +149,17 @@ int nasal_runtime::hash_generation(std::list<std::map<std::string,int> >& local_
nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,function_generation(local_scope,i->get_children().back())); nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,function_generation(local_scope,i->get_children().back()));
nasal_gc.get_scalar(nasal_gc.get_scalar(addr).get_hash().get_hash_member(member_name)).get_function().set_parent_hash_addr(addr); nasal_gc.get_scalar(nasal_gc.get_scalar(addr).get_hash().get_hash_member(member_name)).get_function().set_parent_hash_addr(addr);
} }
else else if(var_type==__add_operator || var_type==__sub_operator || var_type==__mul_operator || var_type==__div_operator || var_type==__link_operator ||
{ var_type==__cmp_equal || var_type==__cmp_less || var_type==__cmp_more || var_type==__cmp_not_equal || var_type==__cmp_less_or_equal || var_type==__cmp_more_or_equal ||
; var_type==__and_operator || var_type==__or_operator || var_type==__ques_mark ||
} var_type==__equal || var_type==__add_equal || var_type==__sub_equal || var_type==__div_equal || var_type==__mul_equal || var_type==__link_equal)
nasal_gc.get_scalar(addr).get_hash().hash_push(member_name,calculation(local_scope,*i));
} }
} }
for(;call_node!=node.get_children().end();++call_node)
{
;
}
return addr; return addr;
} }
int nasal_runtime::function_generation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node) int nasal_runtime::function_generation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node)
@ -152,8 +172,21 @@ int nasal_runtime::function_generation(std::list<std::map<std::string,int> >& lo
} }
int nasal_runtime::calculation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node) int nasal_runtime::calculation(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node)
{ {
int operator_type=node.get_node_type(); // calculation will return a value that points to a new area in memory
if(operator_type==__add_operator) int node_type=node.get_node_type();
if(node_type==__number)
return number_generation(node);
else if(node_type==__string)
return string_generation(node);
else if(node_type==__vector)
return vector_generation(local_scope,node);
else if(node_type==__hash)
return hash_generation(local_scope,node);
else if(node_type==__function)
return function_generation(local_scope,node);
else if(node_type==__id)
return call_identifier(local_scope,node);
else if(node_type==__add_operator)
{ {
node.get_children().front(); node.get_children().front();
node.get_children().back(); node.get_children().back();
@ -202,10 +235,12 @@ void nasal_runtime::func_proc(std::list<std::map<std::string,int> >& local_scope
} }
std::map<std::string,int> new_scope; std::map<std::string,int> new_scope;
local_scope.push_back(new_scope); local_scope.push_back(new_scope);
// loading parameters
for(std::list<abstract_syntax_tree>::iterator iter=func_root.get_children().front().get_children().begin();iter!=func_root.get_children().front().get_children().end();++iter) for(std::list<abstract_syntax_tree>::iterator iter=func_root.get_children().front().get_children().begin();iter!=func_root.get_children().front().get_children().end();++iter)
{ {
} }
// process
for(std::list<abstract_syntax_tree>::iterator iter=func_root.get_children().back().get_children().begin();iter!=func_root.get_children().back().get_children().end();++iter) for(std::list<abstract_syntax_tree>::iterator iter=func_root.get_children().back().get_children().begin();iter!=func_root.get_children().back().get_children().end();++iter)
{ {
// use local value node_type to avoid calling function too many times. // use local value node_type to avoid calling function too many times.