From fa39c6abc5a53d349d3d0b9c83a9670190db94f6 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Thu, 8 Aug 2019 02:24:49 +0800 Subject: [PATCH] version 0.1 not finished --- version0.1/lab.cpp | 39 ++ version0.1/nasal.h | 17 + version0.1/nasal_func.h | 156 +++++ version0.1/nasal_func_stack.h | 124 ++++ version0.1/nasal_functional.h | 426 ++++++++++++++ version0.1/nasal_hash.cpp | 467 +++++++++++++++ version0.1/nasal_hash.h | 41 ++ version0.1/nasal_lexer.h | 279 +++++++++ version0.1/nasal_list.cpp | 1009 +++++++++++++++++++++++++++++++++ version0.1/nasal_list.h | 60 ++ version0.1/nasal_print.h | 85 +++ version0.1/nasal_var_stack.h | 121 ++++ 12 files changed, 2824 insertions(+) create mode 100644 version0.1/lab.cpp create mode 100644 version0.1/nasal.h create mode 100644 version0.1/nasal_func.h create mode 100644 version0.1/nasal_func_stack.h create mode 100644 version0.1/nasal_functional.h create mode 100644 version0.1/nasal_hash.cpp create mode 100644 version0.1/nasal_hash.h create mode 100644 version0.1/nasal_lexer.h create mode 100644 version0.1/nasal_list.cpp create mode 100644 version0.1/nasal_list.h create mode 100644 version0.1/nasal_print.h create mode 100644 version0.1/nasal_var_stack.h diff --git a/version0.1/lab.cpp b/version0.1/lab.cpp new file mode 100644 index 0000000..417489e --- /dev/null +++ b/version0.1/lab.cpp @@ -0,0 +1,39 @@ +#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."< +#include +#include "nasal_list.h" +#include "nasal_func.h" +#include "nasal_hash.h" + +#include "nasal_list.cpp" +#include "nasal_hash.cpp" + +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" +#include "nasal_lexer.h" + +#endif diff --git a/version0.1/nasal_func.h b/version0.1/nasal_func.h new file mode 100644 index 0000000..f4d00a0 --- /dev/null +++ b/version0.1/nasal_func.h @@ -0,0 +1,156 @@ +#ifndef __NASAL_FUNC_H__ +#define __NASAL_FUNC_H__ + +#include +#include + +namespace nasal +{ + +struct func_unit +{ + std::string type; + std::string content; + int line; + func_unit *next; +}; + +class func +{ + private: + func_unit *head; + public: + func() + { + head=new func_unit; + head->type="Begin"; + head->content="__process_begin"; + head->line=0; + head->next=NULL; + } + ~func() + { + func_unit *temp=head; + func_unit *this_node=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + void delete_all() + { + if(!head->next) + return; + func_unit *temp=head; + func_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 char *_type,std::string &_content,const int _line) + { + func_unit *temp=head; + while(temp->next) + temp=temp->next; + temp->next=new func_unit; + temp=temp->next; + temp->next=NULL; + temp->type=_type; + temp->line=_line; + temp->content=_content; + return; + } + void print() + { + func_unit *temp=head; + std::cout<<"line "<line<<": "<<"( ProcessBegin | "<content<<" )"<next) + return; + while(temp->next) + { + temp=temp->next; + std::cout<<"line "<line<<": "; + if(temp->type=="Operator") + std::cout<<"( Operator | "; + else if(temp->type=="Identifier") + std::cout<<"( Identifier | "; + else if(temp->type=="Number") + std::cout<<"( Number | "; + else if(temp->type=="ReserveWord") + std::cout<<"( ReserveWord | "; + else if(temp->type=="String") + std::cout<<"( String | "; + std::cout<content<<" )"<next) + { + ptemp=ptemp->next; + temp=new func_unit; + temp->type=ptemp->type; + temp->content=ptemp->content; + temp->line=ptemp->line; + temp->next=NULL; + } + temp=new func_unit; + temp->type=ptemp->type; + temp->content=ptemp->content; + temp->line=ptemp->line; + temp->next=NULL; + return *this; + } + void run() + { + func_unit *temp=head; + if(!head->next) + { + std::cout<<"Running complete."<next) + { + temp=temp->next; + if(temp->type=="Operator") + { + ; + } + else if(temp->type=="Identifier") + { + ; + } + else if(temp->type=="Number") + { + ; + } + else if(temp->type=="ReserveWord") + { + ; + } + else if(temp->type=="String") + { + ; + } + } + return; + } +}; + +} + +#endif diff --git a/version0.1/nasal_func_stack.h b/version0.1/nasal_func_stack.h new file mode 100644 index 0000000..a48d2a8 --- /dev/null +++ b/version0.1/nasal_func_stack.h @@ -0,0 +1,124 @@ +#ifndef __NASAL_FUNC_STACK_H__ +#define __NASAL_FUNC_STACK_H__ + +#include "nasal_func.h" + + +namespace nasal +{ + +struct func_stack_unit +{ + std::string func_name; + func func_statement; + func_stack_unit *next; +}; + +class func_stack +{ + private: + func_stack_unit *head; + public: + func_stack() + { + head=new func_stack_unit; + head->func_name="null"; + head->next=NULL; + } + ~func_stack() + { + func_stack_unit *temp=head; + func_stack_unit *this_node=NULL; + if(head->next) + { + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + else + delete head; + } + void append_function(std::string &function_name,func &temp_func) + { + func_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->func_name==function_name) + { + std::cout<<"[Error] Redeclaration of function \""<next=new func_stack_unit; + temp=temp->next; + temp->next=NULL; + temp->func_statement=temp_func; + return; + } + void run_function(std::string &function_name) + { + func_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->func_name==function_name) + { + temp->func_statement.run(); + return; + } + } + std::cout<<"[Error] Could not find this function."<next) + { + temp=temp->next; + std::cout<<"function: "<func_name<func_name<next) + return; + while(temp->next) + { + end_temp=temp; + temp=temp->next; + } + 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; + } +}; + +} + + +#endif diff --git a/version0.1/nasal_functional.h b/version0.1/nasal_functional.h new file mode 100644 index 0000000..7d33590 --- /dev/null +++ b/version0.1/nasal_functional.h @@ -0,0 +1,426 @@ +#ifndef __NASAL_FUNCTIONAL_H__ +#define __NASAL_FUNCTIONAL_H__ + +#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; + std::string content; + int line; + token_unit *next; +}; + +class token_list +{ + private: + token_unit *head; + public: + token_list() + { + head=new token_unit; + head->type="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 char *_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=="Operator") + std::cout<<"( Operator | "; + else if(temp->type=="Identifier") + std::cout<<"( Identifier | "; + else if(temp->type=="Number") + std::cout<<"( Number | "; + else if(temp->type=="ReserveWord") + std::cout<<"( ReserveWord | "; + else if(temp->type=="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."<data=NULL; + head->next=NULL; +} + +NasalHash::NasalHash(NasalHash &Source) +{ + HashUnit *temp; + HashUnit *SourceTemp=Source.head; + + head=new HashUnit; + head->next=NULL; + head->data=NULL; + + temp=head; + while(SourceTemp->next) + { + SourceTemp=SourceTemp->next; + temp->next=new HashUnit; + temp=temp->next; + temp->Type=SourceTemp->Type; + temp->VarName=SourceTemp->VarName; + if(temp->Type=="int") + { + temp->data=new int; + *((int *)temp->data)=*((int *)SourceTemp->data); + } + else if(temp->Type=="float") + { + temp->data=new float; + *((float *)temp->data)=*((float *)SourceTemp->data); + } + else if(temp->Type=="double") + { + temp->data=new double; + *((double *)temp->data)=*((double *)SourceTemp->data); + } + else if(temp->Type=="char") + { + temp->data=new char; + *((char *)temp->data)=*((char *)SourceTemp->data); + } + else if(temp->Type=="long long int") + { + temp->data=new long long int; + *((long long int *)temp->data)=*((long long int *)SourceTemp->data); + } + else if(temp->Type=="string") + { + temp->data=new std::string; + *((std::string *)temp->data)=*((std::string *)SourceTemp->data); + } + else if(temp->Type=="array") + { + temp->data=new NasalList; + *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); + } + else if(temp->Type=="hash") + { + temp->data=new NasalHash; + *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); + } + temp->next=NULL; + } +} + +NasalHash::~NasalHash() +{ + HashUnit *temp=head; + while(temp->next) + { + head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + temp=head; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; +} + +void NasalHash::PrintHash() +{ + HashUnit *temp=head; + std::cout<<"{ "; + while(temp->next) + { + temp=temp->next; + std::cout<VarName<<":"; + if(temp->next) + { + if(temp->Type=="int") + std::cout<<*((int *)temp->data)<<", "; + else if(temp->Type=="float") + std::cout<<*((float *)temp->data)<<", "; + else if(temp->Type=="double") + std::cout<<*((double *)temp->data)<<", "; + else if(temp->Type=="char") + std::cout<<"\""<<*((char *)temp->data)<<"\", "; + else if(temp->Type=="long long int") + std::cout<<*((long long int *)temp->data)<<", "; + else if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; + else if(temp->Type=="array") + { + ((NasalList *)temp->data)->PrintList(); + std::cout<<", "; + } + else if(temp->Type=="hash") + { + ((NasalHash *)temp->data)->PrintHash(); + std::cout<<", "; + } + } + else + { + if(temp->Type=="int") + std::cout<<*((int *)temp->data); + else if(temp->Type=="float") + std::cout<<*((float *)temp->data); + else if(temp->Type=="double") + std::cout<<*((double *)temp->data); + else if(temp->Type=="char") + std::cout<<"\""<<*((char *)temp->data)<<"\""; + else if(temp->Type=="long long int") + std::cout<<*((long long int *)temp->data); + else if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\""; + else if(temp->Type=="array") + ((NasalList *)temp->data)->PrintList(); + else if(temp->Type=="hash") + ((NasalHash *)temp->data)->PrintHash(); + } + } + std::cout<<"}"; +} + +void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeName) +{ + NasalHash TempHash;//sometimes user may use n.append(n) + if(TypeName=="hash") + TempHash=*((NasalHash *)AppendData); + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + } + HashUnit *NewHashMember=new HashUnit; + NewHashMember->data=NULL; + temp->next=NewHashMember; + NewHashMember->VarName=VariaName; + NewHashMember->Type=TypeName; + if(TypeName=="int") + { + NewHashMember->data=new int; + *((int *)NewHashMember->data)=*((int *)AppendData); + } + else if(TypeName=="float") + { + NewHashMember->data=new float; + *((float *)NewHashMember->data)=*((float *)AppendData); + } + else if(TypeName=="double") + { + NewHashMember->data=new double; + *((double *)NewHashMember->data)=*((double *)AppendData); + } + else if(TypeName=="char") + { + NewHashMember->data=new char; + *((char *)NewHashMember->data)=*((char *)AppendData); + } + else if(TypeName=="long long int") + { + NewHashMember->data=new long long int; + *((long long int *)NewHashMember->data)=*((long long int *)AppendData); + } + else if(TypeName=="string") + { + NewHashMember->data=new std::string; + *((std::string *)NewHashMember->data)=*((std::string *)AppendData); + } + else if(TypeName=="array") + { + NewHashMember->data=new NasalList; + *((NasalList *)NewHashMember->data)=*((NasalList *)AppendData); + } + else if(TypeName=="hash") + { + NewHashMember->data=new NasalHash; + *((NasalHash *)NewHashMember->data)=TempHash; + } + else if(TypeName=="var") + { + NewHashMember->Type=(*(var *)AppendData).Type; + if((*(var *)AppendData).Type=="int") + { + NewHashMember->data=new int; + *((int *)NewHashMember->data)=*((int *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="float") + { + NewHashMember->data=new float; + *((float *)NewHashMember->data)=*((float *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="double") + { + NewHashMember->data=new double; + *((double *)NewHashMember->data)=*((double *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="char") + { + NewHashMember->data=new char; + *((char *)NewHashMember->data)=*((char *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="long long int") + { + NewHashMember->data=new long long int; + *((long long int *)NewHashMember->data)=*((long long int *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="string") + { + NewHashMember->data=new std::string; + *((std::string *)NewHashMember->data)=*((std::string *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="array") + { + NewHashMember->data=new NasalList; + *((NasalList *)NewHashMember->data)=*((NasalList *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="hash") + { + NewHashMember->data=new NasalHash; + *((NasalHash *)NewHashMember->data)=*((NasalHash *)(*(var *)AppendData).data); + } + } + NewHashMember->next=NULL; +} +int NasalHash::Contains(const char *VariaName) +{ + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->VarName==VariaName) + return 1; + } + return 0; +} + +NasalList NasalHash::Keys() +{ + NasalList FeedBackList; + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + FeedBackList.Append(&(temp->VarName),"string"); + } + return FeedBackList; +} + +void NasalHash::Delete(const char *VariaName) +{ + HashUnit *temp=head; + HashUnit *LastNode; + while(temp->next) + { + LastNode=temp; + temp=temp->next; + if(temp->VarName==VariaName) + { + LastNode->next=temp->next; + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + delete temp; + return; + } + } + std::cout<<"[Error]: Could not find this element \""<next) + { + head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + temp=head; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + head=new HashUnit; + head->next=NULL; + + temp=head; + while(SourceTemp->next) + { + SourceTemp=SourceTemp->next; + temp->next=new HashUnit; + temp=temp->next; + temp->Type=SourceTemp->Type; + temp->VarName=SourceTemp->VarName; + if(temp->Type=="int") + { + temp->data=new int; + *((int *)temp->data)=*((int *)SourceTemp->data); + } + else if(temp->Type=="float") + { + temp->data=new float; + *((float *)temp->data)=*((float *)SourceTemp->data); + } + else if(temp->Type=="double") + { + temp->data=new double; + *((double *)temp->data)=*((double *)SourceTemp->data); + } + else if(temp->Type=="char") + { + temp->data=new char; + *((char *)temp->data)=*((char *)SourceTemp->data); + } + else if(temp->Type=="long long int") + { + temp->data=new long long int; + *((long long int *)temp->data)=*((long long int *)SourceTemp->data); + } + else if(temp->Type=="string") + { + temp->data=new std::string; + *((std::string *)temp->data)=*((std::string *)SourceTemp->data); + } + else if(temp->Type=="array") + { + temp->data=new NasalList; + *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); + } + else if(temp->Type=="hash") + { + temp->data=new NasalHash; + *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); + } + temp->next=NULL; + } + return *this; +} + +HashUnit NasalHash::SearchElement(std::string &ElementName) +{ + HashUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->VarName==ElementName) + return *temp; + } + std::cout<<"[Error] Could not find \""< +#include +#include "nasal_list.h" + +namespace nasal +{ +#ifndef nil +#define nil -1 +#endif + +struct HashUnit +{ + std::string VarName; + std::string Type; + void *data; + HashUnit* next; +}; + +class NasalHash +{ + private: + HashUnit *head; + public: + NasalHash(); + NasalHash(NasalHash&); + ~NasalHash(); + void PrintHash(); + void Append(const char *,void *,const char *); + int Contains(const char *); + NasalList Keys(); + void Delete(const char *); + NasalHash& operator=(const NasalHash&); + HashUnit SearchElement(std::string &); +}; + +} + +#endif diff --git a/version0.1/nasal_lexer.h b/version0.1/nasal_lexer.h new file mode 100644 index 0000000..ce7875a --- /dev/null +++ b/version0.1/nasal_lexer.h @@ -0,0 +1,279 @@ +#ifndef __NASAL_LEXER_H__ +#define __NASAL_LEXER_H__ + + +#include +#include +#include +#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 //异常错误 + +std::string ReserveWord[26]= +{ + "for","foreach","forindex","while", + "var","func","break","continue","return", + "if","else","elsif","nil","and","or", + "print","cmp","append","setsize","subvec","pop", + "sort","contains","delete","keys","typeof" +}; + +std::string OperatorOrDelimiter[40]= +{ + "+","-","*","/","=","+=","-=","*=","/=", + "\n","\t","\r","\\","\'","\"",".", + "<","<=",">",">=","==","!=","~=","!","~", + ",",";","(",")","[","]","{","}","#","?",":", + "&","|","%","^" +}; + +std::string IdentifierTable[1000]={""}; +char ResourcePrograme[16777216]; + +int isReserveWord(std::string &p) +{ + for(int i=0;i<26;++i) + if(ReserveWord[i]==p) + return i+1; + return FAIL; +} + +int isOperatorOrDelimiter(std::string &p) +{ + for(int i=0;i<40;++i) + if(OperatorOrDelimiter[i]==p) + return i+1; + return FAIL; +} + +bool isLetter(char t) +{ + return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z')); +} + +bool isNumber(char t) +{ + return (('0'<=t) && (t<='9')); +} + +void InputFile(std::string &FileName) +{ + std::ifstream fin(FileName); + if(fin.fail()) + { + std::cout<<"[Error] Failed to load file: "<127) + { + ++ptr; + if(temp=='\n') + ++line; + temp=Source[ptr]; + } + + token=""; + if(isLetter(temp) || temp=='_') + { + token+=temp; + ++ptr; + temp=Source[ptr]; + while(isLetter(temp) || isNumber(temp) || temp=='_') + { + token+=temp; + ++ptr; + temp=Source[ptr]; + } + Syn=isReserveWord(token); + if(Syn==FAIL) + Syn=IDENTIFIER; + else + Syn=RESERVEWORD; + } + else if(isNumber(temp)) + { + int PointCnt=0; + while(isNumber(temp)) + { + token+=temp; + ++ptr; + temp=Source[ptr]; + if(temp=='.' && !PointCnt) + { + ++PointCnt; + token+=temp; + ++ptr; + temp=Source[ptr]; + } + } + Syn=NUMBER; + } + else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || + temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' || + temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&'|| + temp=='%' || temp=='$' || temp=='^') + { + token+=temp; + ++ptr; + Syn=OPERATOR; + } + else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~') + { + Syn=OPERATOR; + token+=temp; + ++ptr; + temp=Source[ptr]; + if(temp=='=') + { + token+=temp; + ++ptr; + } + } + else if(temp=='\\') + { + Syn=OPERATOR; + token+=temp; + ++ptr; + temp=Source[ptr]; + if(temp=='=' || temp=='n' || temp=='t' || temp=='r' || temp=='\\' || temp=='\'' || temp=='\"') + { + token+=temp; + ++ptr; + } + } + else if(temp=='\"') + { + Syn=STRING; + token+=temp; + ++ptr; + temp=Source[ptr]; + while(temp!='\"') + { + if(temp=='\\') + { + token+=temp; + + ++ptr; + temp=Source[ptr]; + token+=temp; + + ++ptr; + temp=Source[ptr]; + } + else + { + token+=temp; + ++ptr; + temp=Source[ptr]; + } + if(temp=='@' || temp=='\n') + break; + } + //add the last char \" + if(temp=='\"') + { + token+=temp; + ++ptr; + } + else + token+=" __missing_end_of_string"; + } + else if(temp=='@') + { + Syn=SCANEND; + return; + } + else + { + Syn=FAIL; + std::cout<<"[Error] Unexpected error occurred: "<> Complete scanning \""<data=NULL; + head->next=NULL; +} + +NasalList::NasalList(NasalList &Source) +{ + ListUnit *temp; + ListUnit *SourceTemp=Source.head; + + head=new ListUnit; + head->next=NULL; + + temp=head; + while(SourceTemp->next) + { + SourceTemp=SourceTemp->next; + temp->next=new ListUnit; + temp=temp->next; + temp->Type=SourceTemp->Type; + if(temp->Type=="int") + { + temp->data=new int; + *((int *)temp->data)=*((int *)SourceTemp->data); + } + else if(temp->Type=="float") + { + temp->data=new float; + *((float *)temp->data)=*((float *)SourceTemp->data); + } + else if(temp->Type=="double") + { + temp->data=new double; + *((double *)temp->data)=*((double *)SourceTemp->data); + } + else if(temp->Type=="char") + { + temp->data=new char; + *((char *)temp->data)=*((char *)SourceTemp->data); + } + else if(temp->Type=="long long int") + { + temp->data=new long long int; + *((long long int *)temp->data)=*((long long int *)SourceTemp->data); + } + else if(temp->Type=="string") + { + temp->data=new std::string; + *((std::string *)temp->data)=*((std::string *)SourceTemp->data); + } + else if(temp->Type=="array") + { + temp->data=new NasalList; + *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); + } + else if(temp->Type=="hash") + { + temp->data=new NasalHash; + *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); + } + else if(temp->Type=="null") + { + temp->data=NULL; + } + temp->next=NULL; + } +} + +NasalList::~NasalList() +{ + ListUnit *temp=head; + while(temp->next) + { + head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + temp=head; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; +} + +void NasalList::PrintList() +{ + ListUnit *temp=head; + std::cout<<"[ "; + while(temp->next) + { + temp=temp->next; + if(temp->next) + { + if(temp->Type=="int") + std::cout<<*((int *)temp->data)<<", "; + else if(temp->Type=="float") + std::cout<<*((float *)temp->data)<<", "; + else if(temp->Type=="double") + std::cout<<*((double *)temp->data)<<", "; + else if(temp->Type=="char") + std::cout<<"\""<<*((char *)temp->data)<<"\", "; + else if(temp->Type=="long long int") + std::cout<<*((long long int *)temp->data)<<", "; + else if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; + else if(temp->Type=="array") + { + ((NasalList *)temp->data)->PrintList(); + std::cout<<", "; + } + else if(temp->Type=="hash") + { + ((NasalHash *)temp->data)->PrintHash(); + std::cout<<", "; + } + else if(temp->Type=="null") + { + std::cout<<"NULL, "; + } + } + else + { + if(temp->Type=="int") + std::cout<<*((int *)temp->data); + else if(temp->Type=="float") + std::cout<<*((float *)temp->data); + else if(temp->Type=="double") + std::cout<<*((double *)temp->data); + else if(temp->Type=="char") + std::cout<<"\""<<*((char *)temp->data)<<"\""; + else if(temp->Type=="long long int") + std::cout<<*((long long int *)temp->data); + else if(temp->Type=="string") + std::cout<<"\""<<*((std::string *)temp->data)<<"\""; + else if(temp->Type=="array") + ((NasalList *)temp->data)->PrintList(); + else if(temp->Type=="hash") + ((NasalHash *)temp->data)->PrintHash(); + else if(temp->Type=="null") + std::cout<<"NULL"; + } + } + std::cout<<"]"; +} + +void NasalList::Append(void *AppendData,const char *TypeName) +{ + NasalList TempList;//sometimes user may use n.append(n) + if(TypeName=="array") + TempList=*((NasalList *)AppendData); + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + } + ListUnit *NewListMember=new ListUnit; + temp->next=NewListMember; + NewListMember->data=NULL; + NewListMember->Type=TypeName; + if(TypeName=="int") + { + NewListMember->data=new int; + *((int *)NewListMember->data)=*((int *)AppendData); + } + else if(TypeName=="float") + { + NewListMember->data=new float; + *((float *)NewListMember->data)=*((float *)AppendData); + } + else if(TypeName=="double") + { + NewListMember->data=new double; + *((double *)NewListMember->data)=*((double *)AppendData); + } + else if(TypeName=="char") + { + NewListMember->data=new char; + *((char *)NewListMember->data)=*((char *)AppendData); + } + else if(TypeName=="long long int") + { + NewListMember->data=new long long int; + *((long long int *)NewListMember->data)=*((long long int *)AppendData); + } + else if(TypeName=="string") + { + NewListMember->data=new std::string; + *((std::string *)NewListMember->data)=*((std::string *)AppendData); + } + else if(TypeName=="array") + { + NewListMember->data=new NasalList; + *((NasalList *)NewListMember->data)=TempList; + } + else if(TypeName=="hash") + { + NewListMember->data=new NasalHash; + *((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData); + } + else if(TypeName=="var") + { + NewListMember->Type=(*(var *)AppendData).Type; + if((*(var *)AppendData).Type=="int") + { + NewListMember->data=new int; + *((int *)NewListMember->data)=*((int *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="float") + { + NewListMember->data=new float; + *((float *)NewListMember->data)=*((float *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="double") + { + NewListMember->data=new double; + *((double *)NewListMember->data)=*((double *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="char") + { + NewListMember->data=new char; + *((char *)NewListMember->data)=*((char *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="long long int") + { + NewListMember->data=new long long int; + *((long long int *)NewListMember->data)=*((long long int *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="string") + { + NewListMember->data=new std::string; + *((std::string *)NewListMember->data)=*((std::string *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="array") + { + NewListMember->data=new NasalList; + *((NasalList *)NewListMember->data)=*((NasalList *)(*(var *)AppendData).data); + } + else if((*(var *)AppendData).Type=="hash") + { + NewListMember->data=new NasalHash; + *((NasalHash *)NewListMember->data)=*((NasalHash *)(*(var *)AppendData).data); + } + } + NewListMember->next=NULL; +} + +void NasalList::Append(void *AppendData,std::string &TypeName) +{ + NasalList TempList;//sometimes user may use n.append(n) + if(TypeName=="array") + TempList=*((NasalList *)AppendData); + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + } + ListUnit *NewListMember=new ListUnit; + temp->next=NewListMember; + NewListMember->data=NULL; + NewListMember->Type=TypeName; + if(TypeName=="int") + { + NewListMember->data=new int; + *((int *)NewListMember->data)=*((int *)AppendData); + } + else if(TypeName=="float") + { + NewListMember->data=new float; + *((float *)NewListMember->data)=*((float *)AppendData); + } + else if(TypeName=="double") + { + NewListMember->data=new double; + *((double *)NewListMember->data)=*((double *)AppendData); + } + else if(TypeName=="char") + { + NewListMember->data=new char; + *((char *)NewListMember->data)=*((char *)AppendData); + } + else if(TypeName=="long long int") + { + NewListMember->data=new long long int; + *((long long int *)NewListMember->data)=*((long long int *)AppendData); + } + else if(TypeName=="string") + { + NewListMember->data=new std::string; + *((std::string *)NewListMember->data)=*((std::string *)AppendData); + } + else if(TypeName=="array") + { + NewListMember->data=new NasalList; + *((NasalList *)NewListMember->data)=TempList; + } + else if(TypeName=="hash") + { + NewListMember->data=new NasalHash; + *((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData); + } + NewListMember->next=NULL; +} + +NasalList& NasalList::operator=(const NasalList &Source) +{ + ListUnit *temp=head; + ListUnit *SourceTemp=Source.head; + while(temp->next) + { + head=temp->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + temp=head; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + head=new ListUnit; + head->next=NULL; + + temp=head; + while(SourceTemp->next) + { + SourceTemp=SourceTemp->next; + temp->next=new ListUnit; + temp=temp->next; + temp->Type=SourceTemp->Type; + if(temp->Type=="int") + { + temp->data=new int; + *((int *)temp->data)=*((int *)SourceTemp->data); + } + else if(temp->Type=="float") + { + temp->data=new float; + *((float *)temp->data)=*((float *)SourceTemp->data); + } + else if(temp->Type=="double") + { + temp->data=new double; + *((double *)temp->data)=*((double *)SourceTemp->data); + } + else if(temp->Type=="char") + { + temp->data=new char; + *((char *)temp->data)=*((char *)SourceTemp->data); + } + else if(temp->Type=="long long int") + { + temp->data=new long long int; + *((long long int *)temp->data)=*((long long int *)SourceTemp->data); + } + else if(temp->Type=="string") + { + temp->data=new std::string; + *((std::string *)temp->data)=*((std::string *)SourceTemp->data); + } + else if(temp->Type=="array") + { + temp->data=new NasalList; + *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); + } + else if(temp->Type=="hash") + { + temp->data=new NasalHash; + *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); + } + else if(temp->Type=="null") + { + temp->data=NULL; + } + temp->next=NULL; + } + return *this; +} + +void NasalList::SetSize(const int Size) +{ + if(Size==0) + { + ListUnit *temp=head; + ListUnit *DeleteNode; + temp=temp->next; + head->next=NULL; + while(temp->next) + { + DeleteNode=temp; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + temp=temp->next; + delete DeleteNode; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + return; + } + int cnt=0; + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + ++cnt; + if(cnt==Size) + { + ListUnit *DeleteNode=temp->next; + temp->next=NULL; + temp=DeleteNode; + while(DeleteNode->next) + { + DeleteNode=DeleteNode->next; + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + temp=DeleteNode; + } + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + return; + } + } + while(cntnext=new ListUnit; + temp=temp->next; + temp->Type="null"; + temp->data=NULL; + ++cnt; + } + temp->next=NULL; + return; +} +NasalList NasalList::SubVec(const int start,const int length=nil) +{ + int cnt=-1; + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + ++cnt; + if(cnt==start) + { + NasalList NewNasalList; + while(temp->next) + { + NewNasalList.Append(temp->data,temp->Type); + temp=temp->next; + if(cnt==length) + return NewNasalList; + ++cnt; + } + if(length!=nil&&cntdata,temp->Type); + return NewNasalList; + } + } + std::cout<<"[Error] Starting place out of range: (start) "<next) + { + var temp_var; + temp_var.data=NULL; + temp_var.Type="null"; + temp_var.isGlobal=false; + return temp_var; + } + ListUnit *NewLastUnit; + while(temp->next) + { + NewLastUnit=temp; + temp=temp->next; + } + NewLastUnit->next=NULL; + var TempVar; + TempVar.Type=temp->Type; + if(TempVar.Type=="int") + { + TempVar.data=new int; + *((int *)TempVar.data)=*((int *)temp->data); + } + else if(TempVar.Type=="float") + { + TempVar.data=new float; + *((float *)TempVar.data)=*((float *)temp->data); + } + else if(TempVar.Type=="double") + { + TempVar.data=new double; + *((double *)TempVar.data)=*((double *)temp->data); + } + else if(TempVar.Type=="char") + { + TempVar.data=new char; + *((char *)TempVar.data)=*((char *)temp->data); + } + else if(TempVar.Type=="long long int") + { + TempVar.data=new long long int; + *((long long int *)TempVar.data)=*((long long int *)temp->data); + } + else if(TempVar.Type=="string") + { + TempVar.data=new std::string; + *((std::string *)TempVar.data)=*((std::string *)temp->data); + } + else if(TempVar.Type=="array") + { + TempVar.data=new NasalList; + *((NasalList *)TempVar.data)=*((NasalList *)temp->data); + } + else if(TempVar.Type=="hash") + { + TempVar.data=new NasalHash; + *((NasalHash *)TempVar.data)=*((NasalHash*)temp->data); + } + else if(TempVar.Type=="null") + TempVar.data=NULL; + + if(temp->data) + { + if(temp->Type=="int") + delete (int *)temp->data; + else if(temp->Type=="float") + delete (float *)temp->data; + else if(temp->Type=="double") + delete (double *)temp->data; + else if(temp->Type=="char") + delete (char *)temp->data; + else if(temp->Type=="long long int") + delete (long long int *)temp->data; + else if(temp->Type=="string") + delete (std::string *)temp->data; + else if(temp->Type=="array") + delete (NasalList *)temp->data; + else if(temp->Type=="hash") + delete (NasalHash *)temp->data; + } + delete temp; + + return TempVar; +} + +#ifndef __SORT_TYPE__ +#define SORT_INT 1 +#define SORT_STRING 2 +#endif + +NasalList NasalList::Sort(const int SortType,const int _cmp) +{ + NasalList TempList; + if(SortType==SORT_INT) + { + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->Type!="int") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be int."<Type!="int") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be int."<next; + while(FirstTempThis->next) + { + NodeThis=FirstTempThis; + SecondTempThis=FirstTempThis->next; + while(SecondTempThis->next) + { + if(_cmp>0 && *((int *)NodeThis->data)>*((int *)SecondTempThis->data))//from small to large + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((int *)NodeThis->data)<*((int *)SecondTempThis->data))//from large to small + { + NodeThis=SecondTempThis; + } + SecondTempThis=SecondTempThis->next; + } + if(_cmp>0 && *((int *)NodeThis->data)>*((int *)SecondTempThis->data))//from small to large func(a,b) a-b + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((int *)NodeThis->data)<*((int *)SecondTempThis->data))//from large to small func(a,b) b-a + { + NodeThis=SecondTempThis; + } + if(NodeThis!=FirstTempThis) + { + int t; + t=*((int *)FirstTempThis->data); + *((int *)FirstTempThis->data)=*((int *)NodeThis->data); + *((int *)NodeThis->data)=t; + } + FirstTempThis=FirstTempThis->next; + } + } + else if(SortType==SORT_STRING) + { + ListUnit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->Type!="string") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be string."<Type!="string") + { + std::cout<<"[Error] Incorrect type inside: "<Type<<".But type must be string."<next; + while(FirstTempThis->next) + { + NodeThis=FirstTempThis; + SecondTempThis=FirstTempThis->next; + while(SecondTempThis->next) + { + if(_cmp>0 && *((std::string *)NodeThis->data)>*((std::string *)SecondTempThis->data))//from small to large + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((std::string *)NodeThis->data)<*((std::string *)SecondTempThis->data))//from large to small + { + NodeThis=SecondTempThis; + } + SecondTempThis=SecondTempThis->next; + } + if(_cmp>0 && *((std::string *)NodeThis->data)>*((std::string *)SecondTempThis->data))//from small to large func(a,b) cmp(a,b) + { + NodeThis=SecondTempThis; + } + else if(_cmp<=0 && *((std::string *)NodeThis->data)<*((std::string *)SecondTempThis->data))//from large to small func(a,b) -cmp(a,b) or cmp(b,a) + { + NodeThis=SecondTempThis; + } + if(NodeThis!=FirstTempThis) + { + std::string t; + t=*((std::string *)FirstTempThis->data); + *((std::string *)FirstTempThis->data)=*((std::string *)NodeThis->data); + *((std::string *)NodeThis->data)=t; + } + FirstTempThis=FirstTempThis->next; + } + } + return TempList; +} + +ListUnit NasalList::SearchElement(const int n) +{ + if(n<0) + { + std::cout<<"[Error] "<next) + { + temp=temp->next; + cnt++; + if(cnt==n) + return *temp; + } + std::cout<<"[Error] Out of range: real end: "< +#include + + +namespace nasal +{ +#ifndef nil +#define nil -1 +#endif + +class var; + +struct ListUnit +{ + std::string Type; + void *data; + ListUnit* next; +}; + +class NasalList +{ + private: + ListUnit *head; + public: + NasalList(); + NasalList(NasalList &); + ~NasalList(); + void PrintList(); + void Append(void *,const char *); + void Append(void *,std::string &); + NasalList& operator=(const NasalList &); + void SetSize(const int); + NasalList SubVec(const int,const int); + var Pop(); + NasalList Sort(const int,const int); + ListUnit SearchElement(const int); +}; + +class var +{ + public: + std::string Type; + void *data; + bool isGlobal; + var() + { + data=NULL; + } + var(const var &); + ~var(); + var& operator=(const var &); + void Print(); +}; + +} + +#endif diff --git a/version0.1/nasal_print.h b/version0.1/nasal_print.h new file mode 100644 index 0000000..95b880c --- /dev/null +++ b/version0.1/nasal_print.h @@ -0,0 +1,85 @@ +#ifndef __NASAL_PRINT_H__ +#define __NASAL_PRINT_H__ + +#include +#include +#include "nasal_hash.cpp" +#include "nasal_list.cpp" + +namespace nasal +{ + +void PrintString(std::string &PrintInfo) +{ + for(int i=0;i<(int)PrintInfo.length();++i) + { + if(PrintInfo[i]=='\\' && i+1<(int)PrintInfo.length()) + { + switch(PrintInfo[i+1]) + { + case 'n': + std::cout<<"\n"; + ++i; + break; + case 't': + std::cout<<"\t"; + ++i; + break; + case 'r': + std::cout<<"\r"; + ++i; + break; + case '\\': + std::cout<<"\\"; + ++i; + break; + case '\'': + std::cout<<"\'"; + ++i; + break; + case '\"': + std::cout<<"\""; + ++i; + break; + default: + //error occurred + std::cout<<"[Error]: Incorrect escape character \'"<=(int)PrintInfo.length()) + { + //error occurred + std::cout<<"[Error]: Missing character after \'\\\'"; + } + else + std::cout<PrintList(); + else if(Var.Type=="hash") + ((NasalHash *)Var.data)->PrintHash(); + else + std::cout<<"null"; +} + +} + +#endif diff --git a/version0.1/nasal_var_stack.h b/version0.1/nasal_var_stack.h new file mode 100644 index 0000000..6ea83b0 --- /dev/null +++ b/version0.1/nasal_var_stack.h @@ -0,0 +1,121 @@ +#ifndef __NASAL_VAR_STACK_H__ +#define __NASAL_VAR_STACK_H__ + +#include "nasal_list.h" +#include "nasal_print.h" + + +namespace nasal +{ + +struct var_stack_unit +{ + std::string var_name; + var var_detail; + var_stack_unit *next; +}; + +class var_stack +{ + private: + var_stack_unit *head; + public: + var_stack() + { + head=new var_stack_unit; + head->var_name="null"; + head->next=NULL; + } + ~var_stack() + { + var_stack_unit *temp=head; + var_stack_unit *this_node=NULL; + if(head->next) + { + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + else + delete head; + } + void append_var(std::string &varia_name,var &temp_var) + { + var_stack_unit *temp=head; + while(temp->next) + temp=temp->next; + temp->next=new var_stack_unit; + temp=temp->next; + temp->var_name=varia_name; + temp->var_detail=temp_var; + temp->next=NULL; + } + void print_var() + { + var_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + 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<next) + { + temp=temp->next; + if(temp->var_name==varia_name) + temp_var=temp->var_detail; + } + return temp_var; + } + void pop_var() + { + var_stack_unit *temp=head; + var_stack_unit *end_temp; + if(!head->next) + return; + while(temp->next) + { + end_temp=temp; + temp=temp->next; + } + end_temp->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; + } +}; + +} + +#endif