This commit is contained in:
Valk Richard Li 2020-01-30 22:27:51 +08:00 committed by GitHub
parent 9118ca336b
commit 6f12a2d0d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 143 additions and 74 deletions

View File

@ -6,6 +6,7 @@ class abstract_syntax_tree
private: private:
// basic elements // basic elements
int line; int line;
int symbol_number;
int node_type; int node_type;
std::list<abstract_syntax_tree> children; std::list<abstract_syntax_tree> children;
@ -30,6 +31,7 @@ class abstract_syntax_tree
void set_clear(); void set_clear();
void set_node_type(const int); void set_node_type(const int);
void set_node_line(const int); void set_node_line(const int);
void set_symbol_number(const int);
void set_var_string(std::string); void set_var_string(std::string);
void set_var_number(std::string); void set_var_number(std::string);
void set_var_name(std::string); void set_var_name(std::string);
@ -38,6 +40,7 @@ class abstract_syntax_tree
// get // get
int get_node_type(); int get_node_type();
int get_node_line(); int get_node_line();
int get_symbol_number();
double get_var_number(); double get_var_number();
std::string get_var_string(); std::string get_var_string();
std::string get_var_name(); std::string get_var_name();
@ -48,6 +51,7 @@ abstract_syntax_tree::abstract_syntax_tree()
{ {
node_type=__null_type; node_type=__null_type;
line=0; line=0;
symbol_number=-1;
var_number=0; var_number=0;
var_string=""; var_string="";
var_name=""; var_name="";
@ -59,6 +63,7 @@ abstract_syntax_tree::abstract_syntax_tree(const abstract_syntax_tree& tmp)
{ {
node_type=tmp.node_type; node_type=tmp.node_type;
line=tmp.line; line=tmp.line;
symbol_number=tmp.symbol_number;
var_number=tmp.var_number; var_number=tmp.var_number;
var_string=tmp.var_string; var_string=tmp.var_string;
var_name=tmp.var_name; var_name=tmp.var_name;
@ -76,6 +81,7 @@ abstract_syntax_tree& abstract_syntax_tree::operator=(const abstract_syntax_tree
{ {
node_type=tmp.node_type; node_type=tmp.node_type;
line=tmp.line; line=tmp.line;
symbol_number=tmp.symbol_number;
var_number=tmp.var_number; var_number=tmp.var_number;
var_string=tmp.var_string; var_string=tmp.var_string;
var_name=tmp.var_name; var_name=tmp.var_name;
@ -114,6 +120,7 @@ void abstract_syntax_tree::set_clear()
{ {
node_type=__null_type; node_type=__null_type;
line=0; line=0;
symbol_number=-1;
var_number=0; var_number=0;
var_string=""; var_string="";
var_name=""; var_name="";
@ -139,6 +146,12 @@ void abstract_syntax_tree::set_node_line(const int __line)
return; return;
} }
void abstract_syntax_tree::set_symbol_number(const int __sym_num)
{
symbol_number=__sym_num;
return;
}
void abstract_syntax_tree::set_var_string(std::string __str) void abstract_syntax_tree::set_var_string(std::string __str)
{ {
var_string=__str; var_string=__str;
@ -176,6 +189,11 @@ int abstract_syntax_tree::get_node_line()
return line; return line;
} }
int abstract_syntax_tree::get_symbol_number()
{
return symbol_number;
}
double abstract_syntax_tree::get_var_number() double abstract_syntax_tree::get_var_number()
{ {
return var_number; return var_number;

View File

@ -9,24 +9,17 @@ class nasal_parse
token this_token; token this_token;
int error; int error;
abstract_syntax_tree root; abstract_syntax_tree root;
public:
// basic // most important function of parser
void delete_all_elements() // these two functions are used to get and push token
{ // by using them,parser can generate ast
// used in 'del' command
while(!parse_token_stream.empty())
parse_token_stream.pop();
while(!checked_tokens.empty())
checked_tokens.pop();
root.set_clear();
return;
}
void print_detail_token();
void get_token_list(std::list<token>&);
void get_token(); void get_token();
void push_token(); void push_token();
int get_error();
abstract_syntax_tree& get_root(); // block statements generation
abstract_syntax_tree block_generate();
// check ';'
void check_semi(); void check_semi();
// check '(' confliction // check '(' confliction
@ -35,10 +28,6 @@ class nasal_parse
bool check_var_in_curve(); // check multi_definition: (var id,id,id) bool check_var_in_curve(); // check multi_definition: (var id,id,id)
bool check_function_end(abstract_syntax_tree&); // check end of definition or '=' is a function bool check_function_end(abstract_syntax_tree&); // check end of definition or '=' is a function
// abstract_syntax_tree generation
// block statements generation
void main_generate();
abstract_syntax_tree block_generate();
/* /*
calculation() will get elements generated by and_calculation() calculation() will get elements generated by and_calculation()
and_calculation() will get elements generated by or_calculation() and_calculation() will get elements generated by or_calculation()
@ -61,6 +50,7 @@ class nasal_parse
abstract_syntax_tree assign_calculation(); abstract_syntax_tree assign_calculation();
abstract_syntax_tree scalar_generate(); abstract_syntax_tree scalar_generate();
// normal data type generation
abstract_syntax_tree hash_generate(); abstract_syntax_tree hash_generate();
abstract_syntax_tree vector_generate(); abstract_syntax_tree vector_generate();
abstract_syntax_tree function_generate(); abstract_syntax_tree function_generate();
@ -71,6 +61,26 @@ class nasal_parse
abstract_syntax_tree definition(); abstract_syntax_tree definition();
abstract_syntax_tree loop_expr(); abstract_syntax_tree loop_expr();
abstract_syntax_tree conditional_expr(); abstract_syntax_tree conditional_expr();
public:
// basic functions
void delete_all_elements()
{
// used in 'del' command
while(!parse_token_stream.empty())
parse_token_stream.pop();
while(!checked_tokens.empty())
checked_tokens.pop();
root.set_clear();
return;
}
void print_detail_token();
void get_token_list(std::list<token>&);
int get_error();
abstract_syntax_tree& get_root();
// abstract_syntax_tree generation
// main process of parser
void main_generate();
}; };
void nasal_parse::print_detail_token() void nasal_parse::print_detail_token()

View File

@ -33,21 +33,21 @@ class nasal_symbol_table
{ {
private: private:
int error; int error;
std::list<symbol_table_unit> global_symbol_dictionary; std::list<symbol_table_unit> global_symbol_dictionary;// used to save symbols appeared in global scope
std::list<symbol_table_unit> global_scope; std::list<symbol_table_unit> local_symbol_dictionary; // used to save symbols appeared in local scopes
std::list<symbol_table_unit> local_symbol_dictionary; std::list<symbol_table_unit> global_scope; // global scope
std::list<std::list<symbol_table_unit> > block_list; std::list<std::list<symbol_table_unit> > local_list; // local scopes
void symbol_table_block_generate(abstract_syntax_tree&); void symbol_table_block_generate(abstract_syntax_tree&);
void global_scope_add_symbol(symbol_table_unit);
void local_list_add_scope();
void local_list_del_scope();
void local_scope_add_symbol(symbol_table_unit);
int search_symbol_id(symbol_table_unit);
public: public:
nasal_symbol_table(); nasal_symbol_table();
~nasal_symbol_table(); ~nasal_symbol_table();
void set_scope_clear(); void set_scope_clear();
void print_symbol_table(); void print_symbol_table();
void global_scope_add_symbol(symbol_table_unit);
void block_list_add_scope();
void block_list_del_scope();
void block_scope_add_symbol(symbol_table_unit);
int search_symbol_id(symbol_table_unit);
int get_error(); int get_error();
void symbol_table_main_generate(abstract_syntax_tree&); void symbol_table_main_generate(abstract_syntax_tree&);
}; };
@ -58,7 +58,7 @@ nasal_symbol_table::nasal_symbol_table()
global_symbol_dictionary.clear(); global_symbol_dictionary.clear();
global_scope.clear(); global_scope.clear();
local_symbol_dictionary.clear(); local_symbol_dictionary.clear();
block_list.clear(); local_list.clear();
return; return;
} }
@ -68,7 +68,7 @@ nasal_symbol_table::~nasal_symbol_table()
global_symbol_dictionary.clear(); global_symbol_dictionary.clear();
global_scope.clear(); global_scope.clear();
local_symbol_dictionary.clear(); local_symbol_dictionary.clear();
block_list.clear(); local_list.clear();
return; return;
} }
@ -78,7 +78,7 @@ void nasal_symbol_table::set_scope_clear()
global_symbol_dictionary.clear(); global_symbol_dictionary.clear();
global_scope.clear(); global_scope.clear();
local_symbol_dictionary.clear(); local_symbol_dictionary.clear();
block_list.clear(); local_list.clear();
return; return;
} }
@ -109,6 +109,7 @@ void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp)
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i) for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
{ {
// this conditional expression is used to avoid wrong use of this function // this conditional expression is used to avoid wrong use of this function
// repeat symbol will get the same number
if(i->symbol_name==tmp.symbol_name) if(i->symbol_name==tmp.symbol_name)
return; return;
symbol_number_gen=i->symbol_number; symbol_number_gen=i->symbol_number;
@ -119,39 +120,50 @@ void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp)
return; return;
} }
void nasal_symbol_table::block_list_add_scope() void nasal_symbol_table::local_list_add_scope()
{ {
std::list<symbol_table_unit> new_scope; std::list<symbol_table_unit> new_scope;
block_list.push_back(new_scope); local_list.push_back(new_scope);
return; return;
} }
void nasal_symbol_table::block_list_del_scope() void nasal_symbol_table::local_list_del_scope()
{ {
if(block_list.empty()) if(local_list.empty())
{ {
++error; ++error;
std::cout<<">>[Symbol-error] fatal error: empty block_scope_list."<<std::endl; std::cout<<">>[Symbol-error] fatal error: empty block_scope_list."<<std::endl;
} }
else else
block_list.pop_back(); local_list.pop_back();
return; return;
} }
void nasal_symbol_table::block_scope_add_symbol(symbol_table_unit tmp) void nasal_symbol_table::local_scope_add_symbol(symbol_table_unit tmp)
{ {
if(!block_list.empty()) if(!local_list.empty())
{ {
int symbol_number_gen=0; int symbol_number_gen=0;
for(std::list<symbol_table_unit>::iterator i=block_list.back().begin();i!=block_list.back().end();++i) // get last symbol number
for(std::list<std::list<symbol_table_unit> >::iterator i= local_list.begin();i!=local_list.end();++i)
if(!i->empty())
for(std::list<symbol_table_unit>::iterator j=i->begin();j!=i->end();++j)
{ {
// this conditional expression is used to avoid wrong use of this function // this conditional expression is used to avoid wrong use of this function
if(i->symbol_name==tmp.symbol_name) // repeat symbol will get the same number
++i;
if(i==local_list.end())
{
--i;
if(j->symbol_name==tmp.symbol_name)
return; return;
symbol_number_gen=i->symbol_number; }
else
--i;
symbol_number_gen=j->symbol_number;
} }
tmp.symbol_number=symbol_number_gen+1; tmp.symbol_number=symbol_number_gen+1;
block_list.back().push_back(tmp); local_list.back().push_back(tmp);
local_symbol_dictionary.push_back(tmp); local_symbol_dictionary.push_back(tmp);
} }
else else
@ -167,8 +179,8 @@ int nasal_symbol_table::search_symbol_id(symbol_table_unit tmp)
// in both global and local scope // in both global and local scope
// symbol_number begins with 1 // symbol_number begins with 1
int ret_number=-1; int ret_number=-1;
if(!block_list.empty()) if(!local_list.empty())
for(std::list<std::list<symbol_table_unit> >::iterator i=block_list.begin();i!=block_list.end();++i) for(std::list<std::list<symbol_table_unit> >::iterator i=local_list.begin();i!=local_list.end();++i)
for(std::list<symbol_table_unit>::iterator j=i->begin();j!=i->end();++j) for(std::list<symbol_table_unit>::iterator j=i->begin();j!=i->end();++j)
if(j->symbol_name==tmp.symbol_name) if(j->symbol_name==tmp.symbol_name)
ret_number=j->symbol_number; ret_number=j->symbol_number;
@ -200,6 +212,7 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
tmp.symbol_line=i->get_children().front().get_node_line(); tmp.symbol_line=i->get_children().front().get_node_line();
tmp.symbol_name=i->get_children().front().get_var_name(); tmp.symbol_name=i->get_children().front().get_var_name();
this->global_scope_add_symbol(tmp); this->global_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
this->symbol_table_block_generate(i->get_children().back()); this->symbol_table_block_generate(i->get_children().back());
} }
else else
@ -211,6 +224,34 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node) void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
{ {
if(node.get_node_type()==__function)
{
this->local_list_add_scope();
this->local_list_del_scope();
}
else if(node.get_node_type()==__conditional)
{
}
else if(node.get_node_type()==__normal_statement_block)
{
}
else if(node.get_node_type()==__id)
{
symbol_table_unit tmp;
tmp.symbol_line=node.get_node_line();
tmp.symbol_name=node.get_var_name();
int id_symbol_number=this->search_symbol_id(tmp);
node.set_symbol_number(id_symbol_number);
}
else
{
if(!node.get_children().empty())
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
this->symbol_table_block_generate(*i);
}
return; return;
} }