add something new in class var

This commit is contained in:
Valk Richard Li 2019-07-28 21:02:43 +08:00 committed by GitHub
parent c38edccd20
commit ccaddf314b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 431 additions and 16 deletions

41
interpreter_lab.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include "process_stack.h"
#include "lexer.h"
using namespace std;
using namespace nasal;
int main()
{
_unit.line=1;
_unit.type="function";
_unit.Name="__main()";
_unit.unitdata.Type="string";
_unit.unitdata.data=new std::string;
*((std::string *)_unit.unitdata.data)="in function main.";
_unit.global=true;
_test.stack_append(_unit);
_unit.line=2;
_unit.type="int";
_unit.Name="NewInt";
_unit.global=true;
_unit.unitdata.Type="int";
_unit.unitdata.data=new int;
*((int *)_unit.unitdata.data)=2147483647;
_test.stack_append(_unit);
_unit.line=3;
_unit.type="int";
_unit.Name="ptr";
_unit.global=true;
_unit.unitdata.Type="int";
*((int *)_unit.unitdata.data)=1073741824;
_test.stack_append(_unit);
//_test.stack_print(true);
_test.stack_print(false);
CommandProcess();
return 0;
}

183
lexer.h Normal file
View File

@ -0,0 +1,183 @@
#ifndef __LEXER_H__
#define __LEXER_H__
#include <iostream>
#include <fstream>
#include <cstring>
#include "nasal.h"
#include "process_stack.h"
using namespace nasal;
std::string text;
process_stack _test;
process_stack_unit _unit;
void PrintProcess(std::string content)
{
std::string Sentence="";
int len=(int)content.length();
for(int i=0;i<len;++i)
{
if(content[i]==',')
Sentence="";
else if(content[i]=='\"')//string mode
{
Sentence="";
for(int j=i+1;j<len;++j)
{
if(content[j]=='\\' && j+1<len)
{
Sentence+=content[j];
Sentence+=content[j+1];
++j;
}
else if(content[j]=='\"')
{
i=j;
break;
}
else
Sentence+=content[j];
}
PrintString(Sentence);
}
else if(content[i]!=' ' && content[i]!='\"' && content[i]!=',')//check var
{
Sentence="";
for(int j=i;j<len;++j)
{
if(content[j]!=' ' && content[j]!='\"' && content[j]!=',')
Sentence+=content[j];
if(content[j]==',' || content[j]==' ' || j==len-1)
{
i=j;
break;
}
}
if(_test.check_stack(Sentence))
{
_test.stack_content_print(Sentence);
}
else
std::cout<<std::endl<<"[Error] "<<Sentence<<" is not declared in this scope."<<std::endl;
}
}
std::cout<<std::endl;
return;
}
void FileProcess(const char *FileName)
{
std::ifstream fin(FileName);
fin.close();
return;
}
void CommandProcess()
{
while(1)
{
std::cout<<">> ";
std::getline(std::cin,text);
int len=(int)text.length();
int sharpDetected=0;
for(int i=len-1;i>=0;--i)
{
if(text[i]=='#')
{
//ignore sharp
len=i;
sharpDetected=i;
}
if(text[i]==';')
{
len=i+1;
//find the real end of the sentence
if(sharpDetected)
{
for(int j=sharpDetected-1;j>=len;--j)
if(text[j]!=' ')
{
len=j+1;
break;
}
}
else
{
for(int j=(int)text.length()-1;j>=len;--j)
if(text[j]!=' ')
{
len=j+1;
break;
}
}
break;
}
}
if(text[len-1]==';')
{
for(int i=0;i<len;++i)
{
if(text[i]=='p' && i+1<len && text[i+1]=='r' && i+2<len && text[i+2]=='i' && i+3<len && text[i+3]=='n' && i+4<len && text[i+4]=='t')
{
//In this part every error leads to an error information and breaks the loop!
//check the first char after print is '(' or not
int string_beg=len-1;
for(int j=i+5;j<len;++j)
if(text[j]!=' ')
{
string_beg=j;
break;
}
if(text[string_beg]!='(')
{
std::cout<<std::endl<<"[Error] Missing \'(\' ."<<std::endl;
break;
}
//check the ')' and the place
int string_end=len-1;
for(int j=len-2;j>=0;--j)
if(text[j]!=' ')
{
string_end=j;
break;
}
if(text[string_end]!=')')
{
std::cout<<std::endl<<"[Error] Missing \')\' ."<<std::endl;
break;
}
std::string content="";
for(int j=string_beg+1;j<string_end;++j)
content+=text[j];
std::cout<<std::endl<<"Target string: "<<content<<std::endl;
PrintProcess(content);
break;
}
else if(text[i]=='v' && i+1<len && text[i+1]=='a' && i+2<len && text[i+2]=='r')
{
;
}
else if(i==len-1)
std::cout<<std::endl<<"[Error] Incorrect command."<<std::endl;
}
}
else
{
std::cout<<std::endl<<"[Error] Expected \';\' after this line."<<std::endl;
}
}
return;
}
#endif

View File

@ -378,19 +378,19 @@ NasalList& NasalList::operator=(const NasalList &Source)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
else if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
else if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
else if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
else if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
else if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
@ -819,6 +819,51 @@ NasalList NasalList::Sort(const int SortType,const int _cmp)
return TempList;
}
var::var(var &p)
{
Type=p.Type;
if(Type=="int")
{
data=new int;
*((int *)data)=*((int *)p.data);
}
else if(Type=="float")
{
data=new float;
*((float *)data)=*((float *)p.data);
}
else if(Type=="double")
{
data=new double;
*((double *)data)=*((double *)p.data);
}
else if(Type=="char")
{
data=new char;
*((char *)data)=*((char *)p.data);
}
else if(Type=="long long int")
{
data=new long long int;
*((long long int *)data)=*((long long int *)p.data);
}
else if(Type=="string")
{
data=new std::string;
*((std::string *)data)=*((std::string *)p.data);
}
else if(Type=="array")
{
data=new NasalList;
*((NasalList *)data)=*((NasalList *)p.data);
}
else if(Type=="hash")
{
data=new NasalHash;
*((NasalHash *)data)=*((NasalHash *)p.data);
}
}
var::~var()
{
if(data)
@ -842,6 +887,91 @@ var::~var()
}
}
var& var::operator=(const var &p)
{
if(data)
{
if(Type=="int")
delete (int *)data;
else if(Type=="float")
delete (float *)data;
else if(Type=="double")
delete (double *)data;
else if(Type=="char")
delete (char *)data;
else if(Type=="long long int")
delete (long long int *)data;
else if(Type=="string")
delete (std::string *)data;
else if(Type=="array")
delete (NasalList *)data;
else if(Type=="hash")
delete (NasalHash *)data;
}
Type=p.Type;
if(Type=="int")
{
data=new int;
*((int *)data)=*((int *)p.data);
}
else if(Type=="float")
{
data=new float;
*((float *)data)=*((float *)p.data);
}
else if(Type=="double")
{
data=new double;
*((double *)data)=*((double *)p.data);
}
else if(Type=="char")
{
data=new char;
*((char *)data)=*((char *)p.data);
}
else if(Type=="long long int")
{
data=new long long int;
*((long long int *)data)=*((long long int *)p.data);
}
else if(Type=="string")
{
data=new std::string;
*((std::string *)data)=*((std::string *)p.data);
}
else if(Type=="array")
{
data=new NasalList;
*((NasalList *)data)=*((NasalList *)p.data);
}
else if(Type=="hash")
{
data=new NasalHash;
*((NasalHash *)data)=*((NasalHash *)p.data);
}
return *this;
}
void var::Print()
{
if(Type=="int")
std::cout<<*((int *)data);
else if(Type=="float")
std::cout<<*((float *)data);
else if(Type=="double")
std::cout<<*((double *)data);
else if(Type=="char")
std::cout<<*((char *)data);
else if(Type=="long long int")
std::cout<<*((long long int *)data);
else if(Type=="string")
std::cout<<*((std::string *)data);
else if(Type=="array")
(*((NasalList *)data)).PrintList();
else if(Type=="hash")
(*((NasalHash *)data)).PrintHash();
}
}
#endif

View File

@ -47,7 +47,10 @@ class var
{
data=NULL;
}
var(var &);
~var();
var& operator=(const var &);
void Print();
};
}

View File

@ -11,8 +11,10 @@ namespace nasal
struct process_stack_unit
{
int line; //place the unit first appear
std::string content;//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
var unitdata;
bool global;
process_stack_unit *next;
process_stack_unit *last;
};
@ -25,8 +27,15 @@ class process_stack
public:
process_stack()
{
head=new process_stack_unit;
head->line=0;
head->content="# Nasal language for FlightGear.";
head->Name="InterpreterInfo";
head->global=false;
head->type="information";
head->unitdata.Type="string";
head->unitdata.data=new std::string;
*((std::string *)head->unitdata.data)="# Nasal language for FlightGear.";
head->last=NULL;
head->next=NULL;
ptr=NULL;
@ -48,18 +57,67 @@ class process_stack
process_stack_unit *last_node;
while(temp->next)
{
if(!temp->next->next)
last_node=temp;
temp=temp->next;
}
if(temp==head)
last_node=head;
temp=new process_stack_unit;
temp->next=new process_stack_unit;
last_node=temp;
temp=temp->next;
temp->last=last_node;
last_node->next=temp;
temp->next=NULL;
temp->content=p.content;
temp->line=p.last;
temp->Name=p.Name;
temp->line=p.line;
temp->type=p.type;
temp->global=p.global;
temp->unitdata=p.unitdata;
return;
}
void stack_print(bool reverse_mode_used)
{
process_stack_unit *temp=head;
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)
while(temp->last)
{
std::cout<<temp->line<<": |"<<temp->type<<"| \""<<temp->Name<<"\""<<std::endl;
temp=temp->last;
}
std::cout<<"End."<<std::endl;
return;
}
bool check_stack(std::string &ElementName)
{
process_stack_unit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->Name==ElementName)
return true;
}
return false;
}
void stack_content_print(std::string &ElementName)
{
process_stack_unit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->Name==ElementName)
{
temp->unitdata.Print();
return;
}
}
return;
}
};