diff --git a/version0.2/nasal_functional.cpp b/version0.2/nasal_functional.cpp index 3be7cac..643a505 100644 --- a/version0.2/nasal_functional.cpp +++ b/version0.2/nasal_functional.cpp @@ -1,4 +1,32 @@ #ifndef __NASAL_FUNCTIONAL_CPP__ #define __NASAL_FUNCTIONAL_CPP__ +#include "nasal_functional.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->next=NULL; +} +func::func(const func& temp) +{ + ; +} +func::~func() +{ + ; +} +func& func::operator=(const func& temp) +{ + return *this; +} + #endif diff --git a/version0.2/nasal_functional.h b/version0.2/nasal_functional.h index 0372be3..dd53185 100644 --- a/version0.2/nasal_functional.h +++ b/version0.2/nasal_functional.h @@ -1,6 +1,8 @@ #ifndef __NASAL_FUNCTIONAL_H__ #define __NASAL_FUNCTIONAL_H__ +#include "nasal_var.h" + #define FUNC_BEGIN 0 #define FUNC_OPERATOR 1 #define FUNC_IDENTIFIER 2 @@ -16,13 +18,22 @@ struct token_unit token_unit *next; }; +struct parameter +{ + var param_var; + parameter *next; +}; + class func { private: - token_unit *head; + token_unit *statement_head; + parameter *parameter_head; public: func(); + func(const func&); ~func(); + func& operator=(const func&); }; class token_list diff --git a/version0.2/nasal_hash.cpp b/version0.2/nasal_hash.cpp index de379a9..7eb376b 100644 --- a/version0.2/nasal_hash.cpp +++ b/version0.2/nasal_hash.cpp @@ -1,17 +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 &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.2/nasal_hash.h b/version0.2/nasal_hash.h index 5773e9a..b28f4d4 100644 --- a/version0.2/nasal_hash.h +++ b/version0.2/nasal_hash.h @@ -1,12 +1,12 @@ #ifndef __NASAL_HASH_H__ #define __NASAL_HASH_H__ +#include "nasal_var.h" struct nasal_hash_unit { std::string name; - std::string type; - void *data; + var hash_var; nasal_hash_unit *next; }; @@ -18,9 +18,7 @@ class nasal_hash nasal_hash(); ~nasal_hash(); nasal_hash& operator=(const nasal_hash&); - void append_var(var&); - void append_list(nasal_list&); - void append_hash(nasal_hash&); + void append(std::string&,var&); int contains(std::string&); int delete_element(std::string&); nasal_list keys(); diff --git a/version0.2/nasal_interpreter.cpp b/version0.2/nasal_interpreter.cpp index 93e3fe5..8be3772 100644 --- a/version0.2/nasal_interpreter.cpp +++ b/version0.2/nasal_interpreter.cpp @@ -1,9 +1,43 @@ #include #include +#include #include "nasal.h" int main() { + nasal::nasal_list m; + nasal::nasal_hash n; + nasal::var k; + k.type=VAR_STRING; + k.data=new std::string; + *((std::string *)k.data)="hello"; + m.append(k); + m.pop(); + std::string command; + 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."<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 &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,bool cmp_rule=true) +{ + 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) + { + 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."<