diff --git a/lab.cpp b/lab.cpp new file mode 100644 index 0000000..718e8a7 --- /dev/null +++ b/lab.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include "nasal.h" + +using namespace nasal; + +int main() +{ + std::cout<<">> input \"help\" to find help."<> "; + std::cin>>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."<next) + if(head->next) { - this_node=temp; - temp=temp->next; - delete this_node; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; } - delete temp; + else + delete head; } void append_function(std::string &function_name,func &temp_func) { @@ -95,6 +100,22 @@ class func_stack end_temp->next=NULL; delete temp; } + void delete_all() + { + func_stack_unit *temp=head->next; + func_stack_unit *this_node=NULL; + head->next=NULL; + if(!temp) + return; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + return; + } }; } diff --git a/nasal_functional.h b/nasal_functional.h index ad1816b..5365e7c 100644 --- a/nasal_functional.h +++ b/nasal_functional.h @@ -4,15 +4,78 @@ #include #include +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" + namespace nasal { +bool isFloat(std::string &str) +{ + for(int i=0;i<(int)str.length();++i) + if(str[i]=='.') + return true; + return false; +} +long long int int_Str2Num(std::string &str) +{ + for(int i=0;i<(int)str.length();++i) + if(!(('0'<=str[i]) && (str[i]<='9') || (str[i]=='.'))) + { + std::cout<<"[Error] Non-numeric string."<=0;--i) + { + num+=acc*((long long int)(str[i]-'0')); + acc*=10; + } + return num; +} +double double_Str2Num(std::string &str) +{ + for(int i=0;i<(int)str.length();++i) + if(!(('0'<=str[i]) && (str[i]<='9') || (str[i]=='.'))) + { + std::cout<<"[Error] Non-numeric string."<=0;--i) + { + num+=acc*((double)(str[i]-'0')); + acc*=10; + } + return num; +} + #define FUNC_OPERATOR 1 #define FUNC_IDENTIFIER 2 #define FUNC_NUMBER 3 #define FUNC_RESERVEWORD 4 #define FUNC_STRING 5 +var_stack nasal_var_stack; +func_stack nasal_func_stack; +var temp_var; +func temp_func; + struct token_unit { std::string type; @@ -108,6 +171,7 @@ class token_list std::cout<<"Running complete."<next) { temp=temp->next; @@ -125,13 +189,162 @@ class token_list } else if(temp->type=="ReserveWord") { - ; + if(temp->content=="var") + { + if(temp->next) + { + temp=temp->next; + std::string name_of_var; + if(temp->type=="Identifier") + name_of_var=temp->content; + else + { + std::cout<<"line "<line<<": "<<"[Error] Missing identifier after \"var\"."<next) + { + temp=temp->next; + if(temp->content=="=") + { + if(temp->next) + { + temp=temp->next; + if(temp->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) + { + std::cout<<"line "<line<<": "<<"[Error] Expect a \"}\"."<next && temp->next->content==";")) + { + std::cout<<"line "<line<<": "<<"[Error] Expect a \";\" at the end of the statement."<next; + } + else + { + std::cout<<"line "<line<<": "<<"[Error] Missing value after operator = ."<line<<": "<<"[Error] Missing \"=\" after identifier."<line<<": "<<"[Error] Missing elements after identifier."<line<<": "<<"[Error] Missing identifier after \"var\"."<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 + { + std::cout<<"line "<line<<": "<<"[Error] Expect \"(\" after function print."<type=="String") { ; } } + nasal_var_stack.print_var(); + nasal_func_stack.print_function(); + nasal_var_stack.delete_all(); + nasal_func_stack.delete_all(); + std::cout<<"Running complete."< #include "nasal_functional.h" + namespace nasal { @@ -241,32 +242,33 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line) return; } -token_list lexer; +token_list nasal_lexer; -void RunProcess(std::string &FileNameOrCommand) +void RunProcess(std::string &FileName) { int Syn=0;//token type int Ptr=0;//pointer to one char in ResourcePrograme int line=1; std::string token; + nasal_lexer.delete_all(); - InputFile(FileNameOrCommand); + InputFile(FileName); while(Syn!=SCANEND && Syn!=ERRORFOUND) { Scanner(Syn,ResourcePrograme,token,Ptr,line); if(Syn==OPERATOR) - lexer.append("Operator",token,line); + nasal_lexer.append("Operator",token,line); else if(Syn==IDENTIFIER) - lexer.append("Identifier",token,line); + nasal_lexer.append("Identifier",token,line); else if(Syn==NUMBER) - lexer.append("Number",token,line); + nasal_lexer.append("Number",token,line); else if(Syn==RESERVEWORD) - lexer.append("ReserveWord",token,line); + nasal_lexer.append("ReserveWord",token,line); else if(Syn==STRING) - lexer.append("String",token,line); + nasal_lexer.append("String",token,line); } - lexer.print(); - std::cout<<">> Complete scanning \""<> Complete scanning \""<PrintList(); - else if(Var.Type=="hash") - ((NasalHash *)Var.data)->PrintHash(); - else - std::cout<<"null"; -} -void PrintVar(NasalHash &Var) -{ - Var.PrintHash(); -} -void PrintVar(NasalList &Var) -{ - Var.PrintList(); -} - void PrintString(std::string &PrintInfo) { @@ -89,57 +58,27 @@ void PrintString(std::string &PrintInfo) } return; } -/* -void PrintString(const char *PrintInfo) +void PrintVar(var Var) { - for(int i=0;i=strlen(PrintInfo)) - { - //error occurred - std::cout<<"[Error]: Missing character after \'\\\'"<PrintList(); + else if(Var.Type=="hash") + ((NasalHash *)Var.data)->PrintHash(); + else + std::cout<<"null"; } -*/ } diff --git a/nasal_var_stack.h b/nasal_var_stack.h index 793e32a..6ea83b0 100644 --- a/nasal_var_stack.h +++ b/nasal_var_stack.h @@ -30,13 +30,18 @@ class var_stack { var_stack_unit *temp=head; var_stack_unit *this_node=NULL; - while(temp->next) + if(head->next) { - this_node=temp; - temp=temp->next; - delete this_node; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; } - delete temp; + else + delete head; } void append_var(std::string &varia_name,var &temp_var) { @@ -55,16 +60,16 @@ class var_stack while(temp->next) { temp=temp->next; - std::cout<<"["<var_detail.Type<<"]: "<var_name; - PrintVar(temp->var_detail); - std::cout<var_detail.Type<<"]: "<var_name<<" : "; + if(temp->var_detail.Type!="string") + PrintVar(temp->var_detail); + else + std::cout<<*((std::string *)temp->var_detail.data); + std::cout<var_detail.Type<<"]: "<var_name; - PrintVar(temp->var_detail); - std::cout<next=NULL; delete temp; } + void delete_all() + { + var_stack_unit *temp=head->next; + var_stack_unit *this_node=NULL; + head->next=NULL; + if(!temp) + return; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + return; + } }; }