This commit is contained in:
Valk Richard Li 2019-08-05 23:21:53 +08:00 committed by GitHub
parent 41c4b49ca5
commit 196ec3a229
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 693 additions and 0 deletions

183
misc/lexer(useless).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

291
misc/lexical_analysis.cpp Normal file
View File

@ -0,0 +1,291 @@
#include <iostream>
#include <fstream>
#include <cstring>
#define IDENTIFIER -1 //自定义标识符
#define OPERATOR -2 //界符 or 运算符
#define NUMBER -3 //数字
#define RESERVEWORD -4 //关键字
#define STRING -5 //字符串类型
#define FAIL -6 //失败
#define SCANEND -7 //扫描完成
#define ERRORFOUND -8 //异常错误
// \n 换行
// \t tab
// \r 回车
// \\ 反斜线
// \' 单引号
// \" 双引号
std::string ReserveWord[26]=
{
"for","foreach","forindex","while",
"var","func","break","continue","return",
"if","else","elsif","nil","and","or",
"print","cmp","append","setsize","subvec","pop",
"sort","contains","delete","keys","typeof"
};
std::string OperatorOrDelimiter[40]=
{
"+","-","*","/","=","+=","-=","*=","/=",
"\n","\t","\r","\\","\'","\"",".",
"<","<=",">",">=","==","!=","~=","!","~",
",",";","(",")","[","]","{","}","#","?",":",
"&","|","%","^"
};
std::string IdentifierTable[1000]={""};
char ResourcePrograme[16777216];
int isReserveWord(std::string &p)
{
for(int i=0;i<26;++i)
if(ReserveWord[i]==p)
return i+1;
return FAIL;
}
int isOperatorOrDelimiter(std::string &p)
{
for(int i=0;i<40;++i)
if(OperatorOrDelimiter[i]==p)
return i+1;
return FAIL;
}
bool isLetter(char t)
{
return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z'));
}
bool isNumber(char t)
{
return (('0'<=t) && (t<='9'));
}
void InputFile(std::string &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;
bool FindNote=false;
while(!fin.eof())
{
ResourcePrograme[i]=fin.get();
if(ResourcePrograme[i]=='\n')
FindNote=false;
if(ResourcePrograme[i]!='#' && !FindNote)
++i;
else if(ResourcePrograme[i]=='#')
{
FindNote=true;
}
if(fin.eof())
break;
}
ResourcePrograme[i]='@';
++i;
for(int j=0;j<i;++j)
std::cout<<ResourcePrograme[j];
std::cout<<std::endl;
fin.close();
return;
}
void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
{
char temp;
temp=Source[ptr];
while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127)
{
++ptr;
temp=Source[ptr];
}
token="";
if(isLetter(temp) || temp=='_')
{
token+=temp;
++ptr;
temp=Source[ptr];
while(isLetter(temp) || isNumber(temp) || temp=='_')
{
token+=temp;
++ptr;
temp=Source[ptr];
}
Syn=isReserveWord(token);
if(Syn==FAIL)
Syn=IDENTIFIER;
else
Syn=RESERVEWORD;
}
else if(isNumber(temp))
{
int PointCnt=0;
while(isNumber(temp))
{
token+=temp;
++ptr;
temp=Source[ptr];
if(temp=='.' && !PointCnt)
{
++PointCnt;
token+=temp;
++ptr;
temp=Source[ptr];
}
}
Syn=NUMBER;
}
else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' ||
temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' ||
temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&'||
temp=='%' || temp=='$' || temp=='^')
{
token+=temp;
++ptr;
Syn=OPERATOR;
}
else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~')
{
Syn=OPERATOR;
token+=temp;
++ptr;
temp=Source[ptr];
if(temp=='=')
{
token+=temp;
++ptr;
}
}
else if(temp=='\\')
{
Syn=OPERATOR;
token+=temp;
++ptr;
temp=Source[ptr];
if(temp=='=' || temp=='n' || temp=='t' || temp=='r' || temp=='\\' || temp=='\'' || temp=='\"')
{
token+=temp;
++ptr;
}
}
else if(temp=='\"')
{
Syn=STRING;
token+=temp;
++ptr;
temp=Source[ptr];
while(temp!='\"')
{
if(temp=='\\')
{
token+=temp;
++ptr;
temp=Source[ptr];
token+=temp;
++ptr;
temp=Source[ptr];
}
else
{
token+=temp;
++ptr;
temp=Source[ptr];
}
if(temp=='@' || temp=='\n')
break;
}
//add the last char \"
if(temp=='\"')
{
token+=temp;
++ptr;
}
else
token+=" __missing_end_of_string";
}
else if(temp=='@')
{
Syn=SCANEND;
return;
}
else
{
Syn=FAIL;
std::cout<<"[Error] Unexpected error occurred: "<<temp<<std::endl;
system("pause");
++ptr;
return;
}
if(token=="")
{
Syn=ERRORFOUND;
std::cout<<"[Error] Cannot identify "<<std::endl;
}
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()
{
help();
while(1)
{
int Syn=0;//token type
int Ptr=0;//pointer to one char in ResourcePrograme
std::string token;
std::string FileNameOrCommand;
std::cout<<">> ";
std::cin>>FileNameOrCommand;
if(FileNameOrCommand=="exit")
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==OPERATOR)
std::cout<<"( Operator | "<<token<<" )"<<std::endl;
else if(Syn==IDENTIFIER)
std::cout<<"( Identifier | "<<token<<" )"<<std::endl;
else if(Syn==NUMBER)
std::cout<<"( Number | "<<token<<" )"<<std::endl;
else if(Syn==RESERVEWORD)
std::cout<<"( ReserveWord | "<<token<<" )"<<std::endl;
else if(Syn==STRING)
std::cout<<"( String | "<<token<<" )"<<std::endl;
}
std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl;
//fout.close();
}
return 0;
}

159
misc/process_stack.h Normal file
View File

@ -0,0 +1,159 @@
#ifndef __PROCESS_STACK_H__
#define __PROCESS_STACK_H__
#include <iostream>
#include <cstring>
#include "nasal.h"
namespace nasal
{
struct process_stack_unit
{
int line; //place the unit first appear
std::string name; //content of the unit or name of the var/class/function
std::string format_type; //var class function string info
var unitdata;
bool global;
process_stack_unit *next;
process_stack_unit *last;
};
class process_stack
{
private:
process_stack_unit *head;
process_stack_unit *ptr;
public:
process_stack()
{
head=new process_stack_unit;
head->line=0;
head->name="InterpreterInfo";
head->format_type="Info";
head->global=false;
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;
}
~process_stack()
{
process_stack_unit *temp=head;
while(temp->next)
{
temp=temp->next;
delete head;
head=temp;
}
delete head;
}
void stack_append(process_stack_unit &p)
{
process_stack_unit *temp=head;
process_stack_unit *last_node;
while(temp->next)
{
temp=temp->next;
}
temp->next=new process_stack_unit;
last_node=temp;
temp=temp->next;
temp->last=last_node;
last_node->next=temp;
temp->next=NULL;
temp->name=p.name;
temp->line=p.line;
temp->format_type=p.format_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;
if(reverse_mode_used)
{
while(temp->next)
temp=temp->next;
while(temp->last)
{
std::cout<<"line "<<temp->line<<": |"<<temp->format_type<<"|"<<temp->name<<"|\n\t|";
temp->unitdata.Print();
std::cout<<std::endl;
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;
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)
{
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;
}
};
}
#endif

60
misc/str2num.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef __STR2NUM_H__
#define __STR2NUM_H__
#include <iostream>
#include <cstring>
namespace nasal
{
void Str2Num(std::string &str)
{
for(int i=0;i<(int)str.length();++i)
if(!(('0'<=str[i]) && (str[i]<='9') || (str[i]=='.')))
{
std::cout<<"[Error] Non-numeric string."<<std::endl;
return;
}
bool isFloat=false;
int DotPlace=0;
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
{
isFloat=true;
DotPlace=i;
break;
}
if(!isFloat)
{
long long int num=0;
long long int acc=1;
for(int i=(int)str.length()-1;i>=0;--i)
{
num+=acc*((long long int)(str[i]-'0'));
acc*=10;
}
std::cout<<num<<std::endl;
}
else
{
double num=0;
double acc=1;
double aff=0.1;
for(int i=DotPlace+1;i<(int)str.length();++i)
{
num+=aff*((double)(str[i]-'0'));
aff*=0.1;
}
for(int i=DotPlace-1;i>=0;--i)
{
num+=acc*((double)(str[i]-'0'));
acc*=10;
}
std::cout<<num<<std::endl;
}
return;
}
}
#endif