I mean : new version

This commit is contained in:
Valk Richard Li 2019-10-31 17:49:44 +08:00 committed by GitHub
parent f268911841
commit f90d1eef91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 3089 additions and 0 deletions

189
version1.3/ast.h Normal file
View File

@ -0,0 +1,189 @@
#ifndef __NASAL_ABSTRACT_SYNTAX_TREE_RUN_H__
#define __NASAL_ABSTRACT_SYNTAX_TREE_RUN_H__
class abstract_syntax_tree
{
private:
int ast_node_type;
double var_number;
std::string var_string;
std::string var_name;
std::list<abstract_syntax_tree> children;
public:
abstract_syntax_tree()
{
ast_node_type=0;
var_number=0;
var_string="";
var_name="";
children.clear();
return;
}
abstract_syntax_tree(const abstract_syntax_tree& p)
{
ast_node_type=p.ast_node_type;
var_number=p.var_number;
var_string=p.var_string;
var_name=p.var_name;
children=p.children;
return;
}
abstract_syntax_tree& operator=(const abstract_syntax_tree& p)
{
ast_node_type=p.ast_node_type;
var_number=p.var_number;
var_string=p.var_string;
var_name=p.var_name;
children.clear();
children=p.children;
return *this;
}
void set_clear()
{
ast_node_type=0;
var_number=0;
var_string="";
var_name="";
children.clear();
return;
}
void print_tree(const int n)
{
std::string str="";
for(int i=0;i<n;++i)
str+="| ";
std::cout<<str;
print_token(ast_node_type);
switch(ast_node_type)
{
case __number:std::cout<<": "<<var_number;break;
case __string:std::cout<<": "<<var_string;break;
case __id:
case __list_search:
case __hash_search:
case __call_function:std::cout<<": "<<var_name;break;
}
std::cout<<std::endl;
if(!children.empty())
{
for(auto i=children.begin();i!=children.end();++i)
i->print_tree(n+1);
}
return;
}
void set_node_type(const int type)
{
ast_node_type=type;
return;
}
void set_var_string(std::string str)
{
var_string=str;
return;
}
void set_var_number(std::string str)
{
if(str=="nil")
{
var_number=0;
return;
}
if((int)str.length()>2 && (str[1]=='x' || str[1]=='o'))
{
if(str[1]=='x')
{
int num=0;
int pw=1;
for(int i=(int)str.length()-1;i>1;--i)
{
if('0'<=str[i] && str[i]<='9')
num+=(str[i]-'0')*pw;
else if('a'<=str[i] && str[i]<='f')
num+=(10+str[i]-'a')*pw;
else if('A'<=str[i] && str[i]<='F')
num+=(10+str[i]-'A')*pw;
pw<<=4;
}
var_number=(double)num;
}
else
{
int num=0;
int pw=1;
for(int i=(int)str.length()-1;i>1;--i)
{
num+=(str[i]-'0')*pw;
pw<<=3;
}
var_number=(double)num;
}
return;
}
int dot_place=-1;
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
{
dot_place=i;
break;
}
if(dot_place==-1)
{
var_number=0;
double pw=1;
for(int i=(int)str.length()-1;i>=0;--i)
{
var_number+=(str[i]-'0')*pw;
pw*=10;
}
}
else
{
var_number=0;
double pw=0.1;
for(int i=dot_place+1;i<(int)str.length();++i)
{
var_number+=(str[i]-'0')*pw;
pw/=10;
}
pw=1;
for(int i=dot_place-1;i>=0;--i)
{
var_number+=(str[i]-'0')*pw;
pw*=10;
}
}
return;
}
void set_var_name(std::string& str)
{
var_name=str;
return;
}
void add_child(abstract_syntax_tree p)
{
children.push_back(p);
return;
}
int get_type()
{
return ast_node_type;
}
double get_var_number()
{
return var_number;
}
std::string get_var_string()
{
return var_string;
}
std::string get_var_name()
{
return var_name;
}
std::list<abstract_syntax_tree>& get_children()
{
return children;
}
};
#endif

86
version1.3/main.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "nasal.h"
int main()
{
resource_programme_process prog;
nasal_lexer lex;
nasal_parser pas;
nasal_runtime vm;
std::string command;
std::cout<<">> Nasal interpreter 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 interpreter by ValKmjolnir"<<std::endl;
std::cout<<">> 1. [ ] |input file name to load the file."<<std::endl;
std::cout<<">> 2. [cls ] |clear the screen."<<std::endl;
std::cout<<">> 3. [exit ] |shut down the interpreter."<<std::endl;
std::cout<<">> 4. [lexer ] |run and show the lexer. (-lexer)"<<std::endl;
std::cout<<">> 5. [parser] |run parser and see parse stack & parse result(s). (-parser)"<<std::endl;
std::cout<<">> 6. [ast ] |print the abstract syntax tree."<<std::endl;
std::cout<<">> 7. [del ] |delete program in memory."<<std::endl;
std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;
std::cout<<">> 9. [rs ] |check the source program."<<std::endl;
}
else if(command=="cls")
{
system("cls");
//windows system("cls");
//linux system("clear");
//macOS system("clear");
}
else if(command=="rs")
prog.print_file();
else if(command=="exit")
break;
else if(command=="lexer")
{
lex.lexer_process(prog.use_file());
lex.print_lexer();
}
else if(command=="del")
{
prog.del_file();
std::cout<<">>[Delete] Complete."<<std::endl;
}
else if(command=="parser")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.print_parser_stack();
pas.parse_main_work();
}
else if(command=="ast")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.parse_main_work();
if(!pas.get_error_num())
pas.print_generated_ast();
else
std::cout<<">>[Abstract_syntax_tree] error(s) occurred,stop."<<std::endl;
}
else if(command=="run")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.parse_main_work();
if(!pas.get_error_num())
{
vm.set_root(pas.get_tree());
vm.run();
}
else
std::cout<<">>[Runtime] error(s) occurred,stop."<<std::endl;
}
else
prog.input_file(command);
}
return 0;
}

19
version1.3/nasal.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NASAL_H__
#define __NASAL_H__
#include <iostream>
#include <cstring>
#include <fstream>
#include <thread>
#include <stack>
#include <list>
#include <ctime>
#include "nasal_token_type.h"
#include "ast.h"
#include "nasal_vm.h"
#include "nasal_lexer.h"
#include "nasal_parser.h"
#endif

543
version1.3/nasal_lexer.h Normal file
View File

@ -0,0 +1,543 @@
#ifndef __NASAL_LEXER_H__
#define __NASAL_LEXER_H__
#include <iostream>
#include <fstream>
#include <list>
#include <cstring>
#define OPERATOR 1 // operator
#define IDENTIFIER 2 // id
#define NUMBER 3 // number
#define RESERVEWORD 4 // reserve word
#define STRING 5 // string
#define DYNAMIC_ID 6 // id...
#define FAIL -1 //fail
#define SCANEND -2 //complete scanning
#define ERRORFOUND -3 //error occurred
std::string reserve_word[15]=
{
"for","foreach","forindex","while",
"var","func","break","continue","return",
"if","else","elsif","nil","and","or"
};
int isReserveWord(std::string &p)
{
for(int i=0;i<15;++i)
if(reserve_word[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'));
}
bool isHex(char t)
{
return ((('0'<=t) && (t<='9')) || (('a'<=t) && (t<='f')));
}
bool isOct(char t)
{
return (('0'<=t) && (t<='7'));
}
class resource_programme_process
{
private:
char *resource;
public:
resource_programme_process()
{
resource=NULL;
resource=new char[16777216];
}
~resource_programme_process()
{
if(resource)
delete []resource;
}
char* use_file()
{
return resource;
}
void input_file(std::string& filename)
{
std::ifstream fin(filename);
if(fin.fail())
{
std::cout<<">>[Error] Cannot load file: "<<filename<<" ."<<std::endl;
fin.close();
return;
}
memset(resource,0,sizeof(char));
int i=0;
int instring=0;
bool findnote=false;// to find the note with # at the head of line.
while(!fin.eof())
{
resource[i]=fin.get();
if(resource[i]=='\'' || resource[i]=='\"')
++instring;
if(resource[i]=='\n')
findnote=false;
//when meeting '\n' the findnote is set to false then the next statement can be executed.
if(resource[i]!='#' && !findnote)
++i;
else if(resource[i]=='#')
{
if(instring & 1)
++i;
else
findnote=true;
}
if(fin.eof())
break;
}
resource[i]='\0';
fin.close();
return;
}
void print_file()
{
if(!resource[0])
{
std::cout<<"0 null"<<std::endl;
return;
}
int line=1;
std::cout<<line<<" ";
for(int i=0;i<16777216;++i)
{
if(!resource[i])
break;
std::cout<<resource[i];
if(resource[i]=='\n')
{
++line;
std::cout<<line<<" ";
}
}
std::cout<<std::endl;
return;
}
void del_file()
{
memset(resource,0,sizeof(char));
return;
}
};
struct token
{
int line;
int type;
std::string content;
};
class nasal_lexer
{
private:
std::list<token> lexer;
public:
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;
if((syn==IDENTIFIER) && source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.')
{
__token+="...";
syn=DYNAMIC_ID;
ptr+=3;
}
}
else if(isNumber(temp))
{
if((source[ptr]=='0') && (source[ptr+1]=='x'))
{
__token+=source[ptr];
__token+=source[ptr+1];
ptr+=2;
temp=source[ptr];
while(isNumber(temp) || isHex(temp))
{
__token+=temp;
++ptr;
temp=source[ptr];
}
if((int)__token.length()==2)
{
std::cout<<">>[Lexer-warning] lexer: expect a hex-number string after '0x' ."<<std::endl;
__token+="0";
}
}
else if((source[ptr]=='0') && (source[ptr+1]=='o'))
{
__token+=source[ptr];
__token+=source[ptr+1];
ptr+=2;
temp=source[ptr];
while(isNumber(temp) || isOct(temp))
{
__token+=temp;
++ptr;
temp=source[ptr];
}
if((int)__token.length()==2)
{
std::cout<<">>[Lexer-warning] lexer: expect a oct-number string after '0o' ."<<std::endl;
__token+="0";
}
}
else
{
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=='\'')
{
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=='=' || 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 lexer_process(const char *source)
{
std::cout<<">>[Lexer] max size: "<<lexer.max_size()<<" ."<<std::endl;
lexer.clear();
int syn=0;//token type
int ptr=0;//pointer to one char in ResourcePrograme
int line=1;
std::string __token;
token temp;
while(syn!=SCANEND && syn!=ERRORFOUND)
{
scanner(syn,source,__token,ptr,line);
if(syn>0)//all Syn type is larger than zero
{
temp.line=line;
temp.type=syn;
temp.content=__token;
lexer.push_back(temp);
}
}
std::cout<<">>[Lexer] complete scanning."<<std::endl;
return;
}
void print_lexer()
{
token temp;
for(std::list<token>::iterator i=lexer.begin();i!=lexer.end();++i)
{
temp=*i;
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 | ";
else if(temp.type==DYNAMIC_ID)
std::cout<<"( Identifier | ";
std::cout<<temp.content<<" )"<<std::endl;
}
return;
}
void token_list_type_detail_edit()
{
for(std::list<token>::iterator i=lexer.begin();i!=lexer.end();++i)
{
if((*i).type==RESERVEWORD)
{
if((*i).content=="var")
(*i).type=__var;
else if((*i).content=="func")
(*i).type=__func;
else if((*i).content=="return")
(*i).type=__return;
else if((*i).content=="nil")
(*i).type=__number;
else if((*i).content=="continue")
(*i).type=__continue;
else if((*i).content=="break")
(*i).type=__break;
else if((*i).content=="and")
(*i).type=__and_operator;
else if((*i).content=="or")
(*i).type=__or_operator;
else if((*i).content=="for")
(*i).type=__for;
else if((*i).content=="forindex")
(*i).type=__forindex;
else if((*i).content=="foreach")
(*i).type=__foreach;
else if((*i).content=="while")
(*i).type=__while;
else if((*i).content=="if")
(*i).type=__if;
else if((*i).content=="else")
(*i).type=__else;
else if((*i).content=="elsif")
(*i).type=__elsif;
}
else if(((*i).content=="==") || ((*i).content=="!=") || ((*i).content==">") || ((*i).content==">=") || ((*i).content=="<") || ((*i).content=="<="))
{
if((*i).content=="==")
(*i).type=__cmp_equal;
else if((*i).content=="!=")
(*i).type=__cmp_not_equal;
else if((*i).content==">")
(*i).type=__cmp_more;
else if((*i).content==">=")
(*i).type=__cmp_more_or_equal;
else if((*i).content=="<")
(*i).type=__cmp_less;
else if((*i).content=="<=")
(*i).type=__cmp_less_or_equal;
}
else if(((*i).content==";") || ((*i).content==",") || ((*i).content=="=") || ((*i).content==":") || ((*i).content==".") || ((*i).content=="?") || ((*i).content=="|") || ((*i).content=="&") || ((*i).content=="%") || ((*i).content=="$") || ((*i).content=="`") || ((*i).content=="^") || ((*i).content=="@"))
{
char c=(*i).content[0];
switch(c)
{
case ';':(*i).type=__semi;break;
case ',':(*i).type=__comma;break;
case '=':(*i).type=__equal;break;
case ':':(*i).type=__colon;break;
case '.':(*i).type=__dot;break;
default:(*i).type=__unknown_operator;break;
}
}
else if(((*i).type==NUMBER) || ((*i).type==STRING) || ((*i).type==IDENTIFIER) || ((*i).type==DYNAMIC_ID))
{
int t=(*i).type;
switch(t)
{
case NUMBER:(*i).type=__number;break;
case STRING:(*i).type=__string;break;
case IDENTIFIER:(*i).type=__id;break;
case DYNAMIC_ID:(*i).type=__dynamic_id;break;
}
}
else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!"))
{
char c=(*i).content[0];
switch(c)
{
case '+':(*i).type=__add_operator;break;
case '-':(*i).type=__sub_operator;break;
case '*':(*i).type=__mul_operator;break;
case '/':(*i).type=__div_operator;break;
case '~':(*i).type=__link_operator;break;
case '!':(*i).type=__nor_operator;break;
}
}
else if(((*i).content=="+=") || ((*i).content=="-=") || ((*i).content=="*=") || ((*i).content=="/=") || ((*i).content=="~="))
{
char c=(*i).content[0];
switch(c)
{
case '+':(*i).type=__add_equal;break;
case '-':(*i).type=__sub_equal;break;
case '*':(*i).type=__mul_equal;break;
case '/':(*i).type=__div_equal;break;
case '~':(*i).type=__link_equal;break;
}
}
else if(((*i).content=="(") || ((*i).content==")") || ((*i).content=="[") || ((*i).content=="]") || ((*i).content=="{") || ((*i).content=="}"))
{
char c=(*i).content[0];
switch(c)
{
case '(':(*i).type=__left_curve;break;
case ')':(*i).type=__right_curve;break;
case '[':(*i).type=__left_bracket;break;
case ']':(*i).type=__right_bracket;break;
case '{':(*i).type=__left_brace;break;
case '}':(*i).type=__right_brace;break;
}
}
}
return;
}
std::list<token>& return_list()
{
return lexer;
}
};
#endif

1821
version1.3/nasal_parser.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,118 @@
#ifndef __NASAL_TOKEN_TYPE_H__
#define __NASAL_TOKEN_TYPE_H__
#include <cstring>
enum token_type
{
__stack_end=1,
__equal, // =
__cmp_equal,__cmp_not_equal, // == !=
__cmp_less,__cmp_less_or_equal, // < <=
__cmp_more,__cmp_more_or_equal, // > >=
__and_operator,__or_operator,__nor_operator, // and or !
__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, // ; , : .
__unknown_operator,
__var,__func,__return,
__if,__elsif,__else,
__continue,__break,
__for,__forindex,__foreach,__while,
//operators & reserve words
__number,__string,__id,__dynamic_id,
//basic elements
__root,
__null_type,
__list,__hash,
__hash_member,
__call_function,__list_search,__hash_search,
__normal_statement_block,
__definition,__assignment,
__function,__loop,__ifelse
};
void print_token(int type)
{
std::string context="";
switch(type)
{
case __stack_end: context="#"; break;
case __equal: context="="; break;
case __cmp_equal: context="==";break;
case __cmp_not_equal: context="!=";break;
case __cmp_less: context="<"; break;
case __cmp_less_or_equal: context="<=";break;
case __cmp_more: context=">"; break;
case __cmp_more_or_equal: context=">=";break;
case __and_operator: context="and";break;
case __or_operator: context="or"; break;
case __nor_operator: context="!"; break;
case __add_operator: context="+"; break;
case __sub_operator: context="-"; break;
case __mul_operator: context="*"; break;
case __div_operator: context="/"; break;
case __link_operator: context="~"; break;
case __add_equal: context="+=";break;
case __sub_equal: context="-=";break;
case __mul_equal: context="*=";break;
case __div_equal: context="/=";break;
case __link_equal: context="~=";break;
case __left_brace: context="{"; break;
case __right_brace: context="}"; break;
case __left_bracket: context="["; break;
case __right_bracket: context="]"; break;
case __left_curve: context="("; break;
case __right_curve: context=")"; break;
case __semi: context=";"; break;
case __comma: context=","; break;
case __colon: context=":"; break;
case __dot: context="."; break;
case __unknown_operator: context="unknown_operator";break;
case __var: context="var"; break;
case __func: context="func";break;
case __continue: context="continye"; break;
case __break: context="break"; break;
case __for: context="for"; break;
case __forindex: context="forindex";break;
case __foreach: context="foreach";break;
case __while: context="while";break;
case __if: context="if";break;
case __elsif: context="elsif";break;
case __else: context="else";break;
case __return: context="return";break;
case __id: context="id";break;
case __dynamic_id: context="id...";break;
case __number: context="num";break;
case __string: context="str";break;
case __root: context="root";break;
case __null_type: context="null_type";break;
case __list: context="list";break;
case __hash: context="hash";break;
case __hash_member: context="hash_member";break;
case __call_function: context="call_func";break;
case __list_search: context="call_list";break;
case __hash_search: context="call_hash";break;
case __normal_statement_block:context="block";break;
case __definition: context="definition";break;
case __assignment: context="assignment";break;
case __function: context="function";break;
case __loop: context="loop";break;
case __ifelse: context="if-else";break;
default: context="undefined_token";break;
}
std::cout<<context;
return;
}
#endif

313
version1.3/nasal_vm.h Normal file
View File

@ -0,0 +1,313 @@
#ifndef __NASAL_VM_H__
#define __NASAL_VM_H__
class var
{
private:
int type;
std::string name;
double number;
std::string str;
std::list<var> var_list;
std::list<var> var_hash;
abstract_syntax_tree function;
public:
var()
{
type=0;
name="";
number=0;
str="";
function.set_clear();
}
var(const var& p)
{
type=p.type;
name=p.name;
number=p.number;
str=p.str;
function=p.function;
}
var& operator=(const var& p)
{
type=p.type;
name=p.name;
number=p.number;
str=p.str;
function=p.function;
return *this;
}
void print_information()
{
std::cout<<"[ type: ";print_token(type);std::cout<<" ]";
std::cout<<"[ name: "<<name<<" ]";
std::cout<<"[ number: "<<number<<" ]";
std::cout<<"[ string: "<<str<<" ]"<<std::endl;
std::cout<<"[ function: ";
function.print_tree(1);
std::cout<<" ]"<<std::endl;
return;
}
void print_detail()
{
switch(type)
{
case __function:
std::cout<<std::endl<<">>[Runtime-error] function type cannot be printed."<<std::endl;
break;
case __number:
std::cout<<number;
break;
case __string:
for(int i=1;i<str.length()-1;++i)
{
if(str[i]!='\\')
std::cout<<str[i];
else
{
switch(str[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:std::cout<<"\\";break;
}
}
}
break;
}
return;
}
void set_type(const int var_type)
{
type=var_type;
return;
}
void set_name(std::string var_name_str)
{
name=var_name_str;
return;
}
void set_number(const double var_number)
{
number=var_number;
return;
}
void set_string(std::string s)
{
str=s;
return;
}
void set_function(abstract_syntax_tree func)
{
function=func;
return;
}
void set_list(std::list<var> p)
{
var_list=p;
return;
}
void append_list(var p)
{
var_list.push_back(p);
return;
}
void set_hash(std::list<var> p)
{
var_hash=p;
return;
}
void append_hash(var p)
{
var_hash.push_back(p);
return;
}
int get_type()
{
return type;
}
std::string get_name()
{
return name;
}
double get_number()
{
return number;
}
std::string get_string()
{
return str;
}
abstract_syntax_tree get_function()
{
return function;
}
std::list<var> get_list()
{
return var_list;
}
std::list<var> get_hash()
{
return var_hash;
}
};
class var_scope_manager
{
private:
std::list<std::list<var>> scope_list;
std::list<int> scope_type;
var error_var;
public:
var_scope_manager()
{
scope_list.clear();
std::string str="__nas_strc_error_ret";
error_var.set_name(str);
error_var.set_type(__null_type);
return;
}
void set_clear()
{
scope_list.clear();
scope_type.clear();
return;
}
var& search_var(std::string str)
{
for(std::list<std::list<var>>::iterator i=scope_list.begin();i!=scope_list.end();++i)
for(std::list<var>::iterator j=i->begin();j!=i->end();++j)
if(j->get_name()==str)
return *j;
std::cout<<std::endl<<">>[Runtime-error] could not find the var '"<<str<<"' ."<<std::endl;
return error_var;
}
void add_var(var new_var)
{
std::list<std::list<var>>::iterator i=scope_list.begin();
std::list<int>::iterator t=scope_type.begin();
// get global scopes
for(;i!=scope_list.end();++i,++t)
{
if(*t!=__function)
{
for(std::list<var>::iterator j=i->begin();j!=i->end();++j)
if(j->get_name()==new_var.get_name())
{
std::cout<<std::endl<<">>[Runtime-error] redeclaration of var '"<<new_var.get_name()<<"' ."<<std::endl;
return;
}
}
}
// get parameters_list scope
i=scope_list.end();
--i;
t=scope_type.end();
--t;
if(*t==__function)
{
for(std::list<var>::iterator j=i->begin();j!=i->end();++j)
if(j->get_name()==new_var.get_name())
{
std::cout<<std::endl<<">>[Runtime-error] redeclaration of var '"<<new_var.get_name()<<"' ."<<std::endl;
return;
}
}
if(!scope_list.empty())
{
i=scope_list.end();
--i;
i->push_back(new_var);
}
else
std::cout<<std::endl<<">>[Runtime-error] empty scope list."<<std::endl;
return;
}
void add_new_scope(int type)
{
std::list<var> new_list;
scope_list.push_back(new_list);
scope_type.push_back(type);
return;
}
void pop_last_scope()
{
if(!scope_list.empty())
{
scope_list.pop_back();
scope_type.pop_back();
}
else
std::cout<<std::endl<<">>[Runtime-error] scope poped empty thing."<<std::endl;
return;
}
};
class nasal_code_gen
{
private:
abstract_syntax_tree root;
public:
nasal_code_gen()
{
root.set_clear();
return;
}
void set_root(abstract_syntax_tree& tree)
{
root.set_clear();
root=tree;
std::cout<<">>[Code] runtime got the ast-root: "<<((void *)&tree)<<"->"<<((void *)&root)<<"."<<std::endl;
return;
}
};
class nasal_runtime
{
private:
abstract_syntax_tree root;
var_scope_manager scope;
public:
nasal_runtime()
{
root.set_clear();
return;
}
void run()
{
std::cout<<">>[Runtime] process begins."<<std::endl;
int time_beg,time_end;
time_beg=time(NULL);
scope.set_clear();
run_root(root);
time_end=time(NULL);
std::cout<<std::endl<<">>[Runtime] process exited after "<<time_beg-time_end<<" sec(s)."<<std::endl;
return;
}
void set_root(abstract_syntax_tree& tree)
{
root.set_clear();
root=tree;
std::cout<<">>[Runtime] runtime got the ast-root: "<<((void *)&tree)<<"->"<<((void *)&root)<<"."<<std::endl;
return;
}
void run_definition(abstract_syntax_tree& tree);
void run_assignment(abstract_syntax_tree& tree);
void run_loop(abstract_syntax_tree& tree);
void run_if_else(abstract_syntax_tree& tree);
void run_block(abstract_syntax_tree& tree,int run_type);
void run_root(abstract_syntax_tree& tree);
var run_calculation(abstract_syntax_tree& tree);
var run_function(abstract_syntax_tree& tree);
var list_generation(abstract_syntax_tree& tree);
var hash_generation(abstract_syntax_tree& tree);
var list_search(abstract_syntax_tree& tree);
var hash_search(abstract_syntax_tree& tree);
var identifier_call(abstract_syntax_tree& tree);
var scalar_call(abstract_syntax_tree& tree);
};
#endif