This commit is contained in:
Valk Richard Li 2020-02-05 22:40:50 +08:00 committed by GitHub
parent cffe95f1d8
commit 0e86e5f3c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 304 additions and 125 deletions

View File

@ -3,6 +3,9 @@
resource_file resource;
nasal_lexer lexer;
nasal_parse parser;
abstract_syntax_tree libroot;
abstract_syntax_tree root;
nasal_symbol_table symtable;
std::string command;
@ -33,7 +36,6 @@ int main()
std::cout<<">> [del ] clear the resource code."<<std::endl;
std::cout<<">> [lib ] add lib file."<<std::endl;
std::cout<<">> [rs ] print resource code."<<std::endl;
std::cout<<">> [total ] print resource code with lib code."<<std::endl;
std::cout<<">> [lex ] turn code into tokens."<<std::endl;
std::cout<<">> [par ] turn tokens into abstract syntax tree."<<std::endl;
std::cout<<">> [ast ] check the abstract syntax tree."<<std::endl;
@ -60,20 +62,37 @@ int main()
lexer.delete_all_tokens();
parser.delete_all_elements();
symtable.set_scope_clear();
root.set_clear();
std::cout<<">> [Delete] complete."<<std::endl;
}
else if(command=="lib")
{
libroot.set_clear();
resource.load_lib_file();
lexer.scanner(resource.get_source());
lexer.generate_detail_token();
if(!lexer.get_error())
{
parser.get_token_list(lexer.get_detail_token_list());
parser.main_generate();
if(!parser.get_error())
{
libroot=parser.get_root();
std::cout<<">> [Lib] loaded."<<std::endl;
}
else
std::cout<<">> [Lib-error] lib files have parse error(s),stop."<<std::endl;
}
else
std::cout<<">> [Lib-error] lib files have lexer error(s),stop."<<std::endl;
resource.delete_all_source();
lexer.delete_all_tokens();
parser.delete_all_elements();
symtable.set_scope_clear();
}
else if(command=="rs")
{
resource.print_resource(false);
}
else if(command=="total")
{
resource.print_resource(true);
resource.print_resource();
}
else if(command=="lex")
{
@ -109,7 +128,7 @@ int main()
{
symtable.set_scope_clear();
symtable.symbol_table_main_generate(parser.get_root());
parser.get_root().print_tree(1);
parser.get_root().print_tree();
}
else
std::cout<<">> [Parse] error occurred,stop."<<std::endl;
@ -152,7 +171,7 @@ int main()
symtable.set_scope_clear();
symtable.symbol_table_main_generate(parser.get_root());
if(!symtable.get_error())
;// run code
root=parser.get_root();// run code
else
std::cout<<">> [Symbol] error occurred,stop."<<std::endl;
}
@ -170,7 +189,7 @@ int main()
parser.get_token_list(lexer.get_detail_token_list());
parser.print_detail_token();
parser.main_generate();
parser.get_root().print_tree(1);
parser.get_root().print_tree();
}
else if(command=="exit")
break;

View File

@ -62,7 +62,6 @@ enum parse_token_type
__definition,
__function,__conditional
};
void print_parse_token(int type)
{
std::string context="";
@ -189,7 +188,6 @@ enum parse_error_type
ternary_operator_lack_colon, // lack ':'
};
void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
{
std::string error_info_head=">> [Parse-error] line ";
@ -313,4 +311,29 @@ void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
return;
}
enum scalar_type
{
scalar_nil,
scalar_number=1,
scalar_string,
scalar_vector,
scalar_hash,
scalar_function
};
void print_scalar_type(const int type)
{
switch(type)
{
case scalar_nil: std::cout<<"nil";break;
case scalar_number:
case scalar_string: std::cout<<"scalar";break;
case scalar_vector: std::cout<<"vector";break;
case scalar_hash: std::cout<<"hash";break;
case scalar_function: std::cout<<"function";break;
default: std::cout<<"nil";break;
}
return;
}
#endif

View File

@ -32,6 +32,19 @@
others: __unknown_operator
*/
const std::string lib_filename[9]=
{
"lib/base.nas",
"lib/bits.nas",
"lib/io.nas",
"lib/math.nas",
"lib/readline.nas",
"lib/sqlite.nas",
"lib/thread.nas",
"lib/unix.nas",
"lib/utf8.nas"
};
std::string reserve_word[15]=
{
"for","foreach","forindex","while",
@ -50,9 +63,7 @@ int is_reserve_word(std::string str)
class resource_file
{
private:
std::list<char> libsource;
std::list<char> resource;
std::list<char> totalcode;
public:
/*
resource_file();
@ -61,27 +72,21 @@ class resource_file
void input_file(std::string);
void load_lib_file();
std::list<char>& get_source();
void print_resource(bool);
void print_resource();
*/
resource_file()
{
libsource.clear();
resource.clear();
totalcode.clear();
return;
}
~resource_file()
{
libsource.clear();
resource.clear();
totalcode.clear();
return;
}
void delete_all_source()
{
libsource.clear();
resource.clear();
totalcode.clear();
return;
}
void input_file(std::string filename)
@ -106,33 +111,36 @@ class resource_file
}
void load_lib_file()
{
libsource.clear();
/*
ifstream lib files here
*/
resource.clear();
for(int i=0;i<9;++i)
{
std::ifstream fin(lib_filename[i],std::ios::binary);
if(fin.fail())
std::cout<<">> [Resource] fatal error: lack \'"<<lib_filename[i]<<"\'"<<std::endl;
else
{
char c=0;
while(!fin.eof())
{
c=fin.get();
if(fin.eof())
break;
resource.push_back(c);
}
}
fin.close();
}
return;
}
std::list<char>& get_source()
{
totalcode.clear();
for(std::list<char>::iterator i=libsource.begin();i!=libsource.end();++i)
totalcode.push_back(*i);
for(std::list<char>::iterator i=resource.begin();i!=resource.end();++i)
totalcode.push_back(*i);
return totalcode;
return resource;
}
void print_resource(bool withlib)
void print_resource()
{
std::list<char> tmp;
if(withlib)
for(std::list<char>::iterator i=libsource.begin();i!=libsource.end();++i)
tmp.push_back(*i);
for(std::list<char>::iterator i=resource.begin();i!=resource.end();++i)
tmp.push_back(*i);
int line=1;
std::cout<<line<<"\t";
for(std::list<char>::iterator i=tmp.begin();i!=tmp.end();++i)
for(std::list<char>::iterator i=resource.begin();i!=resource.end();++i)
{
if(32<=*i)
std::cout<<*i;

View File

@ -175,7 +175,6 @@ int nasal_parse::get_error()
abstract_syntax_tree& nasal_parse::get_root()
{
std::cout<<">>[Parse] output root address: "<<(&root)<<" ."<<std::endl;
return root;
}
@ -1430,7 +1429,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
}
this->get_token();
if(this_token.type==__semi)
{
abstract_syntax_tree null_node;
null_node.set_node_type(__null_type);
loop_main_node.add_children(null_node);
this->push_token();
}
else
{
if(this_token.type==__var)
@ -1443,7 +1447,7 @@ abstract_syntax_tree nasal_parse::loop_expr()
this->push_token();
// cannot use calculation() here
// because for-loop's first statement must be definition or call_identifier
loop_main_node.add_children(scalar_generate());
loop_main_node.add_children(calculation());
}
}
this->get_token();
@ -1454,7 +1458,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
}
this->get_token();
if(this_token.type==__semi)
{
abstract_syntax_tree null_node;
null_node.set_node_type(__null_type);
loop_main_node.add_children(null_node);
this->push_token();
}
else
{
this->push_token();
@ -1468,7 +1477,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
}
this->get_token();
if(this_token.type==__right_curve)
{
abstract_syntax_tree null_node;
null_node.set_node_type(__null_type);
loop_main_node.add_children(null_node);
this->push_token();
}
else
{
this->push_token();

70
version2.0/nasal_scalar.h Normal file
View File

@ -0,0 +1,70 @@
#ifndef __NASAL_SCALAR_H__
#define __NASAL_SCALAR_H__
class nasal_scalar
{
private:
int type;
std::string var_string;
double var_number;
std::vector<nasal_scalar> var_array;
std::map<std::string,nasal_scalar> var_hash;
public:
nasal_scalar()
{
type=scalar_nil;
var_string="";
var_number=0;
var_array.clear();
var_hash.clear();
return;
}
nasal_scalar(const nasal_scalar& tmp)
{
type=tmp.type;
var_string=tmp.var_string;
var_number=tmp.var_number;
var_array=tmp.var_array;
var_hash=tmp.var_hash;
return;
}
nasal_scalar& operator=(const nasal_scalar& tmp)
{
type=tmp.type;
var_string=tmp.var_string;
var_number=tmp.var_number;
var_array=tmp.var_array;
var_hash=tmp.var_hash;
return *this;
}
void set_clear()
{
type=scalar_nil;
var_string.clear();
var_number=0;
var_array.clear();
var_hash.clear();
return;
}
void set_type(const int tmp_type)
{
// scalar_function is the last enum in enum::scalar_type
type=tmp_type>scalar_function? scalar_nil:tmp_type;
return;
}
void set_number(const double tmp_number)
{
var_number=tmp_number;
return;
}
void set_string(const std::string& tmp_str)
{
var_string=tmp_str;
return;
}
int get_type() {return type;}
double get_number() {return var_number;}
std::string get_string(){return var_string;}
};
#endif

View File

@ -33,6 +33,8 @@ class nasal_symbol_table
{
private:
int error;
int global_number;
int local_number;
std::list<symbol_table_unit> global_symbol_dictionary;// used to save symbols appeared in global scope
std::list<symbol_table_unit> local_symbol_dictionary; // used to save symbols appeared in local scopes
std::list<symbol_table_unit> global_scope; // global scope
@ -43,6 +45,7 @@ class nasal_symbol_table
void local_list_del_scope();
void local_scope_add_symbol(symbol_table_unit);
int search_symbol_id(symbol_table_unit);
bool search_scope(symbol_table_unit);
public:
nasal_symbol_table();
~nasal_symbol_table();
@ -55,6 +58,8 @@ class nasal_symbol_table
nasal_symbol_table::nasal_symbol_table()
{
error=0;
global_number=0;
local_number=0;
global_symbol_dictionary.clear();
global_scope.clear();
local_symbol_dictionary.clear();
@ -65,6 +70,8 @@ nasal_symbol_table::nasal_symbol_table()
nasal_symbol_table::~nasal_symbol_table()
{
error=0;
global_number=0;
local_number=0;
global_symbol_dictionary.clear();
global_scope.clear();
local_symbol_dictionary.clear();
@ -75,6 +82,8 @@ nasal_symbol_table::~nasal_symbol_table()
void nasal_symbol_table::set_scope_clear()
{
error=0;
global_number=0;
local_number=0;
global_symbol_dictionary.clear();
global_scope.clear();
local_symbol_dictionary.clear();
@ -104,17 +113,14 @@ void nasal_symbol_table::print_symbol_table()
}
void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp)
{
int symbol_number_gen=0;
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
// repeat symbol will get the same number
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
if(i->symbol_name==tmp.symbol_name)
return;
symbol_number_gen=i->symbol_number;
}
tmp.symbol_number=symbol_number_gen+1;
++global_number;
tmp.symbol_number=global_number;
global_scope.push_back(tmp);
global_symbol_dictionary.push_back(tmp);
return;
@ -136,6 +142,9 @@ void nasal_symbol_table::local_list_del_scope()
}
else
local_list.pop_back();
if(local_list.empty())
local_number=0;
return;
}
@ -143,26 +152,15 @@ void nasal_symbol_table::local_scope_add_symbol(symbol_table_unit tmp)
{
if(!local_list.empty())
{
int symbol_number_gen=0;
// get last symbol number
// if in the last scope there is a sym which symbol_name is the same as tmp.symbol_name
// then the symbol will get the same number
std::list<std::list<symbol_table_unit> >::iterator last_scope=local_list.end();--last_scope;
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
// repeat symbol will get the same number
++i;
if(i==local_list.end())
{
--i;
if(j->symbol_name==tmp.symbol_name)
if((i==last_scope) && (j->symbol_name==tmp.symbol_name))
return;
}
else
--i;
symbol_number_gen=j->symbol_number;
}
tmp.symbol_number=symbol_number_gen+1;
++local_number;
tmp.symbol_number=local_number;
local_list.back().push_back(tmp);
local_symbol_dictionary.push_back(tmp);
}
@ -176,28 +174,40 @@ void nasal_symbol_table::local_scope_add_symbol(symbol_table_unit tmp)
int nasal_symbol_table::search_symbol_id(symbol_table_unit tmp)
{
// in both global and local scope
// symbol_number begins with 1
int ret_number=-1;
if(!local_list.empty())
// local scope
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)
if(j->symbol_name==tmp.symbol_name)
ret_number=j->symbol_number;
if(ret_number==-1)
return j->symbol_number;
// global scope
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
if(i->symbol_name==tmp.symbol_name)
ret_number=i->symbol_number;
if(ret_number==-1)
{
return i->symbol_number;
// error
++error;
std::cout<<">> [Symbol-error] line "<<tmp.symbol_line<<": cannot find var named \'"<<tmp.symbol_name<<"\' ."<<std::endl;
return -1;
}
return ret_number;
bool nasal_symbol_table::search_scope(symbol_table_unit tmp)
{
// this function must be used when search_symbol_id()!=-1
// because if seach_symbol_id()==-1 it means it haven't found this symbol
// true means global scope
// false means local scope
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)
if(j->symbol_name==tmp.symbol_name)
return false;
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
if(i->symbol_name==tmp.symbol_name)
return true;
return true;
}
int nasal_symbol_table::get_error()
{
// god knows why i wrote this sh*t.
return error;
}
@ -206,6 +216,7 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
this->set_scope_clear();
for(std::list<abstract_syntax_tree>::iterator i=root.get_children().begin();i!=root.get_children().end();++i)
{
// only definition will call this->global_scope_add_symbol(symbol_table_unit)
if(i->get_node_type()==__definition)
{
symbol_table_unit tmp;
@ -213,9 +224,10 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
tmp.symbol_name=i->get_children().front().get_var_name();
this->global_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
i->get_children().front().set_var_scope(true);
i->get_children().front().set_var_scope(this->search_scope(tmp));
this->symbol_table_block_generate(i->get_children().back());
}
// when in main_generate loop,local scope is totally empty
else
this->symbol_table_block_generate(*i);
}
@ -225,7 +237,8 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
{
if(node.get_node_type()==__function)
int node_type=node.get_node_type();
if(node_type==__function)
{
this->local_list_add_scope();
if(node.get_children().front().get_node_type()==__parameters)
@ -238,7 +251,7 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
tmp.symbol_name=i->get_var_name();
this->local_scope_add_symbol(tmp);
i->set_symbol_number(this->search_symbol_id(tmp));
i->set_var_scope(false);
i->set_var_scope(this->search_scope(tmp));
}
else
{
@ -247,14 +260,14 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
tmp.symbol_name=i->get_children().front().get_var_name();
this->local_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
i->get_children().front().set_var_scope(false);
i->get_children().front().set_var_scope(this->search_scope(tmp));
this->symbol_table_block_generate(i->get_children().back());
}
}
this->symbol_table_block_generate(node.get_children().back());
this->local_list_del_scope();
}
else if(node.get_node_type()==__conditional)
else if(node_type==__conditional)
{
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
{
@ -265,7 +278,32 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
this->local_list_del_scope();
}
}
else if(node.get_node_type()==__normal_statement_block)
else if(node_type==__while)
{
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
this->symbol_table_block_generate(*i);
}
else if(node_type==__for || node_type==__foreach || node_type==__forindex)
{
this->local_list_add_scope();
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
{
if(i->get_node_type()==__definition)
{
symbol_table_unit tmp;
tmp.symbol_line=i->get_children().front().get_node_line();
tmp.symbol_name=i->get_children().front().get_var_name();
this->local_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
i->get_children().front().set_var_scope(this->search_scope(tmp));
this->symbol_table_block_generate(i->get_children().back());
}
else
this->symbol_table_block_generate(*i);
}
this->local_list_del_scope();
}
else if(node_type==__normal_statement_block)
{
for(std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();i!=node.get_children().end();++i)
{
@ -276,22 +314,29 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
tmp.symbol_name=i->get_children().front().get_var_name();
this->local_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
i->get_children().front().set_var_scope(false);
i->get_children().front().set_var_scope(this->search_scope(tmp));
this->symbol_table_block_generate(i->get_children().back());
}
else
this->symbol_table_block_generate(*i);
}
}
else if(node.get_node_type()==__id)
else if(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);
// if this id does not exist in scopes
// then search_scope will not be called
if(id_symbol_number>0)
node.set_var_scope(this->search_scope(tmp));
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);
}
else if(node.get_node_type()==__hash)
else if(node_type==__hash)
{
this->local_list_add_scope();
symbol_table_unit me;
@ -310,7 +355,7 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node)
tmp.symbol_name=i->get_children().front().get_var_string();
this->local_scope_add_symbol(tmp);
i->get_children().front().set_symbol_number(this->search_symbol_id(tmp));
i->get_children().front().set_var_scope(false);
i->get_children().front().set_var_scope(this->search_scope(tmp));
this->symbol_table_block_generate(i->get_children().back());
}
else