From e8fad23955ceadaa0f93ed815422c579ad72c3d9 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sat, 10 Aug 2019 23:41:39 +0800 Subject: [PATCH] update parse --- version0.2/nasal.h | 8 +- version0.2/nasal_func_stack.h | 39 +-- version0.2/nasal_functional.cpp | 573 ++++++++++++++++++------------- version0.2/nasal_functional.h | 35 ++ version0.2/nasal_interpreter.cpp | 13 +- version0.2/nasal_lexer.h | 60 ++-- version0.2/nasal_parse.h | 7 + version0.2/nasal_print.h | 27 +- version0.2/nasal_var_stack.h | 59 +++- 9 files changed, 512 insertions(+), 309 deletions(-) create mode 100644 version0.2/nasal_parse.h diff --git a/version0.2/nasal.h b/version0.2/nasal.h index 759fca1..7c4600e 100644 --- a/version0.2/nasal.h +++ b/version0.2/nasal.h @@ -20,13 +20,13 @@ namespace nasal #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_print.h" +#include "nasal_var_stack.h" +#include "nasal_func_stack.h" #include "nasal_functional.cpp" #include "nasal_lexer.h" - +#include "nasal_parse.h" } #endif diff --git a/version0.2/nasal_func_stack.h b/version0.2/nasal_func_stack.h index a48d2a8..9753fd5 100644 --- a/version0.2/nasal_func_stack.h +++ b/version0.2/nasal_func_stack.h @@ -1,12 +1,9 @@ #ifndef __NASAL_FUNC_STACK_H__ #define __NASAL_FUNC_STACK_H__ -#include "nasal_func.h" +#include "nasal_functional.h" -namespace nasal -{ - struct func_stack_unit { std::string func_name; @@ -60,21 +57,21 @@ class func_stack 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; +// if(temp->func_name==function_name) +// { +// temp->func_statement.run(); +// return; +// } +// } +// std::cout<<"[Error] Could not find this function."<=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; +} + func::func() { @@ -135,6 +193,7 @@ void func::append_token(const int _line,const int _type,std::string &_content) token_list::token_list() { + list_range=0; head=new token_unit; head->type=FUNC_BEGIN; head->content="__process_begin"; @@ -153,6 +212,14 @@ token_list::~token_list() } 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<<": "; @@ -190,6 +257,7 @@ 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; @@ -208,260 +276,305 @@ void token_list::print() { temp=temp->next; std::cout<<"line "<line<<": "; - if(temp->type==FUNC_OPERATOR) + if(temp->type==OPERATOR) std::cout<<"( Operator | "; - else if(temp->type==FUNC_IDENTIFIER) + else if(temp->type==IDENTIFIER) std::cout<<"( Identifier | "; - else if(temp->type==FUNC_NUMBER) + else if(temp->type==NUMBER) std::cout<<"( Number | "; - else if(temp->type==FUNC_RESERVEWORD) + else if(temp->type==RESERVEWORD) std::cout<<"( ReserveWord | "; - else if(temp->type==FUNC_STRING) + else if(temp->type==STRING) std::cout<<"( String | "; std::cout<content<<" )"<next) + len=0; + content_array=new parse_unit[4096]; + statement=new parse_unit[2048]; +} +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) { - std::cout<<">> Running complete."<0) + std::cout<<"[Error] Missing \")\" ."<0) + std::cout<<"[Error] Missing \"]\" ."<0) + std::cout<<"[Error] Missing \"}\" ."<0? '}':'{')<<"\" ."<0? '}':'{')<<"\" ."<next) { temp=temp->next; - if(temp->type=="Operator") + ++len;//begin from 1 + content_array[len].type=temp->type; + content_array[len].line=temp->line; + content_array[len].content=temp->content; + } + brace_check(); + for(int i=1;i<=len;++i) + { + if(content_array[i].type==RESERVEWORD && content_array[i].content=="var") { - ; - } - 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") + int in_brace=0; + statement_set_empty(); + for(int j=i;j<=len;++j) { - if(temp->next) - temp=temp->next; - else + statement[j-i]=content_array[j]; + if(content_array[j].type==OPERATOR && content_array[j].content==";" && in_brace==0) { - 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") + else if(content_array[i].type==IDENTIFIER) { - print_line_token(temp->line); - std::cout<<"line "<line<<": "<<"[Error] String without any tokens."<> Running complete."<> "; - std::cin>>command; + std::getline(std::cin,command); if(command=="help") { std::cout<<">> 1. input file name to run the nasal script."<> 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."<0)//all Syn type is larger than zero nasal_lexer.append(line,Syn,token); } diff --git a/version0.2/nasal_parse.h b/version0.2/nasal_parse.h new file mode 100644 index 0000000..0411e27 --- /dev/null +++ b/version0.2/nasal_parse.h @@ -0,0 +1,7 @@ +#ifndef __NASAL_PARSE_H__ +#define __NASAL_PARSE_H__ +#include "nasal_lexer.h" + +parse nasal_parse; + +#endif diff --git a/version0.2/nasal_print.h b/version0.2/nasal_print.h index 95b880c..6a6b734 100644 --- a/version0.2/nasal_print.h +++ b/version0.2/nasal_print.h @@ -6,9 +6,6 @@ #include "nasal_hash.cpp" #include "nasal_list.cpp" -namespace nasal -{ - void PrintString(std::string &PrintInfo) { for(int i=0;i<(int)PrintInfo.length();++i) @@ -60,26 +57,20 @@ void PrintString(std::string &PrintInfo) } void PrintVar(var Var) { - if(Var.Type=="int") - std::cout<<*((int *)Var.data); - else if(Var.Type=="long long int") + if(Var.type==VAR_LLINT) std::cout<<*((long long int *)Var.data); - else if(Var.Type=="float") - std::cout<<*((float *)Var.data); - else if(Var.Type=="double") + else if(Var.type==VAR_DOUBLE) std::cout<<*((double *)Var.data); - else if(Var.Type=="char") + else if(Var.type==VAR_CHAR) std::cout<<*((char *)Var.data); - else if(Var.Type=="string") + else if(Var.type==VAR_STRING) PrintString(*((std::string *)Var.data)); - else if(Var.Type=="array") - ((NasalList *)Var.data)->PrintList(); - else if(Var.Type=="hash") - ((NasalHash *)Var.data)->PrintHash(); +// else if(Var.type==VAR_LIST) +// ((NasalList *)Var.data)->PrintList(); +// else if(Var.type==VAR_HASH) +// ((NasalHash *)Var.data)->PrintHash(); else - std::cout<<"null"; -} - + std::cout<<"[Error] Null type or function"; } #endif diff --git a/version0.2/nasal_var_stack.h b/version0.2/nasal_var_stack.h index b884e9b..8d97285 100644 --- a/version0.2/nasal_var_stack.h +++ b/version0.2/nasal_var_stack.h @@ -3,9 +3,6 @@ #include "nasal_print.h" -namespace nasal -{ - struct var_stack_unit { std::string var_name; @@ -41,7 +38,7 @@ class var_stack else delete head; } - void append_var(std::string &varia_name,var &temp_var) + void append_var(std::string& varia_name,var& temp_var) { var_stack_unit *temp=head; while(temp->next) @@ -58,8 +55,33 @@ class var_stack while(temp->next) { temp=temp->next; - std::cout<<"["<var_detail.Type<<"]: "<var_name<<" : "; - if(temp->var_detail.Type!="string") + 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); @@ -67,21 +89,37 @@ class var_stack } return; } - var SearchVar(std::string varia_name) + var search_var(std::string varia_name) { var temp_var; temp_var.data=NULL; - temp_var.Type="null"; - temp_var.isGlobal=false; + temp_var.type=VAR_NONE; var_stack_unit *temp=head; while(temp->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; @@ -113,7 +151,6 @@ class var_stack return; } }; - -} +var_stack nasal_var_stack; #endif