diff --git a/version0.17/abstract_syntax_tree.h b/version0.17/abstract_syntax_tree.h new file mode 100644 index 0000000..a9f7ee2 --- /dev/null +++ b/version0.17/abstract_syntax_tree.h @@ -0,0 +1,203 @@ +#ifndef __ABSTRACT_SYNTAX_TREE_H__ +#define __ABSTRACT_SYNTAX_TREE_H__ + +class ast_tree_node +{ + protected: + int line; + int type; + std::list children;// for node that can be extended + + double num; // for number + std::string str; // for string + public: + ast_tree_node() + { + line=0; + type=__root; + children.clear(); + + num=0; + str=""; + } + void set_line(int _line) + { + line=_line; + return; + } + int return_type() + { + return type; + } + int return_line() + { + return line; + } + double run() + { + if(type==__number) + return num; + else if(type==__root) + { + std::cout<<">>[Result(s)]"<::iterator i=children.begin();i!=children.end();++i) + std::cout<run()<::iterator i=children.begin(); + double left_child=i->run(); + ++i; + double right_child=i->run(); + switch(type) + { + case __add_operator:return left_child+right_child;break; + case __sub_operator:return left_child-right_child;break; + case __mul_operator:return left_child*right_child;break; + case __div_operator:return left_child/right_child;break; + } + } + return 0; + } + void add_child(ast_tree_node& new_child) + { + children.push_back(new_child); + return; + } + ast_tree_node& return_last_child() + { + std::list::iterator i; + for(i=children.begin();i!=children.end();++i) + ; + --i; + return *i; + } + void clear_tree() + { + line=0; + type=__root; + children.clear(); + + num=0; + str=""; + return; + } + int child_num() + { + int cnt=0; + for(std::list::iterator i=children.begin();i!=children.end();++i) + ++cnt; + return cnt; + } + void print(int tab_num) + { + for(int i=0;i::iterator i=children.begin();i!=children.end();++i) + i->print(tab_num+1); + for(int i=0;i=0;--i) + { + num+=acc*((double)(str[i]-'0')); + acc*=10; + } + } + else + { + 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; + } +}; +class string_expr:public ast_tree_node +{ + public: + string_expr():ast_tree_node() + { + type=__string; + } + void set_str(std::string& t) + { + str=t; + return; + } + std::string& return_str() + { + return str; + } +}; + +#endif diff --git a/version0.17/ast_generator.h b/version0.17/ast_generator.h new file mode 100644 index 0000000..5f9f9e4 --- /dev/null +++ b/version0.17/ast_generator.h @@ -0,0 +1,196 @@ +#ifndef __AST_GENERATOR_H__ +#define __AST_GENERATOR_H__ + +class ast_generator +{ + private: + ast_tree_node root; + std::stack parse; + std::stack node_cache; + bool error; + public: + ast_generator() + { + error=false; + } + void input_token_stack(std::stack& temp) + { + parse=temp; + return; + } + void number_gen() + { + number_expr t; + t.set_line(parse.top().line); + t.set_number(parse.top().content); + if(node_cache.empty()) + node_cache.push(t); + else if((node_cache.top().return_type()==__add_operator || node_cache.top().return_type()==__sub_operator || node_cache.top().return_type()==__mul_operator || node_cache.top().return_type()==__div_operator) + && (node_cache.top().child_num()==1)) + node_cache.top().add_child(t); + else + { + std::cout<<">>[Error] parse error in line "<>[Error] parse error in line "<>[Error] parse error in line "<>[Error] parse error in line "<>[Error] parse error in line "<>[Error] parse error in line "<>[Error] parse error in line "<>[Parse] Error(s) occurred."<>[Parse] Complete parsing."<>[Parse] 0 error."<>[Parse] Complete parsing."<>[Abstract-syntax-tree]"<>[Parse] Error(s) occurred,stop running the code."<> Nasal interpreter by ValKmjolnir"<> Input [help] to find help."<> "; + std::getline(std::cin,command); + if(command=="help") + { + std::cout<<">> Nasal interpreter by ValKmjolnir"<> 1. [ ] |input file name to load the file."<> 2. [cls ] |clear the screen."<> 3. [exit ] |shut down the interpreter."<> 4. [lexer ] |run and show the lexer. (-lexer)"<> 5. [parser] |run parser. (-parser)"<> 6. [ast ] |print the abstract syntax tree."<> 7. [del ] |delete program in memory."<> 8. [run ] |run the programme in stack. (-lexer -parser)"<> 9. [rs ] |check the source program."<>10. [rule ] |see rules of the language."<>[Delete] Complete."<>[Rule] Token types that you can use: number + - * /"<>[Rule] Each binary operator should have two numbers around it like: 1 + 1"< +#include +#include +#include +#include +#include + +#include "nasal_token_type.h" +#include "nasal_lexer.h" +#include "nasal_parser.h" +#include "abstract_syntax_tree.h" +#include "ast_generator.h" + +#endif diff --git a/version0.17/nasal_lexer.h b/version0.17/nasal_lexer.h new file mode 100644 index 0000000..1229852 --- /dev/null +++ b/version0.17/nasal_lexer.h @@ -0,0 +1,408 @@ +#ifndef __NASAL_LEXER_H__ +#define __NASAL_LEXER_H__ + +#include +#include +#include +#include + +#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: "< 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]; + } + } + 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]; + } + } + 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: "<>[Error] Cannot identify "<>[Lexer] max size: "<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."<::iterator i=lexer.begin();i!=lexer.end();++i) + { + temp=*i; + std::cout<<"line "<& return_list() + { + return lexer; + } +}; + + + + +#endif diff --git a/version0.17/nasal_parser.h b/version0.17/nasal_parser.h new file mode 100644 index 0000000..3bbe01a --- /dev/null +++ b/version0.17/nasal_parser.h @@ -0,0 +1,200 @@ +#ifndef __NASAL_PARSER_H__ +#define __NASAL_PARSER_H__ + +#include "nasal_token_type.h" + +struct parse_unit +{ + int line; + int type; + std::string content; +}; + + +class nasal_parser +{ + private: + std::stack parser; + public: + void print_parser_stack() + { + if(parser.empty()) + return; + int line=0; + std::stack temp; + while(!parser.empty()) + { + temp.push(parser.top()); + if(line!=temp.top().line) + { + if(line+1==temp.top().line) + { + line=temp.top().line; + if(line==1) + std::cout<<"line "< "; + parser.pop(); + } + while(!temp.empty()) + { + parser.push(temp.top()); + temp.pop(); + } + std::cout<& return_stack() + { + return parser; + } + void parse_process(std::list& lexer) + { + while(!parser.empty()) + parser.pop(); + std::stack temp; + for(std::list::iterator i=lexer.begin();i!=lexer.end();++i) + { + parse_unit temp_parse; + temp_parse.line=i->line; + temp_parse.content=i->content; + if((*i).type==RESERVEWORD) + { + if((*i).content=="var") + temp_parse.type=__var; + else if((*i).content=="func") + temp_parse.type=__func; + else if((*i).content=="return") + temp_parse.type=__return; + else if((*i).content=="nil") + temp_parse.type=__number; + else if((*i).content=="continue") + temp_parse.type=__continue; + else if((*i).content=="break") + temp_parse.type=__break; + else if((*i).content=="and") + temp_parse.type=__and_operator; + else if((*i).content=="or") + temp_parse.type=__or_operator; + else if((*i).content=="for") + temp_parse.type=__for; + else if((*i).content=="forindex") + temp_parse.type=__forindex; + else if((*i).content=="foreach") + temp_parse.type=__foreach; + else if((*i).content=="while") + temp_parse.type=__while; + else if((*i).content=="if") + temp_parse.type=__if; + else if((*i).content=="else") + temp_parse.type=__else; + else if((*i).content=="elsif") + temp_parse.type=__elsif; + } + else if(((*i).content=="==") || ((*i).content=="!=") || ((*i).content==">") || ((*i).content==">=") || ((*i).content=="<") || ((*i).content=="<=")) + { + if((*i).content=="==") + temp_parse.type=__cmp_equal; + else if((*i).content=="!=") + temp_parse.type=__cmp_not_equal; + else if((*i).content==">") + temp_parse.type=__cmp_more; + else if((*i).content==">=") + temp_parse.type=__cmp_more_or_equal; + else if((*i).content=="<") + temp_parse.type=__cmp_less; + else if((*i).content=="<=") + temp_parse.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=="@")) + { + char c=(*i).content[0]; + switch(c) + { + case ';':temp_parse.type=__semi;break; + case ',':temp_parse.type=__comma;break; + case '=':temp_parse.type=__equal;break; + case ':':temp_parse.type=__colon;break; + case '.':temp_parse.type=__dot;break; + default:temp_parse.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:temp_parse.type=__number;break; + case STRING:temp_parse.type=__string;break; + case IDENTIFIER:temp_parse.type=__id;break; + case DYNAMIC_ID:temp_parse.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 '+':temp_parse.type=__add_operator;break; + case '-':temp_parse.type=__sub_operator;break; + case '*':temp_parse.type=__mul_operator;break; + case '/':temp_parse.type=__div_operator;break; + case '~':temp_parse.type=__link_operator;break; + case '!':temp_parse.type=__nor_operator;break; + } + } + else if(((*i).content=="+=") || ((*i).content=="-=") || ((*i).content=="*=") || ((*i).content=="/=") || ((*i).content=="~=")) + { + char c=(*i).content[0]; + switch(c) + { + case '+':temp_parse.type=__add_equal;break; + case '-':temp_parse.type=__sub_equal;break; + case '*':temp_parse.type=__mul_equal;break; + case '/':temp_parse.type=__div_equal;break; + case '~':temp_parse.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 '(':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; + } + } + temp.push(temp_parse);//push this into stack + } + if(temp.empty()) + { + std::cout<<">>[Parse] [-Warning] Empty lexer list."< + +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,__operator, +}; + +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="un_optr";break; + case __var: context="var";break; + case __func: context="func";break; + case __continue: context="ctn";break; + case __break: context="brk";break; + case __for: context="for";break; + case __forindex: context="foridx";break; + case __foreach: context="foreh";break; + case __while: context="while";break; + case __if: context="if";break; + case __elsif: context="elsif";break; + case __else: context="else";break; + case __return: context="rtrn";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 __operator: context="optr";break; + default: context="ukn_tkn";break; + } + std::cout<