update
This commit is contained in:
parent
cffe95f1d8
commit
0e86e5f3c3
|
@ -3,6 +3,9 @@
|
||||||
resource_file resource;
|
resource_file resource;
|
||||||
nasal_lexer lexer;
|
nasal_lexer lexer;
|
||||||
nasal_parse parser;
|
nasal_parse parser;
|
||||||
|
abstract_syntax_tree libroot;
|
||||||
|
abstract_syntax_tree root;
|
||||||
|
|
||||||
nasal_symbol_table symtable;
|
nasal_symbol_table symtable;
|
||||||
|
|
||||||
std::string command;
|
std::string command;
|
||||||
|
@ -10,13 +13,13 @@ std::string command;
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::cout<<">>[system] Windows system."<<std::endl;
|
std::cout<<">> [system] Windows system."<<std::endl;
|
||||||
#endif
|
#endif
|
||||||
#ifdef _linux_
|
#ifdef _linux_
|
||||||
std::cout<<">>[system] Linux system."<<std::endl;
|
std::cout<<">> [system] Linux system."<<std::endl;
|
||||||
#endif
|
#endif
|
||||||
#ifdef TARGET_OS_MAC
|
#ifdef TARGET_OS_MAC
|
||||||
std::cout<<">>[system] MacOS system."<<std::endl;
|
std::cout<<">> [system] MacOS system."<<std::endl;
|
||||||
#endif
|
#endif
|
||||||
std::cout<<">> Nasal interpreter ver 2.0 ."<<std::endl;
|
std::cout<<">> Nasal interpreter ver 2.0 ."<<std::endl;
|
||||||
std::cout<<">> Code: https://github.com/ValKmjolnir/Nasal-Interpreter"<<std::endl;
|
std::cout<<">> Code: https://github.com/ValKmjolnir/Nasal-Interpreter"<<std::endl;
|
||||||
|
@ -33,7 +36,6 @@ int main()
|
||||||
std::cout<<">> [del ] clear the resource code."<<std::endl;
|
std::cout<<">> [del ] clear the resource code."<<std::endl;
|
||||||
std::cout<<">> [lib ] add lib file."<<std::endl;
|
std::cout<<">> [lib ] add lib file."<<std::endl;
|
||||||
std::cout<<">> [rs ] print resource code."<<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<<">> [lex ] turn code into tokens."<<std::endl;
|
||||||
std::cout<<">> [par ] turn tokens into abstract syntax tree."<<std::endl;
|
std::cout<<">> [par ] turn tokens into abstract syntax tree."<<std::endl;
|
||||||
std::cout<<">> [ast ] check the 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();
|
lexer.delete_all_tokens();
|
||||||
parser.delete_all_elements();
|
parser.delete_all_elements();
|
||||||
symtable.set_scope_clear();
|
symtable.set_scope_clear();
|
||||||
std::cout<<">>[Delete] complete."<<std::endl;
|
root.set_clear();
|
||||||
|
std::cout<<">> [Delete] complete."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="lib")
|
else if(command=="lib")
|
||||||
{
|
{
|
||||||
|
libroot.set_clear();
|
||||||
resource.load_lib_file();
|
resource.load_lib_file();
|
||||||
std::cout<<">>[Lib] loaded."<<std::endl;
|
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")
|
else if(command=="rs")
|
||||||
{
|
{
|
||||||
resource.print_resource(false);
|
resource.print_resource();
|
||||||
}
|
|
||||||
else if(command=="total")
|
|
||||||
{
|
|
||||||
resource.print_resource(true);
|
|
||||||
}
|
}
|
||||||
else if(command=="lex")
|
else if(command=="lex")
|
||||||
{
|
{
|
||||||
|
@ -82,7 +101,7 @@ int main()
|
||||||
if(!lexer.get_error())
|
if(!lexer.get_error())
|
||||||
lexer.print_token_list();
|
lexer.print_token_list();
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
std::cout<<">> [Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="par")
|
else if(command=="par")
|
||||||
{
|
{
|
||||||
|
@ -95,7 +114,7 @@ int main()
|
||||||
parser.main_generate();
|
parser.main_generate();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
std::cout<<">> [Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="ast")
|
else if(command=="ast")
|
||||||
{
|
{
|
||||||
|
@ -109,13 +128,13 @@ int main()
|
||||||
{
|
{
|
||||||
symtable.set_scope_clear();
|
symtable.set_scope_clear();
|
||||||
symtable.symbol_table_main_generate(parser.get_root());
|
symtable.symbol_table_main_generate(parser.get_root());
|
||||||
parser.get_root().print_tree(1);
|
parser.get_root().print_tree();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Parse] error occurred,stop."<<std::endl;
|
std::cout<<">> [Parse] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
std::cout<<">> [Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="sym")
|
else if(command=="sym")
|
||||||
{
|
{
|
||||||
|
@ -131,13 +150,13 @@ int main()
|
||||||
symtable.symbol_table_main_generate(parser.get_root());
|
symtable.symbol_table_main_generate(parser.get_root());
|
||||||
symtable.print_symbol_table();
|
symtable.print_symbol_table();
|
||||||
if(symtable.get_error())
|
if(symtable.get_error())
|
||||||
std::cout<<">>[Symbol] error occurred,stop."<<std::endl;
|
std::cout<<">> [Symbol] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Parse] error occurred,stop."<<std::endl;
|
std::cout<<">> [Parse] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
std::cout<<">> [Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="run")
|
else if(command=="run")
|
||||||
{
|
{
|
||||||
|
@ -152,15 +171,15 @@ int main()
|
||||||
symtable.set_scope_clear();
|
symtable.set_scope_clear();
|
||||||
symtable.symbol_table_main_generate(parser.get_root());
|
symtable.symbol_table_main_generate(parser.get_root());
|
||||||
if(!symtable.get_error())
|
if(!symtable.get_error())
|
||||||
;// run code
|
root=parser.get_root();// run code
|
||||||
else
|
else
|
||||||
std::cout<<">>[Symbol] error occurred,stop."<<std::endl;
|
std::cout<<">> [Symbol] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Parse] error occurred,stop."<<std::endl;
|
std::cout<<">> [Parse] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error occurred,stop."<<std::endl;
|
std::cout<<">> [Lexer] error occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="info")
|
else if(command=="info")
|
||||||
{
|
{
|
||||||
|
@ -170,7 +189,7 @@ int main()
|
||||||
parser.get_token_list(lexer.get_detail_token_list());
|
parser.get_token_list(lexer.get_detail_token_list());
|
||||||
parser.print_detail_token();
|
parser.print_detail_token();
|
||||||
parser.main_generate();
|
parser.main_generate();
|
||||||
parser.get_root().print_tree(1);
|
parser.get_root().print_tree();
|
||||||
}
|
}
|
||||||
else if(command=="exit")
|
else if(command=="exit")
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -62,7 +62,6 @@ enum parse_token_type
|
||||||
__definition,
|
__definition,
|
||||||
__function,__conditional
|
__function,__conditional
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_parse_token(int type)
|
void print_parse_token(int type)
|
||||||
{
|
{
|
||||||
std::string context="";
|
std::string context="";
|
||||||
|
@ -189,10 +188,9 @@ enum parse_error_type
|
||||||
|
|
||||||
ternary_operator_lack_colon, // lack ':'
|
ternary_operator_lack_colon, // lack ':'
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
|
void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
|
||||||
{
|
{
|
||||||
std::string error_info_head=">>[Parse-error] line ";
|
std::string error_info_head=">> [Parse-error] line ";
|
||||||
std::string warning_info_head=">> [Parse-warning] line ";
|
std::string warning_info_head=">> [Parse-warning] line ";
|
||||||
switch(error_type)
|
switch(error_type)
|
||||||
{
|
{
|
||||||
|
@ -313,4 +311,29 @@ void print_parse_error(int error_type,int line,int error_token_type=__stack_end)
|
||||||
return;
|
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
|
#endif
|
||||||
|
|
|
@ -32,6 +32,19 @@
|
||||||
others: __unknown_operator
|
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]=
|
std::string reserve_word[15]=
|
||||||
{
|
{
|
||||||
"for","foreach","forindex","while",
|
"for","foreach","forindex","while",
|
||||||
|
@ -50,9 +63,7 @@ int is_reserve_word(std::string str)
|
||||||
class resource_file
|
class resource_file
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::list<char> libsource;
|
|
||||||
std::list<char> resource;
|
std::list<char> resource;
|
||||||
std::list<char> totalcode;
|
|
||||||
public:
|
public:
|
||||||
/*
|
/*
|
||||||
resource_file();
|
resource_file();
|
||||||
|
@ -61,27 +72,21 @@ class resource_file
|
||||||
void input_file(std::string);
|
void input_file(std::string);
|
||||||
void load_lib_file();
|
void load_lib_file();
|
||||||
std::list<char>& get_source();
|
std::list<char>& get_source();
|
||||||
void print_resource(bool);
|
void print_resource();
|
||||||
*/
|
*/
|
||||||
resource_file()
|
resource_file()
|
||||||
{
|
{
|
||||||
libsource.clear();
|
|
||||||
resource.clear();
|
resource.clear();
|
||||||
totalcode.clear();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
~resource_file()
|
~resource_file()
|
||||||
{
|
{
|
||||||
libsource.clear();
|
|
||||||
resource.clear();
|
resource.clear();
|
||||||
totalcode.clear();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void delete_all_source()
|
void delete_all_source()
|
||||||
{
|
{
|
||||||
libsource.clear();
|
|
||||||
resource.clear();
|
resource.clear();
|
||||||
totalcode.clear();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void input_file(std::string filename)
|
void input_file(std::string filename)
|
||||||
|
@ -90,7 +95,7 @@ class resource_file
|
||||||
std::ifstream fin(filename,std::ios::binary);
|
std::ifstream fin(filename,std::ios::binary);
|
||||||
if(fin.fail())
|
if(fin.fail())
|
||||||
{
|
{
|
||||||
std::cout<<">>[Resource] cannot open file \'"<<filename<<"\' ."<<std::endl;
|
std::cout<<">> [Resource] cannot open file \'"<<filename<<"\' ."<<std::endl;
|
||||||
fin.close();
|
fin.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -106,33 +111,36 @@ class resource_file
|
||||||
}
|
}
|
||||||
void load_lib_file()
|
void load_lib_file()
|
||||||
{
|
{
|
||||||
libsource.clear();
|
resource.clear();
|
||||||
/*
|
for(int i=0;i<9;++i)
|
||||||
ifstream lib files here
|
{
|
||||||
*/
|
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;
|
return;
|
||||||
}
|
}
|
||||||
std::list<char>& get_source()
|
std::list<char>& get_source()
|
||||||
{
|
{
|
||||||
totalcode.clear();
|
return resource;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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;
|
int line=1;
|
||||||
std::cout<<line<<"\t";
|
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)
|
if(32<=*i)
|
||||||
std::cout<<*i;
|
std::cout<<*i;
|
||||||
|
@ -282,7 +290,7 @@ class nasal_lexer
|
||||||
if(!check_numerable_string(token_str))
|
if(!check_numerable_string(token_str))
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<">>[Lexer-error] line "<<line<<": "<<token_str<<" is not a numerable string."<<std::endl;
|
std::cout<<">> [Lexer-error] line "<<line<<": "<<token_str<<" is not a numerable string."<<std::endl;
|
||||||
token_str="0";
|
token_str="0";
|
||||||
}
|
}
|
||||||
token new_token;
|
token new_token;
|
||||||
|
@ -333,7 +341,7 @@ class nasal_lexer
|
||||||
if(ptr==res.end() || *ptr!=str_begin)
|
if(ptr==res.end() || *ptr!=str_begin)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<">>[Lexer-error] line "<<line<<": this string must have a \' "<<str_begin<<" \' as its end."<<std::endl;
|
std::cout<<">> [Lexer-error] line "<<line<<": this string must have a \' "<<str_begin<<" \' as its end."<<std::endl;
|
||||||
--ptr;
|
--ptr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -378,11 +386,11 @@ class nasal_lexer
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<">>[Lexer-error] line "<<line<<": unknown char."<<std::endl;
|
std::cout<<">> [Lexer-error] line "<<line<<": unknown char."<<std::endl;
|
||||||
++ptr;
|
++ptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout<<">>[Pre-lexer] complete scanning. "<<error<<" error(s)."<<std::endl;
|
std::cout<<">> [Pre-lexer] complete scanning. "<<error<<" error(s)."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void generate_detail_token()
|
void generate_detail_token()
|
||||||
|
@ -484,13 +492,13 @@ class nasal_lexer
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<">>[Lexer-error] line "<<detail_token.line<<": unknown operator \'"<<i->str<<"\'."<<std::endl;
|
std::cout<<">> [Lexer-error] line "<<detail_token.line<<": unknown operator \'"<<i->str<<"\'."<<std::endl;
|
||||||
detail_token.type=__unknown_operator;
|
detail_token.type=__unknown_operator;
|
||||||
}
|
}
|
||||||
detail_token_list.push_back(detail_token);
|
detail_token_list.push_back(detail_token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout<<">>[Detail-lexer] complete generating. "<<error<<" error(s)."<<std::endl;
|
std::cout<<">> [Detail-lexer] complete generating. "<<error<<" error(s)."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int get_error()
|
int get_error()
|
||||||
|
|
|
@ -175,7 +175,6 @@ int nasal_parse::get_error()
|
||||||
|
|
||||||
abstract_syntax_tree& nasal_parse::get_root()
|
abstract_syntax_tree& nasal_parse::get_root()
|
||||||
{
|
{
|
||||||
std::cout<<">>[Parse] output root address: "<<(&root)<<" ."<<std::endl;
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,7 +389,7 @@ void nasal_parse::main_generate()
|
||||||
if(need_semi_check)
|
if(need_semi_check)
|
||||||
check_semi();
|
check_semi();
|
||||||
}
|
}
|
||||||
std::cout<<">>[Parse] complete generation. "<<error<<" error(s)."<<std::endl;
|
std::cout<<">> [Parse] complete generation. "<<error<<" error(s)."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1430,7 +1429,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
|
||||||
}
|
}
|
||||||
this->get_token();
|
this->get_token();
|
||||||
if(this_token.type==__semi)
|
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();
|
this->push_token();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(this_token.type==__var)
|
if(this_token.type==__var)
|
||||||
|
@ -1443,7 +1447,7 @@ abstract_syntax_tree nasal_parse::loop_expr()
|
||||||
this->push_token();
|
this->push_token();
|
||||||
// cannot use calculation() here
|
// cannot use calculation() here
|
||||||
// because for-loop's first statement must be definition or call_identifier
|
// 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();
|
this->get_token();
|
||||||
|
@ -1454,7 +1458,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
|
||||||
}
|
}
|
||||||
this->get_token();
|
this->get_token();
|
||||||
if(this_token.type==__semi)
|
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();
|
this->push_token();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->push_token();
|
this->push_token();
|
||||||
|
@ -1468,7 +1477,12 @@ abstract_syntax_tree nasal_parse::loop_expr()
|
||||||
}
|
}
|
||||||
this->get_token();
|
this->get_token();
|
||||||
if(this_token.type==__right_curve)
|
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();
|
this->push_token();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->push_token();
|
this->push_token();
|
||||||
|
|
|
@ -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
|
|
@ -33,6 +33,8 @@ class nasal_symbol_table
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int error;
|
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> 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> local_symbol_dictionary; // used to save symbols appeared in local scopes
|
||||||
std::list<symbol_table_unit> global_scope; // global scope
|
std::list<symbol_table_unit> global_scope; // global scope
|
||||||
|
@ -43,6 +45,7 @@ class nasal_symbol_table
|
||||||
void local_list_del_scope();
|
void local_list_del_scope();
|
||||||
void local_scope_add_symbol(symbol_table_unit);
|
void local_scope_add_symbol(symbol_table_unit);
|
||||||
int search_symbol_id(symbol_table_unit);
|
int search_symbol_id(symbol_table_unit);
|
||||||
|
bool search_scope(symbol_table_unit);
|
||||||
public:
|
public:
|
||||||
nasal_symbol_table();
|
nasal_symbol_table();
|
||||||
~nasal_symbol_table();
|
~nasal_symbol_table();
|
||||||
|
@ -55,6 +58,8 @@ class nasal_symbol_table
|
||||||
nasal_symbol_table::nasal_symbol_table()
|
nasal_symbol_table::nasal_symbol_table()
|
||||||
{
|
{
|
||||||
error=0;
|
error=0;
|
||||||
|
global_number=0;
|
||||||
|
local_number=0;
|
||||||
global_symbol_dictionary.clear();
|
global_symbol_dictionary.clear();
|
||||||
global_scope.clear();
|
global_scope.clear();
|
||||||
local_symbol_dictionary.clear();
|
local_symbol_dictionary.clear();
|
||||||
|
@ -65,6 +70,8 @@ nasal_symbol_table::nasal_symbol_table()
|
||||||
nasal_symbol_table::~nasal_symbol_table()
|
nasal_symbol_table::~nasal_symbol_table()
|
||||||
{
|
{
|
||||||
error=0;
|
error=0;
|
||||||
|
global_number=0;
|
||||||
|
local_number=0;
|
||||||
global_symbol_dictionary.clear();
|
global_symbol_dictionary.clear();
|
||||||
global_scope.clear();
|
global_scope.clear();
|
||||||
local_symbol_dictionary.clear();
|
local_symbol_dictionary.clear();
|
||||||
|
@ -75,6 +82,8 @@ nasal_symbol_table::~nasal_symbol_table()
|
||||||
void nasal_symbol_table::set_scope_clear()
|
void nasal_symbol_table::set_scope_clear()
|
||||||
{
|
{
|
||||||
error=0;
|
error=0;
|
||||||
|
global_number=0;
|
||||||
|
local_number=0;
|
||||||
global_symbol_dictionary.clear();
|
global_symbol_dictionary.clear();
|
||||||
global_scope.clear();
|
global_scope.clear();
|
||||||
local_symbol_dictionary.clear();
|
local_symbol_dictionary.clear();
|
||||||
|
@ -86,12 +95,12 @@ void nasal_symbol_table::print_symbol_table()
|
||||||
{
|
{
|
||||||
if(!global_symbol_dictionary.empty())
|
if(!global_symbol_dictionary.empty())
|
||||||
{
|
{
|
||||||
std::cout<<">>[Symbol] global scope:"<<std::endl;
|
std::cout<<">> [Symbol] global scope:"<<std::endl;
|
||||||
for(std::list<symbol_table_unit>::iterator i=global_symbol_dictionary.begin();i!=global_symbol_dictionary.end();++i)
|
for(std::list<symbol_table_unit>::iterator i=global_symbol_dictionary.begin();i!=global_symbol_dictionary.end();++i)
|
||||||
std::cout<<" line "<<i->symbol_line<<": "<<i->symbol_name<<"|"<<i->symbol_number<<std::endl;
|
std::cout<<" line "<<i->symbol_line<<": "<<i->symbol_name<<"|"<<i->symbol_number<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Symbol] empty global symbol list."<<std::endl;
|
std::cout<<">> [Symbol] empty global symbol list."<<std::endl;
|
||||||
if(!local_symbol_dictionary.empty())
|
if(!local_symbol_dictionary.empty())
|
||||||
{
|
{
|
||||||
std::cout<<">>[Symbol] local scope:"<<std::endl;
|
std::cout<<">>[Symbol] local scope:"<<std::endl;
|
||||||
|
@ -99,22 +108,19 @@ void nasal_symbol_table::print_symbol_table()
|
||||||
std::cout<<" line "<<i->symbol_line<<": "<<i->symbol_name<<"|"<<i->symbol_number<<std::endl;
|
std::cout<<" line "<<i->symbol_line<<": "<<i->symbol_name<<"|"<<i->symbol_number<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Symbol] empty local symbol list."<<std::endl;
|
std::cout<<">> [Symbol] empty local symbol list."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp)
|
void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp)
|
||||||
{
|
{
|
||||||
int symbol_number_gen=0;
|
// 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)
|
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
|
|
||||||
if(i->symbol_name==tmp.symbol_name)
|
if(i->symbol_name==tmp.symbol_name)
|
||||||
return;
|
return;
|
||||||
symbol_number_gen=i->symbol_number;
|
++global_number;
|
||||||
}
|
tmp.symbol_number=global_number;
|
||||||
tmp.symbol_number=symbol_number_gen+1;
|
|
||||||
global_scope.push_back(tmp);
|
global_scope.push_back(tmp);
|
||||||
global_symbol_dictionary.push_back(tmp);
|
global_symbol_dictionary.push_back(tmp);
|
||||||
return;
|
return;
|
||||||
|
@ -132,10 +138,13 @@ void nasal_symbol_table::local_list_del_scope()
|
||||||
if(local_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
|
||||||
local_list.pop_back();
|
local_list.pop_back();
|
||||||
|
|
||||||
|
if(local_list.empty())
|
||||||
|
local_number=0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,61 +152,62 @@ void nasal_symbol_table::local_scope_add_symbol(symbol_table_unit tmp)
|
||||||
{
|
{
|
||||||
if(!local_list.empty())
|
if(!local_list.empty())
|
||||||
{
|
{
|
||||||
int symbol_number_gen=0;
|
// if in the last scope there is a sym which symbol_name is the same as tmp.symbol_name
|
||||||
// get last symbol number
|
// then the symbol will get the same number
|
||||||
for(std::list<std::list<symbol_table_unit> >::iterator i= local_list.begin();i!=local_list.end();++i)
|
std::list<std::list<symbol_table_unit> >::iterator last_scope=local_list.end();--last_scope;
|
||||||
if(!i->empty())
|
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((i==last_scope) && (j->symbol_name==tmp.symbol_name))
|
||||||
// this conditional expression is used to avoid wrong use of this function
|
return;
|
||||||
// repeat symbol will get the same number
|
++local_number;
|
||||||
++i;
|
tmp.symbol_number=local_number;
|
||||||
if(i==local_list.end())
|
|
||||||
{
|
|
||||||
--i;
|
|
||||||
if(j->symbol_name==tmp.symbol_name)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
--i;
|
|
||||||
symbol_number_gen=j->symbol_number;
|
|
||||||
}
|
|
||||||
tmp.symbol_number=symbol_number_gen+1;
|
|
||||||
local_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
|
||||||
{
|
{
|
||||||
++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;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nasal_symbol_table::search_symbol_id(symbol_table_unit tmp)
|
int nasal_symbol_table::search_symbol_id(symbol_table_unit tmp)
|
||||||
{
|
{
|
||||||
// in both global and local scope
|
// local scope
|
||||||
// symbol_number begins with 1
|
for(std::list<std::list<symbol_table_unit> >::iterator i=local_list.begin();i!=local_list.end();++i)
|
||||||
int ret_number=-1;
|
for(std::list<symbol_table_unit>::iterator j=i->begin();j!=i->end();++j)
|
||||||
if(!local_list.empty())
|
if(j->symbol_name==tmp.symbol_name)
|
||||||
for(std::list<std::list<symbol_table_unit> >::iterator i=local_list.begin();i!=local_list.end();++i)
|
return j->symbol_number;
|
||||||
for(std::list<symbol_table_unit>::iterator j=i->begin();j!=i->end();++j)
|
// global scope
|
||||||
if(j->symbol_name==tmp.symbol_name)
|
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
|
||||||
ret_number=j->symbol_number;
|
if(i->symbol_name==tmp.symbol_name)
|
||||||
if(ret_number==-1)
|
return i->symbol_number;
|
||||||
for(std::list<symbol_table_unit>::iterator i=global_scope.begin();i!=global_scope.end();++i)
|
// error
|
||||||
if(i->symbol_name==tmp.symbol_name)
|
++error;
|
||||||
ret_number=i->symbol_number;
|
std::cout<<">> [Symbol-error] line "<<tmp.symbol_line<<": cannot find var named \'"<<tmp.symbol_name<<"\' ."<<std::endl;
|
||||||
if(ret_number==-1)
|
return -1;
|
||||||
{
|
}
|
||||||
++error;
|
|
||||||
std::cout<<">>[Symbol-error] line "<<tmp.symbol_line<<": cannot find var named \'"<<tmp.symbol_name<<"\' ."<<std::endl;
|
bool nasal_symbol_table::search_scope(symbol_table_unit tmp)
|
||||||
}
|
{
|
||||||
return ret_number;
|
// 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()
|
int nasal_symbol_table::get_error()
|
||||||
{
|
{
|
||||||
|
// god knows why i wrote this sh*t.
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,6 +216,7 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
|
||||||
this->set_scope_clear();
|
this->set_scope_clear();
|
||||||
for(std::list<abstract_syntax_tree>::iterator i=root.get_children().begin();i!=root.get_children().end();++i)
|
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)
|
if(i->get_node_type()==__definition)
|
||||||
{
|
{
|
||||||
symbol_table_unit tmp;
|
symbol_table_unit tmp;
|
||||||
|
@ -213,19 +224,21 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root)
|
||||||
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));
|
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());
|
this->symbol_table_block_generate(i->get_children().back());
|
||||||
}
|
}
|
||||||
|
// when in main_generate loop,local scope is totally empty
|
||||||
else
|
else
|
||||||
this->symbol_table_block_generate(*i);
|
this->symbol_table_block_generate(*i);
|
||||||
}
|
}
|
||||||
std::cout<<">>[Symbol] symbol table generating complete. "<<error<<" error(s)."<<std::endl;
|
std::cout<<">> [Symbol] symbol table generating complete. "<<error<<" error(s)."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
int node_type=node.get_node_type();
|
||||||
|
if(node_type==__function)
|
||||||
{
|
{
|
||||||
this->local_list_add_scope();
|
this->local_list_add_scope();
|
||||||
if(node.get_children().front().get_node_type()==__parameters)
|
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();
|
tmp.symbol_name=i->get_var_name();
|
||||||
this->local_scope_add_symbol(tmp);
|
this->local_scope_add_symbol(tmp);
|
||||||
i->set_symbol_number(this->search_symbol_id(tmp));
|
i->set_symbol_number(this->search_symbol_id(tmp));
|
||||||
i->set_var_scope(false);
|
i->set_var_scope(this->search_scope(tmp));
|
||||||
}
|
}
|
||||||
else
|
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();
|
tmp.symbol_name=i->get_children().front().get_var_name();
|
||||||
this->local_scope_add_symbol(tmp);
|
this->local_scope_add_symbol(tmp);
|
||||||
i->get_children().front().set_symbol_number(this->search_symbol_id(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(i->get_children().back());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->symbol_table_block_generate(node.get_children().back());
|
this->symbol_table_block_generate(node.get_children().back());
|
||||||
this->local_list_del_scope();
|
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)
|
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();
|
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)
|
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();
|
tmp.symbol_name=i->get_children().front().get_var_name();
|
||||||
this->local_scope_add_symbol(tmp);
|
this->local_scope_add_symbol(tmp);
|
||||||
i->get_children().front().set_symbol_number(this->search_symbol_id(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(i->get_children().back());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
this->symbol_table_block_generate(*i);
|
this->symbol_table_block_generate(*i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(node.get_node_type()==__id)
|
else if(node_type==__id)
|
||||||
{
|
{
|
||||||
symbol_table_unit tmp;
|
symbol_table_unit tmp;
|
||||||
tmp.symbol_line=node.get_node_line();
|
tmp.symbol_line=node.get_node_line();
|
||||||
tmp.symbol_name=node.get_var_name();
|
tmp.symbol_name=node.get_var_name();
|
||||||
int id_symbol_number=this->search_symbol_id(tmp);
|
int id_symbol_number=this->search_symbol_id(tmp);
|
||||||
node.set_symbol_number(id_symbol_number);
|
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();
|
this->local_list_add_scope();
|
||||||
symbol_table_unit me;
|
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();
|
tmp.symbol_name=i->get_children().front().get_var_string();
|
||||||
this->local_scope_add_symbol(tmp);
|
this->local_scope_add_symbol(tmp);
|
||||||
i->get_children().front().set_symbol_number(this->search_symbol_id(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(i->get_children().back());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue