update print & var

This commit is contained in:
Valk Richard Li 2019-08-07 02:44:31 +08:00 committed by GitHub
parent 196ec3a229
commit 4b47b87696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 341 additions and 110 deletions

35
lab.cpp Normal file
View File

@ -0,0 +1,35 @@
#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")
break;
else
{
RunProcess(Command);
nasal_lexer.run();
}
}
return 0;
}

View File

@ -29,6 +29,8 @@ class func_stack
{
func_stack_unit *temp=head;
func_stack_unit *this_node=NULL;
if(head->next)
{
while(temp->next)
{
this_node=temp;
@ -37,6 +39,9 @@ class func_stack
}
delete temp;
}
else
delete head;
}
void append_function(std::string &function_name,func &temp_func)
{
func_stack_unit *temp=head;
@ -95,6 +100,22 @@ class func_stack
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;
}
};
}

View File

@ -4,15 +4,78 @@
#include <iostream>
#include <cstring>
#include "nasal_var_stack.h"
#include "nasal_func_stack.h"
namespace nasal
{
bool isFloat(std::string &str)
{
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
return true;
return false;
}
long long int int_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 0;
}
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;
}
return num;
}
double double_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 0;
}
int DotPlace=0;
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
{
DotPlace=i;
break;
}
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;
}
return num;
}
#define FUNC_OPERATOR 1
#define FUNC_IDENTIFIER 2
#define FUNC_NUMBER 3
#define FUNC_RESERVEWORD 4
#define FUNC_STRING 5
var_stack nasal_var_stack;
func_stack nasal_func_stack;
var temp_var;
func temp_func;
struct token_unit
{
std::string type;
@ -108,6 +171,7 @@ class token_list
std::cout<<"Running complete."<<std::endl;
return;
}
while(temp->next)
{
temp=temp->next;
@ -125,13 +189,162 @@ class token_list
}
else if(temp->type=="ReserveWord")
{
;
if(temp->content=="var")
{
if(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
{
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing identifier after \"var\"."<<std::endl;
return;
}
//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
{
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect \"(\" after function print."<<std::endl;
return;
}
}
}
else if(temp->type=="String")
{
;
}
}
nasal_var_stack.print_var();
nasal_func_stack.print_function();
nasal_var_stack.delete_all();
nasal_func_stack.delete_all();
std::cout<<"Running complete."<<std::endl;
return;
}
};

View File

@ -7,6 +7,7 @@
#include <cstring>
#include "nasal_functional.h"
namespace nasal
{
@ -241,32 +242,33 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line)
return;
}
token_list lexer;
token_list nasal_lexer;
void RunProcess(std::string &FileNameOrCommand)
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(FileNameOrCommand);
InputFile(FileName);
while(Syn!=SCANEND && Syn!=ERRORFOUND)
{
Scanner(Syn,ResourcePrograme,token,Ptr,line);
if(Syn==OPERATOR)
lexer.append("Operator",token,line);
nasal_lexer.append("Operator",token,line);
else if(Syn==IDENTIFIER)
lexer.append("Identifier",token,line);
nasal_lexer.append("Identifier",token,line);
else if(Syn==NUMBER)
lexer.append("Number",token,line);
nasal_lexer.append("Number",token,line);
else if(Syn==RESERVEWORD)
lexer.append("ReserveWord",token,line);
nasal_lexer.append("ReserveWord",token,line);
else if(Syn==STRING)
lexer.append("String",token,line);
nasal_lexer.append("String",token,line);
}
lexer.print();
std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl;
nasal_lexer.print();
std::cout<<">> Complete scanning \""<<FileName<<"\"."<<std::endl;
return;
}

View File

@ -850,7 +850,7 @@ ListUnit NasalList::SearchElement(const int n)
return nil_list;
}
var::var(var &p)
var::var(const var &p)
{
Type=p.Type;
if(Type=="int")

View File

@ -49,7 +49,7 @@ class var
{
data=NULL;
}
var(var &);
var(const var &);
~var();
var& operator=(const var &);
void Print();

View File

@ -9,37 +9,6 @@
namespace nasal
{
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")
std::cout<<*((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";
}
void PrintVar(NasalHash &Var)
{
Var.PrintHash();
}
void PrintVar(NasalList &Var)
{
Var.PrintList();
}
void PrintString(std::string &PrintInfo)
{
for(int i=0;i<(int)PrintInfo.length();++i)
@ -89,57 +58,27 @@ void PrintString(std::string &PrintInfo)
}
return;
}
/*
void PrintString(const char *PrintInfo)
void PrintVar(var Var)
{
for(int i=0;i<strlen(PrintInfo);++i)
{
if(PrintInfo[i]=='\\' && i+1<strlen(PrintInfo))
{
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]<<"\' ."<<std::endl;
++i;
break;
}
}
else if(PrintInfo[i]=='\\' && i+1>=strlen(PrintInfo))
{
//error occurred
std::cout<<"[Error]: Missing character after \'\\\'"<<std::endl;
}
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<<PrintInfo[i];
}
return;
std::cout<<"null";
}
*/
}

View File

@ -30,6 +30,8 @@ class var_stack
{
var_stack_unit *temp=head;
var_stack_unit *this_node=NULL;
if(head->next)
{
while(temp->next)
{
this_node=temp;
@ -38,6 +40,9 @@ class var_stack
}
delete temp;
}
else
delete head;
}
void append_var(std::string &varia_name,var &temp_var)
{
var_stack_unit *temp=head;
@ -55,16 +60,16 @@ class var_stack
while(temp->next)
{
temp=temp->next;
std::cout<<"["<<temp->var_detail.Type<<"]: "<<temp->var_name;
std::cout<<"["<<temp->var_detail.Type<<"]: "<<temp->var_name<<" : ";
if(temp->var_detail.Type!="string")
PrintVar(temp->var_detail);
std::cout<<endl;
else
std::cout<<*((std::string *)temp->var_detail.data);
std::cout<<std::endl;
}
std::cout<<"["<<temp->var_detail.Type<<"]: "<<temp->var_name;
PrintVar(temp->var_detail);
std::cout<<endl;
return;
}
var &SearchVar(std::string varia_name)
var SearchVar(std::string varia_name)
{
var temp_var;
temp_var.data=NULL;
@ -93,6 +98,22 @@ class var_stack
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;
}
};
}