bugs fixed

This commit is contained in:
Valk Richard Li 2019-07-29 21:46:55 +08:00 committed by GitHub
parent bfddeeaa9e
commit 8554ffd7fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 104 additions and 52 deletions

View File

@ -2,34 +2,36 @@
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#define IDENTR -1 //界符 or 运算符 #define SELFDEFINE -1 //自定义标识符
#define NUMBER -2 //数字 #define IDENTIFIER -2 //界符 or 运算符
#define RESERV -3 //关键字 #define NUMBER -3 //数字
#define FAIL -4 #define RESERVEWORD -4//关键字
#define SCANEND -5 #define FAIL -5 //失败
#define ERROROCC -6 #define SCANEND -6 //扫描完成
#define SELFD -7 //自定义标识符 #define ERRORFOUND -7 //异常错误
// \n 换行 // \n 换行
// \t tab // \t tab
// \r 回车 // \r 回车
// \\ 反斜线 // \\ 反斜线
// \' 单引号 // \' 单引号
// \" 双引号 // \" 双引号
std::string ReserveWord[25]= std::string ReserveWord[27]=
{ {
"for","foreach","forindex","while", "for","foreach","forindex","while",
"var","func","break","continue","return", "var","func","break","continue","return",
"if","else","elsif","nil","and","or", "if","else","elsif","nil","and","or",
"print","cmp","append","setsize","subvec","pop", "print","cmp","append","setsize","subvec","pop",
"sort","contains","delete","keys" "sort","contains","delete","keys","typeof","id"
}; };
std::string OperatorOrDelimiter[33]= std::string OperatorOrDelimiter[40]=
{ {
"+","-","*","/","=","+=","-=","*=","/=", "+","-","*","/","=","+=","-=","*=","/=",
"\n","\t","\r","\\","\'","\"",".", "\n","\t","\r","\\","\'","\"",".",
"<","<=",">",">=","==","!=","!","~", "<","<=",">",">=","==","!=","~=","!","~",
",",";","(",")","[","]","{","}","#" ",",";","(",")","[","]","{","}","#","?",":",
"&","|","%","^"
}; };
std::string IdentifierTable[1000]={""}; std::string IdentifierTable[1000]={""};
@ -37,7 +39,7 @@ char ResourcePrograme[16384];
int isReserveWord(std::string &p) int isReserveWord(std::string &p)
{ {
for(int i=0;i<25;++i) for(int i=0;i<27;++i)
if(ReserveWord[i]==p) if(ReserveWord[i]==p)
return i+1; return i+1;
return FAIL; return FAIL;
@ -45,7 +47,7 @@ int isReserveWord(std::string &p)
int isOperatorOrDelimiter(std::string &p) int isOperatorOrDelimiter(std::string &p)
{ {
for(int i=0;i<33;++i) for(int i=0;i<40;++i)
if(OperatorOrDelimiter[i]==p) if(OperatorOrDelimiter[i]==p)
return i+1; return i+1;
return FAIL; return FAIL;
@ -61,9 +63,16 @@ bool isNumber(char t)
return ('0'<=t && t<='9'); return ('0'<=t && t<='9');
} }
void InputFile(const char *FileName) void InputFile(std::string &FileName)
{ {
std::ifstream fin(FileName); std::ifstream fin(FileName);
if(fin.fail())
{
std::cout<<"[Error] Failed to load file: "<<FileName<<std::endl;
ResourcePrograme[0]='@';
fin.close();
return;
}
int i=0; int i=0;
bool FindNote=false; bool FindNote=false;
while(!fin.eof()) while(!fin.eof())
@ -93,19 +102,19 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
{ {
char temp; char temp;
temp=Source[ptr]; temp=Source[ptr];
while(temp==' ' || temp=='\n') while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127)
{ {
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
} }
token=""; token="";
if(isLetter(temp)) if(isLetter(temp) || temp=='_')
{ {
token+=temp; token+=temp;
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
while(isLetter(temp) || isNumber(temp) || temp==':' || temp=='?' || temp=='`' || temp=='.') while(isLetter(temp) || isNumber(temp) || temp=='_')
{ {
token+=temp; token+=temp;
++ptr; ++ptr;
@ -113,40 +122,50 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
} }
Syn=isReserveWord(token); Syn=isReserveWord(token);
if(Syn==FAIL) if(Syn==FAIL)
Syn=SELFD; Syn=SELFDEFINE;
else else
Syn=RESERV; Syn=RESERVEWORD;
} }
else if(isNumber(temp)) else if(isNumber(temp))
{ {
int PointCnt=0; int PointCnt=0;
token+=temp;
++ptr;
temp=Source[ptr];
while(isNumber(temp)) while(isNumber(temp))
{ {
token+=temp; token+=temp;
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
if(temp=='.') if(temp=='.' && !PointCnt)
{
++PointCnt; ++PointCnt;
else if(temp!='.' && !isNumber(temp)) token+=temp;
break; ++ptr;
if(PointCnt>1) temp=Source[ptr];
break; }
} }
Syn=NUMBER; Syn=NUMBER;
} }
else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || temp=='}' || temp=='~' || temp==',' || temp==';' || temp=='\"') else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' ||
temp=='}' || temp==',' || temp==';' || temp=='\"' || temp==':' ||
temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&' ||
temp=='|')
{ {
token+=temp; token+=temp;
++ptr; ++ptr;
Syn=IDENTR; Syn=IDENTIFIER;
} }
else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~')
temp=='/' || temp=='=' || temp=='\\' || temp=='.' || temp=='<' || {
temp=='>' Syn=IDENTIFIER;
) token+=temp;
++ptr;
temp=Source[ptr];
if(temp=='=')
{
token+=temp;
++ptr;
}
}
else if(temp=='\\')
{ {
/* /*
"+","-","*","/","=","+=","-=","*=","/=", "+","-","*","/","=","+=","-=","*=","/=",
@ -154,7 +173,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
"<","<=",">",">=","==","!=","!","~", "<","<=",">",">=","==","!=","!","~",
",",";","(",")","[","]","{","}","#" ",",";","(",")","[","]","{","}","#"
*/ */
Syn=IDENTR; Syn=IDENTIFIER;
token+=temp; token+=temp;
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
@ -171,35 +190,68 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
} }
else else
{ {
Syn=SCANEND; Syn=FAIL;
std::cout<<"[Error] Unexpected error occurred: "<<temp<<std::endl;
++ptr;
return; return;
} }
if(token=="") if(token=="")
{ {
Syn=ERROROCC; Syn=ERRORFOUND;
std::cout<<"[Error] Cannot identify special id."<<std::endl; std::cout<<"[Error] Cannot identify "<<std::endl;
} }
return; return;
} }
void help()
{
std::cout<<">> exit: exit the programe."<<std::endl;
std::cout<<">> clear: clean the screen."<<std::endl;
std::cout<<">> help: find help."<<std::endl;
std::cout<<">> input the file name to scan."<<std::endl;
}
int main() int main()
{ {
int Syn=0,Ptr=0; help();
std::string token; while(1)
InputFile("source.nas");
while(Syn!=SCANEND && Syn!=ERROROCC)
{ {
Scanner(Syn,ResourcePrograme,token,Ptr); int Syn=0;//token type
if(Syn==IDENTR) int Ptr=0;//pointer to one char in ResourcePrograme
std::cout<<"( "<<token<<" , ---- )"<<std::endl; std::string token;
else if(Syn==SELFD) std::string FileNameOrCommand;
std::cout<<"( 标识符 , "<<token<<" )"<<std::endl; std::cout<<">> ";
else if(Syn==NUMBER) std::cin>>FileNameOrCommand;
std::cout<<"( Number , "<<token<<" )"<<std::endl;
else if(Syn==RESERV) if(FileNameOrCommand=="exit")
std::cout<<"( Reserve Word , "<<token<<" )"<<std::endl; break;
else if(FileNameOrCommand=="clear")
{
system("cls");
continue;
}
else if(FileNameOrCommand=="help")
{
help();
continue;
}
//std::ofstream fout("Data.txt");
InputFile(FileNameOrCommand);
while(Syn!=SCANEND && Syn!=ERRORFOUND)
{
Scanner(Syn,ResourcePrograme,token,Ptr);
if(Syn==IDENTIFIER)
std::cout<<"( "<<token<<" , ---- )"<<std::endl;
else if(Syn==SELFDEFINE)
std::cout<<"( 标识符 , "<<token<<" )"<<std::endl;
else if(Syn==NUMBER)
std::cout<<"( Number , "<<token<<" )"<<std::endl;
else if(Syn==RESERVEWORD)
std::cout<<"( Reserve Word , "<<token<<" )"<<std::endl;
}
std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl;
//fout.close();
} }
return 0; return 0;
} }