New version(?????)
This commit is contained in:
parent
2f29050abf
commit
a4e0dcced2
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef __NASAL_H__
|
||||
#define __NASAL_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <stack>
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
#include "nasal_functional.h"
|
||||
|
||||
#include "nasal_var.h"
|
||||
#include "nasal_list.h"
|
||||
#include "nasal_hash.h"
|
||||
#include "nasal_token.h"
|
||||
#include "nasal_func.h"
|
||||
|
||||
#include "nasal_var.cpp"
|
||||
#include "nasal_list.cpp"
|
||||
#include "nasal_hash.cpp"
|
||||
#include "nasal_print.h"
|
||||
#include "nasal_var_stack.h"
|
||||
#include "nasal_func_stack.h"
|
||||
#include "nasal_token.cpp"
|
||||
#include "nasal_func.cpp"
|
||||
|
||||
#include "nasal_lexer.h"
|
||||
#include "nasal_parse.h"
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,174 @@
|
|||
#ifndef __NASAL_FUNC_CPP__
|
||||
#define __NASAL_FUNC_CPP__
|
||||
|
||||
#include "nasal_func.h"
|
||||
|
||||
func::func()
|
||||
{
|
||||
statement_head=new token_unit;
|
||||
statement_head->line=0;
|
||||
statement_head->type=FUNC_BEGIN;
|
||||
statement_head->content="__func_begin";
|
||||
statement_head->next=NULL;
|
||||
|
||||
parameter_head=new parameter;
|
||||
parameter_head->param_var.type=VAR_NONE;
|
||||
parameter_head->param_var.data=NULL;
|
||||
parameter_head->var_name="__null_var";
|
||||
parameter_head->next=NULL;
|
||||
}
|
||||
func::func(const func& temp)
|
||||
{
|
||||
token_unit *statement_temp=statement_head;
|
||||
token_unit *temp_state=temp.statement_head;
|
||||
parameter *parameter_temp=parameter_head;
|
||||
parameter *temp_param=temp.parameter_head;
|
||||
while(temp_state->next)
|
||||
{
|
||||
temp_state=temp_state->next;
|
||||
statement_temp->next=new token_unit;
|
||||
statement_temp=statement_temp->next;
|
||||
statement_temp->line=temp_state->line;
|
||||
statement_temp->content=temp_state->content;
|
||||
statement_temp->type=temp_state->type;
|
||||
statement_temp->next=NULL;
|
||||
}
|
||||
while(temp_param->next)
|
||||
{
|
||||
temp_param=temp_param->next;
|
||||
parameter_temp->next=new parameter;
|
||||
parameter_temp=parameter_temp->next;
|
||||
parameter_temp->param_var=temp_param->param_var;
|
||||
parameter_temp->var_name=temp_param->var_name;
|
||||
parameter_temp->next=NULL;
|
||||
}
|
||||
}
|
||||
func::~func()
|
||||
{
|
||||
token_unit *statement_temp=statement_head;
|
||||
token_unit *statement_last_unit=NULL;
|
||||
parameter *parameter_temp=parameter_head;
|
||||
parameter *parameter_last_unit=NULL;
|
||||
|
||||
while(statement_temp->next)
|
||||
{
|
||||
statement_last_unit=statement_temp;
|
||||
statement_temp=statement_temp->next;
|
||||
delete statement_last_unit;
|
||||
}
|
||||
delete statement_temp;
|
||||
while(parameter_temp->next)
|
||||
{
|
||||
parameter_last_unit=parameter_temp;
|
||||
parameter_temp=parameter_temp->next;
|
||||
delete parameter_last_unit;
|
||||
}
|
||||
delete parameter_temp;
|
||||
}
|
||||
func& func::operator=(const func& temp)
|
||||
{
|
||||
token_unit *statement_temp=statement_head->next;
|
||||
token_unit *temp_state=temp.statement_head;
|
||||
parameter *parameter_temp=parameter_head->next;
|
||||
parameter *temp_param=temp.parameter_head;
|
||||
|
||||
token_unit *statement_last=NULL;
|
||||
parameter *parameter_last=NULL;
|
||||
if(statement_temp)
|
||||
{
|
||||
while(statement_temp->next)
|
||||
{
|
||||
statement_last=statement_temp;
|
||||
statement_temp=statement_temp->next;
|
||||
delete statement_last;
|
||||
}
|
||||
delete statement_temp;
|
||||
}
|
||||
if(parameter_temp)
|
||||
{
|
||||
while(parameter_temp->next)
|
||||
{
|
||||
parameter_last=parameter_temp;
|
||||
parameter_temp=parameter_temp->next;
|
||||
delete parameter_last;
|
||||
}
|
||||
delete parameter_temp;
|
||||
}
|
||||
statement_head->next=NULL;
|
||||
parameter_head->next=NULL;
|
||||
statement_temp=statement_head;
|
||||
parameter_temp=parameter_head;
|
||||
while(temp_state->next)
|
||||
{
|
||||
temp_state=temp_state->next;
|
||||
statement_temp->next=new token_unit;
|
||||
statement_temp=statement_temp->next;
|
||||
statement_temp->line=temp_state->line;
|
||||
statement_temp->content=temp_state->content;
|
||||
statement_temp->type=temp_state->type;
|
||||
statement_temp->next=NULL;
|
||||
}
|
||||
while(temp_param->next)
|
||||
{
|
||||
temp_param=temp_param->next;
|
||||
parameter_temp->next=new parameter;
|
||||
parameter_temp=parameter_temp->next;
|
||||
parameter_temp->param_var=temp_param->param_var;
|
||||
parameter_temp->var_name=temp_param->var_name;
|
||||
parameter_temp->next=NULL;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void func::append_var(std::string& varia_name,var& p)
|
||||
{
|
||||
parameter *parameter_temp=parameter_head;
|
||||
while(parameter_temp->next)
|
||||
{
|
||||
parameter_temp=parameter_temp->next;
|
||||
if(parameter_temp->var_name==varia_name)
|
||||
{
|
||||
std::cout<<"[Error] Redefinition of var \""<<varia_name<<"\" ."<<std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
parameter_temp->next=new parameter;
|
||||
parameter_temp=parameter_temp->next;
|
||||
parameter_temp->param_var=p;
|
||||
parameter_temp->var_name=varia_name;
|
||||
parameter_temp->next=NULL;
|
||||
return;
|
||||
}
|
||||
void func::append_token(const int _line,const int _type,std::string &_content)
|
||||
{
|
||||
token_unit *statement_temp=statement_head;
|
||||
while(statement_temp->next)
|
||||
statement_temp=statement_temp->next;
|
||||
statement_temp->next=new token_unit;
|
||||
statement_temp=statement_temp->next;
|
||||
statement_temp->line=_line;
|
||||
statement_temp->type=_type;
|
||||
statement_temp->content=_content;
|
||||
statement_temp->next=NULL;
|
||||
return;
|
||||
}
|
||||
void func::print_info()
|
||||
{
|
||||
parameter *para_temp=parameter_head;
|
||||
token_unit *token_temp=statement_head;
|
||||
std::cout<<"\n\t[parameter] :";
|
||||
while(para_temp->next)
|
||||
{
|
||||
para_temp=para_temp->next;
|
||||
std::cout<<para_temp->var_name<<" ";
|
||||
}
|
||||
std::cout<<"\n\t[statement] :";
|
||||
while(token_temp->next)
|
||||
{
|
||||
token_temp=token_temp->next;
|
||||
std::cout<<token_temp->content<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef __NASAL_FUNC_H__
|
||||
#define __NASAL_FUNC_H__
|
||||
|
||||
#include "nasal_token.h"
|
||||
|
||||
//for function
|
||||
#define FUNC_BEGIN 0
|
||||
#define FUNC_OPERATOR 1
|
||||
#define FUNC_IDENTIFIER 2
|
||||
#define FUNC_NUMBER 3
|
||||
#define FUNC_RESERVEWORD 4
|
||||
#define FUNC_STRING 5
|
||||
|
||||
struct parameter
|
||||
{
|
||||
std::string var_name;
|
||||
var param_var;
|
||||
parameter *next;
|
||||
};
|
||||
|
||||
class func
|
||||
{
|
||||
private:
|
||||
token_unit *statement_head;
|
||||
parameter *parameter_head;
|
||||
public:
|
||||
func();
|
||||
func(const func&);
|
||||
~func();
|
||||
func& operator=(const func&);
|
||||
void append_var(std::string&,var&);
|
||||
void append_token(const int,const int,std::string &);
|
||||
void print_info();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,116 @@
|
|||
#ifndef __NASAL_FUNC_STACK_H__
|
||||
#define __NASAL_FUNC_STACK_H__
|
||||
|
||||
#include "nasal_token.h"
|
||||
#include "nasal_func.h"
|
||||
|
||||
|
||||
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_name=function_name;
|
||||
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;
|
||||
temp->func_statement.print_info();
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
func_stack nasal_func_stack;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,59 @@
|
|||
#ifndef __NASAL_FUNCTIONAL_H__
|
||||
#define __NASAL_FUNCTIONAL_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;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,113 @@
|
|||
#ifndef __NASAL_HASH_CPP__
|
||||
#define __NASAL_HASH_CPP__
|
||||
|
||||
|
||||
#include "nasal_hash.h"
|
||||
|
||||
nasal_hash::nasal_hash()
|
||||
{
|
||||
head=new nasal_hash_unit;
|
||||
head->name="";
|
||||
head->hash_var.type=VAR_NONE;
|
||||
head->hash_var.data=NULL;
|
||||
head->next=NULL;
|
||||
}
|
||||
nasal_hash::~nasal_hash()
|
||||
{
|
||||
nasal_hash_unit *temp=head;
|
||||
nasal_hash_unit *this_node=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
nasal_hash& nasal_hash::operator=(const nasal_hash &p)
|
||||
{
|
||||
nasal_hash_unit *temp=head;
|
||||
nasal_hash_unit *this_node=NULL;
|
||||
if(head->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
head->next=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
temp=head;
|
||||
nasal_hash_unit *temp_p=p.head;
|
||||
|
||||
while(temp_p->next)
|
||||
{
|
||||
temp_p=temp_p->next;
|
||||
temp->next=new nasal_hash_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->hash_var=temp_p->hash_var;
|
||||
temp->name=temp_p->name;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void nasal_hash::append(std::string& var_name,var& p)
|
||||
{
|
||||
nasal_hash_unit *temp=head;
|
||||
while(temp->next)
|
||||
temp=temp->next;
|
||||
temp->next=new nasal_hash_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->hash_var=p;
|
||||
temp->name=var_name;
|
||||
return;
|
||||
}
|
||||
int nasal_hash::contains(std::string& var_name)
|
||||
{
|
||||
nasal_hash_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->name==var_name)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int nasal_hash::delete_element(std::string& var_name)
|
||||
{
|
||||
nasal_hash_unit *temp=head;
|
||||
nasal_hash_unit *last_node=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
last_node=temp;
|
||||
temp=temp->next;
|
||||
if(temp->name==var_name)
|
||||
{
|
||||
last_node->next=temp->next;
|
||||
delete temp;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
nasal_list nasal_hash::keys()
|
||||
{
|
||||
var assist_var;
|
||||
assist_var.type=VAR_STRING;
|
||||
nasal_list temp_list;
|
||||
nasal_hash_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
assist_var.data=new std::string;
|
||||
*((std::string *)assist_var.data)=temp->name;
|
||||
temp_list.append(assist_var);
|
||||
}
|
||||
return temp_list;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef __NASAL_HASH_H__
|
||||
#define __NASAL_HASH_H__
|
||||
|
||||
#include "nasal_var.h"
|
||||
|
||||
struct nasal_hash_unit
|
||||
{
|
||||
std::string name;
|
||||
var hash_var;
|
||||
nasal_hash_unit *next;
|
||||
};
|
||||
|
||||
class nasal_hash
|
||||
{
|
||||
private:
|
||||
nasal_hash_unit *head;
|
||||
public:
|
||||
nasal_hash();
|
||||
~nasal_hash();
|
||||
nasal_hash& operator=(const nasal_hash&);
|
||||
void append(std::string&,var&);
|
||||
int contains(std::string&);
|
||||
int delete_element(std::string&);
|
||||
nasal_list keys();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,54 @@
|
|||
#include "nasal.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string command;
|
||||
std::cout<<">> nasal-- script by ValKmjolnir"<<std::endl;
|
||||
std::cout<<">> input \"help\" to find help."<<std::endl;
|
||||
while(1)
|
||||
{
|
||||
std::cout<<">> ";
|
||||
std::getline(std::cin,command);
|
||||
if(command=="help")
|
||||
{
|
||||
std::cout<<">> nasal-- script by ValKmjolnir"<<std::endl;
|
||||
std::cout<<">> 1. input file name to run the lexer."<<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;
|
||||
std::cout<<">> 4. command \"lexer\" to see tokens in stack."<<std::endl;
|
||||
std::cout<<">> 5. command \"parser\" to run parser."<<std::endl;
|
||||
std::cout<<">> 6. command \"del\" to delete all elements in stack."<<std::endl;
|
||||
std::cout<<">> 7. command \"run\" to run the programme in stack."<<std::endl;
|
||||
std::cout<<">> 8. command \"rs\" to check the source program."<<std::endl;
|
||||
}
|
||||
else if(command=="cls")
|
||||
{
|
||||
system("cls");
|
||||
//linux system("clear");
|
||||
//macOS system("clear");
|
||||
}
|
||||
else if(command=="rs")
|
||||
nasal::PrintSourceFile();
|
||||
else if(command=="exit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if(command=="lexer")
|
||||
nasal::nasal_lexer.print();
|
||||
else if(command=="del")
|
||||
{
|
||||
nasal::nasal_lexer.delete_all();
|
||||
nasal::nasal_parse.stack_set_empty();
|
||||
nasal::nasal_var_stack.delete_all();
|
||||
nasal::nasal_func_stack.delete_all();
|
||||
}
|
||||
else if(command=="parser" || command=="run")
|
||||
{
|
||||
nasal::nasal_parse.parse_work(nasal::nasal_lexer);
|
||||
//nasal::nasal_parse.print_stack();
|
||||
}
|
||||
else
|
||||
nasal::RunProcess(command);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,270 @@
|
|||
#ifndef __NASAL_LEXER_H__
|
||||
#define __NASAL_LEXER_H__
|
||||
|
||||
#include "nasal_functional.h"
|
||||
|
||||
#define FAIL -1 //ʧ°Ü
|
||||
#define SCANEND -2 //ɨÃèÍê³É
|
||||
#define ERRORFOUND -3 //Òì³£´íÎó
|
||||
|
||||
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 ResourceProgram[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);
|
||||
int i=0;
|
||||
bool FindNote=false;
|
||||
while(!fin.eof())
|
||||
{
|
||||
ResourceProgram[i]=fin.get();
|
||||
if(ResourceProgram[i]=='\n')
|
||||
FindNote=false;
|
||||
if(ResourceProgram[i]!='#' && !FindNote)
|
||||
++i;
|
||||
else if(ResourceProgram[i]=='#')
|
||||
{
|
||||
FindNote=true;
|
||||
}
|
||||
if(fin.eof())
|
||||
break;
|
||||
}
|
||||
ResourceProgram[i]=0;
|
||||
|
||||
fin.close();
|
||||
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)
|
||||
{
|
||||
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==0 || temp=='\n')
|
||||
break;
|
||||
}
|
||||
//add the last char \"
|
||||
if(temp=='\"')
|
||||
{
|
||||
token+=temp;
|
||||
++ptr;
|
||||
}
|
||||
else
|
||||
token+=" __missing_end_of_string";
|
||||
}
|
||||
else if(temp==0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
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_var_stack.delete_all();
|
||||
nasal_func_stack.delete_all();
|
||||
InputFile(FileName);
|
||||
while(Syn!=SCANEND && Syn!=ERRORFOUND)
|
||||
{
|
||||
Scanner(Syn,ResourceProgram,token,Ptr,line);
|
||||
if(Syn>0)//all Syn type is larger than zero
|
||||
nasal_lexer.append(line,Syn,token);
|
||||
}
|
||||
//nasal_lexer.print(); //for debug mode
|
||||
std::cout<<">> Complete scanning \""<<FileName<<"\"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,300 @@
|
|||
#ifndef __NASAL_LIST_CPP__
|
||||
#define __NASAL_LIST_CPP__
|
||||
|
||||
#include "nasal_var.h"
|
||||
#include "nasal_list.h"
|
||||
|
||||
nasal_list::nasal_list()
|
||||
{
|
||||
head=new nasal_list_unit;
|
||||
head->list_var.type=VAR_NONE;
|
||||
head->list_var.data=NULL;
|
||||
head->next=NULL;
|
||||
}
|
||||
nasal_list::~nasal_list()
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
nasal_list_unit *this_node=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
nasal_list& nasal_list::operator=(const nasal_list &p)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
nasal_list_unit *this_node=NULL;
|
||||
if(head->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
head->next=NULL;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete temp;
|
||||
}
|
||||
temp=head;
|
||||
nasal_list_unit *temp_p=p.head;
|
||||
|
||||
while(temp_p->next)
|
||||
{
|
||||
temp_p=temp_p->next;
|
||||
temp->next=new nasal_list_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->list_var=temp_p->list_var;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void nasal_list::append(var& p)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
while(temp->next)
|
||||
temp=temp->next;
|
||||
temp->next=new nasal_list_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->list_var=p;
|
||||
return;
|
||||
}
|
||||
void nasal_list::setsize(const int list_size)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
int cnt=0;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
++cnt;
|
||||
if(cnt==list_size)
|
||||
{
|
||||
nasal_list_unit *this_node=NULL;
|
||||
nasal_list_unit *t=temp->next;
|
||||
temp->next=NULL;
|
||||
if(!t)
|
||||
return;
|
||||
while(t->next)
|
||||
{
|
||||
this_node=t;
|
||||
t=t->next;
|
||||
delete this_node;
|
||||
}
|
||||
delete t;
|
||||
return;
|
||||
}
|
||||
}
|
||||
while(cnt<list_size)
|
||||
{
|
||||
temp->next=new nasal_list_unit;
|
||||
temp=temp->next;
|
||||
temp->list_var.type=VAR_NONE;
|
||||
temp->list_var.data=NULL;
|
||||
temp->next=NULL;
|
||||
++cnt;
|
||||
}
|
||||
return;
|
||||
}
|
||||
nasal_list nasal_list::subvec(const int list_begin=0,const int list_end=-1)
|
||||
{
|
||||
nasal_list temp_list;
|
||||
int cnt=-1;
|
||||
nasal_list_unit *temp=head;
|
||||
|
||||
int beg=list_begin;
|
||||
int end=list_end;
|
||||
if(list_end==-1)
|
||||
{
|
||||
int end_place=-1;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
++end_place;
|
||||
}
|
||||
temp=head;
|
||||
end=end_place;
|
||||
}
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
++cnt;
|
||||
if(beg<=cnt && cnt<=end)
|
||||
temp_list.append(temp->list_var);
|
||||
}
|
||||
return temp_list;
|
||||
}
|
||||
var nasal_list::pop()
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
nasal_list_unit *this_node;
|
||||
while(temp->next)
|
||||
{
|
||||
this_node=temp;
|
||||
temp=temp->next;
|
||||
}
|
||||
this_node->next=NULL;
|
||||
var temp_var=temp->list_var;
|
||||
delete temp;
|
||||
return temp_var;
|
||||
}
|
||||
nasal_list nasal_list::sort_list(const int sort_type,const int _cmp=1)
|
||||
{
|
||||
nasal_list temp_list;
|
||||
if(sort_type==SORT_INT)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->list_var.type!=VAR_LLINT)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be long int."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
}
|
||||
if(temp->list_var.type!=VAR_LLINT)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be long int."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
temp_list=*this;
|
||||
nasal_list_unit *first_temp_this;
|
||||
nasal_list_unit *second_temp_this;
|
||||
nasal_list_unit *node_this;
|
||||
first_temp_this=temp_list.head->next;
|
||||
while(first_temp_this->next)
|
||||
{
|
||||
node_this=first_temp_this;
|
||||
second_temp_this=first_temp_this->next;
|
||||
while(second_temp_this->next)
|
||||
{
|
||||
if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small
|
||||
node_this=second_temp_this;
|
||||
second_temp_this=second_temp_this->next;
|
||||
}
|
||||
if(_cmp>0 && *((int *)node_this->list_var.data)>*((int *)second_temp_this->list_var.data))//from small to large func(a,b) a-b
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((int *)node_this->list_var.data)<*((int *)second_temp_this->list_var.data))//from large to small func(a,b) b-a
|
||||
node_this=second_temp_this;
|
||||
if(node_this!=first_temp_this)
|
||||
{
|
||||
int t;
|
||||
t=*((int *)first_temp_this->list_var.data);
|
||||
*((int *)first_temp_this->list_var.data)=*((int *)node_this->list_var.data);
|
||||
*((int *)node_this->list_var.data)=t;
|
||||
}
|
||||
first_temp_this=first_temp_this->next;
|
||||
}
|
||||
}
|
||||
else if(sort_type==SORT_DBL)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->list_var.type!=VAR_DOUBLE)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be float."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
}
|
||||
if(temp->list_var.type!=VAR_DOUBLE)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be float."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
temp_list=*this;
|
||||
nasal_list_unit *first_temp_this;
|
||||
nasal_list_unit *second_temp_this;
|
||||
nasal_list_unit *node_this;
|
||||
first_temp_this=temp_list.head->next;
|
||||
while(first_temp_this->next)
|
||||
{
|
||||
node_this=first_temp_this;
|
||||
second_temp_this=first_temp_this->next;
|
||||
while(second_temp_this->next)
|
||||
{
|
||||
if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small
|
||||
node_this=second_temp_this;
|
||||
second_temp_this=second_temp_this->next;
|
||||
}
|
||||
if(_cmp>0 && *((double *)node_this->list_var.data)>*((double *)second_temp_this->list_var.data))//from small to large func(a,b) a-b
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((double *)node_this->list_var.data)<*((double *)second_temp_this->list_var.data))//from large to small func(a,b) b-a
|
||||
node_this=second_temp_this;
|
||||
if(node_this!=first_temp_this)
|
||||
{
|
||||
double t;
|
||||
t=*((double *)first_temp_this->list_var.data);
|
||||
*((double *)first_temp_this->list_var.data)=*((double *)node_this->list_var.data);
|
||||
*((double *)node_this->list_var.data)=t;
|
||||
}
|
||||
first_temp_this=first_temp_this->next;
|
||||
}
|
||||
}
|
||||
else if(sort_type==SORT_STR)
|
||||
{
|
||||
nasal_list_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->list_var.type!=VAR_STRING)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be string."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
}
|
||||
if(temp->list_var.type!=VAR_STRING)
|
||||
{
|
||||
std::cout<<"[Error] Incorrect type inside: "<<temp->list_var.type<<".But type must be string."<<std::endl;
|
||||
temp_list.setsize(1);
|
||||
return temp_list;
|
||||
}
|
||||
temp_list=*this;
|
||||
nasal_list_unit *first_temp_this;
|
||||
nasal_list_unit *second_temp_this;
|
||||
nasal_list_unit *node_this;
|
||||
first_temp_this=temp_list.head->next;
|
||||
while(first_temp_this->next)
|
||||
{
|
||||
node_this=first_temp_this;
|
||||
second_temp_this=first_temp_this->next;
|
||||
while(second_temp_this->next)
|
||||
{
|
||||
if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small
|
||||
node_this=second_temp_this;
|
||||
second_temp_this=second_temp_this->next;
|
||||
}
|
||||
if(_cmp>0 && *((std::string *)node_this->list_var.data)>*((std::string *)second_temp_this->list_var.data))//from small to large func(a,b) cmp(a,b)
|
||||
node_this=second_temp_this;
|
||||
else if(_cmp<=0 && *((std::string *)node_this->list_var.data)<*((std::string *)second_temp_this->list_var.data))//from large to small func(a,b) cmp(b,a) or -cmp(a,b)
|
||||
node_this=second_temp_this;
|
||||
if(node_this!=first_temp_this)
|
||||
{
|
||||
std::string t;
|
||||
t=*((std::string *)first_temp_this->list_var.data);
|
||||
*((std::string *)first_temp_this->list_var.data)=*((std::string *)node_this->list_var.data);
|
||||
*((std::string *)node_this->list_var.data)=t;
|
||||
}
|
||||
first_temp_this=first_temp_this->next;
|
||||
}
|
||||
}
|
||||
return temp_list;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef __NASAL_LIST_H__
|
||||
#define __NASAL_LIST_H__
|
||||
|
||||
#include "nasal_var.h"
|
||||
|
||||
#define SORT_INT 1
|
||||
#define SORT_DBL 2
|
||||
#define SORT_STR 3
|
||||
|
||||
class nasal_hash;
|
||||
|
||||
struct nasal_list_unit
|
||||
{
|
||||
var list_var;
|
||||
nasal_list_unit *next;
|
||||
};
|
||||
|
||||
class nasal_list
|
||||
{
|
||||
private:
|
||||
nasal_list_unit *head;
|
||||
public:
|
||||
nasal_list();
|
||||
~nasal_list();
|
||||
nasal_list& operator=(const nasal_list&);
|
||||
void append(var&);
|
||||
void setsize(const int);
|
||||
nasal_list subvec(const int,const int);
|
||||
var pop();
|
||||
nasal_list sort_list(const int,const int);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,818 @@
|
|||
#ifndef __NASAL_PARSE_H__
|
||||
#define __NASAL_PARSE_H__
|
||||
#include "nasal_lexer.h"
|
||||
#include "nasal_token.h"
|
||||
|
||||
enum token_type
|
||||
{
|
||||
__stack_end,
|
||||
__equal,// =
|
||||
__add_operator,__sub_operator,__mul_operator,__div_operator,__link_operator,// + - * / ~
|
||||
__add_equal,__sub_equal,__mul_equal,__div_equal,__link_equal,// += -= *= /= ~=
|
||||
__left_brace,__right_brace,// {}
|
||||
__left_bracket,__right_bracket,// []
|
||||
__left_curve,__right_curve,// ()
|
||||
__semi,// ;
|
||||
__comma,// ,
|
||||
__colon,// :
|
||||
__dot,// .
|
||||
__var,// var reserve word
|
||||
__func,// func reserve word
|
||||
__print,// print reserve word
|
||||
__identifier,__identifiers,
|
||||
__scalar,__scalars,
|
||||
__hash_member,__hash_members,
|
||||
__array_search,__hash_search,
|
||||
__statement,__statements,
|
||||
__function,//function(){}
|
||||
__use_function,__class_function,
|
||||
__definition,__assignment,
|
||||
__print_function,// print()
|
||||
__loop,__loop_continue,__loop_break,// for()while() continue; break;
|
||||
__choose,// if else if else
|
||||
__return,__func_return
|
||||
};
|
||||
|
||||
struct parse_unit
|
||||
{
|
||||
int type;
|
||||
int line;
|
||||
};
|
||||
|
||||
class parse
|
||||
{
|
||||
public:
|
||||
std::stack<parse_unit> parser;
|
||||
public:
|
||||
void func_return_reduction();
|
||||
void class_function_reduction();
|
||||
void array_search_reduction();
|
||||
void hash_search_reduction();
|
||||
void function_call_reduction();
|
||||
void hash_member_reduction();
|
||||
void hash_members_reduction();
|
||||
void definition_reduction();
|
||||
void assignment_reduction();
|
||||
void identifier_reduction();
|
||||
void scalar_reduction();
|
||||
void print_reduction();
|
||||
void statement_reduction();
|
||||
void statements_reduction();
|
||||
void function_reduction();
|
||||
void parse_work(token_list&);
|
||||
void print_stack();
|
||||
void stack_set_empty();
|
||||
};
|
||||
|
||||
void parse::func_return_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__return && (tbl[1]==__scalar || tbl[1]==__identifier || tbl[1]==__use_function) && tbl[2]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__func_return;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::class_function_reduction()
|
||||
{
|
||||
int tbl[10]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=9;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<10;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[3]==__func && tbl[4]==__left_curve
|
||||
&& (tbl[5]==__identifier || tbl[5]==__identifiers) && tbl[6]==__right_curve
|
||||
&& tbl[7]==__left_brace && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__class_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<7;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[4]==__func && tbl[5]==__left_curve
|
||||
&& tbl[6]==__right_curve && tbl[7]==__left_brace
|
||||
&& (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__class_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[4]==__func && tbl[5]==__left_curve
|
||||
&& (tbl[6]==__identifier || tbl[6]==__identifiers) && tbl[7]==__right_curve
|
||||
&& tbl[8]==__left_brace && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__class_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[5]==__func
|
||||
&& tbl[6]==__left_curve && tbl[7]==__right_curve
|
||||
&& tbl[8]==__left_brace && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__class_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<5;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::array_search_reduction()
|
||||
{
|
||||
int tbl[4]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=3;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<4;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier && tbl[1]==__left_bracket
|
||||
&& (tbl[2]==__identifier || tbl[2]==__scalar || tbl[2]==__use_function)
|
||||
&& tbl[3]==__right_bracket)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__identifier;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<4;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::hash_search_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier && tbl[1]==__dot && (tbl[2]==__identifier || tbl[2]==__function || tbl[2]==__use_function))
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__identifier;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::function_call_reduction()
|
||||
{
|
||||
int tbl[5]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=4;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<5;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier && tbl[1]==__left_curve
|
||||
&& (tbl[2]==__identifier || tbl[2]==__identifiers || tbl[2]==__scalar || tbl[2]==__scalars)
|
||||
&& tbl[3]==__right_curve)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__use_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<5;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__identifier && tbl[2]==__left_curve && tbl[3]==__right_curve)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__use_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<4;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::hash_member_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier && tbl[1]==__colon && (tbl[2]==__identifier || tbl[2]==__scalar || tbl[2]==__class_function || tbl[2]==__use_function))
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__hash_member;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::hash_members_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if((tbl[0]==__hash_member || tbl[0]==__hash_members) && tbl[1]==__comma && tbl[2]==__hash_member)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__hash_members;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::definition_reduction()
|
||||
{
|
||||
int tbl[7]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=6;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<7;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[2]==__var && tbl[3]==__identifier && tbl[4]==__equal && (tbl[5]==__scalar || tbl[5]==__identifier || tbl[5]==__use_function) && tbl[6]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<5;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__left_bracket
|
||||
&& (tbl[4]==__identifier || tbl[4]==__identifiers || tbl[4]==__scalar || tbl[4]==__scalars || tbl[4]==__use_function || tbl[4]==__array_search || tbl[4]==__hash_search)
|
||||
&& tbl[5]==__right_bracket && tbl[6]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<7;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__left_bracket
|
||||
&& tbl[5]==__right_bracket && tbl[6]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__left_brace
|
||||
&& (tbl[4]==__hash_member || tbl[4]==__hash_members)
|
||||
&& tbl[5]==__right_brace && tbl[6]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<7;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__left_brace
|
||||
&& tbl[5]==__right_brace && tbl[6]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<6;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[3]==__var && tbl[4]==__identifier && tbl[5]==__equal && tbl[6]==__use_function)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<4;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::assignment_reduction()
|
||||
{
|
||||
int tbl[4]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=3;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<4;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__identifier
|
||||
&& (tbl[1]==__equal || tbl[1]==__add_equal || tbl[1]==__sub_equal || tbl[1]==__mul_equal || tbl[1]==__div_equal || tbl[1] ==__link_equal)
|
||||
&& (tbl[2]==__scalar || tbl[2]==__identifier || tbl[2]==__use_function) && tbl[3]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__assignment;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<4;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__identifier
|
||||
&& (tbl[2]==__equal || tbl[2]==__add_equal || tbl[2]==__sub_equal || tbl[2]==__mul_equal || tbl[2]==__div_equal || tbl[2] ==__link_equal)
|
||||
&& tbl[3]==__use_function)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__assignment;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::identifier_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if((tbl[0]==__identifier || tbl[0]==__identifiers) && tbl[1]==__comma && tbl[2]==__identifier)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__identifiers;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::scalar_reduction()
|
||||
{
|
||||
int tbl[3]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=2;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<3;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(((tbl[0]==__scalar || tbl[0]==__scalars || tbl[0]==__identifier || tbl[0]==__identifiers || tbl[0]==__use_function) && tbl[1]==__comma && (tbl[2]==__scalar || tbl[2]==__use_function))
|
||||
|| ((tbl[0]==__scalar || tbl[0]==__scalars || tbl[0]==__use_function) && tbl[1]==__comma && tbl[2]==__identifier))
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__scalars;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<3;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::print_reduction()
|
||||
{
|
||||
int tbl[5]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=4;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<5;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__print && tbl[1]==__left_curve
|
||||
&& (tbl[2]==__scalar || tbl[2]==__scalars || tbl[2]==__identifier || tbl[2]==__identifiers || tbl[2]==__use_function)
|
||||
&& tbl[3]==__right_curve && tbl[4]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__print_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<5;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__print && tbl[2]==__left_curve && tbl[3]==__right_curve && tbl[4]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__print_function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<4;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::statement_reduction()
|
||||
{
|
||||
int top_type=parser.top().type;
|
||||
if(top_type==__definition || top_type==__assignment || top_type==__print_function || top_type==__use_function
|
||||
|| top_type==__loop || top_type==__loop_continue || top_type==__loop_break || top_type==__choose || top_type==__func_return)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__statement;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::statements_reduction()
|
||||
{
|
||||
int tbl[2]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=1;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<2;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if((tbl[0]==__statement || tbl[0]==__statements) && tbl[1]==__statement)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__statements;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<2;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::function_reduction()
|
||||
{
|
||||
int tbl[10]={0};
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=9;i>=0;--i)
|
||||
{
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
for(int i=0;i<10;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__func && tbl[4]==__left_curve
|
||||
&& (tbl[5]==__identifier || tbl[5]==__identifiers) && tbl[6]==__right_curve
|
||||
&& tbl[7]==__left_brace && (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<10;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve
|
||||
&& tbl[6]==__right_curve && tbl[7]==__left_brace
|
||||
&& (tbl[8]==__statement || tbl[8]==__statements) && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<9;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[1]==__var && tbl[2]==__identifier && tbl[3]==__equal && tbl[4]==__func && tbl[5]==__left_curve
|
||||
&& (tbl[6]==__identifier || tbl[6]==__identifiers) && tbl[7]==__right_curve
|
||||
&& tbl[8]==__left_brace && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<9;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else if(tbl[2]==__var && tbl[3]==__identifier && tbl[4]==__equal && tbl[5]==__func
|
||||
&& tbl[6]==__left_curve && tbl[7]==__right_curve
|
||||
&& tbl[8]==__left_brace && tbl[9]==__right_brace)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__function;
|
||||
temp_parse_unit.line=parser.top().line;
|
||||
for(int i=0;i<8;++i)
|
||||
parser.pop();
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void parse::parse_work(token_list& lexer)
|
||||
{
|
||||
parse_unit temp_parse;
|
||||
token_unit *temp=lexer.get_head();
|
||||
stack_set_empty();
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
temp_parse.line=temp->line;
|
||||
if(temp->content=="var" || temp->content=="print" || temp->content=="func" || temp->content=="return" || temp->content=="nil")
|
||||
{
|
||||
if(temp->content=="var")
|
||||
temp_parse.type=__var;
|
||||
else if(temp->content=="print")
|
||||
temp_parse.type=__print;
|
||||
else if(temp->content=="func")
|
||||
temp_parse.type=__func;
|
||||
else if(temp->content=="return")
|
||||
temp_parse.type=__return;
|
||||
else if(temp->content=="nil")
|
||||
temp_parse.type=__scalar;
|
||||
}
|
||||
else if(temp->type==IDENTIFIER)
|
||||
{
|
||||
temp_parse.type=__identifier;
|
||||
}
|
||||
else if(temp->content==";" || temp->content=="," || temp->content=="=" || temp->content==":" || temp->content==".")
|
||||
{
|
||||
if(temp->content==";")
|
||||
temp_parse.type=__semi;
|
||||
else if(temp->content==",")
|
||||
temp_parse.type=__comma;
|
||||
else if(temp->content=="=")
|
||||
temp_parse.type=__equal;
|
||||
else if(temp->content==":")
|
||||
temp_parse.type=__colon;
|
||||
else if(temp->content==".")
|
||||
temp_parse.type=__dot;
|
||||
}
|
||||
else if(temp->type==NUMBER || temp->type==STRING)
|
||||
{
|
||||
temp_parse.type=__scalar;
|
||||
}
|
||||
else if(temp->content=="+" || temp->content=="-" || temp->content=="*" || temp->content=="/" || temp->content=="~")
|
||||
{
|
||||
if(temp->content=="+")
|
||||
temp_parse.type=__add_operator;
|
||||
else if(temp->content=="-")
|
||||
temp_parse.type=__sub_operator;
|
||||
else if(temp->content=="*")
|
||||
temp_parse.type=__mul_operator;
|
||||
else if(temp->content=="/")
|
||||
temp_parse.type=__div_operator;
|
||||
else if(temp->content=="~")
|
||||
temp_parse.type=__link_operator;
|
||||
}
|
||||
else if(temp->content=="+=" || temp->content=="-=" || temp->content=="*=" || temp->content=="/=" || temp->content=="~=")
|
||||
{
|
||||
if(temp->content=="+=")
|
||||
temp_parse.type=__add_equal;
|
||||
else if(temp->content=="-=")
|
||||
temp_parse.type=__sub_equal;
|
||||
else if(temp->content=="*=")
|
||||
temp_parse.type=__mul_equal;
|
||||
else if(temp->content=="/=")
|
||||
temp_parse.type=__div_equal;
|
||||
else if(temp->content=="~=")
|
||||
temp_parse.type=__link_equal;
|
||||
}
|
||||
else if(temp->content=="(" || temp->content==")" || temp->content=="[" || temp->content=="]" || temp->content=="{" || temp->content=="}")
|
||||
{
|
||||
char c=temp->content[0];
|
||||
switch(c)
|
||||
{
|
||||
case '(':
|
||||
temp_parse.type=__left_curve;
|
||||
break;
|
||||
case ')':
|
||||
temp_parse.type=__right_curve;
|
||||
break;
|
||||
case '[':
|
||||
temp_parse.type=__left_bracket;
|
||||
break;
|
||||
case ']':
|
||||
temp_parse.type=__right_bracket;
|
||||
break;
|
||||
case '{':
|
||||
temp_parse.type=__left_brace;
|
||||
break;
|
||||
case '}':
|
||||
temp_parse.type=__right_brace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
parser.push(temp_parse);
|
||||
|
||||
identifier_reduction();
|
||||
scalar_reduction();
|
||||
array_search_reduction();
|
||||
hash_search_reduction();
|
||||
print_reduction();
|
||||
function_call_reduction();
|
||||
func_return_reduction();
|
||||
definition_reduction();
|
||||
assignment_reduction();
|
||||
if(parser.top().type==__definition)
|
||||
std::cout<<"line "<<parser.top().line<<": definition"<<std::endl;
|
||||
else if(parser.top().type==__assignment)
|
||||
std::cout<<"line "<<parser.top().line<<": assignment"<<std::endl;
|
||||
else if(parser.top().type==__print_function)
|
||||
std::cout<<"line "<<parser.top().line<<": print()"<<std::endl;
|
||||
else if(parser.top().type==__use_function)
|
||||
std::cout<<"line "<<parser.top().line<<": call func()"<<std::endl;
|
||||
statement_reduction();//must be put before statements
|
||||
statements_reduction();
|
||||
function_reduction();
|
||||
class_function_reduction();
|
||||
if(parser.top().type==__function)
|
||||
std::cout<<"line "<<parser.top().line<<": function()"<<std::endl;
|
||||
hash_member_reduction();
|
||||
hash_members_reduction();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void parse::print_stack()
|
||||
{
|
||||
std::cout<<">> parser stack:"<<std::endl;
|
||||
while(!parser.empty())
|
||||
{
|
||||
if(parser.top().type==__definition)
|
||||
std::cout<<"line "<<parser.top().line<<": definition"<<std::endl;
|
||||
else if(parser.top().type==__assignment)
|
||||
std::cout<<"line "<<parser.top().line<<": assignment"<<std::endl;
|
||||
else if(parser.top().type==__print_function)
|
||||
std::cout<<"line "<<parser.top().line<<": print()"<<std::endl;
|
||||
else if(parser.top().type==__statement)
|
||||
std::cout<<"line "<<parser.top().line<<": statement"<<std::endl;
|
||||
else if(parser.top().type==__statements)
|
||||
std::cout<<"line "<<parser.top().line<<": statements"<<std::endl;
|
||||
else if(parser.top().type==__function)
|
||||
std::cout<<"line "<<parser.top().line<<": function()"<<std::endl;
|
||||
else
|
||||
std::cout<<"line "<<parser.top().line<<": "<<parser.top().type<<std::endl;
|
||||
parser.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::stack_set_empty()
|
||||
{
|
||||
while(!parser.empty())
|
||||
parser.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
parse nasal_parse;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,74 @@
|
|||
#ifndef __NASAL_PRINT_H__
|
||||
#define __NASAL_PRINT_H__
|
||||
|
||||
#include "nasal_hash.cpp"
|
||||
#include "nasal_list.cpp"
|
||||
|
||||
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==VAR_LLINT)
|
||||
std::cout<<*((long long int *)Var.data);
|
||||
else if(Var.type==VAR_DOUBLE)
|
||||
std::cout<<*((double *)Var.data);
|
||||
else if(Var.type==VAR_CHAR)
|
||||
std::cout<<*((char *)Var.data);
|
||||
else if(Var.type==VAR_STRING)
|
||||
PrintString(*((std::string *)Var.data));
|
||||
else if(Var.type==VAR_LIST)
|
||||
;//((nasal_list *)Var.data)->print_list();
|
||||
else if(Var.type==VAR_HASH)
|
||||
;//((nasal_hash *)Var.data)->PrintHash();
|
||||
else
|
||||
std::cout<<"[Error] Null type or function";
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,108 @@
|
|||
#ifndef __NASAL_TOKEN_CPP__
|
||||
#define __NASAL_TOKEN_CPP__
|
||||
|
||||
#include "nasal_token.h"
|
||||
#include "nasal_var_stack.h"
|
||||
#include "nasal_func_stack.h"
|
||||
|
||||
token_list::token_list()
|
||||
{
|
||||
list_range=0;
|
||||
head=new token_unit;
|
||||
head->type=FUNC_BEGIN;
|
||||
head->content="__process_begin";
|
||||
head->line=0;
|
||||
head->next=NULL;
|
||||
}
|
||||
token_list::~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;
|
||||
}
|
||||
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)
|
||||
{
|
||||
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 token_list::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 token_list::append(const int _line,const int _type,std::string &_content)
|
||||
{
|
||||
token_unit *temp=head;
|
||||
while(temp->next)
|
||||
temp=temp->next;
|
||||
++list_range;
|
||||
temp->next=new token_unit;
|
||||
temp=temp->next;
|
||||
temp->next=NULL;
|
||||
temp->type=_type;
|
||||
temp->line=_line;
|
||||
temp->content=_content;
|
||||
return;
|
||||
}
|
||||
void token_list::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;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef __NASAL_TOKEN_H__
|
||||
#define __NASAL_TOKEN_H__
|
||||
|
||||
#include "nasal_var.h"
|
||||
|
||||
//for token and lexer
|
||||
#define OPERATOR 1 //界符 or 运算符
|
||||
#define IDENTIFIER 2 //自定义标识符
|
||||
#define NUMBER 3 //数字
|
||||
#define RESERVEWORD 4 //关键字
|
||||
#define STRING 5 //字符串类型
|
||||
|
||||
struct token_unit
|
||||
{
|
||||
int type;
|
||||
std::string content;
|
||||
int line;
|
||||
token_unit *next;
|
||||
};
|
||||
|
||||
class token_list
|
||||
{
|
||||
private:
|
||||
token_unit *head;
|
||||
int list_range;
|
||||
public:
|
||||
token_list();
|
||||
~token_list();
|
||||
token_unit* get_head();
|
||||
int get_list_range();
|
||||
void print_line_token(const int);
|
||||
void delete_all();
|
||||
void append(const int,const int,std::string&);
|
||||
void print();
|
||||
};
|
||||
|
||||
token_list nasal_lexer;
|
||||
#endif
|
|
@ -0,0 +1,140 @@
|
|||
#ifndef __NASAL_VAR_CPP__
|
||||
#define __NASAL_VAR_CPP__
|
||||
|
||||
var::var()
|
||||
{
|
||||
type=VAR_NONE;
|
||||
data=NULL;
|
||||
}
|
||||
var::var(const var &temp)
|
||||
{
|
||||
type=temp.type;
|
||||
switch(type)
|
||||
{
|
||||
case VAR_NONE:
|
||||
data=NULL;
|
||||
break;
|
||||
case VAR_LLINT:
|
||||
data=new long long int;
|
||||
*((long long int *)data)=*((long long int *)temp.data);
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
data=new double;
|
||||
*((double *)data)=*((double *)temp.data);
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
data=new char;
|
||||
*((char *)data)=*((char *)temp.data);
|
||||
break;
|
||||
case VAR_STRING:
|
||||
data=new std::string;
|
||||
*((std::string *)data)=*((std::string *)temp.data);
|
||||
break;
|
||||
case VAR_LIST:
|
||||
data=new nasal_list;
|
||||
*((nasal_list *)data)=*((nasal_list *)temp.data);
|
||||
break;
|
||||
case VAR_HASH:
|
||||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
data=new func;
|
||||
*((func *)data)=*((func *)temp.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
var::~var()
|
||||
{
|
||||
if(data)
|
||||
switch(type)
|
||||
{
|
||||
case VAR_LLINT:
|
||||
delete (long long int *)data;
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
delete (double *)data;
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
delete (char *)data;
|
||||
break;
|
||||
case VAR_STRING:
|
||||
delete (std::string *)data;
|
||||
break;
|
||||
case VAR_LIST:
|
||||
delete (nasal_list *)data;
|
||||
break;
|
||||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
delete (func *)data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var& var::operator=(const var &temp)
|
||||
{
|
||||
if(data)
|
||||
switch(type)
|
||||
{
|
||||
case VAR_LLINT:
|
||||
delete (long long int *)data;
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
delete (double *)data;
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
delete (char *)data;
|
||||
break;
|
||||
case VAR_STRING:
|
||||
delete (std::string *)data;
|
||||
break;
|
||||
case VAR_LIST:
|
||||
delete (nasal_list *)data;
|
||||
break;
|
||||
case VAR_HASH:
|
||||
delete (nasal_hash *)data;
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
delete (func *)data;
|
||||
break;
|
||||
}
|
||||
type=temp.type;
|
||||
switch(type)
|
||||
{
|
||||
case VAR_NONE:
|
||||
data=NULL;
|
||||
break;
|
||||
case VAR_LLINT:
|
||||
data=new long long int;
|
||||
*((long long int *)data)=*((long long int *)temp.data);
|
||||
break;
|
||||
case VAR_DOUBLE:
|
||||
data=new double;
|
||||
*((double *)data)=*((double *)temp.data);
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
data=new char;
|
||||
*((char *)data)=*((char *)temp.data);
|
||||
break;
|
||||
case VAR_STRING:
|
||||
data=new std::string;
|
||||
*((std::string *)data)=*((std::string *)temp.data);
|
||||
break;
|
||||
case VAR_LIST:
|
||||
data=new nasal_list;
|
||||
*((nasal_list *)data)=*((nasal_list *)temp.data);
|
||||
break;
|
||||
case VAR_HASH:
|
||||
data=new nasal_hash;
|
||||
*((nasal_hash *)data)=*((nasal_hash *)temp.data);
|
||||
break;
|
||||
case VAR_FUNC:
|
||||
data=new func;
|
||||
*((func *)data)=*((func *)temp.data);
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef __NASAL_VAR_H__
|
||||
#define __NASAL_VAR_H__
|
||||
|
||||
#define VAR_NONE 0
|
||||
#define VAR_LLINT 1
|
||||
#define VAR_DOUBLE 2
|
||||
#define VAR_CHAR 3
|
||||
#define VAR_STRING 4
|
||||
#define VAR_LIST 5
|
||||
#define VAR_HASH 6
|
||||
#define VAR_FUNC 7
|
||||
|
||||
class var
|
||||
{
|
||||
public:
|
||||
int type;
|
||||
void *data;
|
||||
var();
|
||||
var(const var&);
|
||||
~var();
|
||||
var& operator=(const var&);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,151 @@
|
|||
#ifndef __NASAL_VAR_STACK_H__
|
||||
#define __NASAL_VAR_STACK_H__
|
||||
|
||||
#include "nasal_print.h"
|
||||
|
||||
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<<"[";
|
||||
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);
|
||||
else
|
||||
std::cout<<*((std::string *)temp->var_detail.data);
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
var search_var(std::string varia_name)
|
||||
{
|
||||
var temp_var;
|
||||
temp_var.data=NULL;
|
||||
temp_var.type=VAR_NONE;
|
||||
var_stack_unit *temp=head;
|
||||
while(temp->next)
|
||||
{
|
||||
temp=temp->next;
|
||||
if(temp->var_name==varia_name)
|
||||
{
|
||||
temp_var=temp->var_detail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
};
|
||||
var_stack nasal_var_stack;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue