version 0.1 not finished

This commit is contained in:
Valk Richard Li 2019-08-08 02:24:49 +08:00 committed by GitHub
parent 6f6534dcc2
commit fa39c6abc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 2824 additions and 0 deletions

39
version0.1/lab.cpp Normal file
View File

@ -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;
}

17
version0.1/nasal.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef __NASAL_H__
#define __NASAL_H__
#include <iostream>
#include <cstring>
#include "nasal_list.h"
#include "nasal_func.h"
#include "nasal_hash.h"
#include "nasal_list.cpp"
#include "nasal_hash.cpp"
#include "nasal_var_stack.h"
#include "nasal_func_stack.h"
#include "nasal_lexer.h"
#endif

156
version0.1/nasal_func.h Normal file
View File

@ -0,0 +1,156 @@
#ifndef __NASAL_FUNC_H__
#define __NASAL_FUNC_H__
#include <iostream>
#include <cstring>
namespace nasal
{
struct func_unit
{
std::string type;
std::string content;
int line;
func_unit *next;
};
class func
{
private:
func_unit *head;
public:
func()
{
head=new func_unit;
head->type="Begin";
head->content="__process_begin";
head->line=0;
head->next=NULL;
}
~func()
{
func_unit *temp=head;
func_unit *this_node=NULL;
while(temp->next)
{
this_node=temp;
temp=temp->next;
delete this_node;
}
delete temp;
}
void delete_all()
{
if(!head->next)
return;
func_unit *temp=head;
func_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 char *_type,std::string &_content,const int _line)
{
func_unit *temp=head;
while(temp->next)
temp=temp->next;
temp->next=new func_unit;
temp=temp->next;
temp->next=NULL;
temp->type=_type;
temp->line=_line;
temp->content=_content;
return;
}
void print()
{
func_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=="Operator")
std::cout<<"( Operator | ";
else if(temp->type=="Identifier")
std::cout<<"( Identifier | ";
else if(temp->type=="Number")
std::cout<<"( Number | ";
else if(temp->type=="ReserveWord")
std::cout<<"( ReserveWord | ";
else if(temp->type=="String")
std::cout<<"( String | ";
std::cout<<temp->content<<" )"<<std::endl;
}
return;
}
func& operator=(const func &p)
{
delete_all();
func_unit *temp=head;
func_unit *ptemp=p.head;
while(ptemp->next)
{
ptemp=ptemp->next;
temp=new func_unit;
temp->type=ptemp->type;
temp->content=ptemp->content;
temp->line=ptemp->line;
temp->next=NULL;
}
temp=new func_unit;
temp->type=ptemp->type;
temp->content=ptemp->content;
temp->line=ptemp->line;
temp->next=NULL;
return *this;
}
void run()
{
func_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")
{
;
}
else if(temp->type=="ReserveWord")
{
;
}
else if(temp->type=="String")
{
;
}
}
return;
}
};
}
#endif

View File

@ -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

View File

@ -0,0 +1,426 @@
#ifndef __NASAL_FUNCTIONAL_H__
#define __NASAL_FUNCTIONAL_H__
#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;
std::string content;
int line;
token_unit *next;
};
class token_list
{
private:
token_unit *head;
public:
token_list()
{
head=new token_unit;
head->type="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 char *_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=="Operator")
std::cout<<"( Operator | ";
else if(temp->type=="Identifier")
std::cout<<"( Identifier | ";
else if(temp->type=="Number")
std::cout<<"( Number | ";
else if(temp->type=="ReserveWord")
std::cout<<"( ReserveWord | ";
else if(temp->type=="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

467
version0.1/nasal_hash.cpp Normal file
View File

@ -0,0 +1,467 @@
#ifndef __NASAL_HASH_CPP__
#define __NASAL_HASH_CPP__
#include "nasal_hash.h"
#include "nasal_list.h"
namespace nasal
{
NasalHash::NasalHash()
{
head=new HashUnit;
head->data=NULL;
head->next=NULL;
}
NasalHash::NasalHash(NasalHash &Source)
{
HashUnit *temp;
HashUnit *SourceTemp=Source.head;
head=new HashUnit;
head->next=NULL;
head->data=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new HashUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
temp->VarName=SourceTemp->VarName;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
else if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
else if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
else if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
else if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
else if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
else if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
else if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
}
NasalHash::~NasalHash()
{
HashUnit *temp=head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
}
void NasalHash::PrintHash()
{
HashUnit *temp=head;
std::cout<<"{ ";
while(temp->next)
{
temp=temp->next;
std::cout<<temp->VarName<<":";
if(temp->next)
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", ";
else if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", ";
else if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", ";
else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", ";
else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", ";
else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
else if(temp->Type=="array")
{
((NasalList *)temp->data)->PrintList();
std::cout<<", ";
}
else if(temp->Type=="hash")
{
((NasalHash *)temp->data)->PrintHash();
std::cout<<", ";
}
}
else
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data);
else if(temp->Type=="float")
std::cout<<*((float *)temp->data);
else if(temp->Type=="double")
std::cout<<*((double *)temp->data);
else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\"";
else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data);
else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
else if(temp->Type=="array")
((NasalList *)temp->data)->PrintList();
else if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash();
}
}
std::cout<<"}";
}
void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeName)
{
NasalHash TempHash;//sometimes user may use n.append(n)
if(TypeName=="hash")
TempHash=*((NasalHash *)AppendData);
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
HashUnit *NewHashMember=new HashUnit;
NewHashMember->data=NULL;
temp->next=NewHashMember;
NewHashMember->VarName=VariaName;
NewHashMember->Type=TypeName;
if(TypeName=="int")
{
NewHashMember->data=new int;
*((int *)NewHashMember->data)=*((int *)AppendData);
}
else if(TypeName=="float")
{
NewHashMember->data=new float;
*((float *)NewHashMember->data)=*((float *)AppendData);
}
else if(TypeName=="double")
{
NewHashMember->data=new double;
*((double *)NewHashMember->data)=*((double *)AppendData);
}
else if(TypeName=="char")
{
NewHashMember->data=new char;
*((char *)NewHashMember->data)=*((char *)AppendData);
}
else if(TypeName=="long long int")
{
NewHashMember->data=new long long int;
*((long long int *)NewHashMember->data)=*((long long int *)AppendData);
}
else if(TypeName=="string")
{
NewHashMember->data=new std::string;
*((std::string *)NewHashMember->data)=*((std::string *)AppendData);
}
else if(TypeName=="array")
{
NewHashMember->data=new NasalList;
*((NasalList *)NewHashMember->data)=*((NasalList *)AppendData);
}
else if(TypeName=="hash")
{
NewHashMember->data=new NasalHash;
*((NasalHash *)NewHashMember->data)=TempHash;
}
else if(TypeName=="var")
{
NewHashMember->Type=(*(var *)AppendData).Type;
if((*(var *)AppendData).Type=="int")
{
NewHashMember->data=new int;
*((int *)NewHashMember->data)=*((int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="float")
{
NewHashMember->data=new float;
*((float *)NewHashMember->data)=*((float *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="double")
{
NewHashMember->data=new double;
*((double *)NewHashMember->data)=*((double *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="char")
{
NewHashMember->data=new char;
*((char *)NewHashMember->data)=*((char *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="long long int")
{
NewHashMember->data=new long long int;
*((long long int *)NewHashMember->data)=*((long long int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="string")
{
NewHashMember->data=new std::string;
*((std::string *)NewHashMember->data)=*((std::string *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="array")
{
NewHashMember->data=new NasalList;
*((NasalList *)NewHashMember->data)=*((NasalList *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="hash")
{
NewHashMember->data=new NasalHash;
*((NasalHash *)NewHashMember->data)=*((NasalHash *)(*(var *)AppendData).data);
}
}
NewHashMember->next=NULL;
}
int NasalHash::Contains(const char *VariaName)
{
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->VarName==VariaName)
return 1;
}
return 0;
}
NasalList NasalHash::Keys()
{
NasalList FeedBackList;
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
FeedBackList.Append(&(temp->VarName),"string");
}
return FeedBackList;
}
void NasalHash::Delete(const char *VariaName)
{
HashUnit *temp=head;
HashUnit *LastNode;
while(temp->next)
{
LastNode=temp;
temp=temp->next;
if(temp->VarName==VariaName)
{
LastNode->next=temp->next;
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
delete temp;
return;
}
}
std::cout<<"[Error]: Could not find this element \""<<VariaName<<"\"."<<std::endl;
return;
}
NasalHash& NasalHash::operator=(const NasalHash &Source)
{
HashUnit *temp=head;
HashUnit *SourceTemp=Source.head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
head=new HashUnit;
head->next=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new HashUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
temp->VarName=SourceTemp->VarName;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
else if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
else if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
else if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
else if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
else if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
else if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
else if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
return *this;
}
HashUnit NasalHash::SearchElement(std::string &ElementName)
{
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->VarName==ElementName)
return *temp;
}
std::cout<<"[Error] Could not find \""<<ElementName<<"\""<<std::endl;
HashUnit nil_hash;
nil_hash.data=NULL;
nil_hash.next=NULL;
nil_hash.Type="null";
nil_hash.VarName="null";
return nil_hash;
}
}
#endif

41
version0.1/nasal_hash.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef __NASAL_HASH_H__
#define __NASAL_HASH_H__
#include <iostream>
#include <cstring>
#include "nasal_list.h"
namespace nasal
{
#ifndef nil
#define nil -1
#endif
struct HashUnit
{
std::string VarName;
std::string Type;
void *data;
HashUnit* next;
};
class NasalHash
{
private:
HashUnit *head;
public:
NasalHash();
NasalHash(NasalHash&);
~NasalHash();
void PrintHash();
void Append(const char *,void *,const char *);
int Contains(const char *);
NasalList Keys();
void Delete(const char *);
NasalHash& operator=(const NasalHash&);
HashUnit SearchElement(std::string &);
};
}
#endif

279
version0.1/nasal_lexer.h Normal file
View File

@ -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

1009
version0.1/nasal_list.cpp Normal file

File diff suppressed because it is too large Load Diff

60
version0.1/nasal_list.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef __NASAL_LIST_H__
#define __NASAL_LIST_H__
#include <iostream>
#include <cstring>
namespace nasal
{
#ifndef nil
#define nil -1
#endif
class var;
struct ListUnit
{
std::string Type;
void *data;
ListUnit* next;
};
class NasalList
{
private:
ListUnit *head;
public:
NasalList();
NasalList(NasalList &);
~NasalList();
void PrintList();
void Append(void *,const char *);
void Append(void *,std::string &);
NasalList& operator=(const NasalList &);
void SetSize(const int);
NasalList SubVec(const int,const int);
var Pop();
NasalList Sort(const int,const int);
ListUnit SearchElement(const int);
};
class var
{
public:
std::string Type;
void *data;
bool isGlobal;
var()
{
data=NULL;
}
var(const var &);
~var();
var& operator=(const var &);
void Print();
};
}
#endif

85
version0.1/nasal_print.h Normal file
View File

@ -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

View File

@ -0,0 +1,121 @@
#ifndef __NASAL_VAR_STACK_H__
#define __NASAL_VAR_STACK_H__
#include "nasal_list.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