update
This commit is contained in:
parent
5a31a21982
commit
34d06d6ad1
|
@ -25,6 +25,8 @@ namespace nasal
|
||||||
//#include "nasal_func_stack.h"
|
//#include "nasal_func_stack.h"
|
||||||
#include "nasal_functional.cpp"
|
#include "nasal_functional.cpp"
|
||||||
|
|
||||||
|
#include "nasal_lexer.h"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -108,5 +108,360 @@ func& func::operator=(const func& temp)
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
void func::append_var(var& p)
|
||||||
|
{
|
||||||
|
parameter *parameter_temp=parameter_head;
|
||||||
|
while(parameter_temp->next)
|
||||||
|
parameter_temp=parameter_temp->next;
|
||||||
|
parameter_temp->next=new parameter;
|
||||||
|
parameter_temp=parameter_temp->next;
|
||||||
|
parameter_temp->param_var=p;
|
||||||
|
parameter_temp->next=NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void func::append_token(const int _line,const int _type,std::string &_content)
|
||||||
|
{
|
||||||
|
token_unit *statement_temp=statement_head;
|
||||||
|
while(statement_temp->next)
|
||||||
|
statement_temp=statement_temp->next;
|
||||||
|
statement_temp->next=new token_unit;
|
||||||
|
statement_temp=statement_temp->next;
|
||||||
|
statement_temp->line=_line;
|
||||||
|
statement_temp->type=_type;
|
||||||
|
statement_temp->content=_content;
|
||||||
|
statement_temp->next=NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
token_list::token_list()
|
||||||
|
{
|
||||||
|
head=new token_unit;
|
||||||
|
head->type=FUNC_BEGIN;
|
||||||
|
head->content="__process_begin";
|
||||||
|
head->line=0;
|
||||||
|
head->next=NULL;
|
||||||
|
}
|
||||||
|
token_list::~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 token_list::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 token_list::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 token_list::append(const int _line,const int _type,std::string &_content)
|
||||||
|
{
|
||||||
|
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 token_list::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 token_list::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
|
#endif
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "nasal_var.h"
|
#include "nasal_var.h"
|
||||||
|
|
||||||
|
//for function
|
||||||
#define FUNC_BEGIN 0
|
#define FUNC_BEGIN 0
|
||||||
#define FUNC_OPERATOR 1
|
#define FUNC_OPERATOR 1
|
||||||
#define FUNC_IDENTIFIER 2
|
#define FUNC_IDENTIFIER 2
|
||||||
|
@ -10,6 +11,13 @@
|
||||||
#define FUNC_RESERVEWORD 4
|
#define FUNC_RESERVEWORD 4
|
||||||
#define FUNC_STRING 5
|
#define FUNC_STRING 5
|
||||||
|
|
||||||
|
//for token and lexer
|
||||||
|
#define OPERATOR 1 //界符 or 运算符
|
||||||
|
#define IDENTIFIER 2 //自定义标识符
|
||||||
|
#define NUMBER 3 //数字
|
||||||
|
#define RESERVEWORD 4 //关键字
|
||||||
|
#define STRING 5 //字符串类型
|
||||||
|
|
||||||
struct token_unit
|
struct token_unit
|
||||||
{
|
{
|
||||||
int type;
|
int type;
|
||||||
|
@ -34,6 +42,8 @@ class func
|
||||||
func(const func&);
|
func(const func&);
|
||||||
~func();
|
~func();
|
||||||
func& operator=(const func&);
|
func& operator=(const func&);
|
||||||
|
void append_var(var&);
|
||||||
|
void append_token(const int,const int,std::string &);
|
||||||
};
|
};
|
||||||
|
|
||||||
class token_list
|
class token_list
|
||||||
|
@ -41,336 +51,12 @@ class token_list
|
||||||
private:
|
private:
|
||||||
token_unit *head;
|
token_unit *head;
|
||||||
public:
|
public:
|
||||||
token_list()
|
token_list();
|
||||||
{
|
~token_list();
|
||||||
head=new token_unit;
|
void print_line_token(const int);
|
||||||
head->type=FUNC_BEGIN;
|
void delete_all();
|
||||||
head->content="__process_begin";
|
void append(const int,const int,std::string&);
|
||||||
head->line=0;
|
void print();
|
||||||
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
|
#endif
|
||||||
|
|
|
@ -5,15 +5,6 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
nasal::nasal_list m;
|
|
||||||
nasal::nasal_hash n;
|
|
||||||
nasal::var k;
|
|
||||||
k.type=VAR_STRING;
|
|
||||||
k.data=new std::string;
|
|
||||||
*((std::string *)k.data)="hello";
|
|
||||||
m.append(k);
|
|
||||||
m.pop();
|
|
||||||
|
|
||||||
std::string command;
|
std::string command;
|
||||||
std::cout<<">> input \"help\" to find help."<<std::endl;
|
std::cout<<">> input \"help\" to find help."<<std::endl;
|
||||||
while(1)
|
while(1)
|
||||||
|
@ -22,22 +13,31 @@ int main()
|
||||||
std::cin>>command;
|
std::cin>>command;
|
||||||
if(command=="help")
|
if(command=="help")
|
||||||
{
|
{
|
||||||
std::cout<<">> 1.input file name to run the nasal script."<<std::endl;
|
std::cout<<">> 1. input file name to run the nasal script."<<std::endl;
|
||||||
std::cout<<">> 2.command cls to clear the screen."<<std::endl;
|
std::cout<<">> 2. command cls to clear the screen."<<std::endl;
|
||||||
std::cout<<">> 3.command exit to shut down the program."<<std::endl;
|
std::cout<<">> 3. command exit to shut down the program."<<std::endl;
|
||||||
|
std::cout<<">> 4. command lexer to see tokens in stack."<<std::endl;
|
||||||
|
std::cout<<">> 5. command del to delete all elements in stack."<<std::endl;
|
||||||
|
std::cout<<">> 6. command run to run the programme in stack."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="cls")
|
else if(command=="cls")
|
||||||
{
|
{
|
||||||
system("cls");
|
system("cls");
|
||||||
//linux system("clear");
|
//linux system("clear");
|
||||||
//macOS system(clear");
|
//macOS system("clear");
|
||||||
}
|
}
|
||||||
else if(command=="exit")
|
else if(command=="exit")
|
||||||
break;
|
break;
|
||||||
else
|
else if(command=="lexer")
|
||||||
|
nasal::nasal_lexer.print();
|
||||||
|
else if(command=="del")
|
||||||
|
nasal::nasal_lexer.delete_all();
|
||||||
|
else if(command=="run")
|
||||||
{
|
{
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
nasal::RunProcess(command);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,18 +7,9 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "nasal_functional.h"
|
#include "nasal_functional.h"
|
||||||
|
|
||||||
|
#define FAIL -1 //失败
|
||||||
namespace nasal
|
#define SCANEND -2 //扫描完成
|
||||||
{
|
#define ERRORFOUND -3 //异常错误
|
||||||
|
|
||||||
#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]=
|
std::string ReserveWord[26]=
|
||||||
{
|
{
|
||||||
|
@ -256,24 +247,12 @@ void RunProcess(std::string &FileName)
|
||||||
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
||||||
{
|
{
|
||||||
Scanner(Syn,ResourcePrograme,token,Ptr,line);
|
Scanner(Syn,ResourcePrograme,token,Ptr,line);
|
||||||
if(Syn==OPERATOR)
|
if(Syn>0)//all Syn type is larger than zero
|
||||||
nasal_lexer.append("Operator",token,line);
|
nasal_lexer.append(line,Syn,token);
|
||||||
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
|
//nasal_lexer.print(); //for debug mode
|
||||||
std::cout<<">> Complete scanning \""<<FileName<<"\"."<<std::endl;
|
std::cout<<">> Complete scanning \""<<FileName<<"\"."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue