new update
This commit is contained in:
parent
c967a6980f
commit
19792ebe20
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -1,9 +1,43 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#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::endl;
|
||||
while(1)
|
||||
{
|
||||
std::cout<<">> ";
|
||||
std::cin>>command;
|
||||
if(command=="help")
|
||||
{
|
||||
std::cout<<">> 1.input file name to run the nasal script."<<std::endl;
|
||||
std::cout<<">> 2.command cls to clear the screen."<<std::endl;
|
||||
std::cout<<">> 3.command exit to shut down the program."<<std::endl;
|
||||
}
|
||||
else if(command=="cls")
|
||||
{
|
||||
system("cls");
|
||||
//linux system("clear");
|
||||
//macOS system(clear");
|
||||
}
|
||||
else if(command=="exit")
|
||||
break;
|
||||
else
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -1,17 +1,192 @@
|
|||
#ifndef __NASAL_LIST_CPP__
|
||||
#define __NASAL_LIST_CPP__
|
||||
|
||||
#include "nasal_var.h"
|
||||
#include "nasal_list.h"
|
||||
|
||||
nasal_list::nasal_list()
|
||||
{
|
||||
;
|
||||
head=new nasal_list_unit;
|
||||
head->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(cnt<list_size)
|
||||
{
|
||||
temp->next=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: "<<temp->list_var.type<<".But type must be long int."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
}
|
||||
if(temp->list_var.type!=VAR_LLINT)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be long int."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
temp_list=*this;
|
||||
}
|
||||
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: "<<temp->list_var.type<<".But type must be string."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
}
|
||||
if(temp->list_var.type!=VAR_STRING)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be string."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
temp_list=*this;
|
||||
}
|
||||
return temp_list;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
#ifndef __NASAL_LIST_H__
|
||||
#define __NASAL_LIST_H__
|
||||
|
||||
#include "nasal_var.h"
|
||||
|
||||
#define SORT_INT 1
|
||||
#define SORT_STR 2
|
||||
|
||||
class nasal_hash;
|
||||
|
||||
struct nasal_list_unit
|
||||
{
|
||||
std::string type;
|
||||
void *data;
|
||||
var list_var;
|
||||
nasal_list_unit *next;
|
||||
};
|
||||
|
||||
|
@ -18,13 +22,11 @@ class nasal_list
|
|||
nasal_list();
|
||||
~nasal_list();
|
||||
nasal_list& operator=(const nasal_list&);
|
||||
void append_var(var&);
|
||||
void append_list(nasal_list&);
|
||||
void append_hash(nasal_hash&);
|
||||
void append(var&);
|
||||
void setsize(const int);
|
||||
nasal_list subvec(const int,const int);
|
||||
var pop();
|
||||
void sort_list(bool);
|
||||
nasal_list sort_list(const int,bool);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -38,6 +38,10 @@ var::var(const var &temp)
|
|||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
data=new func;
|
||||
*((func *)data)=*((func *)temp.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
var::~var()
|
||||
|
@ -63,6 +67,9 @@ var::~var()
|
|||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
delete (func *)data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var& var::operator=(const var &temp)
|
||||
|
@ -88,6 +95,9 @@ var& var::operator=(const var &temp)
|
|||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
delete (func *)data;
|
||||
break;
|
||||
}
|
||||
type=temp.type;
|
||||
switch(type)
|
||||
|
@ -119,6 +129,10 @@ var& var::operator=(const var &temp)
|
|||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
data=new func;
|
||||
*((func *)data)=*((func *)temp.data);
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#define VAR_STRING 4
|
||||
#define VAR_LIST 5
|
||||
#define VAR_HASH 6
|
||||
#define VAR_FUNC 7
|
||||
|
||||
class var
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue