new structure
This commit is contained in:
parent
fa39c6abc5
commit
c967a6980f
|
@ -0,0 +1,30 @@
|
|||
#ifndef __NASAL_H__
|
||||
#define __NASAL_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
#include "nasal_var.h"
|
||||
#include "nasal_list.h"
|
||||
#include "nasal_hash.h"
|
||||
#include "nasal_functional.h"
|
||||
|
||||
#include "nasal_var.cpp"
|
||||
#include "nasal_list.cpp"
|
||||
#include "nasal_hash.cpp"
|
||||
//#include "nasal_print.h"
|
||||
//#include "nasal_var_stack.h"
|
||||
//#include "nasal_func_stack.h"
|
||||
#include "nasal_functional.cpp"
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,124 @@
|
|||
#ifndef __NASAL_FUNC_STACK_H__
|
||||
#define __NASAL_FUNC_STACK_H__
|
||||
|
||||
#include "nasal_func.h"
|
||||
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
struct func_stack_unit
|
||||
{
|
||||
std::string func_name;
|
||||
func func_statement;
|
||||
func_stack_unit *next;
|
||||
};
|
||||
|
||||
class func_stack
|
||||
{
|
||||
private:
|
||||
func_stack_unit *head;
|
||||
public:
|
||||
func_stack()
|
||||
{
|
||||
head=new func_stack_unit;
|
||||
head->func_name="null";
|
||||
head->next=NULL;
|
||||
}
|
||||
~func_stack()
|
||||
{
|
||||
func_stack_unit *temp=head;
|
||||
func_stack_unit *this_node=NULL;
|
||||
if(head->next)
|
||||
{
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
else
|
||||
delete head;
|
||||
}
|
||||
void append_function(std::string &function_name,func &temp_func)
|
||||
{
|
||||
func_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->func_name==function_name)
|
||||
{
|
||||
std::cout<<"[Error] Redeclaration of function \""<<function_name<<"\""<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
temp->next=new func_stack_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->func_statement=temp_func;
|
||||
return;
|
||||
}
|
||||
void run_function(std::string &function_name)
|
||||
{
|
||||
func_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->func_name==function_name)
|
||||
{
|
||||
temp->func_statement.run();
|
||||
return;
|
||||
}
|
||||
}
|
||||
std::cout<<"[Error] Could not find this function."<<std::endl;
|
||||
return;
|
||||
}
|
||||
void print_function()
|
||||
{
|
||||
func_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
std::cout<<"function: "<<temp->func_name<<std::endl;
|
||||
}
|
||||
std::cout<<"function: "<<temp->func_name<<std::endl;
|
||||
return;
|
||||
}
|
||||
void pop_function()
|
||||
{
|
||||
func_stack_unit *temp=head;
|
||||
func_stack_unit *end_temp;
|
||||
if(!head->next)
|
||||
return;
|
||||
while(temp->next)
|
||||
{
|
||||
end_temp=temp;
|
||||
temp=temp->next;
|
||||
}
|
||||
end_temp->next=NULL;
|
||||
delete temp;
|
||||
}
|
||||
void delete_all()
|
||||
{
|
||||
func_stack_unit *temp=head->next;
|
||||
func_stack_unit *this_node=NULL;
|
||||
head->next=NULL;
|
||||
if(!temp)
|
||||
return;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,4 @@
|
|||
#ifndef __NASAL_FUNCTIONAL_CPP__
|
||||
#define __NASAL_FUNCTIONAL_CPP__
|
||||
|
||||
#endif
|
|
@ -0,0 +1,365 @@
|
|||
#ifndef __NASAL_FUNCTIONAL_H__
|
||||
#define __NASAL_FUNCTIONAL_H__
|
||||
|
||||
#define FUNC_BEGIN 0
|
||||
#define FUNC_OPERATOR 1
|
||||
#define FUNC_IDENTIFIER 2
|
||||
#define FUNC_NUMBER 3
|
||||
#define FUNC_RESERVEWORD 4
|
||||
#define FUNC_STRING 5
|
||||
|
||||
struct token_unit
|
||||
{
|
||||
int type;
|
||||
std::string content;
|
||||
int line;
|
||||
token_unit *next;
|
||||
};
|
||||
|
||||
class func
|
||||
{
|
||||
private:
|
||||
token_unit *head;
|
||||
public:
|
||||
func();
|
||||
~func();
|
||||
};
|
||||
|
||||
class token_list
|
||||
{
|
||||
private:
|
||||
token_unit *head;
|
||||
public:
|
||||
token_list()
|
||||
{
|
||||
head=new token_unit;
|
||||
head->type=FUNC_BEGIN;
|
||||
head->content="__process_begin";
|
||||
head->line=0;
|
||||
head->next=NULL;
|
||||
}
|
||||
~token_list()
|
||||
{
|
||||
token_unit *temp=head;
|
||||
token_unit *this_node=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
void print_line_token(const int _line)
|
||||
{
|
||||
std::cout<<"line "<<_line<<": ";
|
||||
token_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->line==_line)
|
||||
std::cout<<temp->content<<" ";
|
||||
else if(temp->line>_line)
|
||||
break;
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return;
|
||||
}
|
||||
void delete_all()
|
||||
{
|
||||
if(!head->next)
|
||||
return;
|
||||
token_unit *temp=head;
|
||||
token_unit *this_node=NULL;
|
||||
temp=temp->next;
|
||||
head->next=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
return;
|
||||
}
|
||||
void append(const int _type,std::string &_content,const int _line)
|
||||
{
|
||||
token_unit *temp=head;
|
||||
while(temp->next)
|
||||
temp=temp->next;
|
||||
temp->next=new token_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->type=_type;
|
||||
temp->line=_line;
|
||||
temp->content=_content;
|
||||
return;
|
||||
}
|
||||
void print()
|
||||
{
|
||||
token_unit *temp=head;
|
||||
std::cout<<"line "<<temp->line<<": "<<"( ProcessBegin | "<<temp->content<<" )"<<std::endl;
|
||||
if(!head->next)
|
||||
return;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
std::cout<<"line "<<temp->line<<": ";
|
||||
if(temp->type==FUNC_OPERATOR)
|
||||
std::cout<<"( Operator | ";
|
||||
else if(temp->type==FUNC_IDENTIFIER)
|
||||
std::cout<<"( Identifier | ";
|
||||
else if(temp->type==FUNC_NUMBER)
|
||||
std::cout<<"( Number | ";
|
||||
else if(temp->type==FUNC_RESERVEWORD)
|
||||
std::cout<<"( ReserveWord | ";
|
||||
else if(temp->type==FUNC_STRING)
|
||||
std::cout<<"( String | ";
|
||||
std::cout<<temp->content<<" )"<<std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
/*
|
||||
void run()
|
||||
{
|
||||
nasal_var_stack.delete_all();
|
||||
nasal_func_stack.delete_all();
|
||||
token_unit *temp=head;
|
||||
if(!head->next)
|
||||
{
|
||||
std::cout<<">> Running complete."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->type=="Operator")
|
||||
{
|
||||
;
|
||||
}
|
||||
else if(temp->type=="Identifier")
|
||||
{
|
||||
;
|
||||
}
|
||||
else if(temp->type=="Number")
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Number without any tokens."<<std::endl;
|
||||
return;
|
||||
}
|
||||
else if(temp->type=="ReserveWord")
|
||||
{
|
||||
if(temp->content=="var")
|
||||
{
|
||||
if(temp->next)
|
||||
temp=temp->next;
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing elements after \"var\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
//end identifier
|
||||
std::string name_of_var;
|
||||
if(temp->type=="Identifier")
|
||||
name_of_var=temp->content;
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing identifier after \"var\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
if(temp->next)
|
||||
temp=temp->next;
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing operator \"=\" after identifier."<<std::endl;
|
||||
return;
|
||||
}
|
||||
if(temp->content!="=")
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing operator \"=\" after identifier."<<std::endl;
|
||||
return;
|
||||
}
|
||||
if(temp->next)
|
||||
temp=temp->next;
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing value after operator \"=\" ."<<std::endl;
|
||||
return;
|
||||
}
|
||||
if(temp->type=="Number")
|
||||
{
|
||||
temp_var.isGlobal=true;
|
||||
if(isFloat(temp->content))
|
||||
{
|
||||
temp_var.Type="double";
|
||||
temp_var.data=new double;
|
||||
*((double *)temp_var.data)=double_Str2Num(temp->content);
|
||||
nasal_var_stack.append_var(name_of_var,temp_var);
|
||||
delete (double *)temp_var.data;
|
||||
temp_var.data=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_var.Type="long long int";
|
||||
temp_var.data=new long long int;
|
||||
*((long long int *)temp_var.data)=int_Str2Num(temp->content);
|
||||
nasal_var_stack.append_var(name_of_var,temp_var);
|
||||
delete (long long int *)temp_var.data;
|
||||
temp_var.data=NULL;
|
||||
}
|
||||
}
|
||||
else if(temp->type=="String")
|
||||
{
|
||||
temp_var.isGlobal=true;
|
||||
temp_var.Type="string";
|
||||
temp_var.data=new std::string;
|
||||
std::string temp_string="";
|
||||
for(int i=1;i<(int)temp->content.length()-1;++i)
|
||||
temp_string+=temp->content[i];
|
||||
*((std::string *)temp_var.data)=temp_string;
|
||||
nasal_var_stack.append_var(name_of_var,temp_var);
|
||||
delete (std::string *)temp_var.data;
|
||||
temp_var.data=NULL;
|
||||
}
|
||||
else if(temp->type=="Operator" && temp->content=="{")
|
||||
{
|
||||
bool make_pair=false;
|
||||
int cnt=1;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->type=="Operator" && temp->content=="}")
|
||||
{
|
||||
--cnt;
|
||||
if(!cnt)
|
||||
{
|
||||
make_pair=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(temp->type=="Operator" && temp->content=="{")
|
||||
++cnt;
|
||||
}
|
||||
if(!make_pair)
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \"}\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if(temp->type=="Operator" && temp->content=="[")
|
||||
{
|
||||
bool make_pair=false;
|
||||
int cnt=1;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->type=="Operator" && temp->content=="]")
|
||||
{
|
||||
--cnt;
|
||||
if(!cnt)
|
||||
{
|
||||
make_pair=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(temp->type=="Operator" && temp->content=="[")
|
||||
++cnt;
|
||||
}
|
||||
if(!make_pair)
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \"]\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing value after operator \"=\" ."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!(temp->next && temp->next->content==";"))
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl;
|
||||
nasal_var_stack.pop_var();
|
||||
return;
|
||||
}
|
||||
else
|
||||
temp=temp->next;
|
||||
//end var
|
||||
}
|
||||
else if(temp->content=="print")
|
||||
{
|
||||
if(temp->next && temp->next->content=="(")
|
||||
{
|
||||
temp=temp->next;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->type=="String")
|
||||
{
|
||||
std::string temp_string="";
|
||||
for(int i=1;i<(int)temp->content.length()-1;++i)
|
||||
temp_string+=temp->content[i];
|
||||
PrintString(temp_string);
|
||||
}
|
||||
else if(temp->type=="Identifier")
|
||||
PrintVar(nasal_var_stack.SearchVar(temp->content));
|
||||
else if(temp->type=="Operator" && temp->content==")")
|
||||
{
|
||||
if(!temp->next)
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if(temp->type=="Operator" && temp->content==";")
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \")\" at the end of print."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \"(\" after function print."<<std::endl;
|
||||
return;
|
||||
}
|
||||
if(!(temp->next && temp->next->content==";"))
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(temp->type=="String")
|
||||
{
|
||||
print_line_token(temp->line);
|
||||
std::cout<<"line "<<temp->line<<": "<<"[Error] String without any tokens."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
//nasal_var_stack.print_var(); //for debug mode
|
||||
//nasal_func_stack.print_function(); //for debug mode
|
||||
nasal_var_stack.delete_all();
|
||||
nasal_func_stack.delete_all();
|
||||
std::cout<<">> Running complete."<<std::endl;
|
||||
return;
|
||||
}*/
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __NASAL_HASH_CPP__
|
||||
#define __NASAL_HASH_CPP__
|
||||
|
||||
nasal_hash::nasal_hash()
|
||||
{
|
||||
;
|
||||
}
|
||||
nasal_hash::~nasal_hash()
|
||||
{
|
||||
;
|
||||
}
|
||||
nasal_hash& nasal_hash::operator=(const nasal_hash &temp)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef __NASAL_HASH_H__
|
||||
#define __NASAL_HASH_H__
|
||||
|
||||
|
||||
struct nasal_hash_unit
|
||||
{
|
||||
std::string name;
|
||||
std::string type;
|
||||
void *data;
|
||||
nasal_hash_unit *next;
|
||||
};
|
||||
|
||||
class nasal_hash
|
||||
{
|
||||
private:
|
||||
nasal_hash_unit *head;
|
||||
public:
|
||||
nasal_hash();
|
||||
~nasal_hash();
|
||||
nasal_hash& operator=(const nasal_hash&);
|
||||
void append_var(var&);
|
||||
void append_list(nasal_list&);
|
||||
void append_hash(nasal_hash&);
|
||||
int contains(std::string&);
|
||||
int delete_element(std::string&);
|
||||
nasal_list keys();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,9 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "nasal.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,279 @@
|
|||
#ifndef __NASAL_LEXER_H__
|
||||
#define __NASAL_LEXER_H__
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include "nasal_functional.h"
|
||||
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
#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 //异常错误
|
||||
|
||||
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]='@';
|
||||
// print source file
|
||||
// ++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,int &line)
|
||||
{
|
||||
char temp;
|
||||
temp=Source[ptr];
|
||||
while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127)
|
||||
{
|
||||
++ptr;
|
||||
if(temp=='\n')
|
||||
++line;
|
||||
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;
|
||||
}
|
||||
|
||||
token_list nasal_lexer;
|
||||
|
||||
void RunProcess(std::string &FileName)
|
||||
{
|
||||
int Syn=0;//token type
|
||||
int Ptr=0;//pointer to one char in ResourcePrograme
|
||||
int line=1;
|
||||
std::string token;
|
||||
nasal_lexer.delete_all();
|
||||
|
||||
InputFile(FileName);
|
||||
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
||||
{
|
||||
Scanner(Syn,ResourcePrograme,token,Ptr,line);
|
||||
if(Syn==OPERATOR)
|
||||
nasal_lexer.append("Operator",token,line);
|
||||
else if(Syn==IDENTIFIER)
|
||||
nasal_lexer.append("Identifier",token,line);
|
||||
else if(Syn==NUMBER)
|
||||
nasal_lexer.append("Number",token,line);
|
||||
else if(Syn==RESERVEWORD)
|
||||
nasal_lexer.append("ReserveWord",token,line);
|
||||
else if(Syn==STRING)
|
||||
nasal_lexer.append("String",token,line);
|
||||
}
|
||||
//nasal_lexer.print(); //for debug mode
|
||||
std::cout<<">> Complete scanning \""<<FileName<<"\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __NASAL_LIST_CPP__
|
||||
#define __NASAL_LIST_CPP__
|
||||
|
||||
nasal_list::nasal_list()
|
||||
{
|
||||
;
|
||||
}
|
||||
nasal_list::~nasal_list()
|
||||
{
|
||||
;
|
||||
}
|
||||
nasal_list& nasal_list::operator=(const nasal_list &temp)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef __NASAL_LIST_H__
|
||||
#define __NASAL_LIST_H__
|
||||
|
||||
class nasal_hash;
|
||||
|
||||
struct nasal_list_unit
|
||||
{
|
||||
std::string type;
|
||||
void *data;
|
||||
nasal_list_unit *next;
|
||||
};
|
||||
|
||||
class nasal_list
|
||||
{
|
||||
private:
|
||||
nasal_list_unit *head;
|
||||
public:
|
||||
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 setsize(const int);
|
||||
nasal_list subvec(const int,const int);
|
||||
var pop();
|
||||
void sort_list(bool);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,85 @@
|
|||
#ifndef __NASAL_PRINT_H__
|
||||
#define __NASAL_PRINT_H__
|
||||
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include "nasal_hash.cpp"
|
||||
#include "nasal_list.cpp"
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
void PrintString(std::string &PrintInfo)
|
||||
{
|
||||
for(int i=0;i<(int)PrintInfo.length();++i)
|
||||
{
|
||||
if(PrintInfo[i]=='\\' && i+1<(int)PrintInfo.length())
|
||||
{
|
||||
switch(PrintInfo[i+1])
|
||||
{
|
||||
case 'n':
|
||||
std::cout<<"\n";
|
||||
++i;
|
||||
break;
|
||||
case 't':
|
||||
std::cout<<"\t";
|
||||
++i;
|
||||
break;
|
||||
case 'r':
|
||||
std::cout<<"\r";
|
||||
++i;
|
||||
break;
|
||||
case '\\':
|
||||
std::cout<<"\\";
|
||||
++i;
|
||||
break;
|
||||
case '\'':
|
||||
std::cout<<"\'";
|
||||
++i;
|
||||
break;
|
||||
case '\"':
|
||||
std::cout<<"\"";
|
||||
++i;
|
||||
break;
|
||||
default:
|
||||
//error occurred
|
||||
std::cout<<"[Error]: Incorrect escape character \'"<<PrintInfo[i]<<PrintInfo[i+1]<<"\' .";
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(PrintInfo[i]=='\\' && i+1>=(int)PrintInfo.length())
|
||||
{
|
||||
//error occurred
|
||||
std::cout<<"[Error]: Missing character after \'\\\'";
|
||||
}
|
||||
else
|
||||
std::cout<<PrintInfo[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
void PrintVar(var Var)
|
||||
{
|
||||
if(Var.Type=="int")
|
||||
std::cout<<*((int *)Var.data);
|
||||
else if(Var.Type=="long long int")
|
||||
std::cout<<*((long long int *)Var.data);
|
||||
else if(Var.Type=="float")
|
||||
std::cout<<*((float *)Var.data);
|
||||
else if(Var.Type=="double")
|
||||
std::cout<<*((double *)Var.data);
|
||||
else if(Var.Type=="char")
|
||||
std::cout<<*((char *)Var.data);
|
||||
else if(Var.Type=="string")
|
||||
PrintString(*((std::string *)Var.data));
|
||||
else if(Var.Type=="array")
|
||||
((NasalList *)Var.data)->PrintList();
|
||||
else if(Var.Type=="hash")
|
||||
((NasalHash *)Var.data)->PrintHash();
|
||||
else
|
||||
std::cout<<"null";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,126 @@
|
|||
#ifndef __NASAL_VAR_CPP__
|
||||
#define __NASAL_VAR_CPP__
|
||||
|
||||
var::var()
|
||||
{
|
||||
type=VAR_NONE;
|
||||
data=NULL;
|
||||
}
|
||||
var::var(const var &temp)
|
||||
{
|
||||
type=temp.type;
|
||||
switch(type)
|
||||
{
|
||||
case VAR_NONE:
|
||||
data=NULL;
|
||||
break;
|
||||
case VAR_LLINT:
|
||||
data=new long long int;
|
||||
*((long long int *)data)=*((long long int *)temp.data);
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
data=new double;
|
||||
*((double *)data)=*((double *)temp.data);
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
data=new char;
|
||||
*((char *)data)=*((char *)temp.data);
|
||||
break;
|
||||
case VAR_STRING:
|
||||
data=new std::string;
|
||||
*((std::string *)data)=*((std::string *)temp.data);
|
||||
break;
|
||||
case VAR_LIST:
|
||||
data=new nasal_list;
|
||||
*((nasal_list *)data)=*((nasal_list *)temp.data);
|
||||
break;
|
||||
case VAR_HASH:
|
||||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
var::~var()
|
||||
{
|
||||
if(data)
|
||||
switch(type)
|
||||
{
|
||||
case VAR_LLINT:
|
||||
delete (long long int *)data;
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
delete (double *)data;
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
delete (char *)data;
|
||||
break;
|
||||
case VAR_STRING:
|
||||
delete (std::string *)data;
|
||||
break;
|
||||
case VAR_LIST:
|
||||
delete (nasal_list *)data;
|
||||
break;
|
||||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var& var::operator=(const var &temp)
|
||||
{
|
||||
if(data)
|
||||
switch(type)
|
||||
{
|
||||
case VAR_LLINT:
|
||||
delete (long long int *)data;
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
delete (double *)data;
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
delete (char *)data;
|
||||
break;
|
||||
case VAR_STRING:
|
||||
delete (std::string *)data;
|
||||
break;
|
||||
case VAR_LIST:
|
||||
delete (nasal_list *)data;
|
||||
break;
|
||||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
}
|
||||
type=temp.type;
|
||||
switch(type)
|
||||
{
|
||||
case VAR_NONE:
|
||||
data=NULL;
|
||||
break;
|
||||
case VAR_LLINT:
|
||||
data=new long long int;
|
||||
*((long long int *)data)=*((long long int *)temp.data);
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
data=new double;
|
||||
*((double *)data)=*((double *)temp.data);
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
data=new char;
|
||||
*((char *)data)=*((char *)temp.data);
|
||||
break;
|
||||
case VAR_STRING:
|
||||
data=new std::string;
|
||||
*((std::string *)data)=*((std::string *)temp.data);
|
||||
break;
|
||||
case VAR_LIST:
|
||||
data=new nasal_list;
|
||||
*((nasal_list *)data)=*((nasal_list *)temp.data);
|
||||
break;
|
||||
case VAR_HASH:
|
||||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef __NASAL_VAR_H__
|
||||
#define __NASAL_VAR_H__
|
||||
|
||||
#define VAR_NONE 0
|
||||
#define VAR_LLINT 1
|
||||
#define VAR_DOUBLE 2
|
||||
#define VAR_CHAR 3
|
||||
#define VAR_STRING 4
|
||||
#define VAR_LIST 5
|
||||
#define VAR_HASH 6
|
||||
|
||||
class var
|
||||
{
|
||||
public:
|
||||
int type;
|
||||
void *data;
|
||||
var();
|
||||
var(const var&);
|
||||
~var();
|
||||
var& operator=(const var&);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,119 @@
|
|||
#ifndef __NASAL_VAR_STACK_H__
|
||||
#define __NASAL_VAR_STACK_H__
|
||||
|
||||
#include "nasal_print.h"
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
struct var_stack_unit
|
||||
{
|
||||
std::string var_name;
|
||||
var var_detail;
|
||||
var_stack_unit *next;
|
||||
};
|
||||
|
||||
class var_stack
|
||||
{
|
||||
private:
|
||||
var_stack_unit *head;
|
||||
public:
|
||||
var_stack()
|
||||
{
|
||||
head=new var_stack_unit;
|
||||
head->var_name="null";
|
||||
head->next=NULL;
|
||||
}
|
||||
~var_stack()
|
||||
{
|
||||
var_stack_unit *temp=head;
|
||||
var_stack_unit *this_node=NULL;
|
||||
if(head->next)
|
||||
{
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
else
|
||||
delete head;
|
||||
}
|
||||
void append_var(std::string &varia_name,var &temp_var)
|
||||
{
|
||||
var_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
temp=temp->next;
|
||||
temp->next=new var_stack_unit;
|
||||
temp=temp->next;
|
||||
temp->var_name=varia_name;
|
||||
temp->var_detail=temp_var;
|
||||
temp->next=NULL;
|
||||
}
|
||||
void print_var()
|
||||
{
|
||||
var_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
std::cout<<"["<<temp->var_detail.Type<<"]: "<<temp->var_name<<" : ";
|
||||
if(temp->var_detail.Type!="string")
|
||||
PrintVar(temp->var_detail);
|
||||
else
|
||||
std::cout<<*((std::string *)temp->var_detail.data);
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
var SearchVar(std::string varia_name)
|
||||
{
|
||||
var temp_var;
|
||||
temp_var.data=NULL;
|
||||
temp_var.Type="null";
|
||||
temp_var.isGlobal=false;
|
||||
var_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->var_name==varia_name)
|
||||
temp_var=temp->var_detail;
|
||||
}
|
||||
return temp_var;
|
||||
}
|
||||
void pop_var()
|
||||
{
|
||||
var_stack_unit *temp=head;
|
||||
var_stack_unit *end_temp;
|
||||
if(!head->next)
|
||||
return;
|
||||
while(temp->next)
|
||||
{
|
||||
end_temp=temp;
|
||||
temp=temp->next;
|
||||
}
|
||||
end_temp->next=NULL;
|
||||
delete temp;
|
||||
}
|
||||
void delete_all()
|
||||
{
|
||||
var_stack_unit *temp=head->next;
|
||||
var_stack_unit *this_node=NULL;
|
||||
head->next=NULL;
|
||||
if(!temp)
|
||||
return;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue