add some new functions

This commit is contained in:
Valk Richard Li 2019-07-30 21:57:12 +08:00 committed by GitHub
parent cac0a1a5e6
commit 13fd61fe76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 17 deletions

View File

@ -445,5 +445,36 @@ NasalHash& NasalHash::operator=(const NasalHash &Source)
return *this; return *this;
} }
void 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;
}
}
std::cout<<"[Error] Could not find \""<<ElementName<<"\""<<std::endl;
return;
}
} }
#endif #endif

View File

@ -33,6 +33,7 @@ class NasalHash
NasalList Keys(); NasalList Keys();
void Delete(const char *); void Delete(const char *);
NasalHash& operator=(const NasalHash&); NasalHash& operator=(const NasalHash&);
void SearchElement(std::string &);
}; };
} }

View File

@ -819,6 +819,27 @@ NasalList NasalList::Sort(const int SortType,const int _cmp)
return TempList; return TempList;
} }
void NasalList::SearchElement(const int n)
{
if(n<0)
{
std::cout<<"[Error] "<<n<<" is less than 0."<<std::endl;
}
ListUnit *temp=head;
int cnt=-1;
while(temp->next)
{
temp=temp->next;
cnt++;
if(cnt==n)
{
return;
}
}
std::cout<<"[Error] Out of range: real end: "<<cnt<<", less than "<<n<<"."<<std::endl;
return;
}
var::var(var &p) var::var(var &p)
{ {
Type=p.Type; Type=p.Type;
@ -965,7 +986,7 @@ void var::Print()
else if(Type=="long long int") else if(Type=="long long int")
std::cout<<*((long long int *)data); std::cout<<*((long long int *)data);
else if(Type=="string") else if(Type=="string")
std::cout<<*((std::string *)data); std::cout<<"\""<<*((std::string *)data)<<"\"";
else if(Type=="array") else if(Type=="array")
(*((NasalList *)data)).PrintList(); (*((NasalList *)data)).PrintList();
else if(Type=="hash") else if(Type=="hash")

View File

@ -36,6 +36,7 @@ class NasalList
NasalList SubVec(const int,const int); NasalList SubVec(const int,const int);
var Pop(); var Pop();
NasalList Sort(const int,const int); NasalList Sort(const int,const int);
void SearchElement(const int);
}; };
class var class var

View File

@ -10,9 +10,9 @@ namespace nasal
struct process_stack_unit struct process_stack_unit
{ {
int line; //place the unit first appear int line; //place the unit first appear
std::string Name;//content of the unit or name of the var/class/function std::string name; //content of the unit or name of the var/class/function
std::string type; //var class function std::string format_type; //var class function string info
var unitdata; var unitdata;
bool global; bool global;
process_stack_unit *next; process_stack_unit *next;
@ -29,9 +29,9 @@ class process_stack
{ {
head=new process_stack_unit; head=new process_stack_unit;
head->line=0; head->line=0;
head->Name="InterpreterInfo"; head->name="InterpreterInfo";
head->format_type="Info";
head->global=false; head->global=false;
head->type="information";
head->unitdata.Type="string"; head->unitdata.Type="string";
head->unitdata.data=new std::string; head->unitdata.data=new std::string;
*((std::string *)head->unitdata.data)="# Nasal language for FlightGear."; *((std::string *)head->unitdata.data)="# Nasal language for FlightGear.";
@ -68,9 +68,9 @@ class process_stack
last_node->next=temp; last_node->next=temp;
temp->next=NULL; temp->next=NULL;
temp->Name=p.Name; temp->name=p.name;
temp->line=p.line; temp->line=p.line;
temp->type=p.type; temp->format_type=p.format_type;
temp->global=p.global; temp->global=p.global;
temp->unitdata=p.unitdata; temp->unitdata=p.unitdata;
@ -80,28 +80,57 @@ class process_stack
{ {
process_stack_unit *temp=head; process_stack_unit *temp=head;
std::cout<<"In stack: "<<std::endl; std::cout<<"In stack: "<<std::endl;
std::cout<<temp->line<<": |"<<temp->type<<"| \""<<temp->Name<<"\""<<std::endl;
while(temp->next)
{
temp=temp->next;
std::cout<<temp->line<<": |"<<temp->type<<"| \""<<temp->Name<<"\""<<std::endl;
}
if(reverse_mode_used) if(reverse_mode_used)
{
while(temp->next)
temp=temp->next;
while(temp->last) while(temp->last)
{ {
std::cout<<temp->line<<": |"<<temp->type<<"| \""<<temp->Name<<"\""<<std::endl; std::cout<<"line "<<temp->line<<": |"<<temp->format_type<<"|"<<temp->name<<"|\n\t|";
temp->unitdata.Print();
std::cout<<std::endl;
temp=temp->last; temp=temp->last;
} }
std::cout<<"line "<<temp->line<<": |"<<temp->format_type<<"|"<<temp->name<<"|\n\t|";
temp->unitdata.Print();
std::cout<<std::endl;
}
else
{
std::cout<<"line "<<temp->line<<": |"<<temp->format_type<<"|"<<temp->name<<"|\n\t|";
temp->unitdata.Print();
std::cout<<std::endl;
while(temp->next)
{
temp=temp->next;
std::cout<<"line "<<temp->line<<": |"<<temp->format_type<<"|"<<temp->name<<"|\n\t|";
temp->unitdata.Print();
std::cout<<std::endl;
}
}
std::cout<<"End."<<std::endl; std::cout<<"End."<<std::endl;
return; return;
} }
void pop()
{
process_stack_unit *temp=head;
process_stack_unit *last_node;
while(temp->next)
{
last_node=temp;
temp=temp->next;
}
last_node->next=NULL;
delete temp;
return;
}
bool check_stack(std::string &ElementName) bool check_stack(std::string &ElementName)
{ {
process_stack_unit *temp=head; process_stack_unit *temp=head;
while(temp->next) while(temp->next)
{ {
temp=temp->next; temp=temp->next;
if(temp->Name==ElementName) if(temp->name==ElementName)
return true; return true;
} }
return false; return false;
@ -112,7 +141,7 @@ class process_stack
while(temp->next) while(temp->next)
{ {
temp=temp->next; temp=temp->next;
if(temp->Name==ElementName) if(temp->name==ElementName)
{ {
temp->unitdata.Print(); temp->unitdata.Print();
return; return;