From 9d56a418c85d35004422eeb9c31a7987d3687e34 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 12 Aug 2019 23:22:02 +0800 Subject: [PATCH] Parser is really comming. --- version0.3/ebnf.cpp | 110 ++++++++++++ version0.3/ide.txt | 27 +++ version0.3/nasal.h | 35 ++++ version0.3/nasal_func.cpp | 174 ++++++++++++++++++ version0.3/nasal_func.h | 36 ++++ version0.3/nasal_func_stack.h | 116 ++++++++++++ version0.3/nasal_functional.h | 62 +++++++ version0.3/nasal_hash.cpp | 113 ++++++++++++ version0.3/nasal_hash.h | 27 +++ version0.3/nasal_interpreter.cpp | 53 ++++++ version0.3/nasal_lexer.h | 272 ++++++++++++++++++++++++++++ version0.3/nasal_list.cpp | 300 +++++++++++++++++++++++++++++++ version0.3/nasal_list.h | 33 ++++ version0.3/nasal_parse.h | 59 ++++++ version0.3/nasal_print.h | 76 ++++++++ version0.3/nasal_token.cpp | 108 +++++++++++ version0.3/nasal_token.h | 38 ++++ version0.3/nasal_var.cpp | 140 +++++++++++++++ version0.3/nasal_var.h | 24 +++ version0.3/nasal_var_stack.h | 151 ++++++++++++++++ 20 files changed, 1954 insertions(+) create mode 100644 version0.3/ebnf.cpp create mode 100644 version0.3/ide.txt create mode 100644 version0.3/nasal.h create mode 100644 version0.3/nasal_func.cpp create mode 100644 version0.3/nasal_func.h create mode 100644 version0.3/nasal_func_stack.h create mode 100644 version0.3/nasal_functional.h create mode 100644 version0.3/nasal_hash.cpp create mode 100644 version0.3/nasal_hash.h create mode 100644 version0.3/nasal_interpreter.cpp create mode 100644 version0.3/nasal_lexer.h create mode 100644 version0.3/nasal_list.cpp create mode 100644 version0.3/nasal_list.h create mode 100644 version0.3/nasal_parse.h create mode 100644 version0.3/nasal_print.h create mode 100644 version0.3/nasal_token.cpp create mode 100644 version0.3/nasal_token.h create mode 100644 version0.3/nasal_var.cpp create mode 100644 version0.3/nasal_var.h create mode 100644 version0.3/nasal_var_stack.h diff --git a/version0.3/ebnf.cpp b/version0.3/ebnf.cpp new file mode 100644 index 0000000..19cba5f --- /dev/null +++ b/version0.3/ebnf.cpp @@ -0,0 +1,110 @@ +/* +<>术语符号 +[]最多出现一次 +{}可以出现任意次,包括0次 +()分组 +|并列选项,只能选一个 +*/ + + ::= + ::= + ::= + ::= + ::= + ::= + ::= + ::= | + ::= [<(> { <,> } <)>] <{> { <;> } <}> + ::= || + ::= <[> { <,> |||| } <]> + ::= <{> { <:> |||| } <}> + ::= <{> { <:> ||||| } <}> + + ::= <=> || <;> + ::= <=> <;> + + ::= <=> || <;> + ::= <=> || <;> + ::= <[> <]> <=> || <;> + ::= <[> <]> <=> || <;> + + ::= <(> { <,> |||| } <)> <;> + ::= <(> <,> <)> <;> + ::= <(> <,> <,> <)> <;> + ::= <(> <)> <;> + ::= <(> <,> <(> <,> <)> ( <-> )|( <(> <,> <)>) <)> <;> + ::= <(> <,> <)> <;> + ::= <(> <,> <)> <;> + ::= <(> <)> <;> + + ::= [ ] <(> <)> ( <;>)|(<{> { <;> } <}>) + { ( )| }|[ ] <(> <)> ( <;>)|(<{> { <;> } <}>) + ::= <(> <=> <;> <;> <=> <+> <)> +<{> + { <;> } +<}> + ::= <(> <)> +<{> + { <;> } +<}> + ::= | <(> <,> <)> +<{> + { <;> } +<}> + + ::= [<(> { } <)>] +<{> + { <;> } +<}> + ::= <(> { ({<,> ||||})|({<,> <:> ||||}) } <)> + ::= [<(> { } <)>] +<{> + { <(> <(> <)> <==> <)> ([<{> { <;>} <}>])|( <;>) } + <(> <)> <;> +<}> + ::= [<(> { } <)>] +<{> + { <;> } + [<(> {<,> } <)>] + <{> + { <;> } + <}> +<}> + + ::= <[> <]>|[ <.> | ] + ::= <.> | + ::= <(> { <,> ||| } <)> <;> + + ::= <(> <)> <;> + + ::= <(> { <,> [<[> | <]>] } <)> <=> <(> { |||| } <)> <;> + ::= <(> { <,> [<[> | <]>] } <)> <=> <(> { |||| } <)> <;> + + ::= <(> { <,> [<[> | <]>] } <)> <=> <(> { <,> [<[> | <]>] } <)> <;> + ::= <(> { <,> [<[> | <]>] } <)> <=> <;> + ::= <=> <[> { <,> }|( <:> [ ])| <]> <;> + + + nasal-- + ::= + ::= + ::= + ::= + ::= + ::= | + ::= <(> {} <)> <;> + ::= <(> {} <)> + ::= <=> ||||| <;> + ::= <=> ||||| <;> + ::= <=> <(> {} <)> <{> { <;>} <}> + ::= <(> <=> <;> <;> <=> <+> <)> +<{> + { <;> } +<}> + ::= <(> <)> +<{> + { <;> } +<}> + ::= [ ] <(> <)> ( <;>)|(<{> { <;> } <}>) + { ( )| }|[ ] <(> <)> ( <;>)|(<{> { <;> } <}>) + diff --git a/version0.3/ide.txt b/version0.3/ide.txt new file mode 100644 index 0000000..19a45bd --- /dev/null +++ b/version0.3/ide.txt @@ -0,0 +1,27 @@ +var inum=2; +var hnum=5; +var onum=1; +var neuron={ + w:[], + b:0, + in:0, + out:0, + diff:0 + new:func(last_layer_num){ + setsize(w,last_layer_num); + return { parents:[neuron] }; + } +}; +var bp={ + input_layer:[], + hidden_layer:[], + output_layer:[], + new:func(in_num,hidden_num,output_num){ + setsize(input_layer,in_num); + for(var i=0;i +#include +#include +#include +#include +#include +#include + +namespace nasal +{ +#include "nasal_functional.h" + +#include "nasal_var.h" +#include "nasal_list.h" +#include "nasal_hash.h" +#include "nasal_token.h" +#include "nasal_func.h" + +#include "nasal_var.cpp" +#include "nasal_list.cpp" +#include "nasal_hash.cpp" +#include "nasal_print.h" +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" +#include "nasal_token.cpp" +#include "nasal_func.cpp" + +#include "nasal_lexer.h" +#include "nasal_parse.h" +} + +#endif diff --git a/version0.3/nasal_func.cpp b/version0.3/nasal_func.cpp new file mode 100644 index 0000000..fa8de63 --- /dev/null +++ b/version0.3/nasal_func.cpp @@ -0,0 +1,174 @@ +#ifndef __NASAL_FUNC_CPP__ +#define __NASAL_FUNC_CPP__ + +#include "nasal_func.h" + +func::func() +{ + statement_head=new token_unit; + statement_head->line=0; + statement_head->type=FUNC_BEGIN; + statement_head->content="__func_begin"; + statement_head->next=NULL; + + parameter_head=new parameter; + parameter_head->param_var.type=VAR_NONE; + parameter_head->param_var.data=NULL; + parameter_head->var_name="__null_var"; + parameter_head->next=NULL; +} +func::func(const func& temp) +{ + token_unit *statement_temp=statement_head; + token_unit *temp_state=temp.statement_head; + parameter *parameter_temp=parameter_head; + parameter *temp_param=temp.parameter_head; + while(temp_state->next) + { + temp_state=temp_state->next; + statement_temp->next=new token_unit; + statement_temp=statement_temp->next; + statement_temp->line=temp_state->line; + statement_temp->content=temp_state->content; + statement_temp->type=temp_state->type; + statement_temp->next=NULL; + } + while(temp_param->next) + { + temp_param=temp_param->next; + parameter_temp->next=new parameter; + parameter_temp=parameter_temp->next; + parameter_temp->param_var=temp_param->param_var; + parameter_temp->var_name=temp_param->var_name; + parameter_temp->next=NULL; + } +} +func::~func() +{ + token_unit *statement_temp=statement_head; + token_unit *statement_last_unit=NULL; + parameter *parameter_temp=parameter_head; + parameter *parameter_last_unit=NULL; + + while(statement_temp->next) + { + statement_last_unit=statement_temp; + statement_temp=statement_temp->next; + delete statement_last_unit; + } + delete statement_temp; + while(parameter_temp->next) + { + parameter_last_unit=parameter_temp; + parameter_temp=parameter_temp->next; + delete parameter_last_unit; + } + delete parameter_temp; +} +func& func::operator=(const func& temp) +{ + token_unit *statement_temp=statement_head->next; + token_unit *temp_state=temp.statement_head; + parameter *parameter_temp=parameter_head->next; + parameter *temp_param=temp.parameter_head; + + token_unit *statement_last=NULL; + parameter *parameter_last=NULL; + if(statement_temp) + { + while(statement_temp->next) + { + statement_last=statement_temp; + statement_temp=statement_temp->next; + delete statement_last; + } + delete statement_temp; + } + if(parameter_temp) + { + while(parameter_temp->next) + { + parameter_last=parameter_temp; + parameter_temp=parameter_temp->next; + delete parameter_last; + } + delete parameter_temp; + } + statement_head->next=NULL; + parameter_head->next=NULL; + statement_temp=statement_head; + parameter_temp=parameter_head; + while(temp_state->next) + { + temp_state=temp_state->next; + statement_temp->next=new token_unit; + statement_temp=statement_temp->next; + statement_temp->line=temp_state->line; + statement_temp->content=temp_state->content; + statement_temp->type=temp_state->type; + statement_temp->next=NULL; + } + while(temp_param->next) + { + temp_param=temp_param->next; + parameter_temp->next=new parameter; + parameter_temp=parameter_temp->next; + parameter_temp->param_var=temp_param->param_var; + parameter_temp->var_name=temp_param->var_name; + parameter_temp->next=NULL; + } + return *this; +} +void func::append_var(std::string& varia_name,var& p) +{ + parameter *parameter_temp=parameter_head; + while(parameter_temp->next) + { + parameter_temp=parameter_temp->next; + if(parameter_temp->var_name==varia_name) + { + std::cout<<"[Error] Redefinition of var \""<next=new parameter; + parameter_temp=parameter_temp->next; + parameter_temp->param_var=p; + parameter_temp->var_name=varia_name; + 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; +} +void func::print_info() +{ + parameter *para_temp=parameter_head; + token_unit *token_temp=statement_head; + std::cout<<"\n\t[parameter] :"; + while(para_temp->next) + { + para_temp=para_temp->next; + std::cout<var_name<<" "; + } + std::cout<<"\n\t[statement] :"; + while(token_temp->next) + { + token_temp=token_temp->next; + std::cout<content<<" "; + } + std::cout<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_name=function_name; + 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; + temp->func_statement.print_info(); + } + return; + } + void pop_function() + { + func_stack_unit *temp=head; + func_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() + { + 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; + } +}; +func_stack nasal_func_stack; + +#endif diff --git a/version0.3/nasal_functional.h b/version0.3/nasal_functional.h new file mode 100644 index 0000000..dbbdfc6 --- /dev/null +++ b/version0.3/nasal_functional.h @@ -0,0 +1,62 @@ +#ifndef __NASAL_FUNCTIONAL_H__ +#define __NASAL_FUNCTIONAL_H__ + +#include +#include + +bool is_float(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; +} + +#endif diff --git a/version0.3/nasal_hash.cpp b/version0.3/nasal_hash.cpp new file mode 100644 index 0000000..7eb376b --- /dev/null +++ b/version0.3/nasal_hash.cpp @@ -0,0 +1,113 @@ +#ifndef __NASAL_HASH_CPP__ +#define __NASAL_HASH_CPP__ + + +#include "nasal_hash.h" + +nasal_hash::nasal_hash() +{ + head=new nasal_hash_unit; + head->name=""; + head->hash_var.type=VAR_NONE; + head->hash_var.data=NULL; + head->next=NULL; +} +nasal_hash::~nasal_hash() +{ + nasal_hash_unit *temp=head; + nasal_hash_unit *this_node=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; +} +nasal_hash& nasal_hash::operator=(const nasal_hash &p) +{ + nasal_hash_unit *temp=head; + nasal_hash_unit *this_node=NULL; + if(head->next) + { + temp=temp->next; + head->next=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + temp=head; + nasal_hash_unit *temp_p=p.head; + + while(temp_p->next) + { + temp_p=temp_p->next; + temp->next=new nasal_hash_unit; + temp=temp->next; + temp->next=NULL; + temp->hash_var=temp_p->hash_var; + temp->name=temp_p->name; + } + return *this; +} +void nasal_hash::append(std::string& var_name,var& p) +{ + nasal_hash_unit *temp=head; + while(temp->next) + temp=temp->next; + temp->next=new nasal_hash_unit; + temp=temp->next; + temp->next=NULL; + temp->hash_var=p; + temp->name=var_name; + return; +} +int nasal_hash::contains(std::string& var_name) +{ + nasal_hash_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->name==var_name) + return 1; + } + return 0; +} +int nasal_hash::delete_element(std::string& var_name) +{ + nasal_hash_unit *temp=head; + nasal_hash_unit *last_node=NULL; + while(temp->next) + { + last_node=temp; + temp=temp->next; + if(temp->name==var_name) + { + last_node->next=temp->next; + delete temp; + return 1; + } + } + return 0; +} +nasal_list nasal_hash::keys() +{ + var assist_var; + assist_var.type=VAR_STRING; + nasal_list temp_list; + nasal_hash_unit *temp=head; + while(temp->next) + { + temp=temp->next; + assist_var.data=new std::string; + *((std::string *)assist_var.data)=temp->name; + temp_list.append(assist_var); + } + return temp_list; +} + +#endif diff --git a/version0.3/nasal_hash.h b/version0.3/nasal_hash.h new file mode 100644 index 0000000..b28f4d4 --- /dev/null +++ b/version0.3/nasal_hash.h @@ -0,0 +1,27 @@ +#ifndef __NASAL_HASH_H__ +#define __NASAL_HASH_H__ + +#include "nasal_var.h" + +struct nasal_hash_unit +{ + std::string name; + var hash_var; + nasal_hash_unit *next; +}; + +class nasal_hash +{ + private: + nasal_hash_unit *head; + public: + nasal_hash(); + ~nasal_hash(); + nasal_hash& operator=(const nasal_hash&); + void append(std::string&,var&); + int contains(std::string&); + int delete_element(std::string&); + nasal_list keys(); +}; + +#endif diff --git a/version0.3/nasal_interpreter.cpp b/version0.3/nasal_interpreter.cpp new file mode 100644 index 0000000..5598b59 --- /dev/null +++ b/version0.3/nasal_interpreter.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include "nasal.h" + +int main() +{ + std::string command; + std::cout<<">> input \"help\" to find help."<> "; + std::getline(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."<> 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."<> 7. command rs to check the source program."< +#include +#include +#include "nasal_functional.h" + +#define FAIL -1 //失败 +#define SCANEND -2 //扫描完成 +#define ERRORFOUND -3 //异常错误 + +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 ResourceProgram[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); + int i=0; + bool FindNote=false; + while(!fin.eof()) + { + ResourceProgram[i]=fin.get(); + if(ResourceProgram[i]=='\n') + FindNote=false; + if(ResourceProgram[i]!='#' && !FindNote) + ++i; + else if(ResourceProgram[i]=='#') + { + FindNote=true; + } + if(fin.eof()) + break; + } + ResourceProgram[i]=0; + + fin.close(); + return; +} + +void PrintSourceFile() +{ + int line=1; + std::cout<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==0 || temp=='\n') + break; + } + //add the last char \" + if(temp=='\"') + { + token+=temp; + ++ptr; + } + else + token+=" __missing_end_of_string"; + } + else if(temp==0) + { + Syn=SCANEND; + return; + } + else + { + Syn=FAIL; + std::cout<<"[Error] Unexpected error occurred: "<0)//all Syn type is larger than zero + nasal_lexer.append(line,Syn,token); + } + //nasal_lexer.print(); //for debug mode + std::cout<<">> Complete scanning \""<list_var.type=VAR_NONE; + head->list_var.data=NULL; + head->next=NULL; +} +nasal_list::~nasal_list() +{ + nasal_list_unit *temp=head; + nasal_list_unit *this_node=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; +} +nasal_list& nasal_list::operator=(const nasal_list &p) +{ + nasal_list_unit *temp=head; + nasal_list_unit *this_node=NULL; + if(head->next) + { + temp=temp->next; + head->next=NULL; + while(temp->next) + { + this_node=temp; + temp=temp->next; + delete this_node; + } + delete temp; + } + temp=head; + nasal_list_unit *temp_p=p.head; + + while(temp_p->next) + { + temp_p=temp_p->next; + temp->next=new nasal_list_unit; + temp=temp->next; + temp->next=NULL; + temp->list_var=temp_p->list_var; + } + return *this; +} +void nasal_list::append(var& p) +{ + nasal_list_unit *temp=head; + while(temp->next) + temp=temp->next; + temp->next=new nasal_list_unit; + temp=temp->next; + temp->next=NULL; + temp->list_var=p; + return; +} +void nasal_list::setsize(const int list_size) +{ + nasal_list_unit *temp=head; + int cnt=0; + while(temp->next) + { + temp=temp->next; + ++cnt; + if(cnt==list_size) + { + nasal_list_unit *this_node=NULL; + nasal_list_unit *t=temp->next; + temp->next=NULL; + if(!t) + return; + while(t->next) + { + this_node=t; + t=t->next; + delete this_node; + } + delete t; + return; + } + } + while(cntnext=new nasal_list_unit; + temp=temp->next; + temp->list_var.type=VAR_NONE; + temp->list_var.data=NULL; + temp->next=NULL; + ++cnt; + } + return; +} +nasal_list nasal_list::subvec(const int list_begin=0,const int list_end=-1) +{ + nasal_list temp_list; + int cnt=-1; + nasal_list_unit *temp=head; + + int beg=list_begin; + int end=list_end; + if(list_end==-1) + { + int end_place=-1; + while(temp->next) + { + temp=temp->next; + ++end_place; + } + temp=head; + end=end_place; + } + while(temp->next) + { + temp=temp->next; + ++cnt; + if(beg<=cnt && cnt<=end) + temp_list.append(temp->list_var); + } + return temp_list; +} +var nasal_list::pop() +{ + nasal_list_unit *temp=head; + nasal_list_unit *this_node; + while(temp->next) + { + this_node=temp; + temp=temp->next; + } + this_node->next=NULL; + var temp_var=temp->list_var; + delete temp; + return temp_var; +} +nasal_list nasal_list::sort_list(const int sort_type,const int _cmp=1) +{ + nasal_list temp_list; + if(sort_type==SORT_INT) + { + nasal_list_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->list_var.type!=VAR_LLINT) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be long int."<list_var.type!=VAR_LLINT) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be long int."<next; + while(first_temp_this->next) + { + node_this=first_temp_this; + second_temp_this=first_temp_this->next; + while(second_temp_this->next) + { + if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large + node_this=second_temp_this; + else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small + node_this=second_temp_this; + second_temp_this=second_temp_this->next; + } + if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large func(a,b) a-b + node_this=second_temp_this; + else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small func(a,b) b-a + node_this=second_temp_this; + if(node_this!=first_temp_this) + { + int t; + t=*((int *)first_temp_this->list_var.data); + *((int *)first_temp_this->list_var.data)=*((int *)node_this->list_var.data); + *((int *)node_this->list_var.data)=t; + } + first_temp_this=first_temp_this->next; + } + } + else if(sort_type==SORT_DBL) + { + nasal_list_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->list_var.type!=VAR_DOUBLE) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be float."<list_var.type!=VAR_DOUBLE) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be float."<next; + while(first_temp_this->next) + { + node_this=first_temp_this; + second_temp_this=first_temp_this->next; + while(second_temp_this->next) + { + if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large + node_this=second_temp_this; + else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small + node_this=second_temp_this; + second_temp_this=second_temp_this->next; + } + if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large func(a,b) a-b + node_this=second_temp_this; + else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small func(a,b) b-a + node_this=second_temp_this; + if(node_this!=first_temp_this) + { + double t; + t=*((double *)first_temp_this->list_var.data); + *((double *)first_temp_this->list_var.data)=*((double *)node_this->list_var.data); + *((double *)node_this->list_var.data)=t; + } + first_temp_this=first_temp_this->next; + } + } + else if(sort_type==SORT_STR) + { + nasal_list_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->list_var.type!=VAR_STRING) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be string."<list_var.type!=VAR_STRING) + { + std::cout<<"[Error] Incorrect type inside: "<list_var.type<<".But type must be string."<next; + while(first_temp_this->next) + { + node_this=first_temp_this; + second_temp_this=first_temp_this->next; + while(second_temp_this->next) + { + if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large + node_this=second_temp_this; + else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small + node_this=second_temp_this; + second_temp_this=second_temp_this->next; + } + if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large func(a,b) cmp(a,b) + node_this=second_temp_this; + else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small func(a,b) cmp(b,a) or -cmp(a,b) + node_this=second_temp_this; + if(node_this!=first_temp_this) + { + std::string t; + t=*((std::string *)first_temp_this->list_var.data); + *((std::string *)first_temp_this->list_var.data)=*((std::string *)node_this->list_var.data); + *((std::string *)node_this->list_var.data)=t; + } + first_temp_this=first_temp_this->next; + } + } + return temp_list; +} + +#endif diff --git a/version0.3/nasal_list.h b/version0.3/nasal_list.h new file mode 100644 index 0000000..11946eb --- /dev/null +++ b/version0.3/nasal_list.h @@ -0,0 +1,33 @@ +#ifndef __NASAL_LIST_H__ +#define __NASAL_LIST_H__ + +#include "nasal_var.h" + +#define SORT_INT 1 +#define SORT_DBL 2 +#define SORT_STR 3 + +class nasal_hash; + +struct nasal_list_unit +{ + var list_var; + nasal_list_unit *next; +}; + +class nasal_list +{ + private: + nasal_list_unit *head; + public: + nasal_list(); + ~nasal_list(); + nasal_list& operator=(const nasal_list&); + void append(var&); + void setsize(const int); + nasal_list subvec(const int,const int); + var pop(); + nasal_list sort_list(const int,const int); +}; + +#endif diff --git a/version0.3/nasal_parse.h b/version0.3/nasal_parse.h new file mode 100644 index 0000000..a8ba9db --- /dev/null +++ b/version0.3/nasal_parse.h @@ -0,0 +1,59 @@ +#ifndef __NASAL_PARSE_H__ +#define __NASAL_PARSE_H__ +#include "nasal_lexer.h" + +struct parse_unit +{ + int type; + std::string content; + int line; +}; + +class parse +{ + private: + parse_unit *content_array; + parse_unit *statement; + int len; + public: + parse(); + ~parse(); + void content_array_set_empty(); + void statement_set_empty(); +}; + +parse::parse() +{ + len=0; + content_array=new parse_unit[4096]; + statement=new parse_unit[1024]; +} +parse::~parse() +{ + if(content_array) + delete []content_array; + if(statement) + delete []statement; +} +void parse::content_array_set_empty() +{ + for(int i=0;i<4096;++i) + { + content_array[i].line=0; + content_array[i].type=0; + } + return; +} +void parse::statement_set_empty() +{ + for(int i=0;i<1024;++i) + { + statement[i].line=0; + statement[i].type=0; + } + return; +} + +parse nasal_parse; + +#endif diff --git a/version0.3/nasal_print.h b/version0.3/nasal_print.h new file mode 100644 index 0000000..8e48a3e --- /dev/null +++ b/version0.3/nasal_print.h @@ -0,0 +1,76 @@ +#ifndef __NASAL_PRINT_H__ +#define __NASAL_PRINT_H__ + +#include +#include +#include "nasal_hash.cpp" +#include "nasal_list.cpp" + +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<print_list(); + else if(Var.type==VAR_HASH) + ;//((nasal_hash *)Var.data)->PrintHash(); + else + std::cout<<"[Error] Null type or function"; +} + +#endif diff --git a/version0.3/nasal_token.cpp b/version0.3/nasal_token.cpp new file mode 100644 index 0000000..cfbc55e --- /dev/null +++ b/version0.3/nasal_token.cpp @@ -0,0 +1,108 @@ +#ifndef __NASAL_TOKEN_CPP__ +#define __NASAL_TOKEN_CPP__ + +#include "nasal_token.h" +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" + +token_list::token_list() +{ + list_range=0; + 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; +} +token_unit* token_list::get_head() +{ + return head; +} +int token_list::get_list_range() +{ + return list_range; +} +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; + ++list_range; + 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==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<<" )"<var_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<<"["; + switch(temp->var_detail.type) + { + case VAR_NONE: + std::cout<<"null"; + break; + case VAR_LLINT: + std::cout<<"int"; + break; + case VAR_DOUBLE: + std::cout<<"float"; + break; + case VAR_CHAR: + std::cout<<"char"; + break; + case VAR_STRING: + std::cout<<"string"; + break; + case VAR_LIST: + std::cout<<"array"; + break; + case VAR_HASH: + std::cout<<"hash"; + break; + } + std::cout<<"]: "<var_name<<" : "; + if(temp->var_detail.type!=VAR_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; + break; + } + } + return temp_var; + } + void edit_var(std::string varia_name,var &temp_var) + { + var_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->var_name==varia_name) + { + temp->var_detail=temp_var; + break; + } + } + return; + } + 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; + } +}; +var_stack nasal_var_stack; + +#endif