From 34d06d6ad1e451cbe5f0b6d43fa0ba83b17821d1 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Thu, 8 Aug 2019 20:59:12 +0800 Subject: [PATCH] update --- version0.2/nasal.h | 2 + version0.2/nasal_functional.cpp | 355 +++++++++++++++++++++++++++++++ version0.2/nasal_functional.h | 346 ++---------------------------- version0.2/nasal_interpreter.cpp | 28 +-- version0.2/nasal_lexer.h | 31 +-- 5 files changed, 392 insertions(+), 370 deletions(-) diff --git a/version0.2/nasal.h b/version0.2/nasal.h index fbfa420..759fca1 100644 --- a/version0.2/nasal.h +++ b/version0.2/nasal.h @@ -25,6 +25,8 @@ namespace nasal //#include "nasal_func_stack.h" #include "nasal_functional.cpp" +#include "nasal_lexer.h" + } #endif diff --git a/version0.2/nasal_functional.cpp b/version0.2/nasal_functional.cpp index 89ddcd3..12ed21e 100644 --- a/version0.2/nasal_functional.cpp +++ b/version0.2/nasal_functional.cpp @@ -108,5 +108,360 @@ func& func::operator=(const func& temp) } return *this; } +void func::append_var(var& p) +{ + parameter *parameter_temp=parameter_head; + while(parameter_temp->next) + parameter_temp=parameter_temp->next; + parameter_temp->next=new parameter; + parameter_temp=parameter_temp->next; + parameter_temp->param_var=p; + parameter_temp->next=NULL; + return; +} +void func::append_token(const int _line,const int _type,std::string &_content) +{ + token_unit *statement_temp=statement_head; + while(statement_temp->next) + statement_temp=statement_temp->next; + statement_temp->next=new token_unit; + statement_temp=statement_temp->next; + statement_temp->line=_line; + statement_temp->type=_type; + statement_temp->content=_content; + statement_temp->next=NULL; + return; +} + +token_list::token_list() +{ + head=new token_unit; + head->type=FUNC_BEGIN; + head->content="__process_begin"; + head->line=0; + head->next=NULL; +} +token_list::~token_list() +{ + token_unit *temp=head; + token_unit *this_node=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; +} +void token_list::print_line_token(const int _line) +{ + std::cout<<"line "<<_line<<": "; + token_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->line==_line) + std::cout<content<<" "; + else if(temp->line>_line) + break; + } + std::cout<next) + return; + token_unit *temp=head; + token_unit *this_node=NULL; + temp=temp->next; + head->next=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + return; +} +void token_list::append(const int _line,const int _type,std::string &_content) +{ + token_unit *temp=head; + while(temp->next) + temp=temp->next; + temp->next=new token_unit; + temp=temp->next; + temp->next=NULL; + temp->type=_type; + temp->line=_line; + temp->content=_content; + return; +} +void token_list::print() +{ + token_unit *temp=head; + std::cout<<"line "<line<<": "<<"( ProcessBegin | "<content<<" )"<next) + return; + while(temp->next) + { + temp=temp->next; + std::cout<<"line "<line<<": "; + if(temp->type==FUNC_OPERATOR) + std::cout<<"( Operator | "; + else if(temp->type==FUNC_IDENTIFIER) + std::cout<<"( Identifier | "; + else if(temp->type==FUNC_NUMBER) + std::cout<<"( Number | "; + else if(temp->type==FUNC_RESERVEWORD) + std::cout<<"( ReserveWord | "; + else if(temp->type==FUNC_STRING) + std::cout<<"( String | "; + std::cout<content<<" )"<next) + { + std::cout<<">> Running complete."<next) + { + temp=temp->next; + if(temp->type=="Operator") + { + ; + } + else if(temp->type=="Identifier") + { + ; + } + else if(temp->type=="Number") + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Number without any tokens."<type=="ReserveWord") + { + if(temp->content=="var") + { + if(temp->next) + temp=temp->next; + else + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Missing elements after \"var\"."<type=="Identifier") + name_of_var=temp->content; + else + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Missing identifier after \"var\"."<next) + temp=temp->next; + else + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Missing operator \"=\" after identifier."<content!="=") + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Missing operator \"=\" after identifier."<next) + temp=temp->next; + else + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Missing value after operator \"=\" ."<type=="Number") + { + temp_var.isGlobal=true; + if(isFloat(temp->content)) + { + temp_var.Type="double"; + temp_var.data=new double; + *((double *)temp_var.data)=double_Str2Num(temp->content); + nasal_var_stack.append_var(name_of_var,temp_var); + delete (double *)temp_var.data; + temp_var.data=NULL; + } + else + { + temp_var.Type="long long int"; + temp_var.data=new long long int; + *((long long int *)temp_var.data)=int_Str2Num(temp->content); + nasal_var_stack.append_var(name_of_var,temp_var); + delete (long long int *)temp_var.data; + temp_var.data=NULL; + } + } + else if(temp->type=="String") + { + temp_var.isGlobal=true; + temp_var.Type="string"; + temp_var.data=new std::string; + std::string temp_string=""; + for(int i=1;i<(int)temp->content.length()-1;++i) + temp_string+=temp->content[i]; + *((std::string *)temp_var.data)=temp_string; + nasal_var_stack.append_var(name_of_var,temp_var); + delete (std::string *)temp_var.data; + temp_var.data=NULL; + } + else if(temp->type=="Operator" && temp->content=="{") + { + bool make_pair=false; + int cnt=1; + while(temp->next) + { + temp=temp->next; + if(temp->type=="Operator" && temp->content=="}") + { + --cnt; + if(!cnt) + { + make_pair=true; + break; + } + } + else if(temp->type=="Operator" && temp->content=="{") + ++cnt; + } + if(!make_pair) + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \"}\"."<type=="Operator" && temp->content=="[") + { + bool make_pair=false; + int cnt=1; + while(temp->next) + { + temp=temp->next; + if(temp->type=="Operator" && temp->content=="]") + { + --cnt; + if(!cnt) + { + make_pair=true; + break; + } + } + else if(temp->type=="Operator" && temp->content=="[") + ++cnt; + } + if(!make_pair) + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \"]\"."<line); + std::cout<<"line "<line<<": "<<"[Error] Missing value after operator \"=\" ."<next && temp->next->content==";")) + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<next; + //end var + } + else if(temp->content=="print") + { + if(temp->next && temp->next->content=="(") + { + temp=temp->next; + while(temp->next) + { + temp=temp->next; + if(temp->type=="String") + { + std::string temp_string=""; + for(int i=1;i<(int)temp->content.length()-1;++i) + temp_string+=temp->content[i]; + PrintString(temp_string); + } + else if(temp->type=="Identifier") + PrintVar(nasal_var_stack.SearchVar(temp->content)); + else if(temp->type=="Operator" && temp->content==")") + { + if(!temp->next) + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<type=="Operator" && temp->content==";") + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \")\" at the end of print."<line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \"(\" after function print."<next && temp->next->content==";")) + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<type=="String") + { + print_line_token(temp->line); + std::cout<<"line "<line<<": "<<"[Error] String without any tokens."<> Running complete."<type=FUNC_BEGIN; - head->content="__process_begin"; - head->line=0; - head->next=NULL; - } - ~token_list() - { - token_unit *temp=head; - token_unit *this_node=NULL; - while(temp->next) - { - this_node=temp; - temp=temp->next; - delete this_node; - } - delete temp; - } - void print_line_token(const int _line) - { - std::cout<<"line "<<_line<<": "; - token_unit *temp=head; - while(temp->next) - { - temp=temp->next; - if(temp->line==_line) - std::cout<content<<" "; - else if(temp->line>_line) - break; - } - std::cout<next) - return; - token_unit *temp=head; - token_unit *this_node=NULL; - temp=temp->next; - head->next=NULL; - while(temp->next) - { - this_node=temp; - temp=temp->next; - delete this_node; - } - delete temp; - return; - } - void append(const int _type,std::string &_content,const int _line) - { - token_unit *temp=head; - while(temp->next) - temp=temp->next; - temp->next=new token_unit; - temp=temp->next; - temp->next=NULL; - temp->type=_type; - temp->line=_line; - temp->content=_content; - return; - } - void print() - { - token_unit *temp=head; - std::cout<<"line "<line<<": "<<"( ProcessBegin | "<content<<" )"<next) - return; - while(temp->next) - { - temp=temp->next; - std::cout<<"line "<line<<": "; - if(temp->type==FUNC_OPERATOR) - std::cout<<"( Operator | "; - else if(temp->type==FUNC_IDENTIFIER) - std::cout<<"( Identifier | "; - else if(temp->type==FUNC_NUMBER) - std::cout<<"( Number | "; - else if(temp->type==FUNC_RESERVEWORD) - std::cout<<"( ReserveWord | "; - else if(temp->type==FUNC_STRING) - std::cout<<"( String | "; - std::cout<content<<" )"<next) - { - std::cout<<">> Running complete."<next) - { - temp=temp->next; - if(temp->type=="Operator") - { - ; - } - else if(temp->type=="Identifier") - { - ; - } - else if(temp->type=="Number") - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Number without any tokens."<type=="ReserveWord") - { - if(temp->content=="var") - { - if(temp->next) - temp=temp->next; - else - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Missing elements after \"var\"."<type=="Identifier") - name_of_var=temp->content; - else - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Missing identifier after \"var\"."<next) - temp=temp->next; - else - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Missing operator \"=\" after identifier."<content!="=") - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Missing operator \"=\" after identifier."<next) - temp=temp->next; - else - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Missing value after operator \"=\" ."<type=="Number") - { - temp_var.isGlobal=true; - if(isFloat(temp->content)) - { - temp_var.Type="double"; - temp_var.data=new double; - *((double *)temp_var.data)=double_Str2Num(temp->content); - nasal_var_stack.append_var(name_of_var,temp_var); - delete (double *)temp_var.data; - temp_var.data=NULL; - } - else - { - temp_var.Type="long long int"; - temp_var.data=new long long int; - *((long long int *)temp_var.data)=int_Str2Num(temp->content); - nasal_var_stack.append_var(name_of_var,temp_var); - delete (long long int *)temp_var.data; - temp_var.data=NULL; - } - } - else if(temp->type=="String") - { - temp_var.isGlobal=true; - temp_var.Type="string"; - temp_var.data=new std::string; - std::string temp_string=""; - for(int i=1;i<(int)temp->content.length()-1;++i) - temp_string+=temp->content[i]; - *((std::string *)temp_var.data)=temp_string; - nasal_var_stack.append_var(name_of_var,temp_var); - delete (std::string *)temp_var.data; - temp_var.data=NULL; - } - else if(temp->type=="Operator" && temp->content=="{") - { - bool make_pair=false; - int cnt=1; - while(temp->next) - { - temp=temp->next; - if(temp->type=="Operator" && temp->content=="}") - { - --cnt; - if(!cnt) - { - make_pair=true; - break; - } - } - else if(temp->type=="Operator" && temp->content=="{") - ++cnt; - } - if(!make_pair) - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \"}\"."<type=="Operator" && temp->content=="[") - { - bool make_pair=false; - int cnt=1; - while(temp->next) - { - temp=temp->next; - if(temp->type=="Operator" && temp->content=="]") - { - --cnt; - if(!cnt) - { - make_pair=true; - break; - } - } - else if(temp->type=="Operator" && temp->content=="[") - ++cnt; - } - if(!make_pair) - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \"]\"."<line); - std::cout<<"line "<line<<": "<<"[Error] Missing value after operator \"=\" ."<next && temp->next->content==";")) - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<next; - //end var - } - else if(temp->content=="print") - { - if(temp->next && temp->next->content=="(") - { - temp=temp->next; - while(temp->next) - { - temp=temp->next; - if(temp->type=="String") - { - std::string temp_string=""; - for(int i=1;i<(int)temp->content.length()-1;++i) - temp_string+=temp->content[i]; - PrintString(temp_string); - } - else if(temp->type=="Identifier") - PrintVar(nasal_var_stack.SearchVar(temp->content)); - else if(temp->type=="Operator" && temp->content==")") - { - if(!temp->next) - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<type=="Operator" && temp->content==";") - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \")\" at the end of print."<line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \"(\" after function print."<next && temp->next->content==";")) - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<type=="String") - { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] String without any tokens."<> Running complete."<> input \"help\" to find help."<>command; if(command=="help") { - std::cout<<">> 1.input file name to run the nasal script."<> 2.command cls to clear the screen."<> 3.command exit to shut down the program."<> 1. input file name to run the nasal script."<> 2. command cls to clear the screen."<> 3. command exit to shut down the program."<> 4. command lexer to see tokens in stack."<> 5. command del to delete all elements in stack."<> 6. command run to run the programme in stack."< #include "nasal_functional.h" - -namespace nasal -{ - -#define IDENTIFIER -1 //自定义标识符 -#define OPERATOR -2 //界符 or 运算符 -#define NUMBER -3 //数字 -#define RESERVEWORD -4 //关键字 -#define STRING -5 //字符串类型 -#define FAIL -6 //失败 -#define SCANEND -7 //扫描完成 -#define ERRORFOUND -8 //异常错误 +#define FAIL -1 //失败 +#define SCANEND -2 //扫描完成 +#define ERRORFOUND -3 //异常错误 std::string ReserveWord[26]= { @@ -256,24 +247,12 @@ void RunProcess(std::string &FileName) while(Syn!=SCANEND && Syn!=ERRORFOUND) { Scanner(Syn,ResourcePrograme,token,Ptr,line); - if(Syn==OPERATOR) - nasal_lexer.append("Operator",token,line); - else if(Syn==IDENTIFIER) - nasal_lexer.append("Identifier",token,line); - else if(Syn==NUMBER) - nasal_lexer.append("Number",token,line); - else if(Syn==RESERVEWORD) - nasal_lexer.append("ReserveWord",token,line); - else if(Syn==STRING) - nasal_lexer.append("String",token,line); + if(Syn>0)//all Syn type is larger than zero + nasal_lexer.append(line,Syn,token); } //nasal_lexer.print(); //for debug mode std::cout<<">> Complete scanning \""<