update parse

This commit is contained in:
Valk Richard Li 2019-08-10 23:41:39 +08:00 committed by GitHub
parent 7aefdc470c
commit e8fad23955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 512 additions and 309 deletions

View File

@ -20,13 +20,13 @@ namespace nasal
#include "nasal_var.cpp" #include "nasal_var.cpp"
#include "nasal_list.cpp" #include "nasal_list.cpp"
#include "nasal_hash.cpp" #include "nasal_hash.cpp"
//#include "nasal_print.h" #include "nasal_print.h"
//#include "nasal_var_stack.h" #include "nasal_var_stack.h"
//#include "nasal_func_stack.h" #include "nasal_func_stack.h"
#include "nasal_functional.cpp" #include "nasal_functional.cpp"
#include "nasal_lexer.h" #include "nasal_lexer.h"
#include "nasal_parse.h"
} }
#endif #endif

View File

@ -1,12 +1,9 @@
#ifndef __NASAL_FUNC_STACK_H__ #ifndef __NASAL_FUNC_STACK_H__
#define __NASAL_FUNC_STACK_H__ #define __NASAL_FUNC_STACK_H__
#include "nasal_func.h" #include "nasal_functional.h"
namespace nasal
{
struct func_stack_unit struct func_stack_unit
{ {
std::string func_name; std::string func_name;
@ -60,21 +57,21 @@ class func_stack
temp->func_statement=temp_func; temp->func_statement=temp_func;
return; return;
} }
void run_function(std::string &function_name) // void run_function(std::string &function_name)
{ // {
func_stack_unit *temp=head; // func_stack_unit *temp=head;
while(temp->next) // while(temp->next)
{ // {
temp=temp->next; // temp=temp->next;
if(temp->func_name==function_name) // if(temp->func_name==function_name)
{ // {
temp->func_statement.run(); // temp->func_statement.run();
return; // return;
} // }
} // }
std::cout<<"[Error] Could not find this function."<<std::endl; // std::cout<<"[Error] Could not find this function."<<std::endl;
return; // return;
} // }
void print_function() void print_function()
{ {
func_stack_unit *temp=head; func_stack_unit *temp=head;
@ -117,8 +114,6 @@ class func_stack
return; return;
} }
}; };
func_stack nasal_func_stack;
}
#endif #endif

View File

@ -2,6 +2,64 @@
#define __NASAL_FUNCTIONAL_CPP__ #define __NASAL_FUNCTIONAL_CPP__
#include "nasal_functional.h" #include "nasal_functional.h"
#include "nasal_var_stack.h"
#include "nasal_func_stack.h"
bool is_float(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;
}
func::func() func::func()
{ {
@ -135,6 +193,7 @@ void func::append_token(const int _line,const int _type,std::string &_content)
token_list::token_list() token_list::token_list()
{ {
list_range=0;
head=new token_unit; head=new token_unit;
head->type=FUNC_BEGIN; head->type=FUNC_BEGIN;
head->content="__process_begin"; head->content="__process_begin";
@ -153,6 +212,14 @@ token_list::~token_list()
} }
delete temp; delete temp;
} }
token_unit* token_list::get_head()
{
return head;
}
int token_list::get_list_range()
{
return list_range;
}
void token_list::print_line_token(const int _line) void token_list::print_line_token(const int _line)
{ {
std::cout<<"line "<<_line<<": "; std::cout<<"line "<<_line<<": ";
@ -190,6 +257,7 @@ void token_list::append(const int _line,const int _type,std::string &_content)
token_unit *temp=head; token_unit *temp=head;
while(temp->next) while(temp->next)
temp=temp->next; temp=temp->next;
++list_range;
temp->next=new token_unit; temp->next=new token_unit;
temp=temp->next; temp=temp->next;
temp->next=NULL; temp->next=NULL;
@ -208,260 +276,305 @@ void token_list::print()
{ {
temp=temp->next; temp=temp->next;
std::cout<<"line "<<temp->line<<": "; std::cout<<"line "<<temp->line<<": ";
if(temp->type==FUNC_OPERATOR) if(temp->type==OPERATOR)
std::cout<<"( Operator | "; std::cout<<"( Operator | ";
else if(temp->type==FUNC_IDENTIFIER) else if(temp->type==IDENTIFIER)
std::cout<<"( Identifier | "; std::cout<<"( Identifier | ";
else if(temp->type==FUNC_NUMBER) else if(temp->type==NUMBER)
std::cout<<"( Number | "; std::cout<<"( Number | ";
else if(temp->type==FUNC_RESERVEWORD) else if(temp->type==RESERVEWORD)
std::cout<<"( ReserveWord | "; std::cout<<"( ReserveWord | ";
else if(temp->type==FUNC_STRING) else if(temp->type==STRING)
std::cout<<"( String | "; std::cout<<"( String | ";
std::cout<<temp->content<<" )"<<std::endl; std::cout<<temp->content<<" )"<<std::endl;
} }
return; 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) parse::parse()
{ {
temp=temp->next; len=0;
if(temp->type=="Operator") content_array=new parse_unit[4096];
{ statement=new parse_unit[2048];
;
} }
else if(temp->type=="Identifier") parse::~parse()
{ {
; if(content_array)
delete []content_array;
if(statement)
delete []statement;
} }
else if(temp->type=="Number") void parse::content_array_set_empty()
{ {
print_line_token(temp->line); for(int i=0;i<4096;++i)
std::cout<<"line "<<temp->line<<": "<<"[Error] Number without any tokens."<<std::endl; {
content_array[i].line=0;
content_array[i].type=0;
content_array[i].content="";
}
return; return;
} }
else if(temp->type=="ReserveWord") void parse::statement_set_empty()
{ {
if(temp->content=="var") for(int i=0;i<2048;++i)
{ {
if(temp->next) statement[i].line=0;
temp=temp->next; statement[i].type=0;
else statement[i].content="";
{ }
print_line_token(temp->line);
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing elements after \"var\"."<<std::endl;
return; return;
} }
//end identifier void parse::brace_check()
std::string name_of_var;
if(temp->type=="Identifier")
name_of_var=temp->content;
else
{ {
print_line_token(temp->line); int curve=0;
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing identifier after \"var\"."<<std::endl; int bracket=0;
int brace=0;
for(int i=1;i<=len;++i)
{
if(content_array[i].content=="(")
++curve;
else if(content_array[i].content==")")
--curve;
else if(content_array[i].content=="[")
++bracket;
else if(content_array[i].content=="]")
--bracket;
else if(content_array[i].content=="{")
++brace;
else if(content_array[i].content=="}")
--brace;
}
if(curve>0)
std::cout<<"[Error] Missing \")\" ."<<std::endl;
else if(curve<0)
std::cout<<"[Error] Missing \"(\" ."<<std::endl;
if(bracket>0)
std::cout<<"[Error] Missing \"]\" ."<<std::endl;
else if(bracket<0)
std::cout<<"[Error] Missing \"[\" ."<<std::endl;
if(brace>0)
std::cout<<"[Error] Missing \"}\" ."<<std::endl;
else if(brace<0)
std::cout<<"[Error] Missing \"{\" ."<<std::endl;
return; return;
} }
if(temp->next) bool parse::def_function()
temp=temp->next;
else
{ {
print_line_token(temp->line); if(statement[0].content=="var" && statement[1].type==IDENTIFIER && statement[2].content=="=" && statement[3].content=="func")
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing operator \"=\" after identifier."<<std::endl; return true;
return; return false;
} }
if(temp->content!="=") bool parse::def_array()
{ {
print_line_token(temp->line); if(statement[0].content=="var" && statement[1].type==IDENTIFIER && statement[2].content=="=" && statement[3].content=="[")
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing operator \"=\" after identifier."<<std::endl; return true;
return; return false;
} }
if(temp->next) bool parse::def_hash()
temp=temp->next;
else
{ {
print_line_token(temp->line); if(statement[0].content=="var" && statement[1].type==IDENTIFIER && statement[2].content=="=" && statement[3].content=="{")
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing value after operator \"=\" ."<<std::endl; return true;
return; return false;
} }
if(temp->type=="Number") bool parse::def_scalar()
{ {
temp_var.isGlobal=true; if(statement[0].content=="var" && statement[1].type==IDENTIFIER && statement[2].content=="=" && (statement[3].type==NUMBER || statement[3].type==STRING))
if(isFloat(temp->content))
{ {
temp_var.Type="double"; if(statement[3].type==NUMBER)
{
var temp_var;
if(is_float(statement[3].content))
{
temp_var.type=VAR_DOUBLE;
temp_var.data=new double; temp_var.data=new double;
*((double *)temp_var.data)=double_Str2Num(temp->content); *((double *)temp_var.data)=double_str2num(statement[3].content);
nasal_var_stack.append_var(name_of_var,temp_var);
delete (double *)temp_var.data;
temp_var.data=NULL;
} }
else else
{ {
temp_var.Type="long long int"; temp_var.type=VAR_LLINT;
temp_var.data=new long long int; temp_var.data=new long long int;
*((long long int *)temp_var.data)=int_Str2Num(temp->content); *((long long int *)temp_var.data)=int_str2num(statement[3].content);
nasal_var_stack.append_var(name_of_var,temp_var);
delete (long long int *)temp_var.data;
temp_var.data=NULL;
} }
nasal_var_stack.append_var(statement[1].content,temp_var);
} }
else if(temp->type=="String") else if(statement[3].type==STRING)
{ {
temp_var.isGlobal=true; var temp_var;
temp_var.Type="string"; temp_var.type=VAR_STRING;
temp_var.data=new std::string; temp_var.data=new std::string;
std::string temp_string=""; std::string temp_str="";
for(int i=1;i<(int)temp->content.length()-1;++i) for(int i=1;i<(int)statement[3].content.length()-1;++i)
temp_string+=temp->content[i]; temp_str+=statement[3].content[i];
*((std::string *)temp_var.data)=temp_string; *((std::string *)temp_var.data)=temp_str;
nasal_var_stack.append_var(name_of_var,temp_var); nasal_var_stack.append_var(statement[1].content,temp_var);
delete (std::string *)temp_var.data;
temp_var.data=NULL;
} }
else if(temp->type=="Operator" && temp->content=="{") return true;
}
return false;
}
void parse::definition()
{ {
bool make_pair=false; int brace=0;
int cnt=1; for(int i=0;i<2048;++i)
while(temp->next)
{ {
temp=temp->next; if(!statement[i].line)
if(temp->type=="Operator" && temp->content=="}")
{ {
--cnt; std::cout<<"[Error] line "<<statement[0].line<<": Expect a \";\" after "<<statement[0].content<<"."<<std::endl;
if(!cnt)
{
make_pair=true;
break; break;
} }
} if(statement[i].content==";" && !brace)
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; break;
if(statement[i].content=="{")
++brace;
if(statement[i].content=="}")
--brace;
if(!statement[i].type && brace)
std::cout<<"[Error] Missing \""<<(brace>0? '}':'{')<<"\" ."<<std::endl;
} }
} if(def_function() || def_array() || def_hash() || def_scalar())
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; }
else
std::cout<<"[Error] line "<<statement[0].line<<": Missing elements."<<std::endl;
return; return;
} }
bool parse::asi_function()
{
return false;
}
bool parse::asi_array()
{
return false;
}
bool parse::asi_hash()
{
return false;
}
bool parse::asi_scalar()
{
if(statement[0].type==IDENTIFIER && statement[1].content=="=" && statement[2].type==IDENTIFIER && statement[3].content==";")
{
var temp_var=nasal_var_stack.search_var(statement[2].content);
nasal_var_stack.edit_var(statement[0].content,temp_var);
return true;
}
else if(statement[0].type==IDENTIFIER && statement[1].content=="=" && statement[2].type==NUMBER && statement[3].content==";")
{
var temp_var;
if(is_float(statement[2].content))
{
temp_var.type=VAR_DOUBLE;
temp_var.data=new double;
*((double *)temp_var.data)=double_str2num(statement[2].content);
} }
else else
{ {
print_line_token(temp->line); temp_var.type=VAR_LLINT;
std::cout<<"line "<<temp->line<<": "<<"[Error] Missing value after operator \"=\" ."<<std::endl; temp_var.data=new long long int;
return; *((long long int *)temp_var.data)=int_str2num(statement[2].content);
} }
nasal_var_stack.edit_var(statement[0].content,temp_var);
if(!(temp->next && temp->next->content==";")) return true;
}
else if(statement[0].type==IDENTIFIER && statement[1].content=="=" && statement[2].type==STRING && statement[3].content==";")
{ {
print_line_token(temp->line); var temp_var;
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl; temp_var.type=VAR_STRING;
nasal_var_stack.pop_var(); temp_var.data=new std::string;
return; std::string temp_str="";
for(int i=1;i<(int)statement[2].content.length()-1;++i)
temp_str+=statement[2].content[i];
*((std::string *)temp_var.data)=temp_str;
nasal_var_stack.edit_var(statement[1].content,temp_var);
return true;
}
return false;
}
void parse::assignment()
{
int brace=0;
for(int i=0;i<2048;++i)
{
if(statement[i].line==0)
{
std::cout<<"[Error] line "<<statement[0].line<<": Expect a \";\" after "<<statement[0].content<<"."<<std::endl;
break;
}
if(statement[i].content==";" && !brace)
break;
if(statement[i].content=="{")
++brace;
if(statement[i].content=="}")
--brace;
if(!statement[i].type && brace)
std::cout<<"[Error] Missing \""<<(brace>0? '}':'{')<<"\" ."<<std::endl;
}
if(asi_function() || asi_array() || asi_hash() || asi_scalar())
{
;
} }
else else
temp=temp->next; std::cout<<"[Error] line "<<statement[0].line<<": Missing elements."<<std::endl;
//end var return;
} }
else if(temp->content=="print") void parse::total_run(token_list& p)
{ {
if(temp->next && temp->next->content=="(") len=0;
{ token_unit *temp=p.get_head();
temp=temp->next;
while(temp->next) while(temp->next)
{ {
temp=temp->next; temp=temp->next;
if(temp->type=="String") ++len;//begin from 1
{ content_array[len].type=temp->type;
std::string temp_string=""; content_array[len].line=temp->line;
for(int i=1;i<(int)temp->content.length()-1;++i) content_array[len].content=temp->content;
temp_string+=temp->content[i];
PrintString(temp_string);
} }
else if(temp->type=="Identifier") brace_check();
PrintVar(nasal_var_stack.SearchVar(temp->content)); for(int i=1;i<=len;++i)
else if(temp->type=="Operator" && temp->content==")")
{ {
if(!temp->next) if(content_array[i].type==RESERVEWORD && content_array[i].content=="var")
{ {
print_line_token(temp->line); int in_brace=0;
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl; statement_set_empty();
return; for(int j=i;j<=len;++j)
} {
statement[j-i]=content_array[j];
if(content_array[j].type==OPERATOR && content_array[j].content==";" && in_brace==0)
{
i=j;
break; break;
} }
else if(temp->type=="Operator" && temp->content==";") if(content_array[j].type==OPERATOR && content_array[j].content=="{")
++in_brace;
if(content_array[j].type==OPERATOR && content_array[j].content=="}")
--in_brace;
}
definition();
}
else if(content_array[i].type==IDENTIFIER)
{ {
print_line_token(temp->line); int in_brace=0;
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \")\" at the end of print."<<std::endl; statement_set_empty();
return; for(int j=i;j<=len;++j)
}
}
}
else
{ {
print_line_token(temp->line); statement[j-i]=content_array[j];
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \"(\" after function print."<<std::endl; if(content_array[j].type==OPERATOR && content_array[j].content==";" && in_brace==0)
return;
}
if(!(temp->next && temp->next->content==";"))
{ {
print_line_token(temp->line); i=j;
std::cout<<"line "<<temp->line<<": "<<"[Error] Expect a \";\" at the end of the statement."<<std::endl; break;
}
if(content_array[j].type==OPERATOR && content_array[j].content=="{")
++in_brace;
if(content_array[j].type==OPERATOR && content_array[j].content=="}")
--in_brace;
}
assignment();
}
}
nasal_var_stack.print_var();
nasal_func_stack.print_function();
return; 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

View File

@ -50,13 +50,48 @@ class token_list
{ {
private: private:
token_unit *head; token_unit *head;
int list_range;
public: public:
token_list(); token_list();
~token_list(); ~token_list();
token_unit* get_head();
int get_list_range();
void print_line_token(const int); void print_line_token(const int);
void delete_all(); void delete_all();
void append(const int,const int,std::string&); void append(const int,const int,std::string&);
void print(); void print();
}; };
struct parse_unit
{
int type;
std::string content;
int line;
};
class parse
{
private:
parse_unit *content_array;
parse_unit *statement;
int len;
public:
parse();
~parse();
void content_array_set_empty();
void statement_set_empty();
void brace_check();
bool def_function();
bool def_array();
bool def_hash();
bool def_scalar();
bool asi_function();
bool asi_array();
bool asi_hash();
bool asi_scalar();
void definition();
void assignment();
void total_run(token_list&);
};
#endif #endif

View File

@ -10,7 +10,7 @@ int main()
while(1) while(1)
{ {
std::cout<<">> "; std::cout<<">> ";
std::cin>>command; std::getline(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;
@ -19,6 +19,7 @@ int main()
std::cout<<">> 4. command lexer to see tokens in stack."<<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<<">> 5. command del to delete all elements in stack."<<std::endl;
std::cout<<">> 6. command run to run the programme in stack."<<std::endl; std::cout<<">> 6. command run to run the programme in stack."<<std::endl;
std::cout<<">> 7. command rs to check the source program."<<std::endl;
} }
else if(command=="cls") else if(command=="cls")
{ {
@ -26,15 +27,23 @@ int main()
//linux system("clear"); //linux system("clear");
//macOS system("clear"); //macOS system("clear");
} }
else if(command=="rs")
nasal::PrintSourceFile();
else if(command=="exit") else if(command=="exit")
break; break;
else if(command=="lexer") else if(command=="lexer")
nasal::nasal_lexer.print(); nasal::nasal_lexer.print();
else if(command=="del") else if(command=="del")
{
nasal::nasal_lexer.delete_all(); nasal::nasal_lexer.delete_all();
nasal::nasal_var_stack.delete_all();
nasal::nasal_func_stack.delete_all();
}
else if(command=="run") else if(command=="run")
{ {
; nasal::nasal_var_stack.delete_all();
nasal::nasal_func_stack.delete_all();
nasal::nasal_parse.total_run(nasal::nasal_lexer);
} }
else else
nasal::RunProcess(command); nasal::RunProcess(command);

View File

@ -30,7 +30,7 @@ std::string OperatorOrDelimiter[40]=
}; };
std::string IdentifierTable[1000]={""}; std::string IdentifierTable[1000]={""};
char ResourcePrograme[16777216]; char ResourceProgram[16777216];
int isReserveWord(std::string &p) int isReserveWord(std::string &p)
{ {
@ -61,39 +61,46 @@ bool isNumber(char t)
void InputFile(std::string &FileName) void InputFile(std::string &FileName)
{ {
std::ifstream fin(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; int i=0;
bool FindNote=false; bool FindNote=false;
while(!fin.eof()) while(!fin.eof())
{ {
ResourcePrograme[i]=fin.get(); ResourceProgram[i]=fin.get();
if(ResourcePrograme[i]=='\n') if(ResourceProgram[i]=='\n')
FindNote=false; FindNote=false;
if(ResourcePrograme[i]!='#' && !FindNote) if(ResourceProgram[i]!='#' && !FindNote)
++i; ++i;
else if(ResourcePrograme[i]=='#') else if(ResourceProgram[i]=='#')
{ {
FindNote=true; FindNote=true;
} }
if(fin.eof()) if(fin.eof())
break; break;
} }
ResourcePrograme[i]='@'; ResourceProgram[i]=0;
// print source file
// ++i;
// for(int j=0;j<i;++j)
// std::cout<<ResourcePrograme[j];
// std::cout<<std::endl;
fin.close(); fin.close();
return; return;
} }
void PrintSourceFile()
{
int line=1;
std::cout<<line<<" ";
for(int i=0;i<16777216;++i)
{
if(!ResourceProgram[i])
break;
std::cout<<ResourceProgram[i];
if(ResourceProgram[i]=='\n')
{
++line;
std::cout<<line<<" ";
}
}
std::cout<<std::endl;
}
void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line) void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line)
{ {
char temp; char temp;
@ -200,7 +207,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line)
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
} }
if(temp=='@' || temp=='\n') if(temp==0 || temp=='\n')
break; break;
} }
//add the last char \" //add the last char \"
@ -212,7 +219,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr,int &line)
else else
token+=" __missing_end_of_string"; token+=" __missing_end_of_string";
} }
else if(temp=='@') else if(temp==0)
{ {
Syn=SCANEND; Syn=SCANEND;
return; return;
@ -241,12 +248,21 @@ void RunProcess(std::string &FileName)
int Ptr=0;//pointer to one char in ResourcePrograme int Ptr=0;//pointer to one char in ResourcePrograme
int line=1; int line=1;
std::string token; std::string token;
std::ifstream fin(FileName);
if(fin.fail())
{
std::cout<<"[Error] Failed to load file: "<<FileName<<std::endl;
fin.close();
return;
}
else
fin.close();
ResourceProgram[0]=0;
nasal_lexer.delete_all(); nasal_lexer.delete_all();
InputFile(FileName); InputFile(FileName);
while(Syn!=SCANEND && Syn!=ERRORFOUND) while(Syn!=SCANEND && Syn!=ERRORFOUND)
{ {
Scanner(Syn,ResourcePrograme,token,Ptr,line); Scanner(Syn,ResourceProgram,token,Ptr,line);
if(Syn>0)//all Syn type is larger than zero if(Syn>0)//all Syn type is larger than zero
nasal_lexer.append(line,Syn,token); nasal_lexer.append(line,Syn,token);
} }

7
version0.2/nasal_parse.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __NASAL_PARSE_H__
#define __NASAL_PARSE_H__
#include "nasal_lexer.h"
parse nasal_parse;
#endif

View File

@ -6,9 +6,6 @@
#include "nasal_hash.cpp" #include "nasal_hash.cpp"
#include "nasal_list.cpp" #include "nasal_list.cpp"
namespace nasal
{
void PrintString(std::string &PrintInfo) void PrintString(std::string &PrintInfo)
{ {
for(int i=0;i<(int)PrintInfo.length();++i) for(int i=0;i<(int)PrintInfo.length();++i)
@ -60,26 +57,20 @@ void PrintString(std::string &PrintInfo)
} }
void PrintVar(var Var) void PrintVar(var Var)
{ {
if(Var.Type=="int") if(Var.type==VAR_LLINT)
std::cout<<*((int *)Var.data);
else if(Var.Type=="long long int")
std::cout<<*((long long int *)Var.data); std::cout<<*((long long int *)Var.data);
else if(Var.Type=="float") else if(Var.type==VAR_DOUBLE)
std::cout<<*((float *)Var.data);
else if(Var.Type=="double")
std::cout<<*((double *)Var.data); std::cout<<*((double *)Var.data);
else if(Var.Type=="char") else if(Var.type==VAR_CHAR)
std::cout<<*((char *)Var.data); std::cout<<*((char *)Var.data);
else if(Var.Type=="string") else if(Var.type==VAR_STRING)
PrintString(*((std::string *)Var.data)); PrintString(*((std::string *)Var.data));
else if(Var.Type=="array") // else if(Var.type==VAR_LIST)
((NasalList *)Var.data)->PrintList(); // ((NasalList *)Var.data)->PrintList();
else if(Var.Type=="hash") // else if(Var.type==VAR_HASH)
((NasalHash *)Var.data)->PrintHash(); // ((NasalHash *)Var.data)->PrintHash();
else else
std::cout<<"null"; std::cout<<"[Error] Null type or function";
}
} }
#endif #endif

View File

@ -3,9 +3,6 @@
#include "nasal_print.h" #include "nasal_print.h"
namespace nasal
{
struct var_stack_unit struct var_stack_unit
{ {
std::string var_name; std::string var_name;
@ -58,8 +55,33 @@ class var_stack
while(temp->next) while(temp->next)
{ {
temp=temp->next; temp=temp->next;
std::cout<<"["<<temp->var_detail.Type<<"]: "<<temp->var_name<<" : "; std::cout<<"[";
if(temp->var_detail.Type!="string") switch(temp->var_detail.type)
{
case VAR_NONE:
std::cout<<"null";
break;
case VAR_LLINT:
std::cout<<"int";
break;
case VAR_DOUBLE:
std::cout<<"float";
break;
case VAR_CHAR:
std::cout<<"char";
break;
case VAR_STRING:
std::cout<<"string";
break;
case VAR_LIST:
std::cout<<"array";
break;
case VAR_HASH:
std::cout<<"hash";
break;
}
std::cout<<"]: "<<temp->var_name<<" : ";
if(temp->var_detail.type!=VAR_STRING)
PrintVar(temp->var_detail); PrintVar(temp->var_detail);
else else
std::cout<<*((std::string *)temp->var_detail.data); std::cout<<*((std::string *)temp->var_detail.data);
@ -67,21 +89,37 @@ class var_stack
} }
return; return;
} }
var SearchVar(std::string varia_name) var search_var(std::string varia_name)
{ {
var temp_var; var temp_var;
temp_var.data=NULL; temp_var.data=NULL;
temp_var.Type="null"; temp_var.type=VAR_NONE;
temp_var.isGlobal=false;
var_stack_unit *temp=head; var_stack_unit *temp=head;
while(temp->next) while(temp->next)
{ {
temp=temp->next; temp=temp->next;
if(temp->var_name==varia_name) if(temp->var_name==varia_name)
{
temp_var=temp->var_detail; temp_var=temp->var_detail;
break;
}
} }
return temp_var; return temp_var;
} }
void edit_var(std::string varia_name,var &temp_var)
{
var_stack_unit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->var_name==varia_name)
{
temp->var_detail=temp_var;
break;
}
}
return;
}
void pop_var() void pop_var()
{ {
var_stack_unit *temp=head; var_stack_unit *temp=head;
@ -113,7 +151,6 @@ class var_stack
return; return;
} }
}; };
var_stack nasal_var_stack;
}
#endif #endif