basical elements
This commit is contained in:
parent
e9c6d78e56
commit
e35311b7d0
26
nasal.h
26
nasal.h
|
@ -1,23 +1,17 @@
|
|||
#ifndef __NASAL_H__
|
||||
#define __NASAL_H__
|
||||
|
||||
#include "nasal_hash.cpp"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "nasal_list.h"
|
||||
#include "nasal_func.h"
|
||||
#include "nasal_hash.h"
|
||||
|
||||
#include "nasal_list.cpp"
|
||||
#include "nasal_print.h"
|
||||
#include "nasal_hash.cpp"
|
||||
|
||||
// int
|
||||
// char
|
||||
// long long int
|
||||
// double
|
||||
// float
|
||||
// string
|
||||
// const char *
|
||||
// NasalHash
|
||||
// NasalList
|
||||
#include "nasal_var_stack.h"
|
||||
#include "nasal_func_stack.h"
|
||||
#include "nasal_lexer.h"
|
||||
|
||||
/*
|
||||
NasalList->delete NasalList & NasalHash
|
||||
|
||||
NasalHash->delete NasalList & NasalHash
|
||||
*/
|
||||
#endif
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,103 @@
|
|||
#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;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,141 @@
|
|||
#ifndef __NASAL_FUNCTIONAL_H__
|
||||
#define __NASAL_FUNCTIONAL_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
||||
#define FUNC_OPERATOR 1
|
||||
#define FUNC_IDENTIFIER 2
|
||||
#define FUNC_NUMBER 3
|
||||
#define FUNC_RESERVEWORD 4
|
||||
#define FUNC_STRING 5
|
||||
|
||||
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 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()
|
||||
{
|
||||
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")
|
||||
{
|
||||
;
|
||||
}
|
||||
else if(temp->type=="ReserveWord")
|
||||
{
|
||||
;
|
||||
}
|
||||
else if(temp->type=="String")
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -445,35 +445,22 @@ NasalHash& NasalHash::operator=(const NasalHash &Source)
|
|||
return *this;
|
||||
}
|
||||
|
||||
void NasalHash::SearchElement(std::string &ElementName)
|
||||
HashUnit NasalHash::SearchElement(std::string &ElementName)
|
||||
{
|
||||
HashUnit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->VarName==ElementName)
|
||||
{
|
||||
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();
|
||||
return;
|
||||
}
|
||||
return *temp;
|
||||
}
|
||||
std::cout<<"[Error] Could not find \""<<ElementName<<"\""<<std::endl;
|
||||
return;
|
||||
HashUnit nil_hash;
|
||||
nil_hash.data=NULL;
|
||||
nil_hash.next=NULL;
|
||||
nil_hash.Type="null";
|
||||
nil_hash.VarName="null";
|
||||
return nil_hash;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ class NasalHash
|
|||
NasalList Keys();
|
||||
void Delete(const char *);
|
||||
NasalHash& operator=(const NasalHash&);
|
||||
void SearchElement(std::string &);
|
||||
HashUnit SearchElement(std::string &);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,277 @@
|
|||
#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 lexer;
|
||||
|
||||
void RunProcess(std::string &FileNameOrCommand)
|
||||
{
|
||||
int Syn=0;//token type
|
||||
int Ptr=0;//pointer to one char in ResourcePrograme
|
||||
int line=1;
|
||||
std::string token;
|
||||
|
||||
InputFile(FileNameOrCommand);
|
||||
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
||||
{
|
||||
Scanner(Syn,ResourcePrograme,token,Ptr,line);
|
||||
if(Syn==OPERATOR)
|
||||
lexer.append("Operator",token,line);
|
||||
else if(Syn==IDENTIFIER)
|
||||
lexer.append("Identifier",token,line);
|
||||
else if(Syn==NUMBER)
|
||||
lexer.append("Number",token,line);
|
||||
else if(Syn==RESERVEWORD)
|
||||
lexer.append("ReserveWord",token,line);
|
||||
else if(Syn==STRING)
|
||||
lexer.append("String",token,line);
|
||||
}
|
||||
lexer.print();
|
||||
std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endif
|
|
@ -612,6 +612,14 @@ NasalList NasalList::SubVec(const int start,const int length=nil)
|
|||
var NasalList::Pop()
|
||||
{
|
||||
ListUnit *temp=head;
|
||||
if(!head->next)
|
||||
{
|
||||
var temp_var;
|
||||
temp_var.data=NULL;
|
||||
temp_var.Type="null";
|
||||
temp_var.isGlobal=false;
|
||||
return temp_var;
|
||||
}
|
||||
ListUnit *NewLastUnit;
|
||||
while(temp->next)
|
||||
{
|
||||
|
@ -819,7 +827,7 @@ NasalList NasalList::Sort(const int SortType,const int _cmp)
|
|||
return TempList;
|
||||
}
|
||||
|
||||
void NasalList::SearchElement(const int n)
|
||||
ListUnit NasalList::SearchElement(const int n)
|
||||
{
|
||||
if(n<0)
|
||||
{
|
||||
|
@ -832,12 +840,14 @@ void NasalList::SearchElement(const int n)
|
|||
temp=temp->next;
|
||||
cnt++;
|
||||
if(cnt==n)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return *temp;
|
||||
}
|
||||
std::cout<<"[Error] Out of range: real end: "<<cnt<<", less than "<<n<<"."<<std::endl;
|
||||
return;
|
||||
ListUnit nil_list;
|
||||
nil_list.data=NULL;
|
||||
nil_list.next=NULL;
|
||||
nil_list.Type="null";
|
||||
return nil_list;
|
||||
}
|
||||
|
||||
var::var(var &p)
|
||||
|
@ -930,6 +940,7 @@ var& var::operator=(const var &p)
|
|||
delete (NasalHash *)data;
|
||||
}
|
||||
Type=p.Type;
|
||||
isGlobal=p.isGlobal;
|
||||
if(Type=="int")
|
||||
{
|
||||
data=new int;
|
||||
|
|
|
@ -36,7 +36,7 @@ class NasalList
|
|||
NasalList SubVec(const int,const int);
|
||||
var Pop();
|
||||
NasalList Sort(const int,const int);
|
||||
void SearchElement(const int);
|
||||
ListUnit SearchElement(const int);
|
||||
};
|
||||
|
||||
class var
|
||||
|
@ -44,6 +44,7 @@ class var
|
|||
public:
|
||||
std::string Type;
|
||||
void *data;
|
||||
bool isGlobal;
|
||||
var()
|
||||
{
|
||||
data=NULL;
|
||||
|
|
|
@ -9,25 +9,26 @@
|
|||
namespace nasal
|
||||
{
|
||||
|
||||
void PrintVar(int Var)
|
||||
void PrintVar(var &Var)
|
||||
{
|
||||
std::cout<<Var;
|
||||
}
|
||||
void PrintVar(float Var)
|
||||
{
|
||||
std::cout<<Var;
|
||||
}
|
||||
void PrintVar(double Var)
|
||||
{
|
||||
std::cout<<Var;
|
||||
}
|
||||
void PrintVar(char Var)
|
||||
{
|
||||
std::cout<<Var;
|
||||
}
|
||||
void PrintVar(long long int Var)
|
||||
{
|
||||
std::cout<<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)
|
||||
{
|
||||
|
@ -88,7 +89,7 @@ void PrintString(std::string &PrintInfo)
|
|||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
void PrintString(const char *PrintInfo)
|
||||
{
|
||||
for(int i=0;i<strlen(PrintInfo);++i)
|
||||
|
@ -138,6 +139,7 @@ void PrintString(const char *PrintInfo)
|
|||
}
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
#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;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
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;
|
||||
PrintVar(temp->var_detail);
|
||||
std::cout<<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 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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue