diff --git a/nasal.h b/nasal.h index 3d16a59..e5dca51 100644 --- a/nasal.h +++ b/nasal.h @@ -1,23 +1,17 @@ #ifndef __NASAL_H__ #define __NASAL_H__ -#include "nasal_hash.cpp" +#include +#include +#include "nasal_list.h" +#include "nasal_func.h" +#include "nasal_hash.h" + #include "nasal_list.cpp" -#include "nasal_print.h" +#include "nasal_hash.cpp" -// int -// char -// long long int -// double -// float -// string -// const char * -// NasalHash -// NasalList +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" +#include "nasal_lexer.h" -/* -NasalList->delete NasalList & NasalHash - -NasalHash->delete NasalList & NasalHash -*/ #endif diff --git a/nasal_func.h b/nasal_func.h new file mode 100644 index 0000000..f4d00a0 --- /dev/null +++ b/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/nasal_func_stack.h b/nasal_func_stack.h new file mode 100644 index 0000000..ea75dde --- /dev/null +++ b/nasal_func_stack.h @@ -0,0 +1,103 @@ +#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; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + 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; + } +}; + +} + + +#endif diff --git a/nasal_functional.h b/nasal_functional.h new file mode 100644 index 0000000..ad1816b --- /dev/null +++ b/nasal_functional.h @@ -0,0 +1,141 @@ +#ifndef __NASAL_FUNCTIONAL_H__ +#define __NASAL_FUNCTIONAL_H__ + +#include +#include + +namespace nasal +{ + +#define FUNC_OPERATOR 1 +#define FUNC_IDENTIFIER 2 +#define FUNC_NUMBER 3 +#define FUNC_RESERVEWORD 4 +#define FUNC_STRING 5 + +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 delete_all() + { + if(!head->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") + { + ; + } + else if(temp->type=="ReserveWord") + { + ; + } + else if(temp->type=="String") + { + ; + } + } + } +}; + +} + + +#endif diff --git a/nasal_hash.cpp b/nasal_hash.cpp index 83fb8dc..e4f0dcc 100644 --- a/nasal_hash.cpp +++ b/nasal_hash.cpp @@ -445,35 +445,22 @@ NasalHash& NasalHash::operator=(const NasalHash &Source) return *this; } -void NasalHash::SearchElement(std::string &ElementName) +HashUnit NasalHash::SearchElement(std::string &ElementName) { HashUnit *temp=head; while(temp->next) { temp=temp->next; if(temp->VarName==ElementName) - { - 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(); - return; - } + return *temp; } std::cout<<"[Error] Could not find \""< +#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 \""<next) + { + var temp_var; + temp_var.data=NULL; + temp_var.Type="null"; + temp_var.isGlobal=false; + return temp_var; + } ListUnit *NewLastUnit; while(temp->next) { @@ -819,7 +827,7 @@ NasalList NasalList::Sort(const int SortType,const int _cmp) return TempList; } -void NasalList::SearchElement(const int n) +ListUnit NasalList::SearchElement(const int n) { if(n<0) { @@ -832,12 +840,14 @@ void NasalList::SearchElement(const int n) temp=temp->next; cnt++; if(cnt==n) - { - return; - } + return *temp; } std::cout<<"[Error] Out of range: real end: "<PrintList(); + else if(Var.Type=="hash") + ((NasalHash *)Var.data)->PrintHash(); + else + std::cout<<"null"; } void PrintVar(NasalHash &Var) { @@ -88,7 +89,7 @@ void PrintString(std::string &PrintInfo) } return; } - +/* void PrintString(const char *PrintInfo) { for(int i=0;ivar_name="null"; + head->next=NULL; + } + ~var_stack() + { + var_stack_unit *temp=head; + var_stack_unit *this_node=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + 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; + PrintVar(temp->var_detail); + std::cout<var_detail.Type<<"]: "<var_name; + PrintVar(temp->var_detail); + 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; + } +}; + +} + +#endif