bug fix
This commit is contained in:
parent
0571cb200b
commit
6f6534dcc2
|
@ -0,0 +1,39 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstring>
|
||||||
|
#include <thread>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include "nasal.h"
|
||||||
|
|
||||||
|
using namespace nasal;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout<<">> input \"help\" to find help."<<std::endl;
|
||||||
|
std::string Command;
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
std::cout<<">> ";
|
||||||
|
std::cin>>Command;
|
||||||
|
if(Command=="help")
|
||||||
|
{
|
||||||
|
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<<">> 3.command exit to shut down the program."<<std::endl;
|
||||||
|
}
|
||||||
|
else if(Command=="cls")
|
||||||
|
system("cls");
|
||||||
|
else if(Command=="exit")
|
||||||
|
{
|
||||||
|
nasal_var_stack.delete_all();
|
||||||
|
nasal_func_stack.delete_all();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RunProcess(Command);
|
||||||
|
nasal_lexer.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -109,6 +109,21 @@ class token_list
|
||||||
}
|
}
|
||||||
delete temp;
|
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()
|
void delete_all()
|
||||||
{
|
{
|
||||||
if(!head->next)
|
if(!head->next)
|
||||||
|
@ -165,10 +180,12 @@ class token_list
|
||||||
}
|
}
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
|
nasal_var_stack.delete_all();
|
||||||
|
nasal_func_stack.delete_all();
|
||||||
token_unit *temp=head;
|
token_unit *temp=head;
|
||||||
if(!head->next)
|
if(!head->next)
|
||||||
{
|
{
|
||||||
std::cout<<"Running complete."<<std::endl;
|
std::cout<<">> Running complete."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,127 +202,157 @@ class token_list
|
||||||
}
|
}
|
||||||
else if(temp->type=="Number")
|
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")
|
else if(temp->type=="ReserveWord")
|
||||||
{
|
{
|
||||||
if(temp->content=="var")
|
if(temp->content=="var")
|
||||||
{
|
{
|
||||||
if(temp->next)
|
if(temp->next)
|
||||||
{
|
|
||||||
temp=temp->next;
|
temp=temp->next;
|
||||||
std::string name_of_var;
|
|
||||||
if(temp->type=="Identifier")
|
|
||||||
name_of_var=temp->content;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing identifier after \"var\"."<<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(temp->next)
|
|
||||||
{
|
|
||||||
temp=temp->next;
|
|
||||||
if(temp->content=="=")
|
|
||||||
{
|
|
||||||
if(temp->next)
|
|
||||||
{
|
|
||||||
temp=temp->next;
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \"}\"."<<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//end {
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!(temp->next && temp->next->content==";"))
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing value after operator = ."<<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing \"=\" after identifier."<<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//end operator
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing elements after identifier."<<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//end identifier
|
|
||||||
}
|
|
||||||
else
|
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;
|
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing identifier after \"var\"."<<std::endl;
|
||||||
return;
|
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
|
//end var
|
||||||
}
|
}
|
||||||
else if(temp->content=="print")
|
else if(temp->content=="print")
|
||||||
|
@ -325,25 +372,50 @@ class token_list
|
||||||
}
|
}
|
||||||
else if(temp->type=="Identifier")
|
else if(temp->type=="Identifier")
|
||||||
PrintVar(nasal_var_stack.SearchVar(temp->content));
|
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
|
else
|
||||||
{
|
{
|
||||||
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect \"(\" after function print."<<std::endl;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(temp->type=="String")
|
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();
|
//nasal_var_stack.print_var(); //for debug mode
|
||||||
nasal_func_stack.print_function();
|
//nasal_func_stack.print_function(); //for debug mode
|
||||||
nasal_var_stack.delete_all();
|
nasal_var_stack.delete_all();
|
||||||
nasal_func_stack.delete_all();
|
nasal_func_stack.delete_all();
|
||||||
std::cout<<"Running complete."<<std::endl;
|
std::cout<<">> Running complete."<<std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue